Python brownie

python brownie network setup, brownie network configuration, brownie networks, brownie hardhat, brownie infura

All You Need to Know About Python Brownie Network Setup

Introduction

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.

Prerequisite

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:

python brownie network setup, brownie network configuration, brownie networks, brownie hardhat

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:

python brownie network setup, brownie network configuration, brownie networks, brownie hardhat, brownie infura

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:

python brownie network setup, brownie network configuration, brownie networks, brownie alchemy api

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:

python brownie network setup, brownie network configuration, brownie networks, brownie import 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:

python brownie network setup, brownie network configuration, brownie networks, brownie modify network

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.

Delete Network

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:

python brownie network setup, brownie network configuration, brownie networks, brownie hardhat deploy contract

From the Hardhat terminal, you shall also see some output as per below:

python brownie network setup, brownie network configuration, brownie networks, brownie hardhat console

Conclusion

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!

Python interact with smart contract, python brownie, python mint NFT, python call smart contract function

Python Interact with Smart Contract

In the previous post, we have discussed how to deploy a NFT smart contract with Python Brownie. Once your contract is deployed into the blockchain, you can interact with your live contract either by building up your own Dapp or using scripts to perform some repetitive tasks like batch token transferring or airdrop. In this article, we will share with you some popular approaches to interact with smart contract in Python once the contract is live in public network.

Prerequisite

If you do not have Brownie installed, you shall go ahead to run the below pip command in your own Python virtual environment to install it:

#run in your venv
pip install eth-brownie

Brownie is heavily relying on Web3.py and use ganache cli as the default emulator for the development network, so you will get these dependencies installed automatically.

And assuming you have already compiled your smart contract and run the deployment script without any issue, if not, you may check through this post before proceeding. Now let’s explore how to interact with a deployed contract.

Work with Contract in Ganache Local Network

Using Ganache CLI

The Ganache CLI is used by default for development, and the local network is spin up when you are running your deployment script and terminated as soon as the script finished execution. In this case, the deployed contract is not stored anywhere permanently and you can only interact with it within same session before Ganache is shut down. The deployed contract is added into Brownie’s deployment map automatically, so you do not have to note down contract address when you want to refer to it. For instance, you can get your last deployed contract as per below:

# assuming a smart contract LegendNFT has been deployed
last_deployed_contract = LegendNFT[-1]

I would recommend you to use Brownie console when you just get started. It’s a Python interactive mode with all Brownie stuff loaded, and you can immediately get feedbacks if something goes wrong in your code. When everything is tested ok in the console, you can then copy your code to the Python script and make it re-usable for later use.

To start Brownie console for your local network, you can use the below command:

brownie console

And from the console, you can also deploy your contract:

>>> contract = LegendNFT.deploy({'from' : accounts[0]})

Transaction sent: 0x38643bde3b513dbaedf887c60162e5bb915b32bde2a804172dfaa2520b4d4ac8
Gas price: 0.0 gwei Gas limit: 12000000 Nonce: 0
LegendNFT.constructor confirmed Block: 1 Gas used: 3188048 (26.57%)
LegendNFT deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

Then you can verify that the contract address returned from deploy function is the same as the last record stored in Brownie’s deployment map:

>>> LegendNFT[-1]
<LegendNFT Contract '0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87'>
>>> contract == LegendNFT[-1]
True

In this case, you can always use LegendNFT[-1] to get the last deployed contract instead of replying on the contract address returned from your deploy function, and the deployment script can be kept separately so that you don’t accidently redeploying the contract when you want to run your other functions.

One advantage of using console is that you can use dir function to inspect what are the available functions in the contract instance, e.g.:

>>> dir(LegendNFT[-1])
[abi, address, approve, balance, balanceOf, bytecode, decode_input, getApproved, 
getTotalNFTsMintedSoFar, get_method, get_method_object, info, isApprovedForAll, 
makeLegendNFT, name, ownerOf, safeTransferFrom, selectors, setApprovalForAll, 
signatures, supportsInterface, symbol, tokenURI, topics, totalSupply, transferFrom, tx]

