Skip to content

Commit 6cd279b

Browse files
fix(ethers5adapter): correct transaction result nonces handling (#3885)
1 parent 75cc5a6 commit 6cd279b

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

.changeset/honest-adults-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix ethers5adapter transaction result nonces

.github/pull_request_template.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,3 @@
22

33
Short description of the bug fixed or feature added
44

5-
## Changes made
6-
7-
- [ ] Public API changes: list the public API changes made if any
8-
- [ ] Internal API changes: explain the internal logic changes
9-
10-
## How to test
11-
12-
- [ ] Automated tests: link to unit test file
13-
- [ ] Manual tests: step by step instructions on how to test
14-
15-
## Contributor NFT
16-
17-
Paste in your wallet address below and we will airdrop you a special NFT when your pull request is merged.
18-
19-
```Address: ```

packages/thirdweb/src/adapters/ethers5.ts

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import type { Chain } from "../chains/types.js";
77
import { getRpcUrlForChain } from "../chains/utils.js";
88
import type { ThirdwebClient } from "../client/client.js";
99
import { type ThirdwebContract, getContract } from "../contract/contract.js";
10-
import { sendTransaction } from "../transaction/actions/send-transaction.js";
10+
import { toSerializableTransaction } from "../transaction/actions/to-serializable-transaction.js";
1111
import { waitForReceipt } from "../transaction/actions/wait-for-tx-receipt.js";
12-
import { prepareTransaction } from "../transaction/prepare-transaction.js";
12+
import type { PreparedTransaction } from "../transaction/prepare-transaction.js";
1313
import { toHex } from "../utils/encoding/hex.js";
1414
import type { Account } from "../wallets/interfaces/wallet.js";
1515

@@ -322,9 +322,17 @@ export async function toEthersSigner(
322322
throw new Error("Account does not support signTransaction");
323323
}
324324
const awaitedTx = await ethers.utils.resolveProperties(transaction);
325-
return account.signTransaction(
326-
await alignTxFromEthers(awaitedTx, ethers),
325+
const alignedTx = await alignTxFromEthers(
326+
client,
327+
chain,
328+
awaitedTx,
329+
ethers,
327330
);
331+
const serialized = await toSerializableTransaction({
332+
transaction: alignedTx,
333+
from: account.address,
334+
});
335+
return account.signTransaction(serialized);
328336
}
329337

330338
/**
@@ -340,33 +348,43 @@ export async function toEthersSigner(
340348
throw new Error("Account does not support sendTransaction");
341349
}
342350
const awaitedTx = await ethers.utils.resolveProperties(transaction);
343-
const alignedTx = await alignTxFromEthers(awaitedTx, ethers);
344-
const tx = prepareTransaction({
345-
client: client,
346-
chain: chain,
347-
accessList: alignedTx.accessList,
348-
data: alignedTx.data,
349-
nonce: alignedTx.nonce,
350-
to: alignedTx.to ?? undefined,
351-
value: alignedTx.value,
352-
});
353-
const result = await sendTransaction({
354-
transaction: tx,
355-
account: account,
351+
const alignedTx = await alignTxFromEthers(
352+
client,
353+
chain,
354+
awaitedTx,
355+
ethers,
356+
);
357+
const serialized = await toSerializableTransaction({
358+
transaction: alignedTx,
359+
from: account.address,
356360
});
361+
const result = await account.sendTransaction(serialized);
357362

358363
const response: ethers5.ethers.providers.TransactionResponse = {
359-
chainId: tx.chain.id,
364+
...serialized,
365+
nonce: serialized.nonce ?? 0,
360366
from: account.address,
361-
data: alignedTx.data ?? "0x",
362-
nonce: alignedTx.nonce ?? 0,
367+
maxFeePerGas: serialized.maxFeePerGas
368+
? ethers.BigNumber.from(serialized.maxFeePerGas)
369+
: undefined,
370+
maxPriorityFeePerGas: serialized.maxPriorityFeePerGas
371+
? ethers.BigNumber.from(serialized.maxPriorityFeePerGas)
372+
: undefined,
373+
gasPrice: serialized.gasPrice
374+
? ethers.BigNumber.from(serialized.gasPrice)
375+
: undefined,
376+
accessList: serialized.accessList as ethers5.ethers.utils.AccessList,
363377
value: ethers.BigNumber.from(alignedTx.value ?? 0),
364378
gasLimit: ethers.BigNumber.from(alignedTx.gas ?? 0),
365379
// biome-ignore lint/style/noNonNullAssertion: TODO: fix later
366380
hash: result.transactionHash!,
367381
confirmations: 0,
368382
wait: async () => {
369-
const receipt = await waitForReceipt(result);
383+
const receipt = await waitForReceipt({
384+
transactionHash: result.transactionHash,
385+
chain,
386+
client,
387+
});
370388
return {
371389
...receipt,
372390
type:
@@ -491,9 +509,11 @@ function alignTxToEthers(
491509
}
492510

493511
async function alignTxFromEthers(
512+
client: ThirdwebClient,
513+
chain: Chain,
494514
tx: ethers5.ethers.providers.TransactionRequest,
495515
ethers: Ethers5,
496-
): Promise<TransactionSerializable> {
516+
): Promise<PreparedTransaction> {
497517
const {
498518
type: ethersType,
499519
accessList,
@@ -514,8 +534,8 @@ async function alignTxFromEthers(
514534
throw new Error("ChainId is required for EIP-2930 transactions");
515535
}
516536
return {
517-
type: "eip2930",
518-
chainId,
537+
client,
538+
chain,
519539
to,
520540
data: (data ?? undefined) as Hex | undefined,
521541
nonce: nonce ? ethers.BigNumber.from(nonce).toNumber() : undefined,
@@ -532,8 +552,8 @@ async function alignTxFromEthers(
532552
throw new Error("ChainId is required for EIP-1559 transactions");
533553
}
534554
return {
535-
type: "eip1559",
536-
chainId,
555+
client,
556+
chain,
537557
to,
538558
data: (data ?? undefined) as Hex | undefined,
539559
nonce: nonce ? ethers.BigNumber.from(nonce).toNumber() : undefined,
@@ -549,8 +569,8 @@ async function alignTxFromEthers(
549569
}
550570
default: {
551571
return {
552-
type: "legacy",
553-
chainId,
572+
client,
573+
chain,
554574
to,
555575
data: (data ?? undefined) as Hex | undefined,
556576
nonce: nonce ? ethers.BigNumber.from(nonce).toNumber() : undefined,

0 commit comments

Comments
 (0)