Skip to content

Commit 03ddcb8

Browse files
committed
remove simulation in send eoa transaction
1 parent ce81074 commit 03ddcb8

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

src/utils/sdk.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export const thirdwebClientId = sha256HexSync(
88

99
export const thirdwebClient = createThirdwebClient({
1010
secretKey: env.THIRDWEB_API_SECRET_KEY,
11+
config: {
12+
rpc: { maxBatchSize: 50 },
13+
},
1114
});
1215

1316
/**

src/worker/queues/sendTransactionQueue.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export class SendTransactionQueue {
1313

1414
private static q = new Queue<string>(this.name, {
1515
connection: redis,
16-
defaultJobOptions,
16+
defaultJobOptions: {
17+
...defaultJobOptions,
18+
attempts: 5,
19+
},
1720
});
1821

1922
// Allow enqueing the same queueId for multiple retries.

src/worker/tasks/cancelUnusedNoncesWorker.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
3737
});
3838
success.push(nonce);
3939
} catch (e: unknown) {
40-
// If the error suggests the nonce is already used, do not release the nonce.
41-
if (isNonceUsedOnchain(e)) {
40+
// Release the nonce if it has not expired.
41+
if (isEthersErrorCode(e, ethers.errors.NONCE_EXPIRED)) {
4242
ignore.push(nonce);
43-
continue;
43+
} else {
44+
await releaseNonce(chainId, walletAddress, nonce);
45+
fail.push(nonce);
4446
}
45-
46-
// Otherwise release the nonce so it can be re-used or cancelled again later.
47-
await releaseNonce(chainId, walletAddress, nonce);
48-
fail.push(nonce);
4947
}
5048
}
5149

@@ -76,18 +74,6 @@ const getAndDeleteUnusedNonces = async (key: string) => {
7674
return results.map(parseInt);
7775
};
7876

79-
/**
80-
* Returns true if the error suggests the nonce is used onchain.
81-
* @param error
82-
* @returns true if the nonce is already used.
83-
*/
84-
const isNonceUsedOnchain = (error: unknown) => {
85-
if (isEthersErrorCode(error, ethers.errors.NONCE_EXPIRED)) {
86-
return true;
87-
}
88-
return false;
89-
};
90-
9177
// Worker
9278
const _worker = new Worker(CANCEL_UNUSED_NONCES_QUEUE_NAME, handler, {
9379
concurrency: 1,

src/worker/tasks/sendTransactionWorker.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Job, Processor, Worker } from "bullmq";
2+
import { ethers } from "ethers";
23
import superjson from "superjson";
34
import { Hex, toSerializableTransaction } from "thirdweb";
45
import { stringify } from "thirdweb/utils";
56
import { bundleUserOp } from "thirdweb/wallets/smart";
7+
import type { TransactionSerializable } from "viem";
68
import { getContractAddress } from "viem";
79
import { TransactionDB } from "../../db/transactions/db";
810
import { acquireNonce, releaseNonce } from "../../db/wallets/walletNonce";
@@ -12,9 +14,9 @@ import { getChain } from "../../utils/chain";
1214
import { msSince } from "../../utils/date";
1315
import { env } from "../../utils/env";
1416
import { prettifyError } from "../../utils/error";
17+
import { isEthersErrorCode } from "../../utils/ethers";
1518
import { redis } from "../../utils/redis/redis";
1619
import { thirdwebClient } from "../../utils/sdk";
17-
import { simulateQueuedTransaction } from "../../utils/transaction/simulateQueuedTransaction";
1820
import {
1921
ErroredTransaction,
2022
QueuedTransaction,
@@ -76,15 +78,6 @@ const _handleQueuedTransaction = async (
7678
const { chainId, from } = queuedTransaction;
7779
const chain = await getChain(chainId);
7880

79-
const simulateError = await simulateQueuedTransaction(queuedTransaction);
80-
if (simulateError) {
81-
return {
82-
...queuedTransaction,
83-
status: "errored",
84-
errorMessage: simulateError,
85-
};
86-
}
87-
8881
// Handle sending an AA user operation.
8982
if (queuedTransaction.isUserOp) {
9083
const signedUserOp = await generateSignedUserOperation(queuedTransaction);
@@ -110,33 +103,45 @@ const _handleQueuedTransaction = async (
110103
};
111104
}
112105

113-
// Prepare nonce + gas settings.
114-
const populatedTransaction = await toSerializableTransaction({
115-
from,
116-
transaction: {
117-
client: thirdwebClient,
118-
chain,
106+
let populatedTransaction: TransactionSerializable;
107+
try {
108+
populatedTransaction = await toSerializableTransaction({
109+
from,
110+
transaction: {
111+
client: thirdwebClient,
112+
chain,
113+
...queuedTransaction,
114+
},
115+
});
116+
} catch (error: unknown) {
117+
// If the transaction will revert, error.message contains the human-readable error.
118+
const errorMessage = (error as Error)?.message ?? `${error}`;
119+
return {
119120
...queuedTransaction,
120-
},
121-
});
122-
job.log(`Populated transaction: ${stringify(populatedTransaction)}`);
121+
status: "errored",
122+
errorMessage,
123+
};
124+
}
123125

124126
// Acquire an unused nonce for this transaction.
125127
const nonce = await acquireNonce(chainId, from);
128+
populatedTransaction.nonce = nonce;
129+
job.log(`Populated transaction: ${stringify(populatedTransaction)}`);
126130

127131
// Send transaction to RPC.
128132
let transactionHash: Hex;
129133
try {
130134
const account = await getAccount({ chainId, from });
131-
const sendTransactionResult = await account.sendTransaction({
132-
...populatedTransaction,
133-
nonce,
134-
});
135+
const sendTransactionResult = await account.sendTransaction(
136+
populatedTransaction,
137+
);
135138
transactionHash = sendTransactionResult.transactionHash;
136-
} catch (e) {
137-
// This transaction was rejected by RPC. Release the nonce to be reused by a future transaction.
138-
await releaseNonce(chainId, from, nonce);
139-
throw e;
139+
} catch (error: unknown) {
140+
// Release the nonce if it has not expired.
141+
if (!isEthersErrorCode(error, ethers.errors.NONCE_EXPIRED)) {
142+
await releaseNonce(chainId, from, nonce);
143+
}
144+
throw error;
140145
}
141146

142147
return {

0 commit comments

Comments
 (0)