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.
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:
To install Brownie using pipx:
Once installation is complete, type brownie to verify that it worked:
Type brownie networks list to view a list of existing networks.
To add a new network:
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.
To create a new project, create a new folder. Inside the new folder, type:
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
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.
The Accounts container (available as accounts or just a) allows you to access all your local accounts.
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:
Compiling contracts will result in compilation to the same project folder.
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:
- 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.