gas

Documentation for eth_defi.gas Python module.

Gas price strategies.

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

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.

class GasPriceMethod

Bases: enum.Enum

What method we did use for setting the gas price.

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

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

None

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

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

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

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

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