Skip to content

Commit 982de3b

Browse files
committed
fix: increase gas multiple on each resend attempt
1 parent 844d297 commit 982de3b

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/tests/math.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { getPercentile } from "../utils/math";
2+
import { clamp, getPercentile } from "../utils/math";
33

44
describe("getPercentile", () => {
55
it("should correctly calculate the p50 (median) of a sorted array", () => {
@@ -27,3 +27,25 @@ describe("getPercentile", () => {
2727
expect(getPercentile(numbers, 50)).toBe(0);
2828
});
2929
});
30+
31+
describe("clamp", () => {
32+
it("clamps the value correctly below the minimum", () => {
33+
expect(clamp(0, { min: 2, max: 10 })).toBe(2);
34+
});
35+
36+
it("clamps the value correctly at the minimum", () => {
37+
expect(clamp(1, { min: 2, max: 10 })).toBe(2);
38+
});
39+
40+
it("returns the value when within bounds", () => {
41+
expect(clamp(3, { min: 2, max: 10 })).toBe(6);
42+
});
43+
44+
it("clamps the value correctly at the maximum", () => {
45+
expect(clamp(5, { min: 2, max: 10 })).toBe(10);
46+
});
47+
48+
it("clamps the value correctly above the maximum", () => {
49+
expect(clamp(6, { min: 2, max: 10 })).toBe(10);
50+
});
51+
});

src/utils/math.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ export const getPercentile = (arr: number[], percentile: number): number => {
77
const index = Math.floor((percentile / 100) * (arr.length - 1));
88
return arr[index];
99
};
10+
11+
export const clamp = (
12+
val: number,
13+
{ min, max }: { min: number; max: number },
14+
) => Math.max(min, Math.min(val * 2, max));

src/worker/tasks/sendTransactionWorker.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { type Job, type Processor, Worker } from "bullmq";
1+
import { Worker, type Job, type Processor } from "bullmq";
22
import assert from "node:assert";
33
import superjson from "superjson";
44
import {
5-
type Hex,
65
getAddress,
76
getContract,
87
readContract,
98
toSerializableTransaction,
9+
type Hex,
1010
} from "thirdweb";
1111
import { stringify } from "thirdweb/utils";
1212
import {
13-
type UserOperation,
1413
bundleUserOp,
1514
createAndSignUserOp,
15+
type UserOperation,
1616
} from "thirdweb/wallets/smart";
1717
import { getContractAddress } from "viem";
1818
import { TransactionDB } from "../../db/transactions/db";
@@ -33,6 +33,7 @@ import {
3333
prettifyError,
3434
prettifyTransactionError,
3535
} from "../../utils/error";
36+
import { clamp } from "../../utils/math";
3637
import { getChecksumAddress } from "../../utils/primitiveTypes";
3738
import { recordMetrics } from "../../utils/prometheus";
3839
import { redis } from "../../utils/redis/redis";
@@ -48,8 +49,8 @@ import { reportUsage } from "../../utils/usage";
4849
import { MineTransactionQueue } from "../queues/mineTransactionQueue";
4950
import { logWorkerExceptions } from "../queues/queues";
5051
import {
51-
type SendTransactionData,
5252
SendTransactionQueue,
53+
type SendTransactionData,
5354
} from "../queues/sendTransactionQueue";
5455

5556
/**
@@ -400,18 +401,20 @@ const _resendTransaction = async (
400401
},
401402
});
402403

403-
// Double gas fee settings if they were not provded in `overrides`.
404+
// Double the gas fee settings each attempt up to 10x.
405+
// Do not update gas if overrides were provided.
406+
const gasMultiple = BigInt(clamp(job.attemptsMade * 2, { min: 2, max: 10 }));
404407
if (populatedTransaction.gasPrice) {
405-
populatedTransaction.gasPrice *= 2n;
408+
populatedTransaction.gasPrice *= gasMultiple;
406409
}
407410
if (populatedTransaction.maxFeePerGas && !overrides?.maxFeePerGas) {
408-
populatedTransaction.maxFeePerGas *= 2n;
411+
populatedTransaction.maxFeePerGas *= gasMultiple;
409412
}
410413
if (
411414
populatedTransaction.maxPriorityFeePerGas &&
412415
!overrides?.maxPriorityFeePerGas
413416
) {
414-
populatedTransaction.maxPriorityFeePerGas *= 2n;
417+
populatedTransaction.maxPriorityFeePerGas *= gasMultiple;
415418
}
416419

417420
job.log(`Populated transaction: ${stringify(populatedTransaction)}`);

0 commit comments

Comments
 (0)