A Python framework for Ethereum smart contract deployment, testing and interaction.
npx @tessl/cli install tessl/pypi-eth-brownie@1.21.0A comprehensive Python framework for Ethereum smart contract deployment, testing and interaction. Brownie provides publication-quality development tools for blockchain applications, serving as the foundational framework for Python-based Ethereum development with built-in support for Solidity and Vyper.
pip install eth-brownieimport brownie
from brownie import accounts, Contract, project, network, chain, history, web3Common imports for specific functionality:
from brownie.convert import Wei, Fixed, to_address
from brownie.test import given, strategy
from brownie.network import gas_limit, gas_price, connect, disconnectfrom brownie import accounts, Contract, project, network
# Connect to local development network
network.connect('development')
# Get account for transactions
account = accounts[0] # Use first local account
# or load account from private key
account = accounts.add('0x...') # Add account from private key
# Deploy a contract
contract_source = '''
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
function set(uint256 x) public { storedData = x; }
function get() public view returns (uint256) { return storedData; }
}
'''
# Compile and deploy
compiled = project.compile_source(contract_source)
contract = compiled.SimpleStorage.deploy({'from': account})
# Interact with contract
tx = contract.set(42, {'from': account})
print(f"Transaction hash: {tx.txid}")
print(f"Stored value: {contract.get()}")
# Check transaction history
print(f"Gas used: {tx.gas_used}")
print(f"Transaction events: {tx.events}")Brownie's architecture centers on several key components:
This design enables complete control over the smart contract development lifecycle from initial development through testing to production deployment.
Comprehensive account management including local private key accounts, external signers, and transaction broadcasting with gas optimization and error handling.
class Accounts:
def __getitem__(self, index: int) -> LocalAccount: ...
def add(self, private_key: str) -> LocalAccount: ...
def load(self, filename: str, password: str = None) -> LocalAccount: ...
class LocalAccount:
def __init__(self, address: str, private_key: str): ...
def transfer(self, to: str, amount: int, **kwargs) -> TransactionReceipt: ...
def deploy(self, contract, *args, **kwargs) -> Contract: ...Smart contract deployment, method calls, event handling, and transaction management with comprehensive debugging and error reporting capabilities.
class Contract:
def __init__(self, address: str, abi: list): ...
def __getattr__(self, name: str): ... # Contract method access
class ProjectContract(Contract):
@classmethod
def deploy(cls, *args, **kwargs) -> 'ProjectContract': ...
class ContractContainer:
def deploy(self, *args, **kwargs) -> ProjectContract: ...
def at(self, address: str) -> ProjectContract: ...Blockchain network connections, RPC node management, chain state control, and multi-network configuration for development, testing, and production environments.
def connect(network: str = None, launch_rpc: bool = True) -> None: ...
def disconnect() -> None: ...
def is_connected() -> bool: ...
class Chain:
def snapshot() -> str: ...
def revert(snapshot_id: str) -> None: ...
def mine(blocks: int = 1) -> None: ...
class Rpc:
def launch(self, cmd: str) -> None: ...
def kill(self) -> None: ...Brownie project creation, contract compilation, script execution, and build artifact management with support for both Solidity and Vyper smart contracts.
def new(path: str = ".", name: str = None, ignore_existing: bool = False) -> Project: ...
def load(project_path: str = ".", name: str = None) -> Project: ...
def compile_source(source: str, solc_version: str = None, **kwargs): ...
def run(script_path: str, method_name: str = None, *args, **kwargs): ...
class Project:
def load_config(self) -> None: ...
def compile(self, **kwargs) -> None: ...
def close(self) -> None: ...Type conversion utilities for seamless interaction between Python and Ethereum data types, plus integration with Hypothesis for property-based testing.
class Wei:
def __init__(self, value: Union[int, str, float]): ...
def to(self, unit: str) -> Union[int, float]: ...
def to_address(value: Any) -> str: ...
def to_uint(value: Any, type_str: str = "uint256") -> int: ...
def to_bytes(value: Any, type_str: str = "bytes32") -> bytes: ...
def given(**strategies) -> Callable: ... # Testing decorator
def strategy(name: str, **kwargs) -> Callable: ... # Strategy creatorComprehensive CLI tools for project management, compilation, testing, and deployment with support for interactive console and GUI.
brownie init [project_name] # Initialize new project
brownie bake [template_name] # Create from brownie-mix template
brownie compile [options] # Compile contracts
brownie console [options] # Interactive Python console
brownie test [options] # Run test suite
brownie run [script] [method] # Execute project scripts
brownie accounts [command] # Manage local accounts
brownie networks [command] # Manage network connections
brownie pm [command] # Package manager
brownie gui [options] # Launch web GUIComprehensive exception hierarchy for handling network, contract, compilation, and transaction errors with detailed error messages and debugging support.
# Network Exceptions
class VirtualMachineError(Exception): ... # EVM execution errors
class TransactionError(VirtualMachineError): ... # Transaction failures
class RPCConnectionError(Exception): ... # RPC connection issues
class MainnetUndefined(Exception): ... # Mainnet configuration errors
# Project Exceptions
class CompilerError(Exception): ... # Contract compilation errors
class ProjectNotFound(Exception): ... # Missing project errors
class ContractNotFound(Exception): ... # Contract reference errors
# Account Exceptions
class UnknownAccount(Exception): ... # Invalid account referencesGlobal configuration system for network settings, compiler options, and development environment customization through brownie-config.yaml files.
# Global configuration access
config: ConfigContainer # Configuration settings container
# Configuration categories
config.networks # Network connection settings
config.compiler # Solidity/Vyper compiler settings
config.dependencies # Package dependency management
config.hypothesis # Property-based testing configuration# Global singletons available after import
accounts: Accounts # Account management
chain: Chain # Blockchain state
history: TxHistory # Transaction history
rpc: Rpc # RPC node control
web3: Web3 # Web3 interface
multicall: Multicall # Batch calls
config: ConfigContainer # Configuration
# Constants
ETH_ADDRESS: str = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
ZERO_ADDRESS: str = "0x0000000000000000000000000000000000000000"