-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ChainAdapterBlueprint.ts
528 lines (454 loc) · 16.3 KB
/
ChainAdapterBlueprint.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import {
getW3mThemeVariables,
type CaipAddress,
type CaipNetwork,
type ChainNamespace
} from '@reown/appkit-common'
import type { ChainAdapterConnector } from './ChainAdapterConnector.js'
import {
AccountController,
OptionsController,
ThemeController,
type AccountType,
type AccountControllerState,
type Connector as AppKitConnector,
type AuthConnector,
type Metadata,
type Tokens,
type WriteContractArgs
} from '@reown/appkit-core'
import type UniversalProvider from '@walletconnect/universal-provider'
import type { W3mFrameProvider } from '@reown/appkit-wallet'
import { ConstantsUtil, PresetsUtil } from '@reown/appkit-utils'
import type { AppKitOptions } from '../utils/index.js'
import type { AppKit } from '../client.js'
import { snapshot } from 'valtio/vanilla'
type EventName = 'disconnect' | 'accountChanged' | 'switchNetwork' | 'pendingTransactions'
type EventData = {
disconnect: () => void
accountChanged: { address: string; chainId?: number | string }
switchNetwork: { address?: string; chainId: number | string }
pendingTransactions: () => void
}
type EventCallback<T extends EventName> = (data: EventData[T]) => void
/**
* Abstract class representing a chain adapter blueprint.
* @template Connector - The type of connector extending ChainAdapterConnector
*/
export abstract class AdapterBlueprint<
Connector extends ChainAdapterConnector = ChainAdapterConnector
> {
public namespace: ChainNamespace | undefined
public caipNetworks?: CaipNetwork[]
public projectId?: string
protected availableConnectors: Connector[] = []
protected connector?: Connector
protected provider?: Connector['provider']
private eventListeners = new Map<EventName, Set<EventCallback<EventName>>>()
/**
* Creates an instance of AdapterBlueprint.
* @param {AdapterBlueprint.Params} params - The parameters for initializing the adapter
*/
constructor(params?: AdapterBlueprint.Params) {
if (params) {
this.construct(params)
}
}
/**
* Initializes the adapter with the given parameters.
* @param {AdapterBlueprint.Params} params - The parameters for initializing the adapter
*/
construct(params: AdapterBlueprint.Params) {
this.caipNetworks = params.networks
this.projectId = params.projectId
this.namespace = params.namespace
}
/**
* Gets the available connectors.
* @returns {Connector[]} An array of available connectors
*/
public get connectors(): Connector[] {
return this.availableConnectors
}
/**
* Gets the supported networks.
* @returns {CaipNetwork[]} An array of supported networks
*/
public get networks(): CaipNetwork[] {
return this.caipNetworks || []
}
/**
* Sets the universal provider for WalletConnect.
* @param {UniversalProvider} universalProvider - The universal provider instance
*/
public setUniversalProvider(universalProvider: UniversalProvider) {
this.addConnector({
id: ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID,
type: 'WALLET_CONNECT',
name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
provider: universalProvider,
imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
chain: this.namespace,
chains: []
} as unknown as Connector)
}
/**
* Sets the auth provider.
* @param {W3mFrameProvider} authProvider - The auth provider instance
*/
public setAuthProvider(authProvider: W3mFrameProvider): void {
this.addConnector({
id: ConstantsUtil.AUTH_CONNECTOR_ID,
type: 'AUTH',
name: 'Auth',
provider: authProvider,
imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.AUTH_CONNECTOR_ID],
chain: this.namespace,
chains: []
} as unknown as Connector)
}
/**
* Adds one or more connectors to the available connectors list.
* @param {...Connector} connectors - The connectors to add
*/
protected addConnector(...connectors: Connector[]) {
if (connectors.some(connector => connector.id === 'ID_AUTH')) {
const authConnector = connectors.find(
connector => connector.id === 'ID_AUTH'
) as AuthConnector
const optionsState = snapshot(OptionsController.state)
const themeMode = ThemeController.getSnapshot().themeMode
const themeVariables = ThemeController.getSnapshot().themeVariables
authConnector?.provider?.syncDappData?.({
metadata: optionsState.metadata as Metadata,
sdkVersion: optionsState.sdkVersion,
projectId: optionsState.projectId,
sdkType: optionsState.sdkType
})
authConnector.provider.syncTheme({
themeMode,
themeVariables,
w3mThemeVariables: getW3mThemeVariables(themeVariables, themeMode)
})
}
const connectorsAdded = new Set<string>()
this.availableConnectors = [...connectors, ...this.availableConnectors].filter(connector => {
if (connectorsAdded.has(connector.id)) {
return false
}
connectorsAdded.add(connector.id)
return true
})
}
protected setStatus(status: AccountControllerState['status'], chainNamespace?: ChainNamespace) {
AccountController.setStatus(status, chainNamespace)
}
/**
* Adds an event listener for a specific event.
* @template T
* @param {T} eventName - The name of the event
* @param {EventCallback<T>} callback - The callback function to be called when the event is emitted
*/
public on<T extends EventName>(eventName: T, callback: EventCallback<T>) {
if (!this.eventListeners.has(eventName)) {
this.eventListeners.set(eventName, new Set())
}
this.eventListeners.get(eventName)?.add(callback as EventCallback<EventName>)
}
/**
* Removes an event listener for a specific event.
* @template T
* @param {T} eventName - The name of the event
* @param {EventCallback<T>} callback - The callback function to be removed
*/
public off<T extends EventName>(eventName: T, callback: EventCallback<T>) {
const listeners = this.eventListeners.get(eventName)
if (listeners) {
listeners.delete(callback as EventCallback<EventName>)
}
}
/**
* Emits an event with the given name and optional data.
* @template T
* @param {T} eventName - The name of the event to emit
* @param {EventData[T]} [data] - The optional data to be passed to the event listeners
*/
protected emit<T extends EventName>(eventName: T, data?: EventData[T]) {
const listeners = this.eventListeners.get(eventName)
if (listeners) {
listeners.forEach(callback => callback(data as EventData[T]))
}
}
/**
* Connects to WalletConnect.
* @param {(uri: string) => void} onUri - Callback function to handle the WalletConnect URI
* @param {number | string} [chainId] - Optional chain ID to connect to
*/
public abstract connectWalletConnect(
onUri: (uri: string) => void,
chainId?: number | string
): Promise<void>
/**
* Connects to a wallet.
* @param {AdapterBlueprint.ConnectParams} params - Connection parameters
* @returns {Promise<AdapterBlueprint.ConnectResult>} Connection result
*/
public abstract connect(
params: AdapterBlueprint.ConnectParams
): Promise<AdapterBlueprint.ConnectResult>
/**
* Gets the accounts for the connected wallet.
* @returns {Promise<AccountType[]>} An array of account objects with their associated type and namespace
*/
public abstract getAccounts(
params: AdapterBlueprint.GetAccountsParams
): Promise<AdapterBlueprint.GetAccountsResult>
/**
* Switches the network.
* @param {AdapterBlueprint.SwitchNetworkParams} params - Network switching parameters
*/
public abstract switchNetwork(params: AdapterBlueprint.SwitchNetworkParams): Promise<void>
/**
* Disconnects the current wallet.
*/
public abstract disconnect(params?: AdapterBlueprint.DisconnectParams): Promise<void>
/**
* Gets the balance for a given address and chain ID.
* @param {AdapterBlueprint.GetBalanceParams} params - Balance retrieval parameters
* @returns {Promise<AdapterBlueprint.GetBalanceResult>} Balance result
*/
public abstract getBalance(
params: AdapterBlueprint.GetBalanceParams
): Promise<AdapterBlueprint.GetBalanceResult>
/**
* Gets the profile for a given address and chain ID.
* @param {AdapterBlueprint.GetProfileParams} params - Profile retrieval parameters
* @returns {Promise<AdapterBlueprint.GetProfileResult>} Profile result
*/
public abstract getProfile(
params: AdapterBlueprint.GetProfileParams
): Promise<AdapterBlueprint.GetProfileResult>
/**
* Synchronizes the connectors with the given options and AppKit instance.
* @param {AppKitOptions} [options] - Optional AppKit options
* @param {AppKit} [appKit] - Optional AppKit instance
*/
public abstract syncConnectors(options?: AppKitOptions, appKit?: AppKit): void
/**
* Synchronizes the connection with the given parameters.
* @param {AdapterBlueprint.SyncConnectionParams} params - Synchronization parameters
* @returns {Promise<AdapterBlueprint.ConnectResult>} Connection result
*/
public abstract syncConnection(
params: AdapterBlueprint.SyncConnectionParams
): Promise<AdapterBlueprint.ConnectResult>
/**
* Signs a message with the connected wallet.
* @param {AdapterBlueprint.SignMessageParams} params - Parameters including message to sign, address, and optional provider
* @returns {Promise<AdapterBlueprint.SignMessageResult>} Object containing the signature
*/
public abstract signMessage(
params: AdapterBlueprint.SignMessageParams
): Promise<AdapterBlueprint.SignMessageResult>
/**
* Estimates gas for a transaction.
* @param {AdapterBlueprint.EstimateGasTransactionArgs} params - Parameters including address, to, data, and optional provider
* @returns {Promise<AdapterBlueprint.EstimateGasTransactionResult>} Object containing the gas estimate
*/
public abstract estimateGas(
params: AdapterBlueprint.EstimateGasTransactionArgs
): Promise<AdapterBlueprint.EstimateGasTransactionResult>
/**
* Sends a transaction.
* @param {AdapterBlueprint.SendTransactionParams} params - Parameters including address, to, data, value, gasPrice, gas, and optional provider
* @returns {Promise<AdapterBlueprint.SendTransactionResult>} Object containing the transaction hash
*/
public abstract sendTransaction(
params: AdapterBlueprint.SendTransactionParams
): Promise<AdapterBlueprint.SendTransactionResult>
/**
* Writes a contract transaction.
* @param {AdapterBlueprint.WriteContractParams} params - Parameters including receiver address, token amount, token address, from address, method, and ABI
* @returns {Promise<AdapterBlueprint.WriteContractResult>} Object containing the transaction hash
*/
public abstract writeContract(
params: AdapterBlueprint.WriteContractParams
): Promise<AdapterBlueprint.WriteContractResult>
/**
* Gets the ENS address for a given name.
* @param {AdapterBlueprint.GetEnsAddressParams} params - Parameters including name
* @returns {Promise<AdapterBlueprint.GetEnsAddressResult>} Object containing the ENS address
*/
public abstract getEnsAddress(
params: AdapterBlueprint.GetEnsAddressParams
): Promise<AdapterBlueprint.GetEnsAddressResult>
/**
* Parses a decimal string value into a bigint with the specified number of decimals.
* @param {AdapterBlueprint.ParseUnitsParams} params - Parameters including value and decimals
* @returns {AdapterBlueprint.ParseUnitsResult} The parsed bigint value
*/
public abstract parseUnits(
params: AdapterBlueprint.ParseUnitsParams
): AdapterBlueprint.ParseUnitsResult
/**
* Formats a bigint value into a decimal string with the specified number of decimals.
* @param {AdapterBlueprint.FormatUnitsParams} params - Parameters including value and decimals
* @returns {AdapterBlueprint.FormatUnitsResult} The formatted decimal string
*/
public abstract formatUnits(
params: AdapterBlueprint.FormatUnitsParams
): AdapterBlueprint.FormatUnitsResult
/**
* Gets the WalletConnect provider.
* @param {AdapterBlueprint.GetWalletConnectProviderParams} params - Parameters including provider, caip networks, and active caip network
* @returns {AdapterBlueprint.GetWalletConnectProviderResult} The WalletConnect provider
*/
public abstract getWalletConnectProvider(
params: AdapterBlueprint.GetWalletConnectProviderParams
): AdapterBlueprint.GetWalletConnectProviderResult
/**
* Reconnects to a wallet.
* @param {AdapterBlueprint.ReconnectParams} params - Reconnection parameters
*/
public reconnect?(params: AdapterBlueprint.ReconnectParams): Promise<void>
public abstract getCapabilities(params: AdapterBlueprint.GetCapabilitiesParams): Promise<unknown>
public abstract grantPermissions(
params: AdapterBlueprint.GrantPermissionsParams
): Promise<unknown>
public abstract revokePermissions(
params: AdapterBlueprint.RevokePermissionsParams
): Promise<`0x${string}`>
}
export namespace AdapterBlueprint {
export type Params = {
namespace?: ChainNamespace
networks?: CaipNetwork[]
projectId?: string
}
export type SwitchNetworkParams = {
caipNetwork: CaipNetwork
provider?: AppKitConnector['provider']
providerType?: AppKitConnector['type']
}
export type GetBalanceParams = {
address: string
chainId: number | string
caipNetwork?: CaipNetwork
tokens?: Tokens
}
export type GetProfileParams = {
address: string
chainId: number | string
}
export type DisconnectParams = {
provider?: AppKitConnector['provider']
providerType?: AppKitConnector['type']
}
export type ConnectParams = {
id: string
provider?: unknown
info?: unknown
type: string
chain?: ChainNamespace
chainId?: number | string
rpcUrl?: string
}
export type ReconnectParams = ConnectParams
export type SyncConnectionParams = {
id: string
namespace: ChainNamespace
chainId?: number | string
rpcUrl: string
}
export type SignMessageParams = {
message: string
address: string
provider?: AppKitConnector['provider']
}
export type SignMessageResult = {
signature: string
}
export type EstimateGasTransactionArgs = {
address: string
to: string
data: string
caipNetwork: CaipNetwork
provider?: AppKitConnector['provider']
}
export type EstimateGasTransactionResult = {
gas: bigint
}
export type WriteContractParams = WriteContractArgs & {
caipNetwork: CaipNetwork
provider?: AppKitConnector['provider']
caipAddress: CaipAddress
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
export type WriteContractResult = {
hash: string
}
export type ParseUnitsParams = {
value: string
decimals: number
}
export type ParseUnitsResult = bigint
export type FormatUnitsParams = {
value: bigint
decimals: number
}
export type FormatUnitsResult = string
export type GetWalletConnectProviderParams = {
provider: AppKitConnector['provider']
caipNetworks: CaipNetwork[]
activeCaipNetwork: CaipNetwork
}
export type GetWalletConnectProviderResult = AppKitConnector['provider']
export type GetCapabilitiesParams = string
export type GrantPermissionsParams = object | readonly unknown[]
export type RevokePermissionsParams = {
pci: string
permissions: unknown[]
expiry: number
address: `0x${string}`
}
export type SendTransactionParams = {
address: `0x${string}`
to: string
data: string
value: bigint | number
gasPrice: bigint | number
gas?: bigint | number
caipNetwork?: CaipNetwork
provider?: AppKitConnector['provider']
}
export type SendTransactionResult = {
hash: string
}
export type GetEnsAddressParams = {
name: string
caipNetwork: CaipNetwork
}
export type GetEnsAddressResult = {
address: string | false
}
export type GetBalanceResult = {
balance: string
symbol: string
}
export type GetProfileResult = {
profileImage?: string
profileName?: string
}
export type ConnectResult = {
id: AppKitConnector['id']
type: AppKitConnector['type']
provider: AppKitConnector['provider']
chainId: number | string
address: string
}
export type GetAccountsResult = { accounts: AccountType[] }
export type GetAccountsParams = {
id: AppKitConnector['id']
namespace?: ChainNamespace
}
}