NetworkEnv
Inherits: Env
Description
NetworkEnv is a specialized environment for interacting with real or forked blockchain networks via RPC. It extends the base Env class with network-specific functionality including account management, transaction broadcasting, and state forking.
tx_settings
boa.env.tx_settings
Description
Access and modify transaction settings for network interactions. These settings control gas estimation, transaction timeouts, base fee calculations, and priority fee calculation.
Attributes
base_fee_estimator_constant: Number of blocks ahead to estimate base fee for (default: 5)priority_fee_strategy: Optional function for EIP-1559 priority fee calculation. Defaults toNone, which useseth_maxPriorityFeePerGas.poll_timeout: Timeout in seconds for waiting for transaction receipts (default: 240.0)estimate_gas_block_identifier: Block identifier for gas estimation (default: "pending")
Examples
>>> import boa
>>> # Increase base fee estimation to 10 blocks ahead (for congested networks)
>>> boa.env.tx_settings.base_fee_estimator_constant = 10
>>>
>>> # Increase timeout for slow networks
>>> boa.env.tx_settings.poll_timeout = 300.0 # 5 minutes
>>>
>>> # Set priority fee to at least 2% of the latest base fee
>>> boa.env.tx_settings.priority_fee_strategy = lambda ctx: max(
... ctx.rpc_priority_fee,
... ctx.base_fee // 50,
... )
>>>
>>> # Don't use block parameter for gas estimation (for certain RPC providers)
>>> boa.env.tx_settings.estimate_gas_block_identifier = None
Note
These settings are particularly useful when dealing with network congestion or RPC provider quirks.
The base_fee_estimator_constant determines how many blocks ahead to calculate the base fee cap. Since EIP-1559 allows base fee to increase by at most 12.5% per block, the maximum base fee after n blocks is calculated as: current_base_fee * (9/8)^n. For example, with the default value of 5, the base fee cap would be current_base_fee * 1.8 (approximately). If you encounter errors like "max fee per gas less than block base fee", try increasing this value.
The priority_fee_strategy, when set, receives a context with base_fee, base_fee_estimate, rpc_priority_fee, and chain_id, and must return the priority fee in wei as an int. More context fields may be exposed as needed.
capabilities
boa.env.capabilities
Description
Access the capabilities detection system that automatically detects EVM features supported by the current network. This property provides information about supported opcodes and EVM versions.
Attributes
has_cancun: Whether Cancun opcodes (PUSH0, MCOPY, TLOAD/TSTORE) are supportedhas_shanghai: Whether Shanghai opcodes are supportedhas_push0: Whether PUSH0 opcode is supportedhas_mcopy: Whether MCOPY opcode is supportedhas_transient: Whether transient storage (TLOAD/TSTORE) is supporteddescribe_capabilities(): Get a human-readable string describing the capabilities
Examples
>>> import boa
>>> boa.set_network_env("https://eth-mainnet.g.alchemy.com/v2/YOUR-KEY")
>>>
>>> # Check if Cancun features are available
>>> if boa.env.capabilities.has_cancun:
... print("Cancun features are supported")
... else:
... print("Cancun features not available")
...
>>> # Get human-readable description
>>> print(boa.env.capabilities.describe_capabilities())
'cancun' # or 'shanghai', 'paris', etc.
Note
This is particularly useful when deploying contracts that use newer opcodes, as it prevents deployment failures on networks that don't support them yet.
add_account
add_account(account: Account, force_eoa=False)
Description
Add an account to the network environment. This account can then be used to sign and send transactions.
Parameters
account: AnAccountobject (e.g., from eth_account library)force_eoa: Whether to force the account to be treated as an EOA even if it has code
Example
anchor
anchor()
Description
Create a state snapshot using the RPC's evm_snapshot method. When used as a context manager, automatically reverts to the snapshot on exit using evm_revert.
Example
>>> import boa
>>> contract = boa.load("MyContract.vy")
>>> initial_value = contract.get_value()
>>>
>>> with boa.env.anchor():
... contract.set_value(42)
... assert contract.get_value() == 42
...
>>> assert contract.get_value() == initial_value # Reverted!
Note
Requires RPC support for evm_snapshot and evm_revert methods. Most local development nodes (Anvil, Hardhat) support these.
fork
fork(url: str, block_identifier: Union[int, str] = "safe", **kwargs)
Description
Fork the state from a remote network. This creates a local copy of the blockchain state that can be modified without affecting the real network.
Parameters
url: The RPC URL to fork fromblock_identifier: Block number or tag to fork from (default: "safe")**kwargs: Additional arguments passed to Web3 provider
Example
deploy_code
deploy_code(bytecode: bytes, *args, value=0, gas=None, sender=None) -> tuple[Address, bytes]
Description
Deploy contract bytecode to the network. Returns the deployed contract address.
Parameters
bytecode: The deployment bytecode*args: Constructor argumentsvalue: ETH value to send with deploymentgas: Gas limit (auto-estimated if None)sender: Sender address (uses env.eoa if None)
Returns
A tuple containing: - The address of the deployed contract - The return data from the deployment transaction
Example
get_balance
get_balance(address: str) -> int
Description
Get the ETH balance of an address from the network.
Parameters
address: The address to query
Returns
The balance in wei.
Example
get_code
get_code(address: str) -> bytes
Description
Get the bytecode at an address from the network.
Parameters
address: The address to query
Returns
The bytecode as bytes.
Example
set_balance
set_balance(address: str, value: int)
Description
Set the ETH balance of an address. Note: This method is not implemented in NetworkEnv and will raise NotImplementedError.
Parameters
address: The address to modifyvalue: The new balance in wei
Example
Note
This method raises NotImplementedError in NetworkEnv. To set balances in a test environment, use boa.fork() which returns a regular Env instance that supports balance manipulation.