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 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. |
- 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)
Get a good gas price for a transaction.
TODO: This is non-optimal, first draft implementation.
- Parameters
web3 (web3.main.Web3) –
- Return type
- estimate_gas_fees(web3, method=None)
Get a good gas price for a transaction.
TODO: This is non-optimal, first draft implementation.
- Parameters
web3 (web3.main.Web3) –
- 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