From 7653b38105b89eb81c89ffdc5c4c41f151f878c6 Mon Sep 17 00:00:00 2001 From: neriumpete Date: Wed, 4 Nov 2026 16:06:04 +0200 Subject: [PATCH] feat(agent): add --max-priority-fee-per-gas to cap the transaction tip --- packages/indexer-agent/README.md | 4 ++ packages/indexer-agent/src/commands/start.ts | 8 ++++ .../src/__tests__/cap-priority-fee.test.ts | 41 +++++++++++++++++++ .../src/network-specification.ts | 6 +++ packages/indexer-common/src/network.ts | 9 ++++ packages/indexer-common/src/transactions.ts | 13 ++++++ 6 files changed, 81 insertions(+) create mode 100644 packages/indexer-common/src/__tests__/cap-priority-fee.test.ts diff --git a/packages/indexer-agent/README.md b/packages/indexer-agent/README.md index be27e2a6b..a3319d347 100644 --- a/packages/indexer-agent/README.md +++ b/packages/indexer-agent/README.md @@ -85,6 +85,10 @@ Ethereum --base-fee-per-gas-max The maximum base fee per gas (gwei) to use for transactions, for legacy transactions this will be treated as the max gas price [number] + --max-priority-fee-per-gas The maximum priority fee (tip) per gas (gwei) + to start transactions with. Resubmissions + still raise it by --gas-increase-factor + [number] --transaction-attempts The maximum number of transaction attempts (Use 0 for unlimited) [number] [default: 0] --confirmation-blocks The number of blocks to wait for a transaction diff --git a/packages/indexer-agent/src/commands/start.ts b/packages/indexer-agent/src/commands/start.ts index 197f0c2d2..c00071b37 100644 --- a/packages/indexer-agent/src/commands/start.ts +++ b/packages/indexer-agent/src/commands/start.ts @@ -92,6 +92,13 @@ export const start = { required: false, group: 'Ethereum', }) + .option('max-priority-fee-per-gas', { + description: + 'The maximum priority fee (tip) per gas (gwei) to start transactions with. Resubmissions still raise it by --gas-increase-factor', + type: 'number', + required: false, + group: 'Ethereum', + }) .option('transaction-attempts', { description: 'The maximum number of transaction attempts (Use 0 for unlimited)', @@ -435,6 +442,7 @@ export async function createNetworkSpecification( gasIncreaseFactor: argv.gasIncreaseFactor, gasPriceMax: argv.gasPriceMax, baseFeePerGasMax: argv.baseFeeGasMax, + maxPriorityFeePerGas: argv.maxPriorityFeePerGas, maxTransactionAttempts: argv.maxTransactionAttempts, confirmationBlocks: argv.confirmationBlocks, } diff --git a/packages/indexer-common/src/__tests__/cap-priority-fee.test.ts b/packages/indexer-common/src/__tests__/cap-priority-fee.test.ts new file mode 100644 index 000000000..2ae64b570 --- /dev/null +++ b/packages/indexer-common/src/__tests__/cap-priority-fee.test.ts @@ -0,0 +1,41 @@ +import { FeeData, parseUnits } from 'ethers' +import { capPriorityFee } from '../transactions' +import { TransactionMonitoring } from '../network-specification' + +const gwei = (x: string) => parseUnits(x, 'gwei') + +describe('capPriorityFee', () => { + test('lowers the tip and maxFeePerGas by the same amount', () => { + // ethers' fallback: 1 gwei tip on a 0.02 gwei base fee + const fee = new FeeData(gwei('0.02'), gwei('1.04'), gwei('1')) + const capped = capPriorityFee(fee, 0n) + expect(capped.maxPriorityFeePerGas).toEqual(0n) + expect(capped.maxFeePerGas).toEqual(gwei('0.04')) + expect(capped.gasPrice).toEqual(gwei('0.02')) + }) + + test('leaves a tip under the cap alone', () => { + const fee = new FeeData(gwei('0.02'), gwei('0.041'), gwei('0.001')) + expect(capPriorityFee(fee, gwei('0.01'))).toBe(fee) + }) + + test('leaves legacy fee data alone', () => { + const fee = new FeeData(gwei('5'), null, null) + expect(capPriorityFee(fee, 0n)).toBe(fee) + }) +}) + +describe('TransactionMonitoring.maxPriorityFeePerGas', () => { + test('accepts zero and converts gwei to wei', () => { + expect( + TransactionMonitoring.parse({ maxPriorityFeePerGas: 0 }).maxPriorityFeePerGas, + ).toEqual(0n) + expect( + TransactionMonitoring.parse({ maxPriorityFeePerGas: 0.001 }).maxPriorityFeePerGas, + ).toEqual(gwei('0.001')) + }) + + test('is unset by default', () => { + expect(TransactionMonitoring.parse({}).maxPriorityFeePerGas).toBeUndefined() + }) +}) diff --git a/packages/indexer-common/src/network-specification.ts b/packages/indexer-common/src/network-specification.ts index 7bb86d6e2..3b50c8e94 100644 --- a/packages/indexer-common/src/network-specification.ts +++ b/packages/indexer-common/src/network-specification.ts @@ -90,6 +90,12 @@ export const TransactionMonitoring = z .default(100) .transform((x) => x * 10 ** 9) .optional(), + maxPriorityFeePerGas: z + .number() + .nonnegative() + .finite() + .transform((x) => BigInt(Math.round(x * 10 ** 9))) + .optional(), maxTransactionAttempts: z.number().nonnegative().finite().default(0), confirmationBlocks: positiveNumber().default(3), }) diff --git a/packages/indexer-common/src/network.ts b/packages/indexer-common/src/network.ts index ee8db5efb..13fb247ee 100644 --- a/packages/indexer-common/src/network.ts +++ b/packages/indexer-common/src/network.ts @@ -34,6 +34,7 @@ import { IndexerErrorCode, SubgraphClient, TransactionManager, + capPriorityFee, specification as spec, GraphNode, NetworkMonitor, @@ -118,6 +119,14 @@ export class Network { specification.networkProvider.pollingInterval, ) + // Signers fill in fees from getFeeData, so capping it here covers every transaction. + const maxPriorityFeePerGas = specification.transactionMonitoring.maxPriorityFeePerGas + if (maxPriorityFeePerGas !== undefined) { + const getFeeData = networkProvider.getFeeData.bind(networkProvider) + networkProvider.getFeeData = async () => + capPriorityFee(await getFeeData(), maxPriorityFeePerGas) + } + // * ----------------------------------------------------------------------- // * Network Subgraph // * ----------------------------------------------------------------------- diff --git a/packages/indexer-common/src/transactions.ts b/packages/indexer-common/src/transactions.ts index fc99bbf20..aacfd3f3e 100644 --- a/packages/indexer-common/src/transactions.ts +++ b/packages/indexer-common/src/transactions.ts @@ -25,6 +25,19 @@ import { SubgraphServiceContracts, } from '@graphprotocol/toolshed/deployments' +// Lowering the tip lowers maxFeePerGas by the same amount, so the base fee headroom is kept. +export function capPriorityFee(feeData: FeeData, cap: bigint): FeeData { + const { gasPrice, maxFeePerGas, maxPriorityFeePerGas } = feeData + if (maxPriorityFeePerGas === null || maxPriorityFeePerGas <= cap) { + return feeData + } + return new FeeData( + gasPrice, + maxFeePerGas === null ? null : maxFeePerGas - (maxPriorityFeePerGas - cap), + cap, + ) +} + export class TransactionManager { ethereum: Provider wallet: HDNodeWallet