Skip to content

Commit 584b1a2

Browse files
committed
dont retry if out of funds
1 parent 37f62e9 commit 584b1a2

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

src/utils/error.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { ethers } from "ethers";
22
import { stringify } from "thirdweb/utils";
33
import { isEthersErrorCode } from "./ethers";
44

5-
export const prettifyError = (error: unknown): string => {
6-
if (error instanceof Error) {
7-
return error.message;
8-
}
9-
return stringify(error);
10-
};
5+
/**
6+
* Pretty print an error with an optional contextual prefix ("[RpcError] \<response from RPC\>").
7+
*/
8+
export const prettifyError = (
9+
error: unknown,
10+
prefix?: "RPC" | "Bundler",
11+
): string =>
12+
`[${prefix}] ${error instanceof Error ? error.message : stringify(error)}`;
1113

1214
const _parseMessage = (error: unknown): string | null => {
1315
return error && typeof error === "object" && "message" in error

src/worker/queues/queues.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Job, JobsOptions, Worker } from "bullmq";
1+
import type { Job, JobsOptions, Worker } from "bullmq";
22
import { env } from "../../utils/env";
33
import { logger } from "../../utils/logger";
44

src/worker/tasks/sendTransactionWorker.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,11 @@ const _sendUserOp = async (
246246
waitForDeployment: false,
247247
})) as UserOperation; // TODO support entrypoint v0.7 accounts
248248
} catch (e) {
249-
const erroredTransaction: ErroredTransaction = {
249+
return {
250250
...queuedTransaction,
251251
status: "errored",
252-
errorMessage: prettifyError(e),
253-
};
254-
job.log(
255-
`Failed to populate transaction: ${erroredTransaction.errorMessage}`,
256-
);
257-
return erroredTransaction;
252+
errorMessage: prettifyError(e, "Bundler"),
253+
} satisfies ErroredTransaction;
258254
}
259255

260256
job.log(`Populated userOp: ${stringify(signedUserOp)}`);
@@ -327,15 +323,11 @@ const _sendTransaction = async (
327323
},
328324
});
329325
} catch (e: unknown) {
330-
const erroredTransaction: ErroredTransaction = {
326+
return {
331327
...queuedTransaction,
332328
status: "errored",
333-
errorMessage: prettifyError(e),
334-
};
335-
job.log(
336-
`Failed to populate transaction: ${erroredTransaction.errorMessage}`,
337-
);
338-
return erroredTransaction;
329+
errorMessage: prettifyError(e, "RPC"),
330+
} satisfies ErroredTransaction;
339331
}
340332

341333
// Handle if `maxFeePerGas` is overridden.
@@ -371,10 +363,10 @@ const _sendTransaction = async (
371363
const sendTransactionResult =
372364
await account.sendTransaction(populatedTransaction);
373365
transactionHash = sendTransactionResult.transactionHash;
374-
} catch (error: unknown) {
366+
} catch (e: unknown) {
375367
// If the nonce is already seen onchain (nonce too low) or in mempool (replacement underpriced),
376368
// correct the DB nonce.
377-
if (isNonceAlreadyUsedError(error) || isReplacementGasFeeTooLow(error)) {
369+
if (isNonceAlreadyUsedError(e) || isReplacementGasFeeTooLow(e)) {
378370
const result = await syncLatestNonceFromOnchainIfHigher(chainId, from);
379371
job.log(`Re-synced nonce: ${result}`);
380372
} else {
@@ -383,18 +375,26 @@ const _sendTransaction = async (
383375
await recycleNonce(chainId, from, nonce);
384376
}
385377

386-
// Prettify "out of funds" error.
387-
if (isInsufficientFundsError(error)) {
378+
// Do not retry errors that are expected to be rejected by RPC again.
379+
if (isInsufficientFundsError(e)) {
388380
const gasPrice =
389381
populatedTransaction.gasPrice ?? populatedTransaction.maxFeePerGas;
382+
383+
let errorMessage = prettifyError(e, "RPC");
390384
if (gasPrice) {
391-
const chainMetadata = await getChainMetadata(chain);
392-
const minGasTokens = toTokens(populatedTransaction.gas * gasPrice, 18);
393-
throw `Insufficient funds in ${account.address} on ${chainMetadata.name}. Transaction requires ${minGasTokens} ${chainMetadata.nativeCurrency.symbol}.`;
385+
const { gas, value = 0n } = populatedTransaction;
386+
const { name, nativeCurrency } = await getChainMetadata(chain);
387+
const minGasTokens = toTokens(gas * gasPrice + value, 18);
388+
errorMessage = `Insufficient funds in ${account.address} on ${name}. Transaction requires > ${minGasTokens} ${nativeCurrency.symbol}.`;
394389
}
390+
return {
391+
...queuedTransaction,
392+
status: "errored",
393+
errorMessage,
394+
} satisfies ErroredTransaction;
395395
}
396396

397-
throw error;
397+
throw prettifyError(e, "RPC");
398398
}
399399

400400
await addSentNonce(chainId, from, nonce);

0 commit comments

Comments
 (0)