Some of these functions are not explicated implemented in the contract but inherited from other contracts, you will be able to call them as long as the functions have public or external visibility specifier.

Now you can call whatever functions available in your contract, for instance:

>>> contract.name()
'Codeforests Legend'
>>> contract.totalSupply()
1000000

>>> contract.getTotalNFTsMintedSoFar()
0

And you can also call your mint function which will change the state of the blockchain:

>>> contract.makeLegendNFT({'from':accounts[0]})

Transaction sent: 0x6dabe95e4c54d43864e91bfeb398c2bfb0549bf48d51db2cd42b9882e501a905
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
  LegendNFT.makeLegendNFT confirmed   Block: 2   Gas used: 777946 (6.48%)

<Transaction '0x6dabe95e4c54d43864e91bfeb398c2bfb0549bf48d51db2cd42b9882e501a905'>

Of course you would still not be able to see how it looks like for your newly minted NFT since the minting was only happening in your local network. If you use the tokenURI function and pass in the first token ID, you shall see some base64 encoded data similar to below:

>>> contract.tokenURI(0)

'data:application/json;base64,eyJuYW1lIjogIlNwbGVuZGlkIEMjIFByb2dyYW0iLCAiZGVzY3JpcHRpb24iOiAiQSBsZWdlbmQgTkZUIGNvbGxlY3Rpb24gZm9yIGFsbCBjb2RlZm9yZXN0cyByZWFkZXJzLiIsICJpbWFnZSI6ICJkYXRhOmltYWdlL3N2Zyt4bWw7YmFzZTY0LFBITjJaeUI0Yld4dWN6MG5hSFIwY0RvdkwzZDNkeTUzTXk1dmNtY3ZNakF3TUM5emRtY25JSEJ5WlhObGNuWmxRWE53WldOMFVtRjBhVzg5SjNoTmFXNVpUV2x1SUcxbFpYUW5JSFpwWlhkQ2IzZzlKekFnTUNBek5UQWdNelV3Sno0OGMzUjViR1UrTG1KaGMyVWdleUJtYVd4c09pQWpSa1ZHUlVaRk95Qm1iMjUwTFdaaGJXbHNlVG9nVEdWaFozVmxJRk53WVhKMFlXNDdJR1p2Ym5RdGMybDZaVG9nTWpSd2VEc2dmVHd2YzNSNWJHVStQSEpsWTNRZ2QybGtkR2c5SnpFd01DVW5JR2hsYVdkb2REMG5NVEF3SlNjZ1ptbHNiRDBuSXpBMU1UWXlNaWNnTHo0OGRHVjRkQ0I0UFNjMU1DVW5JSGs5SnpVd0pTY2dZMnhoYzNNOUoySmhjMlVuSUdSdmJXbHVZVzUwTFdKaGMyVnNhVzVsUFNkdGFXUmtiR1VuSUhSbGVIUXRZVzVqYUc5eVBTZHRhV1JrYkdVblBsTndiR1Z1Wkdsa0lFTWpJRkJ5YjJkeVlXMDhMM1JsZUhRK1BDOXpkbWMrIn0='

This is the URI info for your NFT, and you can use base64 decoder to decode the data to see more details, or use this website to preview the NFT content.

Using Ganache UI

If you are using Ganache desktop version, you can add a new network in Brownie and let Brownie to connect to your Ganache RPC client. Below is the command to add a new network:

brownie networks add Development ganache-local host=http://127.0.0.1 network_id=5777 port=7545 cmd=ganache-cli

The network ID/host/port can be found from your Ganache client as per below:

Python Interact with Smart Contract, Python mint NFT, Python Brownie, Ganache client

Once you have added the network, you can start your Brownie console as per below:

brownie console --network ganache-local

#Attached to local RPC client listening at '127.0.0.1:7545'...

And then deploy your contract:

