Skip to content

Commit af74b4a

Browse files
committed
set max gas price per wei
1 parent 1505c30 commit af74b4a

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/utils/env.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ export const env = createEnv({
102102
/**
103103
* Experimental env vars. These may be renamed or removed in future non-major releases.
104104
*/
105-
// Sets how long the mine worker waits for a transaction receipt before considering the transaction dropped (default: 30 minutes).
105+
// Sets how long the mine worker waits for a transaction receipt before considering the transaction dropped. Default: 30 minutes
106106
EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS: z.coerce
107107
.number()
108108
.default(30 * 60),
109+
// Sets the max gas price for a transaction attempt. Most RPCs reject transactions above a certain gas price. Default: 10^18 wei.
110+
EXPERIMENTAL__MAX_GAS_PRICE_WEI: z.coerce.number().default(10 ** 18),
109111
},
110112
clientPrefix: "NEVER_USED",
111113
client: {},
@@ -144,6 +146,8 @@ export const env = createEnv({
144146
NONCE_MAP_COUNT: process.env.NONCE_MAP_COUNT,
145147
EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS:
146148
process.env.EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS,
149+
EXPERIMENTAL__MAX_GAS_PRICE_WEI:
150+
process.env.EXPERIMENTAL__MAX_GAS_PRICE_WEI,
147151
METRICS_PORT: process.env.METRICS_PORT,
148152
METRICS_ENABLED: process.env.METRICS_ENABLED,
149153
},

src/worker/tasks/sendTransactionWorker.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,34 +565,39 @@ const _minutesFromNow = (minutes: number) =>
565565
* @param populatedTransaction The transaction with estimated gas from RPC.
566566
* @param resendCount The resend attempt #. Example: 2 = the transaction was initially sent, then resent once. This is the second resend attempt.
567567
*/
568-
export const _updateGasFees = (
568+
export function _updateGasFees(
569569
populatedTransaction: PopulatedTransaction,
570570
resendCount: number,
571571
overrides: SentTransaction["overrides"],
572-
): PopulatedTransaction => {
572+
): PopulatedTransaction {
573573
if (resendCount === 0) {
574574
return populatedTransaction;
575575
}
576576

577-
const multiplier = BigInt(Math.min(10, resendCount * 2));
578-
577+
const multiplier = Math.min(10, resendCount * 2);
579578
const updated = { ...populatedTransaction };
580579

581580
// Update gas fees (unless they were explicitly overridden).
581+
// Do not exceed MAX_GAS_PRICE_WEI.
582+
const MAX_GAS_PRICE_WEI = env.EXPERIMENTAL__MAX_GAS_PRICE_WEI;
582583

583-
if (updated.gasPrice && !overrides?.gasPrice) {
584-
updated.gasPrice *= multiplier;
584+
if (updated.gasPrice) {
585+
const newGasPrice = Number(updated.gasPrice) * multiplier;
586+
updated.gasPrice = BigInt(Math.min(newGasPrice, MAX_GAS_PRICE_WEI));
585587
}
586588
if (updated.maxPriorityFeePerGas && !overrides?.maxPriorityFeePerGas) {
587-
updated.maxPriorityFeePerGas *= multiplier;
589+
updated.maxPriorityFeePerGas *= BigInt(multiplier);
588590
}
589591
if (updated.maxFeePerGas && !overrides?.maxFeePerGas) {
590-
updated.maxFeePerGas =
591-
updated.maxFeePerGas * 2n + (updated.maxPriorityFeePerGas ?? 0n);
592+
const maxPriorityFeePerGas = updated.maxPriorityFeePerGas ?? 0n;
593+
const newMaxFeePerGas = Number(
594+
updated.maxFeePerGas * 2n + maxPriorityFeePerGas,
595+
);
596+
updated.maxFeePerGas = BigInt(Math.min(newMaxFeePerGas, MAX_GAS_PRICE_WEI));
592597
}
593598

594599
return updated;
595-
};
600+
}
596601

597602
// Must be explicitly called for the worker to run on this host.
598603
export const initSendTransactionWorker = () => {

0 commit comments

Comments
 (0)