@reown/appkit-wagmi-react-native 1.2.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/adapter.js +143 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/UniversalConnector.js +214 -0
- package/lib/commonjs/connectors/UniversalConnector.js.map +1 -0
- package/lib/commonjs/index.js +8 -121
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/helpers.js +16 -44
- package/lib/commonjs/utils/helpers.js.map +1 -1
- package/lib/module/adapter.js +136 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/UniversalConnector.js +208 -0
- package/lib/module/connectors/UniversalConnector.js.map +1 -0
- package/lib/module/index.js +3 -92
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/helpers.js +13 -39
- package/lib/module/utils/helpers.js.map +1 -1
- package/lib/typescript/adapter.d.ts +24 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts +5 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -25
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/helpers.d.ts +3 -13
- package/lib/typescript/utils/helpers.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/adapter.ts +180 -0
- package/src/connectors/UniversalConnector.ts +228 -0
- package/src/index.tsx +3 -122
- package/src/utils/helpers.ts +17 -56
- package/lib/commonjs/client.js +0 -516
- package/lib/commonjs/client.js.map +0 -1
- package/lib/commonjs/connectors/WalletConnectConnector.js +0 -353
- package/lib/commonjs/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/commonjs/utils/defaultWagmiConfig.js +0 -46
- package/lib/commonjs/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/module/client.js +0 -510
- package/lib/module/client.js.map +0 -1
- package/lib/module/connectors/WalletConnectConnector.js +0 -348
- package/lib/module/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/module/utils/defaultWagmiConfig.js +0 -40
- package/lib/module/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/typescript/client.d.ts +0 -40
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/connectors/WalletConnectConnector.d.ts +0 -74
- package/lib/typescript/connectors/WalletConnectConnector.d.ts.map +0 -1
- package/lib/typescript/utils/defaultWagmiConfig.d.ts +0 -11
- package/lib/typescript/utils/defaultWagmiConfig.d.ts.map +0 -1
- package/src/client.ts +0 -644
- package/src/connectors/WalletConnectConnector.ts +0 -469
- package/src/utils/defaultWagmiConfig.ts +0 -53
package/src/client.ts
DELETED
|
@@ -1,644 +0,0 @@
|
|
|
1
|
-
import { formatUnits, type Hex, parseUnits } from 'viem';
|
|
2
|
-
import {
|
|
3
|
-
type GetAccountReturnType,
|
|
4
|
-
type GetEnsAddressReturnType,
|
|
5
|
-
type Connector as WagmiConnector,
|
|
6
|
-
connect,
|
|
7
|
-
reconnect,
|
|
8
|
-
disconnect,
|
|
9
|
-
signMessage,
|
|
10
|
-
getAccount,
|
|
11
|
-
switchChain,
|
|
12
|
-
watchAccount,
|
|
13
|
-
watchConnectors,
|
|
14
|
-
getEnsName,
|
|
15
|
-
getEnsAvatar as wagmiGetEnsAvatar,
|
|
16
|
-
getEnsAddress as wagmiGetEnsAddress,
|
|
17
|
-
getBalance,
|
|
18
|
-
prepareTransactionRequest,
|
|
19
|
-
estimateGas as wagmiEstimateGas,
|
|
20
|
-
sendTransaction as wagmiSendTransaction,
|
|
21
|
-
waitForTransactionReceipt,
|
|
22
|
-
writeContract as wagmiWriteContract
|
|
23
|
-
} from '@wagmi/core';
|
|
24
|
-
import { normalize } from 'viem/ens';
|
|
25
|
-
import { mainnet, type Chain } from '@wagmi/core/chains';
|
|
26
|
-
import EthereumProvider, { OPTIONAL_METHODS } from '@walletconnect/ethereum-provider';
|
|
27
|
-
import { type JsonRpcError } from '@walletconnect/jsonrpc-types';
|
|
28
|
-
import {
|
|
29
|
-
type CaipAddress,
|
|
30
|
-
type CaipNetwork,
|
|
31
|
-
type CaipNetworkId,
|
|
32
|
-
type ConnectionControllerClient,
|
|
33
|
-
type Connector,
|
|
34
|
-
type LibraryOptions,
|
|
35
|
-
type NetworkControllerClient,
|
|
36
|
-
type PublicStateControllerState,
|
|
37
|
-
type SendTransactionArgs,
|
|
38
|
-
type Token,
|
|
39
|
-
AppKitScaffold,
|
|
40
|
-
type WriteContractArgs,
|
|
41
|
-
type AppKitFrameProvider,
|
|
42
|
-
type EstimateGasTransactionArgs
|
|
43
|
-
} from '@reown/appkit-scaffold-react-native';
|
|
44
|
-
import { HelpersUtil, StorageUtil } from '@reown/appkit-scaffold-utils-react-native';
|
|
45
|
-
import {
|
|
46
|
-
NetworkUtil,
|
|
47
|
-
NamesUtil,
|
|
48
|
-
ErrorUtil,
|
|
49
|
-
ConstantsUtil,
|
|
50
|
-
PresetsUtil,
|
|
51
|
-
type ConnectorType
|
|
52
|
-
} from '@reown/appkit-common-react-native';
|
|
53
|
-
import {
|
|
54
|
-
SIWEController,
|
|
55
|
-
getDidChainId,
|
|
56
|
-
getDidAddress,
|
|
57
|
-
type AppKitSIWEClient
|
|
58
|
-
} from '@reown/appkit-siwe-react-native';
|
|
59
|
-
import {
|
|
60
|
-
getCaipDefaultChain,
|
|
61
|
-
getAuthCaipNetworks,
|
|
62
|
-
getWalletConnectCaipNetworks,
|
|
63
|
-
requireCaipAddress
|
|
64
|
-
} from './utils/helpers';
|
|
65
|
-
import { defaultWagmiConfig } from './utils/defaultWagmiConfig';
|
|
66
|
-
|
|
67
|
-
// -- Types ---------------------------------------------------------------------
|
|
68
|
-
type WagmiConfig = ReturnType<typeof defaultWagmiConfig>;
|
|
69
|
-
|
|
70
|
-
export interface AppKitClientOptions extends Omit<LibraryOptions, 'defaultChain' | 'tokens'> {
|
|
71
|
-
wagmiConfig: WagmiConfig;
|
|
72
|
-
siweConfig?: AppKitSIWEClient;
|
|
73
|
-
defaultChain?: Chain;
|
|
74
|
-
chainImages?: Record<number, string>;
|
|
75
|
-
connectorImages?: Record<string, string>;
|
|
76
|
-
tokens?: Record<number, Token>;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export type AppKitOptions = Omit<AppKitClientOptions, '_sdkVersion'>;
|
|
80
|
-
|
|
81
|
-
// @ts-expect-error: Overriden state type is correct
|
|
82
|
-
interface AppKitState extends PublicStateControllerState {
|
|
83
|
-
selectedNetworkId: number | undefined;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// -- Client --------------------------------------------------------------------
|
|
87
|
-
export class AppKit extends AppKitScaffold {
|
|
88
|
-
private hasSyncedConnectedAccount = false;
|
|
89
|
-
|
|
90
|
-
private options: AppKitClientOptions | undefined = undefined;
|
|
91
|
-
|
|
92
|
-
private wagmiConfig: WagmiConfig;
|
|
93
|
-
|
|
94
|
-
public constructor(options: AppKitClientOptions) {
|
|
95
|
-
const { wagmiConfig, siweConfig, defaultChain, tokens, _sdkVersion, ...appKitOptions } =
|
|
96
|
-
options;
|
|
97
|
-
|
|
98
|
-
if (!wagmiConfig) {
|
|
99
|
-
throw new Error('appkit:constructor - wagmiConfig is undefined');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (!appKitOptions.projectId) {
|
|
103
|
-
throw new Error(ErrorUtil.ALERT_ERRORS.PROJECT_ID_NOT_CONFIGURED.shortMessage);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const networkControllerClient: NetworkControllerClient = {
|
|
107
|
-
switchCaipNetwork: async caipNetwork => {
|
|
108
|
-
const chainId = NetworkUtil.caipNetworkIdToNumber(caipNetwork?.id);
|
|
109
|
-
if (chainId) {
|
|
110
|
-
await switchChain(wagmiConfig, { chainId });
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
|
|
114
|
-
async getApprovedCaipNetworksData() {
|
|
115
|
-
const walletChoice = await StorageUtil.getConnectedConnector();
|
|
116
|
-
const walletConnectType =
|
|
117
|
-
PresetsUtil.ConnectorTypesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID]!;
|
|
118
|
-
|
|
119
|
-
const authType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID]!;
|
|
120
|
-
|
|
121
|
-
if (walletChoice?.includes(walletConnectType)) {
|
|
122
|
-
const connector = wagmiConfig.connectors.find(
|
|
123
|
-
c => c.id === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
return getWalletConnectCaipNetworks(connector);
|
|
127
|
-
} else if (authType) {
|
|
128
|
-
return getAuthCaipNetworks();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return { approvedCaipNetworkIds: undefined, supportsAllNetworks: true };
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const connectionControllerClient: ConnectionControllerClient = {
|
|
136
|
-
connectWalletConnect: async (onUri, walletUniversalLink) => {
|
|
137
|
-
const connector = wagmiConfig.connectors.find(
|
|
138
|
-
c => c.id === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID
|
|
139
|
-
);
|
|
140
|
-
if (!connector) {
|
|
141
|
-
throw new Error(
|
|
142
|
-
'connectionControllerClient:getWalletConnectUri - connector is undefined'
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const provider = (await connector.getProvider()) as Awaited<
|
|
147
|
-
ReturnType<(typeof EthereumProvider)['init']>
|
|
148
|
-
>;
|
|
149
|
-
|
|
150
|
-
provider.on('display_uri', data => {
|
|
151
|
-
onUri(data);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
// When connecting through walletconnect, we need to set the clientId in the store
|
|
155
|
-
const clientId = await provider.signer?.client?.core?.crypto?.getClientId();
|
|
156
|
-
if (clientId) {
|
|
157
|
-
this.setClientId(clientId);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const chainId = NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id);
|
|
161
|
-
|
|
162
|
-
// SIWE
|
|
163
|
-
const siweParams = await siweConfig?.getMessageParams?.();
|
|
164
|
-
// Make sure client uses ethereum provider version that supports `authenticate`
|
|
165
|
-
if (
|
|
166
|
-
siweConfig?.options?.enabled &&
|
|
167
|
-
typeof provider?.authenticate === 'function' &&
|
|
168
|
-
siweParams &&
|
|
169
|
-
Object.keys(siweParams || {}).length > 0
|
|
170
|
-
) {
|
|
171
|
-
// @ts-expect-error - setting requested chains beforehand avoids wagmi auto disconnecting the session when `connect` is called because it things chains are stale
|
|
172
|
-
await connector.setRequestedChainsIds(siweParams.chains);
|
|
173
|
-
const result = await provider.authenticate(
|
|
174
|
-
{
|
|
175
|
-
nonce: await siweConfig.getNonce(),
|
|
176
|
-
methods: [...OPTIONAL_METHODS],
|
|
177
|
-
...siweParams
|
|
178
|
-
},
|
|
179
|
-
walletUniversalLink
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
|
|
183
|
-
const signedCacao = result?.auths?.[0];
|
|
184
|
-
if (signedCacao) {
|
|
185
|
-
const { p, s } = signedCacao;
|
|
186
|
-
const cacaoChainId = getDidChainId(p.iss) || '';
|
|
187
|
-
const address = getDidAddress(p.iss);
|
|
188
|
-
try {
|
|
189
|
-
// Kicks off verifyMessage and populates external states
|
|
190
|
-
const message = provider.signer.client.formatAuthMessage({
|
|
191
|
-
request: p,
|
|
192
|
-
iss: p.iss
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
await SIWEController.verifyMessage({
|
|
196
|
-
message,
|
|
197
|
-
signature: s.s,
|
|
198
|
-
cacao: signedCacao
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
if (address && chainId) {
|
|
202
|
-
const session = {
|
|
203
|
-
address,
|
|
204
|
-
chainId: parseInt(cacaoChainId, 10)
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
SIWEController.setSession(session);
|
|
208
|
-
SIWEController.onSignIn?.(session);
|
|
209
|
-
}
|
|
210
|
-
} catch (error) {
|
|
211
|
-
// eslint-disable-next-line no-console
|
|
212
|
-
console.error('Error verifying message', error);
|
|
213
|
-
// eslint-disable-next-line no-console
|
|
214
|
-
await provider.disconnect().catch(console.error);
|
|
215
|
-
// eslint-disable-next-line no-console
|
|
216
|
-
await SIWEController.signOut().catch(console.error);
|
|
217
|
-
throw error;
|
|
218
|
-
}
|
|
219
|
-
/*
|
|
220
|
-
* Unassign the connector from the wagmiConfig and allow connect() to reassign it in the next step
|
|
221
|
-
* this avoids case where wagmi throws because the connector is already connected
|
|
222
|
-
* what we need connect() to do is to only setup internal event listeners
|
|
223
|
-
*/
|
|
224
|
-
this.wagmiConfig.state.current = '';
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
await connect(this.wagmiConfig, { connector, chainId });
|
|
229
|
-
},
|
|
230
|
-
|
|
231
|
-
connectExternal: async ({ id }) => {
|
|
232
|
-
const connector = wagmiConfig.connectors.find(c => c.id === id);
|
|
233
|
-
if (!connector) {
|
|
234
|
-
throw new Error('connectionControllerClient:connectExternal - connector is undefined');
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// If connecting with something else than walletconnect, we need to clear the clientId in the store
|
|
238
|
-
this.setClientId(null);
|
|
239
|
-
|
|
240
|
-
const chainId = NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id);
|
|
241
|
-
await connect(this.wagmiConfig, { connector, chainId });
|
|
242
|
-
},
|
|
243
|
-
|
|
244
|
-
signMessage: async message => signMessage(this.wagmiConfig, { message }),
|
|
245
|
-
|
|
246
|
-
disconnect: async () => {
|
|
247
|
-
await disconnect(this.wagmiConfig);
|
|
248
|
-
this.setClientId(null);
|
|
249
|
-
|
|
250
|
-
if (siweConfig?.options?.signOutOnDisconnect) {
|
|
251
|
-
await SIWEController.signOut();
|
|
252
|
-
}
|
|
253
|
-
},
|
|
254
|
-
|
|
255
|
-
sendTransaction: async (data: SendTransactionArgs) => {
|
|
256
|
-
const { chainId } = getAccount(this.wagmiConfig);
|
|
257
|
-
|
|
258
|
-
const txParams = {
|
|
259
|
-
account: data.address,
|
|
260
|
-
to: data.to,
|
|
261
|
-
value: data.value,
|
|
262
|
-
gas: data.gas,
|
|
263
|
-
gasPrice: data.gasPrice,
|
|
264
|
-
data: data.data,
|
|
265
|
-
chainId,
|
|
266
|
-
type: 'legacy' as const
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
await prepareTransactionRequest(this.wagmiConfig, txParams);
|
|
270
|
-
const tx = await wagmiSendTransaction(this.wagmiConfig, txParams);
|
|
271
|
-
|
|
272
|
-
await waitForTransactionReceipt(this.wagmiConfig, { hash: tx, timeout: 25000 });
|
|
273
|
-
|
|
274
|
-
return tx;
|
|
275
|
-
},
|
|
276
|
-
|
|
277
|
-
writeContract: async (data: WriteContractArgs) => {
|
|
278
|
-
const caipAddress = this.getCaipAddress() || '';
|
|
279
|
-
const account = requireCaipAddress(caipAddress);
|
|
280
|
-
const chainId = NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id);
|
|
281
|
-
|
|
282
|
-
const tx = await wagmiWriteContract(wagmiConfig, {
|
|
283
|
-
chainId,
|
|
284
|
-
address: data.tokenAddress,
|
|
285
|
-
account,
|
|
286
|
-
abi: data.abi,
|
|
287
|
-
functionName: data.method,
|
|
288
|
-
args: [data.receiverAddress, data.tokenAmount]
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
return tx;
|
|
292
|
-
},
|
|
293
|
-
|
|
294
|
-
estimateGas: async ({
|
|
295
|
-
address,
|
|
296
|
-
to,
|
|
297
|
-
data,
|
|
298
|
-
chainNamespace
|
|
299
|
-
}: EstimateGasTransactionArgs): Promise<bigint> => {
|
|
300
|
-
if (chainNamespace && chainNamespace !== 'eip155') {
|
|
301
|
-
throw new Error('estimateGas - chainNamespace is not eip155');
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
try {
|
|
305
|
-
const result = await wagmiEstimateGas(this.wagmiConfig, {
|
|
306
|
-
account: address as Hex,
|
|
307
|
-
to: to as Hex,
|
|
308
|
-
data: data as Hex,
|
|
309
|
-
type: 'legacy'
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
return result;
|
|
313
|
-
} catch (error) {
|
|
314
|
-
throw new Error('WagmiAdapter:estimateGas - error estimating gas');
|
|
315
|
-
}
|
|
316
|
-
},
|
|
317
|
-
|
|
318
|
-
parseUnits,
|
|
319
|
-
|
|
320
|
-
formatUnits,
|
|
321
|
-
|
|
322
|
-
getEnsAddress: async (value: string) => {
|
|
323
|
-
try {
|
|
324
|
-
if (!this.wagmiConfig) {
|
|
325
|
-
throw new Error(
|
|
326
|
-
'networkControllerClient:getApprovedCaipNetworksData - wagmiConfig is undefined'
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
const chainId = Number(NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id));
|
|
330
|
-
let ensName: boolean | GetEnsAddressReturnType = false;
|
|
331
|
-
let wcName: boolean | string = false;
|
|
332
|
-
if (NamesUtil.isReownName(value)) {
|
|
333
|
-
wcName = (await this.resolveReownName(value)) || false;
|
|
334
|
-
}
|
|
335
|
-
if (chainId === 1) {
|
|
336
|
-
ensName = await wagmiGetEnsAddress(this.wagmiConfig, {
|
|
337
|
-
name: normalize(value),
|
|
338
|
-
chainId
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return ensName || wcName || false;
|
|
343
|
-
} catch {
|
|
344
|
-
return false;
|
|
345
|
-
}
|
|
346
|
-
},
|
|
347
|
-
getEnsAvatar: async (value: string) => {
|
|
348
|
-
const chainId = Number(NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id));
|
|
349
|
-
if (chainId !== mainnet.id) {
|
|
350
|
-
return false;
|
|
351
|
-
}
|
|
352
|
-
const avatar = await wagmiGetEnsAvatar(this.wagmiConfig, {
|
|
353
|
-
name: normalize(value),
|
|
354
|
-
chainId
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
return avatar || false;
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
super({
|
|
362
|
-
networkControllerClient,
|
|
363
|
-
connectionControllerClient,
|
|
364
|
-
siweControllerClient: siweConfig,
|
|
365
|
-
defaultChain: getCaipDefaultChain(defaultChain),
|
|
366
|
-
tokens: HelpersUtil.getCaipTokens(tokens),
|
|
367
|
-
_sdkVersion: _sdkVersion ?? `react-native-wagmi-${ConstantsUtil.VERSION}`,
|
|
368
|
-
...appKitOptions
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
this.options = options;
|
|
372
|
-
this.wagmiConfig = wagmiConfig;
|
|
373
|
-
|
|
374
|
-
this.syncRequestedNetworks([...wagmiConfig.chains]);
|
|
375
|
-
this.syncConnectors([...wagmiConfig.connectors]);
|
|
376
|
-
|
|
377
|
-
watchConnectors(wagmiConfig, {
|
|
378
|
-
onChange: connectors => this.syncConnectors([...connectors])
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
watchAccount(wagmiConfig, {
|
|
382
|
-
onChange: (accountData, prevAccountData) => {
|
|
383
|
-
this.syncAccount({ ...accountData });
|
|
384
|
-
|
|
385
|
-
if (accountData.status === 'disconnected' && prevAccountData.status === 'connected') {
|
|
386
|
-
this.close();
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
// -- Public ------------------------------------------------------------------
|
|
393
|
-
|
|
394
|
-
// @ts-expect-error: Overriden state type is correct
|
|
395
|
-
public override getState() {
|
|
396
|
-
const state = super.getState();
|
|
397
|
-
|
|
398
|
-
return {
|
|
399
|
-
...state,
|
|
400
|
-
selectedNetworkId: NetworkUtil.caipNetworkIdToNumber(state.selectedNetworkId)
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// @ts-expect-error: Overriden state type is correct
|
|
405
|
-
public override subscribeState(callback: (state: AppKitState) => void) {
|
|
406
|
-
return super.subscribeState(state =>
|
|
407
|
-
callback({
|
|
408
|
-
...state,
|
|
409
|
-
selectedNetworkId: NetworkUtil.caipNetworkIdToNumber(state.selectedNetworkId)
|
|
410
|
-
})
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// -- Private -----------------------------------------------------------------
|
|
415
|
-
private syncRequestedNetworks(chains: Chain[]) {
|
|
416
|
-
const requestedCaipNetworks = chains?.map(
|
|
417
|
-
chain =>
|
|
418
|
-
({
|
|
419
|
-
id: `${ConstantsUtil.EIP155}:${chain.id}`,
|
|
420
|
-
name: chain.name,
|
|
421
|
-
imageId: PresetsUtil.EIP155NetworkImageIds[chain.id],
|
|
422
|
-
imageUrl: this.options?.chainImages?.[chain.id]
|
|
423
|
-
}) as CaipNetwork
|
|
424
|
-
);
|
|
425
|
-
this.setRequestedCaipNetworks(requestedCaipNetworks ?? []);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
private async syncAccount({
|
|
429
|
-
address,
|
|
430
|
-
isConnected,
|
|
431
|
-
chainId,
|
|
432
|
-
connector,
|
|
433
|
-
isConnecting,
|
|
434
|
-
isReconnecting
|
|
435
|
-
}: Pick<
|
|
436
|
-
GetAccountReturnType,
|
|
437
|
-
'address' | 'isConnected' | 'chainId' | 'connector' | 'isConnecting' | 'isReconnecting'
|
|
438
|
-
>) {
|
|
439
|
-
this.syncNetwork(address, chainId, isConnected);
|
|
440
|
-
this.setLoading(!!connector && (isConnecting || isReconnecting));
|
|
441
|
-
|
|
442
|
-
if (isConnected && address && chainId) {
|
|
443
|
-
const caipAddress: CaipAddress = `${ConstantsUtil.EIP155}:${chainId}:${address}`;
|
|
444
|
-
this.setIsConnected(isConnected);
|
|
445
|
-
this.setCaipAddress(caipAddress);
|
|
446
|
-
await Promise.all([
|
|
447
|
-
this.syncProfile(address, chainId),
|
|
448
|
-
this.syncBalance(address, chainId),
|
|
449
|
-
this.syncConnectedWalletInfo(connector),
|
|
450
|
-
this.getApprovedCaipNetworksData()
|
|
451
|
-
]);
|
|
452
|
-
this.hasSyncedConnectedAccount = true;
|
|
453
|
-
} else if (!isConnected && !isConnecting && !isReconnecting && this.hasSyncedConnectedAccount) {
|
|
454
|
-
this.resetAccount();
|
|
455
|
-
this.resetWcConnection();
|
|
456
|
-
this.resetNetwork();
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
private async syncNetwork(address?: Hex, chainId?: number, isConnected?: boolean) {
|
|
461
|
-
const chain = this.wagmiConfig.chains.find((c: Chain) => c.id === chainId);
|
|
462
|
-
|
|
463
|
-
if (chain || chainId) {
|
|
464
|
-
const name = chain?.name ?? chainId?.toString();
|
|
465
|
-
const id = Number(chain?.id ?? chainId);
|
|
466
|
-
const caipChainId: CaipNetworkId = `${ConstantsUtil.EIP155}:${id}`;
|
|
467
|
-
this.setCaipNetwork({
|
|
468
|
-
id: caipChainId,
|
|
469
|
-
name,
|
|
470
|
-
imageId: PresetsUtil.EIP155NetworkImageIds[id],
|
|
471
|
-
imageUrl: this.options?.chainImages?.[id]
|
|
472
|
-
});
|
|
473
|
-
if (isConnected && address && chainId) {
|
|
474
|
-
const caipAddress: CaipAddress = `${ConstantsUtil.EIP155}:${id}:${address}`;
|
|
475
|
-
this.setCaipAddress(caipAddress);
|
|
476
|
-
if (chain?.blockExplorers?.default?.url) {
|
|
477
|
-
const url = `${chain.blockExplorers.default.url}/address/${address}`;
|
|
478
|
-
this.setAddressExplorerUrl(url);
|
|
479
|
-
} else {
|
|
480
|
-
this.setAddressExplorerUrl(undefined);
|
|
481
|
-
}
|
|
482
|
-
if (this.hasSyncedConnectedAccount) {
|
|
483
|
-
await this.syncProfile(address, chainId);
|
|
484
|
-
await this.syncBalance(address, chainId);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
private async syncProfile(address: Hex, chainId: number) {
|
|
491
|
-
try {
|
|
492
|
-
const response = await this.fetchIdentity({ address });
|
|
493
|
-
|
|
494
|
-
if (!response) {
|
|
495
|
-
throw new Error('Couldnt fetch idendity');
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
this.setProfileName(response.name);
|
|
499
|
-
this.setProfileImage(response.avatar);
|
|
500
|
-
} catch {
|
|
501
|
-
if (chainId === mainnet.id) {
|
|
502
|
-
const profileName = await getEnsName(this.wagmiConfig, { address, chainId });
|
|
503
|
-
if (profileName) {
|
|
504
|
-
this.setProfileName(profileName);
|
|
505
|
-
const profileImage = await wagmiGetEnsAvatar(this.wagmiConfig, {
|
|
506
|
-
name: profileName,
|
|
507
|
-
chainId
|
|
508
|
-
});
|
|
509
|
-
if (profileImage) {
|
|
510
|
-
this.setProfileImage(profileImage);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
} else {
|
|
514
|
-
this.setProfileName(undefined);
|
|
515
|
-
this.setProfileImage(undefined);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
private async syncBalance(address: Hex, chainId: number) {
|
|
521
|
-
const chain = this.wagmiConfig.chains.find((c: Chain) => c.id === chainId);
|
|
522
|
-
try {
|
|
523
|
-
if (chain) {
|
|
524
|
-
const balance = await getBalance(this.wagmiConfig, {
|
|
525
|
-
address,
|
|
526
|
-
chainId: chain.id,
|
|
527
|
-
token: this.options?.tokens?.[chainId]?.address as Hex
|
|
528
|
-
});
|
|
529
|
-
const formattedBalance = formatUnits(balance.value, balance.decimals);
|
|
530
|
-
this.setBalance(formattedBalance, balance.symbol);
|
|
531
|
-
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
this.setBalance(undefined, undefined);
|
|
535
|
-
} catch {
|
|
536
|
-
this.setBalance(undefined, undefined);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
private async syncConnectedWalletInfo(connector: GetAccountReturnType['connector']) {
|
|
541
|
-
if (!connector) {
|
|
542
|
-
throw Error('syncConnectedWalletInfo - connector is undefined');
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
if (connector.id === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID && connector.getProvider) {
|
|
546
|
-
const walletConnectProvider = (await connector.getProvider()) as Awaited<
|
|
547
|
-
ReturnType<(typeof EthereumProvider)['init']>
|
|
548
|
-
>;
|
|
549
|
-
if (walletConnectProvider.session) {
|
|
550
|
-
this.setConnectedWalletInfo({
|
|
551
|
-
...walletConnectProvider.session.peer.metadata,
|
|
552
|
-
name: walletConnectProvider.session.peer.metadata.name,
|
|
553
|
-
icon: walletConnectProvider.session.peer.metadata.icons?.[0]
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
} else {
|
|
557
|
-
this.setConnectedWalletInfo({
|
|
558
|
-
id: connector.id,
|
|
559
|
-
name: connector.name,
|
|
560
|
-
icon: this.options?.connectorImages?.[connector.id] ?? connector.icon
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
private syncConnectors(connectors: AppKitClientOptions['wagmiConfig']['connectors']) {
|
|
566
|
-
const uniqueIds = new Set();
|
|
567
|
-
const filteredConnectors = connectors.filter(
|
|
568
|
-
item => !uniqueIds.has(item.id) && uniqueIds.add(item.id)
|
|
569
|
-
);
|
|
570
|
-
|
|
571
|
-
const excludedConnectors = [ConstantsUtil.AUTH_CONNECTOR_ID];
|
|
572
|
-
|
|
573
|
-
const _connectors: Connector[] = [];
|
|
574
|
-
filteredConnectors.forEach(({ id, name, icon }) => {
|
|
575
|
-
if (!excludedConnectors.includes(id)) {
|
|
576
|
-
_connectors.push({
|
|
577
|
-
id,
|
|
578
|
-
explorerId: PresetsUtil.ConnectorExplorerIds[id],
|
|
579
|
-
imageId: PresetsUtil.ConnectorImageIds[id] ?? icon,
|
|
580
|
-
imageUrl: this.options?.connectorImages?.[id],
|
|
581
|
-
name: PresetsUtil.ConnectorNamesMap[id] ?? name,
|
|
582
|
-
type: PresetsUtil.ConnectorTypesMap[id] ?? 'EXTERNAL'
|
|
583
|
-
});
|
|
584
|
-
}
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
this.setConnectors(_connectors);
|
|
588
|
-
this.syncWalletConnectListeners(filteredConnectors);
|
|
589
|
-
this.syncAuthConnector(filteredConnectors);
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
private async syncWalletConnectListeners(
|
|
593
|
-
connectors: AppKitClientOptions['wagmiConfig']['connectors']
|
|
594
|
-
) {
|
|
595
|
-
const connector = connectors.find(({ id }) => id === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID);
|
|
596
|
-
if (connector) {
|
|
597
|
-
const provider = (await connector.getProvider()) as EthereumProvider;
|
|
598
|
-
|
|
599
|
-
provider.signer.client.core.relayer.on('relayer_connect', () => {
|
|
600
|
-
provider.signer.client.core.relayer?.provider?.on('payload', (payload: JsonRpcError) => {
|
|
601
|
-
if (payload?.error) {
|
|
602
|
-
this.handleAlertError(payload?.error.message);
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
private async syncAuthConnector(connectors: AppKitClientOptions['wagmiConfig']['connectors']) {
|
|
610
|
-
const authConnector = connectors.find(({ id }) => id === ConstantsUtil.AUTH_CONNECTOR_ID);
|
|
611
|
-
if (authConnector) {
|
|
612
|
-
const provider = await authConnector.getProvider();
|
|
613
|
-
this.addConnector({
|
|
614
|
-
id: ConstantsUtil.AUTH_CONNECTOR_ID,
|
|
615
|
-
type: PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID]!,
|
|
616
|
-
name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.AUTH_CONNECTOR_ID],
|
|
617
|
-
provider
|
|
618
|
-
});
|
|
619
|
-
this.addAuthListeners(authConnector);
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
private async addAuthListeners(connector: WagmiConnector) {
|
|
624
|
-
const connectedConnector: ConnectorType | undefined = await StorageUtil.getItem(
|
|
625
|
-
'@w3m/connected_connector'
|
|
626
|
-
);
|
|
627
|
-
|
|
628
|
-
if (connectedConnector === 'AUTH') {
|
|
629
|
-
// Set loader until it reconnects
|
|
630
|
-
super.setLoading(true);
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
const provider = (await connector.getProvider()) as AppKitFrameProvider;
|
|
634
|
-
|
|
635
|
-
provider.onSetPreferredAccount(async () => {
|
|
636
|
-
await reconnect(this.wagmiConfig, { connectors: [connector] });
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
provider.setOnTimeout(async () => {
|
|
640
|
-
this.handleAlertError(ErrorUtil.ALERT_ERRORS.SOCIALS_TIMEOUT);
|
|
641
|
-
this.setLoading(false);
|
|
642
|
-
});
|
|
643
|
-
}
|
|
644
|
-
}
|