forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useSyncWalletSigner.ts
42 lines (36 loc) · 1.16 KB
/
useSyncWalletSigner.ts
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 { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { useEffect } from 'react'
import { useSetRecoilState } from 'recoil'
import { signingCosmWasmClientAtom } from '@dao-dao/state/recoil'
import { useWallet } from './useWallet'
// Save wallet signer in recoil atom so it can be used by contract selectors.
export const useSyncWalletSigner = () => {
const {
chain: { chain_id: chainId },
getSigningCosmWasmClient,
address,
isWalletConnected,
} = useWallet()
const setSigningCosmWasmClient = useSetRecoilState(
signingCosmWasmClientAtom({ chainId })
)
// Save signing client in recoil atom so it can be used by contract selectors.
useEffect(() => {
if (!isWalletConnected) {
setSigningCosmWasmClient(undefined)
return
}
;(async () => {
const signingCosmWasmClient = await getSigningCosmWasmClient()
setSigningCosmWasmClient(
// cosmos-kit has an older version of the package. This is a workaround.
signingCosmWasmClient as unknown as SigningCosmWasmClient
)
})()
}, [
setSigningCosmWasmClient,
address,
isWalletConnected,
getSigningCosmWasmClient,
])
}