-
Notifications
You must be signed in to change notification settings - Fork 0
/
WalletContext.tsx
68 lines (58 loc) · 2.02 KB
/
WalletContext.tsx
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
63
64
65
66
67
68
import { FC, PropsWithChildren, useEffect, useMemo, useState } from 'react'
import { GasPrice, SigningStargateClient } from '@cosmjs/stargate'
import { useChain } from '@cosmos-kit/react'
import { chainName } from '../config'
import { AMINO_TYPES, JUNO_GAS_PRICE, REGISTRY } from './BurnerClientProvider'
import { ExtendedHttpEndpoint } from '@cosmos-kit/core/types/types/manager'
import createContext from '../utils/createContext'
interface WalletReturn {
signingClient: SigningStargateClient | undefined
address: string | undefined
isWalletConnected: boolean
connect: () => Promise<void>
rpcEndpoint: string | ExtendedHttpEndpoint | undefined
}
const [useWallet, _WalletProvider] = createContext<WalletReturn>('')
const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
const {
isWalletConnected,
connect,
address,
getOfflineSignerDirect: getOfflineSigner,
getRpcEndpoint,
} = useChain(chainName)
const [signingClient, setSigningClient] = useState<SigningStargateClient>()
const [rpcEndpoint, setRpcEndpoint] = useState<string | ExtendedHttpEndpoint>()
useEffect(() => {
;(async function init() {
if (signingClient) return
const offlineSigner = await getOfflineSigner()
const endpoint = await getRpcEndpoint()
setRpcEndpoint(endpoint)
setSigningClient(await SigningStargateClient.connectWithSigner(
endpoint,
offlineSigner,
{
// @ts-ignore
registry: REGISTRY,
aminoTypes: AMINO_TYPES,
gasPrice: JUNO_GAS_PRICE
},
))
})()
}, [getOfflineSigner, getRpcEndpoint, signingClient])
const contextValue = useMemo<WalletReturn>(
() => ({
signingClient,
address,
connect,
isWalletConnected,
// TODO: don't hard-code
rpcEndpoint: 'https://uni-rpc.reece.sh'
}),
[address, connect, isWalletConnected, signingClient]
)
return <_WalletProvider value={contextValue}>{children}</_WalletProvider>
}
export default useWallet
export { WalletProvider }