Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/indexer-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down Expand Up @@ -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,
}
Expand Down
41 changes: 41 additions & 0 deletions packages/indexer-common/src/__tests__/cap-priority-fee.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
6 changes: 6 additions & 0 deletions packages/indexer-common/src/network-specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
9 changes: 9 additions & 0 deletions packages/indexer-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
IndexerErrorCode,
SubgraphClient,
TransactionManager,
capPriorityFee,
specification as spec,
GraphNode,
NetworkMonitor,
Expand Down Expand Up @@ -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
// * -----------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions packages/indexer-common/src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading