Documentation for eth_defi.gas.apply_gas function.

apply_gas(tx, suggestion)[source]

Apply gas fees to a raw transaction dict.


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)
tx_hash = web3.eth.send_raw_transaction(signed.rawTransaction)
receipt = web3.eth.get_transaction_receipt(tx_hash)