Developers
EVM Tools

Python Brownie

11min

Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.

Brownie requires the Ganache local test blockchain to be installed first. Else it will not work.



To install Ganache you must install it globally using npm install -g.

Shell


Installing Brownie

The recommended way to install Brownie is via pipx. Pipx is a tool to help you install and run end-user applications written in Python. It’s roughly similar to macOS’s brew, JavaScript’s npx, and Linux’s apt.

Pipx installs Brownie into a virtual environment and makes it available directly from the commandline. Once installed, you will never have to activate a virtual environment prior to using Brownie.

Pipx does not ship with Python. If you have not used it before you will probably need to install it.

To install pipx:

Shell


To install Brownie using pipx:

Shell


Once installation is complete, type brownie to verify that it worked:

Shell


Type brownie networks list to view a list of existing networks.

Add Caduceus To Brownie:

To add a new network:

Shell


When declaring a new network, the following fields must always be included:

environment: the category that the network should be placed in, e.g. “Ethereum”, “Ethereum Classic”, or “Development” id: a unique identifier for the network, e.g. “mainnet” host: the address of the node to connect e.g. https://galaxy.block.caduceus.foundation The following fields are optional:

name: A longer name to use for the network. If not given, id is used. timeout: The number of seconds to wait for a response when making an RPC call. Defaults to 30.

Creating a new project

To create a new project, create a new folder. Inside the new folder, type:

Shell


Every Brownie project includes the following sub folders: contracts/: Contract sources interfaces/: Interface sources scripts/: Scripts for deployment and interaction tests/: Scripts for testing the project The following folders are also created, and used internally by Brownie for managing the project. You should not edit or delete files within these folders. build/: Project data such as compiler artifacts and unit test results reports/: JSON report files for use in the GUI



Writing code for Caduceus EVM

The contracts folder holds all contract source files for the project. Each time Brownie is run, it checks for new or modified files within this folder. If any are found, they are compiled and included within the project.

Contracts may be written in Solidity (with a .sol extension) or Vyper (with a .vy extension).

We recommend Solidity due to the wider support base available.

You can write contracts and any dependencies inside the contracts folder.

Working with Accounts

The Accounts container (available as accounts or just a) allows you to access all your local accounts.

JS


Each individual account is represented by an Account object that can perform actions such as querying a balance or sending ETH.



The Accounts.add method is used to randomly generate a new account:

You would likely add an existing account from a private key, as follows:

Shell




Compiling Contracts

Compiling contracts will result in compilation to the same project folder.

Shell






Writing a Deployment Script

Deployment scripts function in the same way as any other Brownie script, but there are a couple of things to keep in mind when writing one for a non-local network:



  1. Unless you are using your own node you will have to unlock a local account prior to deploying. This is handled within the script by calling Accounts.load. If you have not yet added a local account to Brownie, read the documentation on local account management.
  2. Most networks require that you to pay gas to miners. If no values are specified Brownie will calculate the gas price and limit automatically, but in some cases you may wish to manually declare these values.



Shell




Shell




Updated 25 Mar 2024
Doc contributor
Did this page help you?