-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuseInterchainClient.ts
More file actions
62 lines (54 loc) · 2.65 KB
/
useInterchainClient.ts
File metadata and controls
62 lines (54 loc) · 2.65 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { HttpEndpoint } from '@interchainjs/types'
import { CosmWasmSigningClient } from 'interchainjs/cosmwasm'
import { CosmosSigningClient } from 'interchainjs/cosmos'
import { SigningClient } from 'interchainjs/signing-client'
import { RpcQuery } from 'interchainjs/query/rpc'
import { ref, watch, Ref } from 'vue'
import { useWalletManager } from './useWalletManager'
import { Chain } from '@chain-registry/v2-types'
import { useAccount } from './useAccount'
import { WalletState } from '@interchain-kit/core'
import { InjSigningClient } from '@interchainjs/injective/signing-client'
export function useInterchainClient(chainName: Ref<string>, walletName: Ref<string>) {
const rpcEndpoint = ref<string | HttpEndpoint | undefined>()
const queryClient = ref<RpcQuery | null>(null)
const signingClient = ref<SigningClient | null>(null)
const signingCosmosClient = ref<CosmosSigningClient | null>(null)
const signingCosmWasmClient = ref<CosmWasmSigningClient | null>(null)
const signingInjectiveClient = ref<InjSigningClient | null>(null)
const error = ref<string | unknown | null>(null)
const isLoading = ref(false)
const walletManager = useWalletManager()
const account = useAccount(chainName, walletName)
const initialize = async () => {
const wallet = walletManager.wallets.find((w) => w.option.name === walletName.value)
const chainToShow = walletManager.chains.find((c: Chain) => c.chainName === chainName.value)
if (wallet && chainToShow && wallet?.walletState === WalletState.Connected) {
try {
isLoading.value = true
rpcEndpoint.value = await walletManager.getRpcEndpoint(wallet, chainName.value)
queryClient.value = await walletManager.getQueryClient(walletName.value, chainName.value)
signingClient.value = await walletManager.getSigningClient(walletName.value, chainName.value)
signingCosmosClient.value = await walletManager.getSigningCosmosClient(walletName.value, chainName.value)
signingCosmWasmClient.value = await walletManager.getSigningCosmwasmClient(walletName.value, chainName.value)
signingInjectiveClient.value = await walletManager.getSigningInjectiveClient(walletName.value, chainName.value)
} catch (err) {
error.value = err
console.log("create client error", err)
} finally {
isLoading.value = false
}
}
}
watch([chainName, walletName, account], initialize)
return {
rpcEndpoint,
signingClient: computed(() => chainName.value === 'injective' ? signingInjectiveClient.value : signingClient.value),
queryClient,
signingCosmosClient,
signingCosmWasmClient,
signingInjectiveClient,
error,
isLoading
}
}