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.

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)

Get a good gas price for a transaction.

TODO: This is non-optimal, first draft implementation.

Parameters

web3 (web3.main.Web3) –

Return type

eth_defi.gas.GasPriceSuggestion

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

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