-
Notifications
You must be signed in to change notification settings - Fork 5
/
wallet-client-provider.tsx
179 lines (160 loc) · 4.06 KB
/
wallet-client-provider.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
import React, {
ReactNode,
useCallback,
createContext,
useContext,
useMemo,
useEffect,
} from 'react';
import type { Chain } from 'wagmi';
import { useWallet } from './wallet/use-wallet';
import { useNetwork } from './network/use-network';
import { useAccountChangeListen } from './wallet/use-account-change-listen';
import { useConfig } from '../base-provider';
import { getChainIdNetworkMap } from './utils';
import { useDebug } from '../hooks/use-debug';
import { useNetworkSwitchListen } from './network/use-network-switch-listen';
type WalletClientValue = ReturnType<typeof useWallet> &
ReturnType<typeof useNetwork>;
export const WalletContext = createContext({} as WalletClientValue);
export interface WalletClientProps {
children: ReactNode;
}
export const WalletClientProvider = ({ children }: WalletClientProps) => {
const { forceChain } = useConfig();
const {
address,
provider,
signer,
connector,
isConnected,
isConnecting,
isDisconnected,
openConnectModal,
openChainModal,
openAccountModal,
disconnect,
disconnectAsync,
isLoadingSigner,
isLoadingBalance,
balance,
previousAddress,
setPreviousAddress,
} = useWallet();
const {
connectors,
switchNetwork,
switchingToChain,
stopSwitching,
canProgramaticallySwitchNetwork,
activeChain,
isSwitchingChain,
chains,
isActiveChainUnsupported,
availibleChains,
pendingConnector,
networkError,
} = useNetwork();
const onAccountChange = useCallback(() => console.log('account changed'), []);
const onChainChange = useCallback(() => console.log('account changed'), []);
useAccountChangeListen({
onAccountChange,
onChainChange,
});
useNetworkSwitchListen({
activeConnector: connector,
stopSwitching,
networkError,
});
const chainIdsByNetwork = useMemo(() => {
if (!chains || chains.length === 0) return {};
return getChainIdNetworkMap([...chains, activeChain as Chain]);
}, [chains, activeChain]);
useEffect(() => {
if (!forceChain) return;
// * NOTE: comment the following 3 lines out to disable automatic network switching for forced network
if (canProgramaticallySwitchNetwork) {
// switchNetwork?.(chainIdsByNetwork[forceChain] as number);
}
}, [
forceChain,
canProgramaticallySwitchNetwork,
// switchNetwork,
chainIdsByNetwork,
chains,
]);
const walletClientValue = useMemo(
() =>
({
// wallet
address,
provider,
signer,
connector,
isConnected,
isConnecting,
isDisconnected,
openConnectModal,
openChainModal,
openAccountModal,
disconnect,
disconnectAsync,
isLoadingSigner,
isLoadingBalance,
balance,
previousAddress,
setPreviousAddress,
// network
connectors,
switchNetwork,
switchingToChain,
canProgramaticallySwitchNetwork,
activeChain,
isSwitchingChain,
chains,
isActiveChainUnsupported,
availibleChains,
pendingConnector,
}) as WalletClientValue,
[
// wallet
address,
provider,
signer,
connector,
isConnected,
isConnecting,
isDisconnected,
openConnectModal,
openChainModal,
openAccountModal,
disconnect,
disconnectAsync,
isLoadingSigner,
isLoadingBalance,
balance,
previousAddress,
setPreviousAddress,
// network
connectors,
switchNetwork,
switchingToChain,
canProgramaticallySwitchNetwork,
activeChain,
isSwitchingChain,
chains,
isActiveChainUnsupported,
availibleChains,
pendingConnector,
]
);
useDebug({ name: 'WalletClientProvider', value: walletClientValue }, []);
return (
<WalletContext.Provider value={walletClientValue}>
{children}
</WalletContext.Provider>
);
};
export const useWalletClient = (): WalletClientValue =>
useContext(WalletContext);
export default WalletClientProvider;