Skip to content

Commit 6eb255d

Browse files
arcoravenfarhanW3
andauthored
feat: show clearer message if out of gas (#432)
* feat: show clearer message if out of gas * add error to retry flow * add chain name * fix build * simplify processTx a bit to be able to reuse parseTxError * undo schema * more cleanup * remove unneeded try/catch * move low balance check to be async * undo blocknumber optimization * Simulate + Error Message + Process Flow Update (#439) * updates to handle RPC Calls to get tx data & block-number. Db Config update * updates * config updates + retry update (#434) * updated retry query (#435) * re-added the gas check as checkout needs this check (#436) * reverted back the configs to original config as per chain it varies (#438) * updated to use env * updates for error message & simulation * config updates + retry update (#434) * updated retry query (#435) * reverted back the configs to original config as per chain it varies (#438) --------- Co-authored-by: farhanW3 <farhan@thirdweb.com> Co-authored-by: farhanW3 <132962163+farhanW3@users.noreply.github.com>
1 parent 06ade95 commit 6eb255d

File tree

12 files changed

+1689
-515
lines changed

12 files changed

+1689
-515
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"pino": "^8.15.1",
6767
"pino-pretty": "^10.0.0",
6868
"prisma": "^5.2.0",
69+
"thirdweb": "^1.0.0-beta-0e947333cefd127c2ccda059c775d2975021be33-20240229054327",
6970
"uuidv4": "^6.2.13",
7071
"viem": "^1.14.0",
7172
"zod": "^3.21.4"

src/db/transactions/getQueuedTxs.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { Transactions } from "@prisma/client";
2-
import { Static } from "@sinclair/typebox";
32
import { PrismaTransaction } from "../../schema/prisma";
4-
import { transactionResponseSchema } from "../../server/schemas/transaction";
53
import { getConfig } from "../../utils/cache/getConfig";
64
import { getPrismaWithPostgresTx } from "../client";
7-
import { cleanTxs } from "./cleanTxs";
85

96
interface GetQueuedTxsParams {
107
pgtx?: PrismaTransaction;
118
}
129

1310
export const getQueuedTxs = async ({ pgtx }: GetQueuedTxsParams = {}): Promise<
14-
Static<typeof transactionResponseSchema>[]
11+
Transactions[]
1512
> => {
1613
const prisma = getPrismaWithPostgresTx(pgtx);
1714
const config = await getConfig();
@@ -35,5 +32,5 @@ export const getQueuedTxs = async ({ pgtx }: GetQueuedTxsParams = {}): Promise<
3532
FOR UPDATE SKIP LOCKED
3633
`;
3734

38-
return cleanTxs(txs);
35+
return txs;
3936
};

src/db/transactions/updateTx.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ethers } from "ethers";
1+
import { BigNumber, ethers } from "ethers";
22
import { PrismaTransaction } from "../../schema/prisma";
33
import { TransactionStatusEnum } from "../../server/schemas/transaction";
44
import { getPrismaWithPostgresTx } from "../client";
@@ -24,7 +24,7 @@ type UpdateTxData =
2424
status: TransactionStatusEnum.Submitted;
2525
sentAt: Date;
2626
transactionHash: string;
27-
res: ethers.providers.TransactionResponse | null;
27+
res: ethers.providers.TransactionRequest;
2828
sentAtBlockNumber: number;
2929
retryCount?: number;
3030
}
@@ -90,7 +90,7 @@ export const updateTx = async ({ pgtx, queueId, data }: UpdateTxParams) => {
9090
transactionHash: data.transactionHash,
9191
sentAtBlockNumber: data.sentAtBlockNumber,
9292
retryCount: data.retryCount,
93-
nonce: data.res?.nonce,
93+
nonce: BigNumber.from(data.res.nonce).toNumber(),
9494
transactionType: data.res?.type || undefined,
9595
gasPrice: data.res?.gasPrice?.toString(),
9696
gasLimit: data.res?.gasLimit?.toString(),

src/server/middleware/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const withErrorHandler = async (server: FastifyInstance) => {
5959
});
6060
} else {
6161
// Handle non-custom errors
62-
reply.status(500).send({
62+
reply.status(StatusCodes.INTERNAL_SERVER_ERROR).send({
6363
error: {
6464
statusCode: 500,
6565
code: "INTERNAL_SERVER_ERROR",

src/server/routes/transaction/blockchain/sendSignedUserOp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Static, Type } from "@sinclair/typebox";
22
import { Value } from "@sinclair/typebox/value";
33
import { FastifyInstance } from "fastify";
44
import { StatusCodes } from "http-status-codes";
5-
import { thirdwebClientId } from "../../../../utils/api-keys";
65
import { env } from "../../../../utils/env";
6+
import { thirdwebClientId } from "../../../../utils/sdk";
77
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
88
import { getChainIdFromChain } from "../../../utils/chain";
99

src/utils/date.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Returns the milliseconds since a given past date.
3+
* Returns 0 if the date is in the future.
4+
* @param from A past date
5+
* @returns number Milliseconds since the `from` date.
6+
*/
7+
export const msSince = (from: Date) => {
8+
const ms = new Date().getTime() - from.getTime();
9+
return Math.min(ms, 0);
10+
};

src/utils/errors.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Transactions } from ".prisma/client";
2+
import { getChainByChainIdAsync } from "@thirdweb-dev/chains";
3+
import { ethers } from "ethers";
4+
import {
5+
createThirdwebClient,
6+
prepareTransaction,
7+
simulateTransaction,
8+
} from "thirdweb";
9+
import { env } from "./env";
10+
11+
interface EthersError {
12+
reason: string;
13+
code: string;
14+
error: any;
15+
method: string;
16+
transaction: any;
17+
}
18+
19+
const client = createThirdwebClient({
20+
secretKey: env.THIRDWEB_API_SECRET_KEY,
21+
});
22+
23+
export const parseTxError = async (
24+
tx: Transactions,
25+
err: any,
26+
): Promise<string> => {
27+
if (!err) {
28+
return "Unexpected error.";
29+
}
30+
31+
const chain = await getChainByChainIdAsync(Number(tx.chainId));
32+
33+
if ((err as EthersError)?.code === ethers.errors.INSUFFICIENT_FUNDS) {
34+
return `Insufficient ${chain.nativeCurrency?.symbol} on ${
35+
chain.name
36+
} in backend wallet ${tx.fromAddress!}.`;
37+
}
38+
39+
if ((err as EthersError)?.code === ethers.errors.UNPREDICTABLE_GAS_LIMIT) {
40+
try {
41+
const transaction = prepareTransaction({
42+
to: tx.toAddress!,
43+
value: BigInt(tx.value!),
44+
data: tx.data! as `0x${string}`,
45+
chain: {
46+
id: Number(tx.chainId!),
47+
rpc: chain.rpc[0],
48+
},
49+
client,
50+
});
51+
await simulateTransaction({ transaction, from: tx.fromAddress! });
52+
} catch (simErr: any) {
53+
return simErr?.message ?? simErr.toString();
54+
}
55+
}
56+
57+
if ("message" in err) {
58+
return err.message;
59+
}
60+
return err.toString();
61+
};
File renamed without changes.

src/utils/usage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { FastifyInstance } from "fastify";
55
import { contractParamSchema } from "../server/schemas/sharedApiSchemas";
66
import { walletParamSchema } from "../server/schemas/wallet";
77
import { getChainIdFromChain } from "../server/utils/chain";
8-
import { thirdwebClientId } from "./api-keys";
98
import { env } from "./env";
109
import { logger } from "./logger";
10+
import { thirdwebClientId } from "./sdk";
1111

1212
type CreateHeaderForRequestParams = {
1313
clientId: string;
@@ -29,7 +29,6 @@ export interface ReportUsageParams {
2929
extension?: string;
3030
retryCount?: number;
3131
provider?: string;
32-
transactionValue?: string;
3332
msSinceQueue?: number;
3433
msSinceSend?: number;
3534
};

0 commit comments

Comments
 (0)