>>> LegendNFT.deploy({'from' : accounts[0]})

You shall see some transactions went through if you check the Ganache client:

Python Interact with Smart Contract, Python mint NFT, Python Brownie, Ganache client

So you will be able to trace the transaction history from Ganache client, and once you restart it, those history data will be gone and you will get a fresh new blockchain environment same as how the Ganache CLI works.

Work with Smart Contract in Public Network

When you use Brownie to deploy your contracts to public network such as rinkeby or mainnet, it automatically organize the contract ABI file by chain ID into the build/deployments folder as per below:

Python Interact with Smart Contract, Python mint NFT, Python Brownie, Ganache client

So as long as you do not delete these files, you can use ContractName[-1] to get the last deployed contract. To connect to rinkeby in Brownie console, specify the network as rinkeby:

brownie console --network rinkeby

Then you can interact with your contract same as working in the local network except you will need to load a real wallet. If you want to be sure you are working on the correct contract, you can use LegendNFT.at with deployed contract address instead of LegendNFT[-1] :

Python Interact with Smart Contract, Python mint NFT, Python Brownie, Ganache client

Awesome! you can now mint your NFT from your deployed contract in a public network. Probably you don’t want to always type the same code in the console window when you want to mint a new NFT. so let’s put the code we have tested into a script called mint.py in our scripts folder as per below:

from brownie import network, config, accounts, LegendNFT

def get_account():
    if network.show_active() in ["development", "ganache-local"]:
        return accounts[0]
    else:
        return accounts.add(config["wallets"]["from_key"])

def mint_nft():
    contract = LegendNFT[-1]
    account = get_account()
    tx = contract.makeLegendNFT({"from" : account})
    tx.wait(1)
    print("Minted a new NFT with txn hash:", tx.txid)

def main():
    mint_nft()

So when we are running this script in local network, it will just pick a dummy account for deployment, but when working in public network, it will load the account from our config for deployment.

Let’s run this script with below:

brownie run mint.py --network rinkeby

You shall see similar output as we did from console window:

Python Interact with Smart Contract, Python mint NFT, Python Brownie, Ganache client

And here is the transaction details we can see from the etherscan by searching the transaction hash.

What if I have accidentally deleted my ABI file or I want to access the contract created by someone else where I do not have the ABI data?

Don’t worried, as long as you have your contract address, you will still be able to access the contract. You can use the below from_explorer from the Contract object to fetch the information from network explorer. For instance:

>>> contract = Contract.from_explorer("0xaa629be88190fe2077b970aa02f52da40ce98454")
Fetching source of 0xaA629BE88190fe2077b970aa02f52dA40cE98454 from api-rinkeby.etherscan.io...

>>> contract.name()
'Codeforests Legend'

And if you want to try the hard way, you can actually copy the ABI data from the network explorer such as etherscan, ployscan etc. into a local file, then load it manually into a JSON object. For instance, I have saved the ABI data from here into a file called abi_from_rinkeby.json:

Python Interact with Smart Contract, Python mint NFT, Python Brownie, Smart Contract ABI

and with Brownie console connected to Rinkeby,  I can access my contract as per below:

>>> import json
>>> with open("./scripts/abi_from_rinkeby.json") as file:
...     contract_abi = json.load(file)
...
>>> contract = Contract.from_abi("LegendNFT", "0xaA629BE88190fe2077b970aa02f52dA40cE98454", contract_abi)
>>>
>>> contract.totalSupply()
1000000
>>>
>>> contract.getTotalNFTsMintedSoFar()
3

Now with the above approach, you can start mint an NFT from someone else’s contract without going through their website.

Conclusion

In this article, we have reviewed through a few approaches you can use to interact with your smart contract. Brownie is built up on top of the Web3.py, but you can still use the APIs from the web3 module to interact with your contract. There are also quite a few of popular JavaScript libraries such as web3.js, ethers.js etc., you may take a look if you are planning to interact with the contract from your frontend JavaScript code.