-
Notifications
You must be signed in to change notification settings - Fork 242
/
ethers6.ts
547 lines (510 loc) · 16.4 KB
/
ethers6.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import type { Abi } from "abitype";
import * as universalethers from "ethers";
import type * as ethers5 from "ethers5";
import type * as ethers6 from "ethers6";
import type { AccessList, Hex, TransactionSerializable } from "viem";
import type { Chain } from "../chains/types.js";
import { getRpcUrlForChain } from "../chains/utils.js";
import type { ThirdwebClient } from "../client/client.js";
import { type ThirdwebContract, getContract } from "../contract/contract.js";
import { toSerializableTransaction } from "../transaction/actions/to-serializable-transaction.js";
import type { PreparedTransaction } from "../transaction/prepare-transaction.js";
import { toHex, uint8ArrayToHex } from "../utils/encoding/hex.js";
import { resolvePromisedValue } from "../utils/promise/resolve-promised-value.js";
import type { Account } from "../wallets/interfaces/wallet.js";
import { normalizeChainId } from "../wallets/utils/normalizeChainId.js";
type Ethers6 = typeof ethers6;
/**
* Checks if the given ethers object is of type ethers5.
* @param ethers - The ethers object to check.
* @returns True if the ethers object is of type ethers5, false otherwise.
* @internal
*/
function isEthers5(
ethers_: typeof ethers5 | typeof ethers6,
): ethers_ is typeof ethers5 {
return "providers" in ethers_;
}
/**
* Asserts that the provided ethers object is of type ethers5.
* If the object is not of type ethers5, an error is thrown.
* @param ethers - The ethers object to be asserted.
* @throws Error - If the ethers object is not of type ethers5.
* @internal
*/
function assertEthers6(
ethers_: typeof ethers5 | typeof ethers6,
): asserts ethers_ is typeof ethers6 {
if (isEthers5(ethers_)) {
throw new Error(
"You seem to be using ethers@5, please use the `ethers5Adapter()",
);
}
}
export const ethers6Adapter = /* @__PURE__ */ (() => {
const ethers = universalethers;
return {
provider: {
/**
* Converts a Thirdweb client and chain ID into an ethers.js provider.
* @param options - The options for converting the Thirdweb client and chain ID into an ethers.js provider.
* @param options.client - The Thirdweb client.
* @param options.chain - The chain.
* @returns The ethers.js provider.
* @example
* ```ts
* import { ethers6Adapter } from "thirdweb/adapters/ethers6";
* const provider = ethers6Adapter.provider.toEthers({ client, chainId });
* ```
*/
toEthers: (options: { client: ThirdwebClient; chain: Chain }) => {
assertEthers6(ethers);
return toEthersProvider(ethers, options.client, options.chain);
},
},
contract: {
/**
* Converts a ThirdwebContract to an ethers.js Contract.
* @param options - The options for converting the ThirdwebContract to an ethers.js Contract.
* @param options.thirdwebContract - The ThirdwebContract to convert.
* @returns A Promise that resolves to an ethers.js Contract.
* @example
* ```ts
* import { ethers6Adapter } from "thirdweb/adapters/ethers6";
* const ethersContract = await ethers6Adapter.contract.toEthers({ thirdwebContract });
* ```
*/
toEthers: (options: { thirdwebContract: ThirdwebContract }) => {
assertEthers6(ethers);
return toEthersContract(ethers, options.thirdwebContract);
},
/**
* Creates a ThirdwebContract instance from an ethers.js contract.
* @param options - The options for creating the ThirdwebContract instance.
* @returns A promise that resolves to a ThirdwebContract instance.
* @example
* ```ts
* import { ethers6Adapter } from "thirdweb/adapters/ethers6";
*
* const twContract = await ethers6Adapter.contract.fromEthersContract({
* client,
* ethersContract,
* chainId,
* });
* ```
*/
fromEthers: (options: FromEthersContractOptions) => {
assertEthers6(ethers);
return fromEthersContract(options);
},
},
signer: {
/**
* Converts an ethers6 Signer into an Wallet object.
* @param options - The options for converting the ethers6 Signer into a Wallet object.
* @param options.signer - The ethers6 Signer object.
* @returns - A Promise that resolves to an Wallet object.
* @example
* ```ts
* import { ethers6Adapter } from "thirdweb/adapters/ethers6";
* const wallet = await ethers6Adapter.signer.fromEthersSigner({ signer });
* ```
*/
fromEthers: (options: { signer: ethers6.Signer }) => {
assertEthers6(ethers);
return fromEthersSigner(options.signer);
},
/**
* Converts a Thirdweb wallet to an ethers.js signer.
* @param options - The options for converting the Thirdweb wallet to an ethers.js signer.
* @param options.client - The thirdweb client.
* @param options.account - The account.
* @param options.chain - The chain.
* @returns A promise that resolves to an ethers.js signer.
* @example
* ```ts
* import { ethers6Adapter } from "thirdweb/adapters/ethers6";
* const signer = await ethers6Adapter.signer.toEthers({ client, chain, account });
* ```
*/
toEthers: (options: {
client: ThirdwebClient;
account: Account;
chain: Chain;
}) => {
assertEthers6(ethers);
return toEthersSigner(
ethers,
options.client,
options.account,
options.chain,
);
},
},
};
})();
/**
* Converts a Thirdweb client and chain ID into an ethers.js provider.
* @param ethers - The ethers.js library instance.
* @param client - The Thirdweb client.
* @param chain - The chain.
* @returns The ethers.js provider.
* @internal
*/
function toEthersProvider(
ethers: Ethers6,
client: ThirdwebClient,
chain: Chain,
) {
const url = getRpcUrlForChain({ client, chain });
const fetchRequest = new ethers.FetchRequest(url);
if (client.secretKey) {
fetchRequest.setHeader("x-secret-key", client.secretKey);
fetchRequest.setHeader("Content-Type", "application/json");
}
return new ethers.JsonRpcProvider(fetchRequest, chain.id, {
staticNetwork: true,
});
}
/**
* Converts a ThirdwebContract to an ethers.js Contract.
* @param ethers - The ethers.js instance.
* @param twContract - The ThirdwebContract to convert.
* @returns A Promise that resolves to an ethers.js Contract.
* @internal
*/
async function toEthersContract<abi extends Abi = []>(
ethers: Ethers6,
twContract: ThirdwebContract<abi>,
): Promise<ethers6.Contract> {
if (twContract.abi) {
return new ethers.Contract(
twContract.address,
JSON.stringify(twContract.abi),
toEthersProvider(ethers, twContract.client, twContract.chain),
);
}
const { resolveContractAbi } = await import(
"../contract/actions/resolve-abi.js"
);
const abi = await resolveContractAbi(twContract);
return new ethers.Contract(
twContract.address,
JSON.stringify(abi),
toEthersProvider(ethers, twContract.client, twContract.chain),
);
}
type FromEthersContractOptions = {
client: ThirdwebClient;
ethersContract: ethers6.Contract;
chain: Chain;
};
/**
* Creates a ThirdwebContract instance from an ethers.js contract.
* @param options - The options for creating the ThirdwebContract instance.
* @returns A promise that resolves to a ThirdwebContract instance.
* @internal
*/
async function fromEthersContract<abi extends Abi>({
client,
ethersContract,
chain,
}: FromEthersContractOptions): Promise<ThirdwebContract<abi>> {
return getContract({
client,
address: await ethersContract.getAddress(),
abi: JSON.parse(ethersContract.interface.formatJson()) as abi,
chain,
});
}
/**
* Converts an ethers5 Signer into an Account object.
* @param signer - The ethers5 Signer object.
* @returns - A Promise that resolves to an Account object.
* @internal
*/
async function fromEthersSigner(signer: ethers6.Signer): Promise<Account> {
const address = await signer.getAddress();
const account: Account = {
address,
signMessage: async ({ message }) => {
return signer.signMessage(
typeof message === "string" ? message : message.raw,
) as Promise<Hex>;
},
signTransaction: async (tx) => {
return signer.signTransaction(alignTxToEthers(tx)) as Promise<Hex>;
},
sendTransaction: async (tx) => {
const transactionHash = (
await signer.sendTransaction(alignTxToEthers(tx))
).hash as Hex;
return {
transactionHash,
};
},
signTypedData: async (data) => {
return (await signer.signTypedData(
data.domain as ethers6.TypedDataDomain,
data.types as Record<string, ethers6.ethers.TypedDataField[]>,
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix later
data.message as Record<string, any>,
)) as Hex;
},
};
return account;
}
/**
* Converts a Thirdweb account to an ethers.js signer.
* @param ethers - The ethers.js library.
* @param client - The Thirdweb client.
* @param chain - The blockchain chain.
* @param account - The Thirdweb account.
* @returns A promise that resolves to an ethers.js signer.
* @internal
*/
export async function toEthersSigner(
ethers: Ethers6,
client: ThirdwebClient,
account: Account,
chain: Chain,
): Promise<ethers6.Signer> {
class ThirdwebAdapterSigner extends ethers.AbstractSigner<ethers6.JsonRpcProvider> {
private address: string;
override provider: ethers6.ethers.JsonRpcProvider;
constructor(provider: ethers6.JsonRpcProvider, address: string) {
super(provider);
this.address = address;
this.provider = provider;
}
override async getAddress(): Promise<string> {
// needs to be a promise because ethers6 returns a promise
return this.address;
}
override connect(): ethers6.ethers.Signer {
return this;
}
override async sendTransaction(
tx: ethers6.ethers.TransactionRequest,
): Promise<ethers6.ethers.TransactionResponse> {
if (!account) {
throw new Error("Account not found");
}
const ethersTx = await alignTxFromEthers({
chain,
client,
tx,
});
const serializableTx = await toSerializableTransaction({
transaction: ethersTx,
from: account.address,
});
const result = await account.sendTransaction(serializableTx);
const txResponseParams: ethers6.TransactionResponseParams = {
...serializableTx,
blockHash: null,
from: this.address,
hash: result.transactionHash as string,
blockNumber: null,
index: 0,
gasLimit: serializableTx.gas as bigint,
// @ts-expect-error - we don't have this reliably so we'll just not include it
signature: null,
};
return new ethers.TransactionResponse(txResponseParams, this.provider);
}
override async signTransaction(
tx: ethers6.ethers.TransactionRequest,
): Promise<string> {
if (!account) {
throw new Error("Account not found");
}
if (!account.signTransaction) {
throw new Error("Account does not support signing transactions");
}
const ethersTx = await alignTxFromEthers({
chain,
client,
tx,
});
const serializableTx = await toSerializableTransaction({
transaction: ethersTx,
from: account.address,
});
return account.signTransaction(serializableTx);
}
override signMessage(message: string | Uint8Array): Promise<string> {
if (!account) {
throw new Error("Account not found");
}
return account.signMessage({
message:
typeof message === "string" ? message : uint8ArrayToHex(message),
});
}
override signTypedData(
domain: ethers6.ethers.TypedDataDomain,
types: Record<string, ethers6.ethers.TypedDataField[]>,
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix later
value: Record<string, any>,
): Promise<string> {
if (!account) {
throw new Error("Account not found");
}
const typedDataEncoder = new ethers.TypedDataEncoder(types);
const typedData = {
primaryType: typedDataEncoder.primaryType,
domain: {
chainId: domain.chainId
? bigNumberIshToNumber(domain.chainId)
: undefined,
name: domain.name ?? undefined,
salt: domain.salt ? toHex(domain.salt) : undefined,
verifyingContract: domain.verifyingContract ?? undefined,
version: domain.version ?? undefined,
},
types,
message: value,
};
return account.signTypedData(typedData);
}
}
return new ThirdwebAdapterSigner(
toEthersProvider(ethers, client, chain),
account.address,
);
}
/**
* Aligns a transaction object to fit the format expected by ethers6 library.
* @param tx - The transaction object to align.
* @returns The aligned transaction object.
* @internal
*/
function alignTxToEthers(
tx: TransactionSerializable,
): ethers6.TransactionRequest {
const { type: viemType, ...rest } = tx;
// massage "type" to fit ethers
let type: number | null;
switch (viemType) {
case "legacy": {
type = 0;
break;
}
case "eip2930": {
type = 1;
break;
}
case "eip1559": {
type = 2;
break;
}
default: {
type = null;
break;
}
}
return { ...rest, type };
}
async function alignTxFromEthers(options: {
client: ThirdwebClient;
chain: Chain;
tx: ethers6.TransactionRequest;
}): Promise<PreparedTransaction> {
const { client, chain, tx } = options;
const {
type: ethersType,
accessList,
chainId: ethersChainId,
to: ethersTo,
data,
nonce,
value,
gasPrice,
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
} = tx;
let chainId: number | undefined;
if (ethersChainId) {
chainId = normalizeChainId(ethersChainId);
}
const to = await resolveEthers6Address(ethersTo);
// massage "type" to fit ethers
switch (ethersType) {
case 1: {
if (!chainId) {
throw new Error("ChainId is required for EIP-2930 transactions");
}
return {
client,
chain,
accessList: accessList as AccessList,
to: (to ?? undefined) as string | undefined,
data: (data ?? undefined) as Hex | undefined,
gasPrice: gasPrice ? bigNumberIshToBigint(gasPrice) : undefined,
gas: gasLimit ? bigNumberIshToBigint(gasLimit) : undefined,
nonce: nonce ?? undefined,
value: value ? bigNumberIshToBigint(value) : undefined,
};
}
case 2: {
if (!chainId) {
throw new Error("ChainId is required for EIP-1559 transactions");
}
return {
client,
chain,
accessList: accessList as AccessList,
to: (to ?? undefined) as string | undefined,
data: (data ?? undefined) as Hex | undefined,
gas: gasLimit ? bigNumberIshToBigint(gasLimit) : undefined,
nonce: nonce ?? undefined,
value: value ? bigNumberIshToBigint(value) : undefined,
maxFeePerGas: maxFeePerGas
? bigNumberIshToBigint(maxFeePerGas)
: undefined,
maxPriorityFeePerGas: maxPriorityFeePerGas
? bigNumberIshToBigint(maxPriorityFeePerGas)
: undefined,
};
}
default: {
// fall back to legacy
return {
client,
chain,
to: (to ?? undefined) as string | undefined,
data: (data ?? undefined) as Hex | undefined,
nonce: nonce ?? undefined,
value: value ? bigNumberIshToBigint(value) : undefined,
gasPrice: gasPrice ? bigNumberIshToBigint(gasPrice) : undefined,
gas: gasLimit ? bigNumberIshToBigint(gasLimit) : undefined,
};
}
}
}
async function resolveEthers6Address(
address: ethers6.AddressLike | null | undefined,
): Promise<string | null | undefined> {
if (!address) {
return address;
}
let resolvedAddress = address;
if (resolvedAddress) {
resolvedAddress = await resolvePromisedValue(resolvedAddress);
}
if (typeof resolvedAddress === "string") {
return resolvedAddress;
}
return resolvedAddress?.getAddress();
}
function bigNumberIshToBigint(value: ethers6.BigNumberish): bigint {
if (typeof value === "bigint") {
return value;
}
return BigInt(value);
}
function bigNumberIshToNumber(value: ethers6.BigNumberish): number {
return Number(bigNumberIshToBigint(value));
}