@@ -5,11 +5,10 @@ import type { Abi } from "abitype";
5
5
import type { AccessList , Hex , TransactionSerializable } from "viem" ;
6
6
import type { ThirdwebClient } from "../client/client.js" ;
7
7
import type { Account } from "../wallets/interfaces/wallet.js" ;
8
- import { defineChain , getRpcUrlForChain } from "../chains/utils.js" ;
8
+ import { getRpcUrlForChain } from "../chains/utils.js" ;
9
9
import type { Chain } from "../chains/types.js" ;
10
10
import { getContract , type ThirdwebContract } from "../contract/contract.js" ;
11
- import { uint8ArrayToHex } from "../utils/encoding/hex.js" ;
12
- import { resolvePromisedValue } from "../utils/promise/resolve-promised-value.js" ;
11
+ import { toHex , uint8ArrayToHex } from "../utils/encoding/hex.js" ;
13
12
import { waitForReceipt } from "../transaction/actions/wait-for-tx-receipt.js" ;
14
13
import { prepareTransaction } from "../transaction/prepare-transaction.js" ;
15
14
import { sendTransaction } from "../transaction/actions/send-transaction.js" ;
@@ -47,36 +46,44 @@ function assertEthers5(
47
46
48
47
export const ethers5Adapter = /* @__PURE__ */ ( ( ) => {
49
48
const ethers = universalethers ;
50
- assertEthers5 ( ethers ) ;
51
49
return {
52
50
provider : {
53
51
/**
54
52
* Converts a Thirdweb client and chain ID into an ethers.js provider.
55
- * @param client - The Thirdweb client.
56
- * @param chain - The chain.
53
+ * @param options - The options for converting the Thirdweb client and chain ID into an ethers.js provider.
54
+ * @param options.client - The Thirdweb client.
55
+ * @param options.chain - The chain.
57
56
* @returns The ethers.js provider.
58
57
* @example
59
58
* ```ts
60
59
* import { ethers5Adapter } from "thirdweb/adapters/ethers5";
61
- * const provider = ethers5Adapter.provider.toEthers(client, chainId);
60
+ * const provider = ethers5Adapter.provider.toEthers({ client, chainId } );
62
61
* ```
63
62
*/
64
- toEthers : ( client : ThirdwebClient , chain : Chain ) =>
65
- toEthersProvider ( ethers , client , chain ) ,
63
+ toEthers : ( options : { client : ThirdwebClient ; chain : Chain } ) => {
64
+ assertEthers5 ( ethers ) ;
65
+ return toEthersProvider ( ethers , options . client , options . chain ) ;
66
+ } ,
66
67
} ,
67
68
contract : {
68
69
/**
69
70
* Converts a ThirdwebContract to an ethers.js Contract.
70
- * @param twContract - The ThirdwebContract to convert.
71
+ * @param options - The options for converting the ThirdwebContract to an ethers.js Contract.
72
+ * @param options.thirdwebContract - The ThirdwebContract to convert.
71
73
* @returns A Promise that resolves to an ethers.js Contract.
72
74
* @example
73
75
* ```ts
74
76
* import { ethers5Adapter } from "thirdweb/adapters/ethers5";
75
- * const ethersContract = await ethers5Adapter.contract.toEthers(twContract);
77
+ * const ethersContract = await ethers5Adapter.contract.toEthers({
78
+ * thirdwebContract,
79
+ * });
76
80
* ```
77
81
*/
78
- toEthers : ( twContract : ThirdwebContract ) =>
79
- toEthersContract ( ethers , twContract ) ,
82
+ toEthers : ( options : { thirdwebContract : ThirdwebContract } ) => {
83
+ assertEthers5 ( ethers ) ;
84
+ return toEthersContract ( ethers , options . thirdwebContract ) ;
85
+ } ,
86
+
80
87
/**
81
88
* Creates a ThirdwebContract instance from an ethers.js contract.
82
89
* @param options - The options for creating the ThirdwebContract instance.
@@ -92,35 +99,54 @@ export const ethers5Adapter = /* @__PURE__ */ (() => {
92
99
* });
93
100
* ```
94
101
*/
95
- fromEthers : ( options : FromEthersContractOptions ) =>
96
- fromEthersContract ( options ) ,
102
+ fromEthers : ( options : FromEthersContractOptions ) => {
103
+ assertEthers5 ( ethers ) ;
104
+ return fromEthersContract ( options ) ;
105
+ } ,
97
106
} ,
98
107
signer : {
99
108
/**
100
109
* Converts an ethers5 Signer into a Wallet object.
101
- * @param signer - The ethers5 Signer object.
110
+ * @param options - The options for converting the ethers5 Signer into a Wallet object.
111
+ * @param options.signer - The ethers5 Signer object.
102
112
* @returns - A Promise that resolves to aa Wallet object.
103
113
* @example
104
114
* ```ts
105
115
* import { ethers5Adapter } from "thirdweb/adapters/ethers5";
106
- * const wallet = await ethers5Adapter.signer.fromEthersSigner(signer);
116
+ * const wallet = await ethers5Adapter.signer.fromEthersSigner({ signer } );
107
117
* ```
108
118
*/
109
- fromEthers : ( signer : ethers5 . Signer ) => fromEthersSigner ( signer ) ,
119
+ fromEthers : ( options : { signer : ethers5 . Signer } ) => {
120
+ assertEthers5 ( ethers ) ;
121
+ return fromEthersSigner ( options . signer ) ;
122
+ } ,
110
123
111
124
/**
112
125
* Converts a Thirdweb wallet to an ethers.js signer.
113
- * @param client - The thirdweb client.
114
- * @param account - The account.
126
+ * @param options - The options for converting the Thirdweb wallet to an ethers.js signer.
127
+ * @param options.client - The thirdweb client.
128
+ * @param options.chain - The chain.
129
+ * @param options.account - The account.
115
130
* @returns A promise that resolves to an ethers.js signer.
116
131
* @example
117
132
* ```ts
118
133
* import { ethers5Adapter } from "thirdweb/adapters/ethers5";
119
- * const signer = await ethers5Adapter.signer.toEthers(client, chain, account);
134
+ * const signer = await ethers5Adapter.signer.toEthers({ client, chain, account } );
120
135
* ```
121
136
*/
122
- toEthers : ( client : ThirdwebClient , account : Account ) =>
123
- toEthersSigner ( ethers , client , account ) ,
137
+ toEthers : ( options : {
138
+ client : ThirdwebClient ;
139
+ chain : Chain ;
140
+ account : Account ;
141
+ } ) => {
142
+ assertEthers5 ( ethers ) ;
143
+ return toEthersSigner (
144
+ ethers ,
145
+ options . client ,
146
+ options . account ,
147
+ options . chain ,
148
+ ) ;
149
+ } ,
124
150
} ,
125
151
} ;
126
152
} ) ( ) ;
@@ -240,18 +266,40 @@ async function fromEthersSigner(signer: ethers5.Signer): Promise<Account> {
240
266
return account ;
241
267
}
242
268
243
- async function toEthersSigner (
269
+ /**
270
+ * @internal
271
+ */
272
+ export async function toEthersSigner (
244
273
ethers : Ethers5 ,
245
274
client : ThirdwebClient ,
246
275
account : Account ,
276
+ chain : Chain ,
247
277
) {
248
278
class ThirdwebAdapterSigner extends ethers . Signer {
279
+ /**
280
+ * @internal
281
+ */
282
+ constructor ( ) {
283
+ super ( ) ;
284
+ ethers . utils . defineReadOnly (
285
+ this ,
286
+ "provider" ,
287
+ toEthersProvider ( ethers , client , chain ) ,
288
+ ) ;
289
+ }
290
+
291
+ /**
292
+ * @internal
293
+ */
249
294
override getAddress ( ) : Promise < string > {
250
295
if ( ! account ) {
251
296
throw new Error ( "Account not found" ) ;
252
297
}
253
298
return Promise . resolve ( account . address ) ;
254
299
}
300
+ /**
301
+ * @internal
302
+ */
255
303
override signMessage ( message : string | Uint8Array ) : Promise < string > {
256
304
if ( ! account ) {
257
305
throw new Error ( "Account not found" ) ;
@@ -261,6 +309,9 @@ async function toEthersSigner(
261
309
typeof message === "string" ? message : uint8ArrayToHex ( message ) ,
262
310
} ) ;
263
311
}
312
+ /**
313
+ * @internal
314
+ */
264
315
override async signTransaction (
265
316
transaction : ethers5 . ethers . utils . Deferrable < ethers5 . ethers . providers . TransactionRequest > ,
266
317
) : Promise < string > {
@@ -276,6 +327,9 @@ async function toEthersSigner(
276
327
) ;
277
328
}
278
329
330
+ /**
331
+ * @internal
332
+ */
279
333
override async sendTransaction (
280
334
transaction : ethers5 . ethers . utils . Deferrable <
281
335
ethers5 . ethers . providers . TransactionRequest & { chainId : number }
@@ -290,8 +344,8 @@ async function toEthersSigner(
290
344
const awaitedTx = await ethers . utils . resolveProperties ( transaction ) ;
291
345
const alignedTx = await alignTxFromEthers ( awaitedTx , ethers ) ;
292
346
const tx = prepareTransaction ( {
293
- client,
294
- chain : defineChain ( await resolvePromisedValue ( transaction . chainId ) ) ,
347
+ client : client ,
348
+ chain : chain ,
295
349
accessList : alignedTx . accessList ,
296
350
data : alignedTx . data ,
297
351
gas : alignedTx . gas ,
@@ -303,7 +357,10 @@ async function toEthersSigner(
303
357
to : alignedTx . to ?? undefined ,
304
358
value : alignedTx . value ,
305
359
} ) ;
306
- const result = await sendTransaction ( { transaction : tx , account } ) ;
360
+ const result = await sendTransaction ( {
361
+ transaction : tx ,
362
+ account : account ,
363
+ } ) ;
307
364
308
365
const response : ethers5 . ethers . providers . TransactionResponse = {
309
366
chainId : tx . chain . id ,
@@ -350,6 +407,39 @@ async function toEthersSigner(
350
407
return response ;
351
408
}
352
409
410
+ // eslint-disable-next-line jsdoc/require-jsdoc
411
+ async _signTypedData (
412
+ domain : ethers5 . ethers . TypedDataDomain ,
413
+ types : Record < string , ethers5 . ethers . TypedDataField [ ] > ,
414
+ value : Record < string , any > ,
415
+ ) : Promise < string > {
416
+ if ( ! account ) {
417
+ throw new Error ( "Account not found" ) ;
418
+ }
419
+ const typedDataEncoder = new ethers . utils . _TypedDataEncoder ( types ) ;
420
+
421
+ const typedData = {
422
+ primaryType : typedDataEncoder . primaryType ,
423
+ domain : {
424
+ chainId : domain . chainId
425
+ ? bigNumberIshToNumber ( domain . chainId )
426
+ : undefined ,
427
+ name : domain . name ?? undefined ,
428
+
429
+ salt : domain . salt ? toHex ( domain . salt . toString ( ) ) : undefined ,
430
+ verifyingContract : domain . verifyingContract ?? undefined ,
431
+ version : domain . version ?? undefined ,
432
+ } ,
433
+ types,
434
+ message : value ,
435
+ } ;
436
+
437
+ return account . signTypedData ( typedData ) ;
438
+ }
439
+
440
+ /**
441
+ * @internal
442
+ */
353
443
override connect ( ) : ethers5 . ethers . Signer {
354
444
return this ;
355
445
}
@@ -472,3 +562,14 @@ async function alignTxFromEthers(
472
562
}
473
563
}
474
564
}
565
+
566
+ function bigNumberIshToBigint ( value : ethers5 . BigNumberish ) : bigint {
567
+ if ( typeof value === "bigint" ) {
568
+ return value ;
569
+ }
570
+ return BigInt ( value . toString ( ) ) ;
571
+ }
572
+
573
+ function bigNumberIshToNumber ( value : ethers5 . BigNumberish ) : number {
574
+ return Number ( bigNumberIshToBigint ( value ) ) ;
575
+ }
0 commit comments