Skip to content

Commit f200de3

Browse files
committed
test(sdk): increase coverage
1 parent 1f444ba commit f200de3

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ethers5 from "ethers5";
2+
import * as ethers6 from "ethers6";
23
import { describe, expect, it, test } from "vitest";
34
import { USDT_CONTRACT } from "~test/test-contracts.js";
45
import { ANVIL_CHAIN, FORKED_ETHEREUM_CHAIN } from "../../test/src/chains.js";
@@ -10,6 +11,7 @@ import { randomBytesBuffer } from "../utils/random.js";
1011
import { privateKeyToAccount } from "../wallets/private-key.js";
1112
import {
1213
fromEthersContract,
14+
fromEthersSigner,
1315
toEthersContract,
1416
toEthersProvider,
1517
toEthersSigner,
@@ -149,4 +151,94 @@ describe("ethers5 adapter", () => {
149151
const _decimals = await decimals({ contract: thirdwebContract });
150152
expect(_decimals).toBe(6);
151153
});
154+
155+
test("toEthersProvider should return a valid provider", async () => {
156+
const provider = toEthersProvider(ethers5, TEST_CLIENT, ANVIL_CHAIN);
157+
expect(provider).toBeDefined();
158+
expect(provider.getBlockNumber).toBeDefined();
159+
160+
// Test if the provider can fetch the block number
161+
const blockNumber = await provider.getBlockNumber();
162+
expect(typeof blockNumber).toBe("number");
163+
});
164+
165+
test("toEthersProvider should throw error with invalid ethers version", () => {
166+
const invalidEthers = ethers6;
167+
expect(() =>
168+
// biome-ignore lint/suspicious/noExplicitAny: Testing invalid data
169+
toEthersProvider(invalidEthers as any, TEST_CLIENT, ANVIL_CHAIN),
170+
).toThrow(
171+
"You seem to be using ethers@6, please use the `ethers6Adapter()`",
172+
);
173+
});
174+
});
175+
176+
describe("fromEthersSigner", () => {
177+
it("should convert an ethers5 Signer to an Account", async () => {
178+
const wallet = new ethers5.Wallet(ANVIL_PKEY_A);
179+
const account = await fromEthersSigner(wallet);
180+
181+
expect(account).toBeDefined();
182+
expect(account.address).toBe(await wallet.getAddress());
183+
});
184+
185+
it("should sign a message", async () => {
186+
const wallet = new ethers5.Wallet(ANVIL_PKEY_A);
187+
const account = await fromEthersSigner(wallet);
188+
189+
const message = "Hello, world!";
190+
const signature = await account.signMessage({ message });
191+
192+
expect(signature).toBe(await wallet.signMessage(message));
193+
});
194+
195+
it("should sign a transaction", async () => {
196+
const wallet = new ethers5.Wallet(
197+
ANVIL_PKEY_A,
198+
ethers5.getDefaultProvider(),
199+
);
200+
const account = await fromEthersSigner(wallet);
201+
202+
const transaction = {
203+
to: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
204+
value: 1n,
205+
};
206+
207+
const signedTransaction = await account.signTransaction?.(transaction);
208+
209+
expect(signedTransaction).toBe(await wallet.signTransaction(transaction));
210+
});
211+
212+
it("should sign typed data", async () => {
213+
const wallet = new ethers5.Wallet(ANVIL_PKEY_A);
214+
const account = await fromEthersSigner(wallet);
215+
216+
const domain = {
217+
name: "Ether Mail",
218+
version: "1",
219+
chainId: 1,
220+
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
221+
};
222+
223+
const types = {
224+
Person: [
225+
{ name: "name", type: "string" },
226+
{ name: "wallet", type: "address" },
227+
],
228+
};
229+
230+
const value = {
231+
name: "Alice",
232+
wallet: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
233+
};
234+
235+
const signature = await account.signTypedData({
236+
primaryType: "Person",
237+
domain,
238+
types,
239+
message: value,
240+
});
241+
242+
expect(signature).toBeDefined();
243+
});
152244
});

packages/thirdweb/src/adapters/ethers5.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function assertEthers5(
4040
): asserts ethers is typeof ethers5 {
4141
if (!isEthers5(ethers)) {
4242
throw new Error(
43-
"You seem to be using ethers@6, please use the `ethers6Adapter()",
43+
"You seem to be using ethers@6, please use the `ethers6Adapter()`",
4444
);
4545
}
4646
}
@@ -280,6 +280,7 @@ export function toEthersProvider(
280280
client: ThirdwebClient,
281281
chain: Chain,
282282
): ethers5.providers.Provider {
283+
assertEthers5(ethers);
283284
const url = getRpcUrlForChain({ chain, client });
284285
const headers: HeadersInit = {
285286
"Content-Type": "application/json",
@@ -624,8 +625,7 @@ function alignTxToEthers(
624625
gasLimit: gas,
625626
to,
626627
type,
627-
accessList: tx.accessList as ethers5.utils.AccessListish | undefined,
628-
};
628+
} as ethers5.ethers.utils.Deferrable<ethers5.ethers.providers.TransactionRequest>;
629629
}
630630

631631
async function alignTxFromEthers(

packages/thirdweb/src/utils/any-evm/keyless-transaction.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ describe("getKeylessTransaction", () => {
4949
);
5050
});
5151

52+
it("should throw if yParity is explicitly undefined", async () => {
53+
const invalidSignature = {
54+
r: mockSignature.r,
55+
s: mockSignature.s,
56+
yParity: undefined,
57+
};
58+
59+
await expect(
60+
getKeylessTransaction({
61+
transaction: mockTransaction,
62+
// biome-ignore lint/suspicious/noExplicitAny: Testing invalid data
63+
signature: invalidSignature as any,
64+
}),
65+
).rejects.toThrow();
66+
});
67+
68+
it("should throw if a signature is not recoverable", async () => {
69+
const invalidSignature = { ...mockSignature, v: undefined };
70+
71+
await expect(
72+
getKeylessTransaction({
73+
transaction: mockTransaction,
74+
// biome-ignore lint/suspicious/noExplicitAny: Testing invalid data
75+
signature: invalidSignature as any,
76+
}),
77+
).rejects.toThrow();
78+
});
79+
5280
it("should throw an error if the transaction is invalid", async () => {
5381
const invalidTransaction = { ...mockTransaction, value: "invalid" };
5482

0 commit comments

Comments
 (0)