-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClientContext.tsx
243 lines (211 loc) · 6.62 KB
/
ClientContext.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use client'
import Client from '@walletconnect/sign-client'
import { PairingTypes, SessionTypes } from '@walletconnect/types'
import { getAppMetadata, getSdkError } from '@walletconnect/utils'
import { Web3Modal } from '@web3modal/standalone'
import { createContext, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import useBalances from '@/hooks/useBalances'
import {
DEFAULT_APP_METADATA,
DEFAULT_CHAINS,
DEFAULT_LOGGER,
DEFAULT_PROJECT_ID,
DEFAULT_RELAY_URL,
DEFAULT_REQUIRED_NAMESPACES
} from '@/constants'
import { AccountBalances } from '@/types/accounts'
interface IContext {
client: Client | undefined
session: SessionTypes.Struct | undefined
connect: (pairing?: { topic: string }) => Promise<void>
disconnect: () => Promise<void>
isInitializing: boolean
relayerRegion: string
pairings: PairingTypes.Struct[]
accounts: string[]
balances: AccountBalances
isFetchingBalances: boolean
setRelayerRegion: any
}
export const ClientContext = createContext<IContext>({} as IContext)
const web3Modal = new Web3Modal({
projectId: DEFAULT_PROJECT_ID!,
themeMode: 'light',
walletConnectVersion: 2,
// Suggest Fireblocks Desktop wallet
explorerRecommendedWalletIds: [
'5864e2ced7c293ed18ac35e0db085c09ed567d67346ccb6f58a0327a75137489'
],
explorerExcludedWalletIds: 'ALL'
})
export const ClientContextProvider = ({ children }: { children: React.ReactNode }) => {
const [client, setClient] = useState<Client>()
const [pairings, setPairings] = useState<PairingTypes.Struct[]>([])
const [session, setSession] = useState<SessionTypes.Struct>()
const [isInitializing, setIsInitializing] = useState(false)
const prevRelayerValue = useRef<string>('')
const [accounts, setAccounts] = useState<string[]>([])
const [relayerRegion, setRelayerRegion] = useState<string>(DEFAULT_RELAY_URL!)
const { balances, isFetchingBalances } = useBalances(accounts)
const reset = () => {
setSession(undefined)
setAccounts([])
setRelayerRegion(DEFAULT_RELAY_URL!)
}
const onSessionConnected = useCallback(async (session: SessionTypes.Struct) => {
// temporary fix to filter duplicate accounts from chains I'm not requesting
let _session = { ...session }
_session.namespaces.algorand.accounts = _session.namespaces.algorand.accounts.filter(
(account) => DEFAULT_CHAINS.some((chain) => account.startsWith(chain))
)
const allAccounts = Object.values(_session.namespaces)
.map((namespace) => namespace.accounts)
.flat()
setSession(_session)
setAccounts(allAccounts)
}, [])
const connect = useCallback(
async (pairing?: { topic: string }) => {
if (typeof client === 'undefined') {
throw new Error('WalletConnect is not initialized')
}
try {
const { uri, approval } = await client.connect({
pairingTopic: pairing?.topic,
requiredNamespaces: DEFAULT_REQUIRED_NAMESPACES
})
if (uri) {
web3Modal.openModal({ uri, standaloneChains: DEFAULT_CHAINS })
}
const session = await approval()
await onSessionConnected(session)
setPairings(client.pairing.getAll({ active: true }))
} catch (error) {
console.error(error)
} finally {
web3Modal.closeModal()
}
},
[client, onSessionConnected]
)
const disconnect = useCallback(async () => {
if (typeof client === 'undefined') {
throw new Error('WalletConnect is not initialized')
}
if (typeof session === 'undefined') {
throw new Error('Session is not connected')
}
try {
await client.disconnect({
topic: session.topic,
reason: getSdkError('USER_DISCONNECTED')
})
} catch (error) {
console.error('SignClient.disconnect failed:', error)
} finally {
reset()
}
}, [client, session])
const subscribeToEvents = useCallback(
async (client: Client) => {
if (typeof client === undefined) {
throw new Error('WalletConnect is not initialized')
}
client.on('session_event', (args) => {
console.log('EVENT', 'session_event', args)
})
client.on('session_update', ({ topic, params }) => {
console.log('EVENT', 'session_update', { topic, params })
const { namespaces } = params
const session = client.session.get(topic)
const updatedSession = { ...session, namespaces }
onSessionConnected(updatedSession)
})
client.on('session_delete', () => {
console.log('EVENT', 'session_delete')
reset()
})
},
[onSessionConnected]
)
const checkPersistedState = useCallback(
async (client: Client) => {
if (typeof client === 'undefined') {
throw new Error('WalletConnect is not initialized')
}
setPairings(client.pairing.getAll({ active: true }))
if (typeof session !== 'undefined') return
if (client.session.length) {
const lastKeyIndex = client.session.keys.length - 1
const restoredSession = client.session.get(client.session.keys[lastKeyIndex])
await onSessionConnected(restoredSession)
return restoredSession
}
},
[onSessionConnected, session]
)
const createClient = useCallback(async () => {
try {
setIsInitializing(true)
const client = await Client.init({
logger: DEFAULT_LOGGER,
relayUrl: relayerRegion,
projectId: DEFAULT_PROJECT_ID!,
metadata: getAppMetadata() || DEFAULT_APP_METADATA
})
setClient(client)
prevRelayerValue.current = relayerRegion
await subscribeToEvents(client)
await checkPersistedState(client)
} catch (error) {
throw error
} finally {
setIsInitializing(false)
}
}, [checkPersistedState, relayerRegion, subscribeToEvents])
useEffect(() => {
if (!client) {
createClient()
} else if (prevRelayerValue.current !== relayerRegion) {
client.core.relayer.restartTransport(relayerRegion)
prevRelayerValue.current = relayerRegion
}
}, [client, createClient, relayerRegion])
const contextValue = useMemo(
() => ({
accounts,
balances,
client,
connect,
disconnect,
isFetchingBalances,
isInitializing,
pairings,
relayerRegion,
session,
setRelayerRegion
}),
[
accounts,
balances,
client,
connect,
disconnect,
isFetchingBalances,
isInitializing,
pairings,
relayerRegion,
session,
setRelayerRegion
]
)
return (
<ClientContext.Provider
value={{
...contextValue
}}
>
{children}
</ClientContext.Provider>
)
}