Skip to content

Latest commit

 

History

History
166 lines (99 loc) · 8.25 KB

core-gas.rst

File metadata and controls

166 lines (99 loc) · 8.25 KB

Setting Transaction Gas Prices

Dynamic Fee Transactions

If the network you are interacting with implements EIP-1559, you can use a better fee model when sending transactions. Instead of specifying gas_price, you specify priority_fee and an optional max_fee.

  • priority_fee determines maxPriorityFeePerGas, which is tipped to the miner. The recommended priority fee can be read from chain.priority_fee.
  • max_fee determines maxFeePerGas, which includes the base fee, which is the same for all transactions in the block and is burned, and your priority fee. The current base fee can be read from chain.base_fee.

Brownie uses base_fee * 2 + priority_fee as max_fee if you only specify the priority fee.

>>> accounts[0].transfer(accounts[1], priority_fee="2 gwei")
Transaction sent: 0x090755e0b75648d12b1ada31fa5957a07aadcbe8a34b8f9af59098f1890d1063
  Max fee: 4.0 gwei   Priority fee: 2.0 gwei   Gas limit: 30000000   Nonce: 0
  Transaction confirmed   Block: 1   Gas used: 21000 (0.07%)   Gas price: 2.875 gwei

Dynamic fee transactions do not support (and arguably don't need) gas strategies. The section below only applies to legacy transactions which use gas_price.

Setting Default Dynamic Fees

You can use network.priority_fee <main.max_fee> to set a default priority fee for all transactions:

>>> from brownie.network import gas_price
>>> priority_fee("2 gwei")

Setting the default to "auto" will dynamically determine the priority fee using web3.eth.max_priority_fee <web3.eth.Eth.max_priority_fee>. Seting to None will return to legacy-style transactions.

Gas Strategies

Gas strategies are objects that dynamically generate a gas price for a transaction. They can also be used to automatically replace pending transactions within the mempool.

Gas strategies come in three basic types:

  • Simple strategies provide a gas price once, but do not replace pending transactions.
  • Block strategies provide an initial price, and optionally replace pending transactions based on the number of blocks that have been mined since the first transaction was broadcast.
  • Time strategies provide an initial price, and optionally replace pending transactions based on the amount of time that has passed since the first transaction was broadcast.

Using a Gas Strategy

To use a gas strategy, first import it from brownie.network.gas.strategies:

>>> from brownie.network.gas.strategies import GasNowStrategy
>>> gas_strategy = GasNowStrategy("fast")

You can then provide the object in the gas_price field when making a transaction:

>>> accounts[0].transfer(accounts[1], "1 ether", gas_price=gas_strategy)

When the strategy replaces a pending transaction, the returned TransactionReceipt <brownie.network.transaction.TransactionReceipt> object will be for the transaction that confirms.

During non-blocking transactions <core-accounts-non-blocking>, all pending transactions are available within the history <brownie.network.state.TxHistory> object. As soon as one transaction confirms, the remaining dropped transactions are removed.

Setting a Default Gas Strategy

You can use network.gas_price <main.gas_price> to set a gas strategy as the default for all transactions:

>>> from brownie.network import gas_price
>>> gas_price(gas_strategy)

Available Gas Strategies

Building your own Gas Strategy

To implement your own gas strategy you must subclass from one of the gas strategy abstract base classes <api-network-gas-abc>.