In my previous posts, I have reviewed through how to use Python Brownie to deploy smart contract and interact with a live contract by loading from ABI file or a block explorer. Brownie provides a list of predefined network configurations which probably will be sufficient for you most of the time, but It also gives you flexibility to configure and work on your own customized networks.
In this article, I will walk through with you a few tips on the Python Brownie network setup.
You will need to install the latest Brownie package in your Python virtual environment if you have not done yet so. Below is the pip command:
pip install eth-brownie
By default, Brownie uses Ganache for the development network. But if you plan to use hardhat with Brownie, you may also install hardhat. Read further from more details.
Brownie Check Existing Networks
To check the list of networks currently available, you can use the below command:
brownie networks list
And you shall see a long list of networks that Brownie supported by default, below is the first few:
Almost all the EVM compatible networks are included as well as the other networks which provided public JSON-RPC endpoint and block explorer.
To check the network details, such as the chain ID, host or block explorer etc, you can set the verbose option as true. For instance:
brownie networks list true
You shall see the network details similar to below:
This basically outputs all the information in the network-config.yaml file which is located under your useraccount/.brownie folder. And this setup can be shared among multiple of your brownie projects.
Brownie Add New Network
There are a few ways to add a new network to Brownie config, you can either do it with command line or through the configuration file.
Adding Network from Command Line
When you want to add a public network, Brownie requires at least network id, host and chainid. The rest are optional, but you may still need them, for instance, you will need to specify the block explorer if you want to get your source code verified by block explorer when deploying your contract.
Brownie uses the network id to lookup for the configurations, so it shall be unique among all the networks you have configured. For instance, you can configure as much networks as you can with different ids for Ethereum mainnet. And below is an example to add a network by using Alchemy API to access the Ethereum mainnet:
brownie networks add Ethereum mainnet-alchemy chainid=1 explorer=https://api.etherscan.io/api host=https://eth-mainnet.alchemyapi.io/v2/$ALCHEMY_PROJECT_TOKEN_ID multicall2=0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696 name="Mainnet (alchemy)"
You shall see below new network added successfully:
When adding a development network, you must specify the environment as development and provide two additional fields – cmd and cmd_settings. This would let Brownie knows how to launch the local network and connect to it.
Brownie Import Network Configuration File
When you have a network YAML file shared from someone else, you can easily load it into your Brownie network with the import command. E.g. below is a yaml file with the network details:
live: - name: Ethereum networks: - chainid: 1 explorer: https://api.etherscan.io/api host: https://eth-mainnet.alchemyapi.io/v2/$ALCHEMY_PROJECT_TOKEN_ID id: mainnet-alchemy multicall2: '0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696' name: Mainnet (alchemy)
We can save it to network-config.yaml to your current folder and run below command:
brownie networks import ./network-config.yaml
You shall see below output from your terminal:
Brownie v1.17.0 - Python development framework for Ethereum SUCCESS: Network settings imported from 'network-config.yaml'
If you check network list again, you shall see the newly added network:
You will encounter error if the id already exists, but if you intend to replace the original setup, you can add a replace option when importing the configuration. E.g.:
brownie networks import ./network-config.yaml true
As I mentioned earlier that Brownie actually saved the network configurations under the .brownie folder. You can also add new network directly into the file, but I would not recommend this approach just to avoid messing up the entire Brownie environment.
Modify Existing Network
Brownie also allows to modify your existing network from the command line. For instance, you can change the network details by using below:
brownie networks modify mainnet-alchemy name=mainnet-alchemy
Output as per below:
With the modify option, you can also update the other attributes such as the host such as to switch the network provider from Infura to Alchemy or Pocket Network etc.
Deleting an existing network can be done easily with below command:
brownie networks delete mainnet-alchemy
You shall get below output if Brownie found the network id from your existing configuration:
SUCCESS: Network 'mainnet-alchemy' has been deleted
Brownie Export Network Config
The network details can also be exported from Brownie command line. You can use the below command to export it to your current folder:
brownie networks export .
This would generate below outputs and you shall see a yaml file created with all the network details:
Brownie v1.17.0 - Python development framework for Ethereum SUCCESS: Network settings exported as 'network-config.yaml'
This would be very helpful when you want to share your network setup to someone else.
Using Hardhat with Brownie
Hardhat is an Ethereum development environment with similar functionality to Brownie. It includes a hardhat network which is similar to Ganache, and Brownie added the support for hardhat network in the recent release.
To use hardhat, you shall follow this installation guide to install nodejs and hardhat. And then launch your hardhat node as per below:
npx hardhat node --port 8545
With the above command, it shall start a local server at below port:
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
And from another terminal window, you can use below command:
brownie console --network hardhat
Brownie will connect to the hardhat RPC server, and now you can do your development and testing same as what you do in Ganache. E.g. To deploy a smart contract with the hardhat default accounts:
From the Hardhat terminal, you shall also see some output as per below:
In this post, we have reviewed through a few commands that Brownie provided for managing the network configurations such as adding new networks, modifying or delete existing networks as well as import/export network configuration files. One thing to be noted is that the Brownie project configuration brownie-config.yaml also allows you to specify a networks section, but this is only meant for specifying the default network for the current project, anything else you have defined under this section would not overwrite the Brownie network configuration.
Follow me on twitter if you have any feedback!