Skip to content

Commit 69ee979

Browse files
chore: add more adapter tests (#2672)
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
1 parent 44d9630 commit 69ee979

File tree

5 files changed

+98
-40
lines changed

5 files changed

+98
-40
lines changed

packages/thirdweb/src/adapters/ethers5.test.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { TEST_CLIENT } from "../../test/src/test-clients.js";
44
import { toEthersSigner } from "./ethers5.js";
55
import { ANVIL_CHAIN } from "../../test/src/chains.js";
66
import * as ethers5 from "ethers5";
7-
8-
const FAKE_PKEY =
9-
"0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
7+
import { ANVIL_PKEY_A, TEST_ACCOUNT_B } from "../../test/src/test-wallets.js";
108

119
const account = privateKeyAccount({
12-
privateKey: FAKE_PKEY,
10+
privateKey: ANVIL_PKEY_A,
1311
client: TEST_CLIENT,
1412
});
1513

@@ -25,6 +23,18 @@ describe("ethers5 adapter", () => {
2523
expect(signer.signMessage).toBeDefined();
2624
});
2725

26+
test("should sign message", async () => {
27+
const signer = await toEthersSigner(
28+
ethers5,
29+
TEST_CLIENT,
30+
account,
31+
ANVIL_CHAIN,
32+
);
33+
const expectedSig = await account.signMessage({ message: "hello world" });
34+
const sig = await signer.signMessage("hello world");
35+
expect(sig).toBe(expectedSig);
36+
});
37+
2838
test("should sign typed data", async () => {
2939
const signer = await toEthersSigner(
3040
ethers5,
@@ -70,8 +80,20 @@ describe("ethers5 adapter", () => {
7080

7181
const signature = await signer._signTypedData(domain, types, value);
7282

73-
expect(signature).toMatchInlineSnapshot(
74-
`"0x10d3ce8040590e48889801080ad40f3d514c2c3ce03bbbe3e179bbf5ba56c75425951fa15220f637e2ab79fd033b99c4b340339e00e360316547e956c61ffcb01c"`,
83+
expect(signature.length).toBe(132);
84+
});
85+
86+
test("should send a tx", async () => {
87+
const signer = await toEthersSigner(
88+
ethers5,
89+
TEST_CLIENT,
90+
account,
91+
ANVIL_CHAIN,
7592
);
93+
const txResponse = await signer.sendTransaction({
94+
to: TEST_ACCOUNT_B.address,
95+
value: 100,
96+
});
97+
expect(txResponse.hash.length).toBe(66);
7698
});
7799
});

packages/thirdweb/src/adapters/ethers5.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,7 @@ export async function toEthersSigner(
331331
* @internal
332332
*/
333333
override async sendTransaction(
334-
transaction: ethers5.ethers.utils.Deferrable<
335-
ethers5.ethers.providers.TransactionRequest & { chainId: number }
336-
>,
334+
transaction: ethers5.ethers.utils.Deferrable<ethers5.ethers.providers.TransactionRequest>,
337335
): Promise<ethers5.ethers.providers.TransactionResponse> {
338336
if (!account) {
339337
throw new Error("Account not found");

packages/thirdweb/src/adapters/ethers6.test.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { privateKeyAccount } from "../wallets/private-key.js";
33
import { TEST_CLIENT } from "../../test/src/test-clients.js";
44
import { ethers6Adapter } from "./ethers6.js";
55
import { ANVIL_CHAIN } from "../../test/src/chains.js";
6-
7-
const FAKE_PKEY =
8-
"0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
6+
import { ANVIL_PKEY_A, TEST_ACCOUNT_B } from "../../test/src/test-wallets.js";
97

108
const account = privateKeyAccount({
11-
privateKey: FAKE_PKEY,
9+
privateKey: ANVIL_PKEY_A,
1210
client: TEST_CLIENT,
1311
});
1412

@@ -23,6 +21,17 @@ describe("toEthersSigner", () => {
2321
expect(signer.signMessage).toBeDefined();
2422
});
2523

24+
test("should sign message", async () => {
25+
const signer = await ethers6Adapter.signer.toEthers({
26+
client: TEST_CLIENT,
27+
account,
28+
chain: ANVIL_CHAIN,
29+
});
30+
const expectedSig = await account.signMessage({ message: "hello world" });
31+
const sig = await signer.signMessage("hello world");
32+
expect(sig).toBe(expectedSig);
33+
});
34+
2635
test("should sign typed data", async () => {
2736
const signer = await ethers6Adapter.signer.toEthers({
2837
client: TEST_CLIENT,
@@ -67,8 +76,19 @@ describe("toEthersSigner", () => {
6776

6877
const signature = await signer.signTypedData(domain, types, value);
6978

70-
expect(signature).toMatchInlineSnapshot(
71-
`"0x10d3ce8040590e48889801080ad40f3d514c2c3ce03bbbe3e179bbf5ba56c75425951fa15220f637e2ab79fd033b99c4b340339e00e360316547e956c61ffcb01c"`,
72-
);
79+
expect(signature.length).toBe(132);
80+
});
81+
82+
test("should send a tx", async () => {
83+
const signer = await ethers6Adapter.signer.toEthers({
84+
client: TEST_CLIENT,
85+
account,
86+
chain: ANVIL_CHAIN,
87+
});
88+
const txResponse = await signer.sendTransaction({
89+
to: TEST_ACCOUNT_B.address,
90+
value: 100,
91+
});
92+
expect(txResponse.hash.length).toBe(66);
7393
});
7494
});

packages/thirdweb/src/adapters/ethers6.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { getRpcUrlForChain } from "../chains/utils.js";
1111
import type { Chain } from "../chains/types.js";
1212
import { getContract, type ThirdwebContract } from "../contract/contract.js";
1313
import { toHex, uint8ArrayToHex } from "../utils/encoding/hex.js";
14+
import { toSerializableTransaction } from "../transaction/actions/to-serializable-transaction.js";
15+
import type { PreparedTransaction } from "../transaction/prepare-transaction.js";
1416

1517
type Ethers6 = typeof ethers6;
1618

@@ -305,25 +307,30 @@ async function toEthersSigner(
305307
}
306308
// eslint-disable-next-line jsdoc/require-jsdoc
307309
override async sendTransaction(
308-
tx: ethers6.ethers.TransactionRequest & { chainId: number },
310+
tx: ethers6.ethers.TransactionRequest,
309311
): Promise<ethers6.ethers.TransactionResponse> {
310-
const alignedTx = await alignTxFromEthers(tx);
311312
if (!account) {
312313
throw new Error("Account not found");
313314
}
314-
const result = await account.sendTransaction({
315-
...alignedTx,
316-
chainId: tx.chainId,
315+
const ethersTx = await alignTxFromEthers({
316+
chain,
317+
client,
318+
tx,
319+
});
320+
const serializableTx = await toSerializableTransaction({
321+
transaction: ethersTx,
322+
from: account.address,
317323
});
324+
const result = await account.sendTransaction(serializableTx);
318325

319326
const txResponseParams: ethers6.TransactionResponseParams = {
320-
...alignedTx,
327+
...serializableTx,
321328
blockHash: null,
322329
from: this.address,
323330
hash: result.transactionHash as string,
324331
blockNumber: null,
325332
index: 0,
326-
gasLimit: alignedTx.gas as bigint,
333+
gasLimit: serializableTx.gas as bigint,
327334
// @ts-expect-error - we don't have this reliably so we'll just not include it
328335
signature: null,
329336
};
@@ -340,9 +347,16 @@ async function toEthersSigner(
340347
if (!account.signTransaction) {
341348
throw new Error("Account does not support signing transactions");
342349
}
343-
const viemTx = await alignTxFromEthers(tx);
344-
345-
return account.signTransaction(viemTx);
350+
const ethersTx = await alignTxFromEthers({
351+
chain,
352+
client,
353+
tx,
354+
});
355+
const serializableTx = await toSerializableTransaction({
356+
transaction: ethersTx,
357+
from: account.address,
358+
});
359+
return account.signTransaction(serializableTx);
346360
}
347361
// eslint-disable-next-line jsdoc/require-jsdoc
348362
override signMessage(message: string | Uint8Array): Promise<string> {
@@ -423,9 +437,12 @@ function alignTxToEthers(
423437
return { ...rest, type };
424438
}
425439

426-
async function alignTxFromEthers(
427-
tx: ethers6.TransactionRequest,
428-
): Promise<TransactionSerializable> {
440+
async function alignTxFromEthers(options: {
441+
client: ThirdwebClient;
442+
chain: Chain;
443+
tx: ethers6.TransactionRequest;
444+
}): Promise<PreparedTransaction> {
445+
const { client, chain, tx } = options;
429446
const {
430447
type: ethersType,
431448
accessList,
@@ -453,11 +470,10 @@ async function alignTxFromEthers(
453470
throw new Error("ChainId is required for EIP-2930 transactions");
454471
}
455472
return {
456-
type: "eip2930",
457-
458-
chainId,
473+
client,
474+
chain,
459475
accessList: accessList as AccessList,
460-
to,
476+
to: (to ?? undefined) as string | undefined,
461477
data: (data ?? undefined) as Hex | undefined,
462478
gasPrice: gasPrice ? bigNumberIshToBigint(gasPrice) : undefined,
463479
gas: gasLimit ? bigNumberIshToBigint(gasLimit) : undefined,
@@ -470,10 +486,10 @@ async function alignTxFromEthers(
470486
throw new Error("ChainId is required for EIP-1559 transactions");
471487
}
472488
return {
473-
type: "eip1559",
474-
chainId,
489+
client,
490+
chain,
475491
accessList: accessList as AccessList,
476-
to,
492+
to: (to ?? undefined) as string | undefined,
477493
data: (data ?? undefined) as Hex | undefined,
478494
gas: gasLimit ? bigNumberIshToBigint(gasLimit) : undefined,
479495
nonce: nonce ?? undefined,
@@ -490,9 +506,9 @@ async function alignTxFromEthers(
490506
default: {
491507
// fall back to legacy
492508
return {
493-
type: "legacy",
494-
chainId,
495-
to,
509+
client,
510+
chain,
511+
to: (to ?? undefined) as string | undefined,
496512
data: (data ?? undefined) as Hex | undefined,
497513
nonce: nonce ?? undefined,
498514
value: value ? bigNumberIshToBigint(value) : undefined,

packages/thirdweb/src/rpc/fetch-rpc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export async function fetchRpc(
7373

7474
if (!response.ok) {
7575
response.body?.cancel();
76-
throw new Error(`RPC request failed with status ${response.status}`);
76+
throw new Error(
77+
`RPC request failed with status ${response.status} - ${response.statusText}`,
78+
);
7779
}
7880

7981
let result;

0 commit comments

Comments
 (0)