-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuseAccount.ts
More file actions
42 lines (35 loc) · 1.32 KB
/
useAccount.ts
File metadata and controls
42 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { ref, watch, onMounted, onUnmounted, Ref } from 'vue'
import { WalletAccount, WalletState } from "@interchain-kit/core"
import { useWalletManager } from './useWalletManager'
export function useAccount(chainName: Ref<string>, walletName: Ref<string>): Ref<WalletAccount | null> {
const walletManager = useWalletManager()
const account = ref<WalletAccount | null>(null)
const getAccount = async () => {
const wallet = walletManager.wallets.find(w => w.option.name === walletName.value)
const chain = walletManager.chains.find(c => c.chainName === chainName.value)
if (wallet && chain) {
if (wallet.walletState === WalletState.Connected) {
const newAccount = await wallet.getAccount(chain.chainId)
account.value = newAccount
}
if (wallet.walletState === WalletState.Disconnected) {
account.value = null
}
}
}
watch([chainName, walletName], getAccount)
onMounted(() => {
const wallet = walletManager.wallets.find(w => w.option.name === walletName.value)
if (wallet) {
wallet.events.on('keystoreChange', getAccount)
}
getAccount()
})
onUnmounted(() => {
const wallet = walletManager.wallets.find(w => w.option.name === walletName.value)
if (wallet) {
wallet.events.off('keystoreChange', getAccount)
}
})
return account
}