Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 4 additions & 31 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
import { ethers } from "ethers";
import { getChainMetadata } from "thirdweb/chains";
import { stringify } from "thirdweb/utils";
import { getChain } from "./chain";
import { isEthersErrorCode } from "./ethers";
import { doSimulateTransaction } from "./transaction/simulateQueuedTransaction";
import type { AnyTransaction } from "./transaction/types";

export const prettifyError = (error: unknown): string => {
if (error instanceof Error) {
return error.message;
}
return stringify(error);
};

export const prettifyTransactionError = async (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was used to prettify an error. But the "simulate" step below wasn't used since v5 isn't returning ethers error codes anymore. Moving the insufficient funds logic inline in SendTransactionWorker.

transaction: AnyTransaction,
error: Error,
): Promise<string> => {
if (!transaction.isUserOp) {
if (isInsufficientFundsError(error)) {
const chain = await getChain(transaction.chainId);
const metadata = await getChainMetadata(chain);
return `Insufficient ${metadata.nativeCurrency?.symbol} on ${metadata.name} in ${transaction.from}.`;
}
export const wrapError = (error: unknown, prefix: "RPC" | "Bundler") =>
new Error(`[${prefix}] ${prettifyError(error)}`);

if (isEthersErrorCode(error, ethers.errors.UNPREDICTABLE_GAS_LIMIT)) {
const simulateError = await doSimulateTransaction(transaction);
if (simulateError) {
return simulateError;
}
}
}

return error.message;
};
export const prettifyError = (error: unknown): string =>
error instanceof Error ? error.message : stringify(error);

const _parseMessage = (error: unknown): string | null => {
return error && typeof error === "object" && "message" in error
Expand Down
2 changes: 1 addition & 1 deletion src/worker/queues/queues.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Job, JobsOptions, Worker } from "bullmq";
import type { Job, JobsOptions, Worker } from "bullmq";
import { env } from "../../utils/env";
import { logger } from "../../utils/logger";

Expand Down
55 changes: 35 additions & 20 deletions src/worker/tasks/sendTransactionWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
getContract,
readContract,
toSerializableTransaction,
toTokens,
type Hex,
} from "thirdweb";
import { getChainMetadata } from "thirdweb/chains";
import { stringify } from "thirdweb/utils";
import type { Account } from "thirdweb/wallets";
import {
Expand All @@ -32,10 +34,10 @@ import { getChain } from "../../utils/chain";
import { msSince } from "../../utils/date";
import { env } from "../../utils/env";
import {
isInsufficientFundsError,
isNonceAlreadyUsedError,
isReplacementGasFeeTooLow,
prettifyError,
prettifyTransactionError,
wrapError,
} from "../../utils/error";
import { getChecksumAddress } from "../../utils/primitiveTypes";
import { recordMetrics } from "../../utils/prometheus";
Expand Down Expand Up @@ -243,16 +245,12 @@ const _sendUserOp = async (
// we don't want this behavior in the engine context
waitForDeployment: false,
})) as UserOperation; // TODO support entrypoint v0.7 accounts
} catch (e) {
const erroredTransaction: ErroredTransaction = {
} catch (error) {
return {
...queuedTransaction,
status: "errored",
errorMessage: prettifyError(e),
};
job.log(
`Failed to populate transaction: ${erroredTransaction.errorMessage}`,
);
return erroredTransaction;
errorMessage: wrapError(error, "Bundler").message,
} satisfies ErroredTransaction;
}

job.log(`Populated userOp: ${stringify(signedUserOp)}`);
Expand Down Expand Up @@ -325,15 +323,11 @@ const _sendTransaction = async (
},
});
} catch (e: unknown) {
const erroredTransaction: ErroredTransaction = {
return {
...queuedTransaction,
status: "errored",
errorMessage: prettifyError(e),
};
job.log(
`Failed to populate transaction: ${erroredTransaction.errorMessage}`,
);
return erroredTransaction;
errorMessage: wrapError(e, "RPC").message,
} satisfies ErroredTransaction;
}

// Handle if `maxFeePerGas` is overridden.
Expand Down Expand Up @@ -380,7 +374,28 @@ const _sendTransaction = async (
job.log(`Recycling nonce: ${nonce}`);
await recycleNonce(chainId, from, nonce);
}
throw error;

// Do not retry errors that are expected to be rejected by RPC again.
if (isInsufficientFundsError(error)) {
const { name, nativeCurrency } = await getChainMetadata(chain);
const { gas, value = 0n } = populatedTransaction;
const gasPrice =
populatedTransaction.gasPrice ?? populatedTransaction.maxFeePerGas;

const minGasTokens = gasPrice
? toTokens(gas * gasPrice + value, 18)
: null;
const errorMessage = minGasTokens
? `Insufficient funds in ${account.address} on ${name}. Transaction requires > ${minGasTokens} ${nativeCurrency.symbol}.`
: `Insufficient funds in ${account.address} on ${name}. Transaction requires more ${nativeCurrency.symbol}.`;
return {
...queuedTransaction,
status: "errored",
errorMessage,
} satisfies ErroredTransaction;
}

throw wrapError(error, "RPC");
}

await addSentNonce(chainId, from, nonce);
Expand Down Expand Up @@ -466,7 +481,7 @@ const _resendTransaction = async (
job.log("A pending transaction exists with >= gas fees. Do not resend.");
return null;
}
throw error;
throw wrapError(error, "RPC");
}

return {
Expand Down Expand Up @@ -572,7 +587,7 @@ export const initSendTransactionWorker = () => {
const erroredTransaction: ErroredTransaction = {
...transaction,
status: "errored",
errorMessage: await prettifyTransactionError(transaction, error),
errorMessage: error.message,
};
job.log(`Transaction errored: ${stringify(erroredTransaction)}`);

Expand Down
Loading