gas

Documentation for eth_defi.gas Python module.

Gas price strategies.

Web3.py no longer support gas price strategies post London hard work.

Module Attributes

GAS_PRICE_BUFFER_MULTIPLIER

Safety buffer multiplier applied to maxFeePerGas in EIP-1559 transactions.

MIN_PRIORITY_FEE_PER_CHAIN

Per-chain minimum maxPriorityFeePerGas floors in wei, keyed by chain id.

Functions

apply_gas(tx, suggestion)

Apply gas fees to a raw transaction dict.

estimate_gas_fees(web3[, method, ...])

Get a good gas price for a transaction.

estimate_gas_price(web3[, method, ...])

Get a good gas price for a transaction.

node_default_gas_price_strategy(web3, ...)

Gas price strategy for blockchains not supporting dynamic gas fees.

Classes

GasPriceMethod

What method we did use for setting the gas price.

GasPriceSuggestion

Gas price details.

GAS_PRICE_BUFFER_MULTIPLIER = 1.12

Safety buffer multiplier applied to maxFeePerGas in EIP-1559 transactions.

On L2 chains like Arbitrum, the base fee can fluctuate between the time gas is estimated and the time the signed transaction reaches the sequencer. Without a buffer, this race condition causes rejections with:

max fee per gas less than block base fee

A 12% buffer absorbs typical L2 base fee volatility while keeping overpayment minimal (unused gas fee is refunded by the protocol).

For more information see:

MIN_PRIORITY_FEE_PER_CHAIN: dict[int, int] = {1: 1000000000, 137: 30000000000}

Per-chain minimum maxPriorityFeePerGas floors in wei, keyed by chain id.

The eth_maxPriorityFeePerGas RPC suggestion follows recently included transactions and on a quiet Ethereum mainnet can collapse to an effectively zero tip (e.g. 375,524 wei = 0.0004 gwei). Block builders order transactions by effective tip, so a near-zero tip transaction can linger unmined for minutes when the base fee ticks up. This aborted a multichain Lagoon vault deployment on 2026-06-10: a Safe addOwnerWithThreshold() transaction with a 375,524 wei tip took ~3.5 minutes (17 blocks) to confirm, blowing the 120 second receipt timeout.

Floors:

  • Ethereum mainnet (1): 1 gwei. Practically guarantees next-block inclusion, while costing only fractions of a dollar for a typical transaction and a few dollars even for a multi-million gas contract deployment.

  • Polygon (137): 30 gwei, the protocol-enforced spam prevention minimum.

Rollups (Arbitrum, Base, etc.) are deliberately not listed: their sequencers include zero-tip transactions normally, and a flat floor would overpay heavily relative to their tiny base fees.

Override per call with the min_priority_fee argument of estimate_gas_price().

class GasPriceMethod

Bases: enum.Enum

What method we did use for setting the gas price.

legacy = 'legacy'

Legacy chains

london = 'london'

Post London hard work

class GasPriceSuggestion

Bases: object

Gas price details.

Capture the necessary information for the gas price to used during the transaction building.

  • EIP-1559 London hard fork chains (Ethereumm mainnet)

  • Legacy EVM: Polygon, BNB Chain

method: eth_defi.gas.GasPriceMethod

How the gas price was determined

legacy_gas_price: Optional[int] = None

Non London hard fork chains

base_fee: Optional[int] = None

London hard fork chains

max_priority_fee_per_gas: Optional[int] = None

London hard fork chains

max_fee_per_gas: Optional[int] = None

London hard fork chains

get_tx_gas_params()

Get gas params as they are applied to ContractFunction.build_transaction()

Return type

dict

pformat()

Pretty format for logging.

Return type

str

__init__(method, legacy_gas_price=None, base_fee=None, max_priority_fee_per_gas=None, max_fee_per_gas=None)
Parameters
Return type

None

estimate_gas_price(web3, method=None, gas_price_buffer_multiplier=1.12, min_priority_fee=None)

Get a good gas price for a transaction.

Applies a safety buffer to maxFeePerGas to absorb base fee fluctuations between estimation and transaction submission. See GAS_PRICE_BUFFER_MULTIPLIER for details.

