Brownie

1.21.0 · active · verified Thu Apr 16

Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine (EVM). It simplifies the process of deploying, testing, and interacting with Solidity and Vyper smart contracts. The library is actively maintained with regular updates, including development releases leading to minor version bumps, typically focusing on performance, compiler support, and ecosystem compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Brownie project, define a simple Solidity contract, deploy it to a local development network, and interact with its functions. It assumes a local blockchain (like Ganache or Hardhat) is running or can be launched by Brownie. Run `brownie init` and create the `SimpleStorage.sol` file as described in the comments before executing the Python script.

# 1. First, set up a Brownie project and a simple contract:
# $ brownie init simple_project
# $ cd simple_project
# $ mkdir contracts
# $ echo '// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\ncontract SimpleStorage {\n    uint256 public number;\n    function store(uint256 _number) public { number = _number; }\n    function retrieve() public view returns (uint256) { return number; }\n}' > contracts/SimpleStorage.sol
# $ mkdir scripts

# 2. Then, create scripts/deploy_and_interact.py with the following content:

from brownie import SimpleStorage, accounts, network

def main():
    # Connect to a local development network (e.g., Ganache, Hardhat)
    # If not already connected, Brownie will attempt to launch a local 'ganache-cli' fork
    if not network.is_connected():
        network.connect('development')

    print(f"Active network: {network.show_active()}")

    # Get the first available account for deployment
    deployer = accounts[0]
    print(f"Deploying from account: {deployer.address}")

    # Deploy the SimpleStorage contract
    print("Deploying SimpleStorage...")
    simple_storage = SimpleStorage.deploy({"from": deployer})
    print(f"SimpleStorage deployed at: {simple_storage.address}")

    # Interact with the contract
    initial_value = simple_storage.retrieve()
    print(f"Initial stored value: {initial_value}")

    new_value = 777
    print(f"Storing new value: {new_value}")
    tx = simple_storage.store(new_value, {"from": deployer})
    tx.wait(1) # Wait for the transaction to be mined

    updated_value = simple_storage.retrieve()
    print(f"Updated stored value: {updated_value}")

# 3. Finally, run the script from your project root:
# $ brownie run scripts/deploy_and_interact.py --network development

view raw JSON →