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
Safety buffer multiplier applied to |
Functions
|
Apply gas fees to a raw transaction dict. |
|
Get a good gas price for a transaction. |
|
Get a good gas price for a transaction. |
|
Gas price strategy for blockchains not supporting dynamic gas fees. |
Classes
What method we did use for setting the gas price. |
|
Gas price details. |
- GAS_PRICE_BUFFER_MULTIPLIER = 1.12
Safety buffer multiplier applied to
maxFeePerGasin 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 feeA 12% buffer absorbs typical L2 base fee volatility while keeping overpayment minimal (unused gas fee is refunded by the protocol).
For more information see:
- class GasPriceMethod
Bases:
enum.EnumWhat method we did use for setting the gas price.
- legacy = 'legacy'
Legacy chains
- london = 'london'
Post London hard work
- class GasPriceSuggestion
Bases:
objectGas 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
- get_tx_gas_params()
Get gas params as they are applied to ContractFunction.build_transaction()
- Return type
- __init__(method, legacy_gas_price=None, base_fee=None, max_priority_fee_per_gas=None, max_fee_per_gas=None)
- estimate_gas_price(web3, method=None, gas_price_buffer_multiplier=1.12)
Get a good gas price for a transaction.
Applies a safety buffer to
maxFeePerGasto absorb base fee fluctuations between estimation and transaction submission. SeeGAS_PRICE_BUFFER_MULTIPLIERfor 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
maxFeePerGasto absorb base fee fluctuations. Defaults toGAS_PRICE_BUFFER_MULTIPLIER(1.12 = 12% buffer). Set to1.0to disable the buffer.
- Return type
- estimate_gas_fees(web3, method=None, gas_price_buffer_multiplier=1.12)
Get a good gas price for a transaction.
Applies a safety buffer to
maxFeePerGasto absorb base fee fluctuations between estimation and transaction submission. SeeGAS_PRICE_BUFFER_MULTIPLIERfor 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
maxFeePerGasto absorb base fee fluctuations. Defaults toGAS_PRICE_BUFFER_MULTIPLIER(1.12 = 12% buffer). Set to1.0to disable the buffer.
- Return type
- 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
tx (dict) –
suggestion (eth_defi.gas.GasPriceSuggestion) –
- Return type
- 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