On EIP-1559 chains, the RPC suggested priority fee is floored to a per-chain minimum so transactions do not get stuck in the mempool with an effectively zero tip. See MIN_PRIORITY_FEE_PER_CHAIN for details.

Parameters
  • web3 (web3.main.Web3) – Web3 instance connected to a node.

  • method – Force a specific gas pricing method. If None, auto-detect based on whether the chain supports EIP-1559.

  • gas_price_buffer_multiplier (float) – Multiplier applied to maxFeePerGas to absorb base fee fluctuations. Defaults to GAS_PRICE_BUFFER_MULTIPLIER (1.12 = 12% buffer). Set to 1.0 to disable the buffer.

  • min_priority_fee (Optional[int]) –

    Minimum maxPriorityFeePerGas in wei for EIP-1559 chains.

    The RPC suggested priority fee is raised to at least this value. If None, the per-chain default from MIN_PRIORITY_FEE_PER_CHAIN is used, with no floor applied for chains not listed there. Set to 0 to always use the raw RPC suggestion.

Return type

eth_defi.gas.GasPriceSuggestion

estimate_gas_fees(web3, method=None, gas_price_buffer_multiplier=1.12, min_priority_fee=None)

Get a good gas price for a transaction.

Applies a safety buffer to maxFeePerGas to absorb base fee fluctuations between estimation and transaction submission. See GAS_PRICE_BUFFER_MULTIPLIER for details.

On EIP-1559 chains, the RPC suggested priority fee is floored to a per-chain minimum so transactions do not get stuck in the mempool with an effectively zero tip. See MIN_PRIORITY_FEE_PER_CHAIN for details.

Parameters
  • web3 (web3.main.Web3) – Web3 instance connected to a node.

  • method – Force a specific gas pricing method. If None, auto-detect based on whether the chain supports EIP-1559.

  • gas_price_buffer_multiplier (float) – Multiplier applied to maxFeePerGas to absorb base fee fluctuations. Defaults to GAS_PRICE_BUFFER_MULTIPLIER (1.12 = 12% buffer). Set to 1.0 to disable the buffer.

  • min_priority_fee (Optional[int]) –

    Minimum maxPriorityFeePerGas in wei for EIP-1559 chains.

    The RPC suggested priority fee is raised to at least this value. If None, the per-chain default from MIN_PRIORITY_FEE_PER_CHAIN is used, with no floor applied for chains not listed there. Set to 0 to always use the raw RPC suggestion.

Return type

eth_defi.gas.GasPriceSuggestion

apply_gas(tx, suggestion)

Apply gas fees to a raw transaction dict.

Example:

from web3 import Web3
from web3._utils.transactions import fill_nonce
from eth_account.signers.local import LocalAccount

web3: Web3
hot_wallet: LocalAccount

# Move 10 tokens from deployer to user1
tx = token.functions.transfer(hot_wallet.address, 10 * 10**18).build_transaction({
    "from": hot_wallet.address,
    'chainId': web3.eth.chain_id,
    "gas": 150_000,  # 150k gas should be more than enough for ERC20.transfer()
})

tx = fill_nonce(web3, tx)
gas_fees = estimate_gas_fees(web3)
apply_gas(tx, gas_fees)

signed = hot_wallet.sign_transaction(tx)
raw_bytes = get_tx_broadcast_data(signed)
tx_hash = web3.eth.send_raw_transaction(raw_bytes)
receipt = web3.eth.get_transaction_receipt(tx_hash)
Returns

Mutated dict

Parameters
Return type

dict

node_default_gas_price_strategy(web3, transaction_params)

Gas price strategy for blockchains not supporting dynamic gas fees.

This gas price strategy will query the JSON-RPC for the suggested flat fee. It works on chains that do not support EIP-1559 London hardfork style base fee + max fee dynamic pricing.

These include

  • BNB Chain

Example:

from eth_defi.gas import node_default_gas_price_strategy
web3.eth.set_gas_price_strategy(node_default_gas_price_strategy)

For more information see

Parameters
  • web3 (web3.main.Web3) –

  • transaction_params (dict) –

Return type

int