Brownie in Web3.0 – Develop Ethereum Smart Contracts Interacting with Cryptocurrency Network Using Brownie and Python

For Web3.0 smart contract developers, Brownies makes your life much easier to develop, import, test, and debug smart contracts deployed on the Ethereum blockchain network. Moreover, it facilitates you to set up transactions, methods, and variables to interact with Ethereum.

Since that, I will go through basic knowledge regarding Brownie and expand on how to use Brownie to develop Ethereum smart contracts and deploy them onto the blockchain network.

For Web3.0 smart contract developers, Brownies makes your life much easier to develop, import, test, and debug smart contracts deployed on the Ethereum blockchain network. Moreover, it facilitates you to set up transactions, methods, and variables to interact with Ethereum.

Since that, I will go through basic knowledge regarding Brownie and expand on how to use Brownie to develop Ethereum smart contracts and deploy them onto the blockchain network.

Brownie Web3.0 – Ingredients to Develop Ethereum Smart Contracts Interacting with Cryptocurrency Network

Brownie, Python, Infura, os, Ganache, MetaMask, ETH Goerli test network

Table of Content

What’s Brownie

Brownie is a friendly and easy-to-use framework for developing Ethereum smart contracts. The main value from its scope of functions has 3 aspects:

Deploy the smart contracts needed on blockchain networks

Create different types and purposes of smart contracts and deploy them onto the blockchain such as Ethereum. And you can create and initiate any transactional elements that matter in the interaction with the blockchain.

Interact with the smart contracts

Brownie framework and folder path facilitate you to write scripts or use the Brownie console to interact with the smart contracts deployed on the main net. Or you can implement UAT in a local blockchain environment, e.g: Ganache

Test the smart contracts under development

Brownie provides a quick, friendly, and easy-to-use smart contract development testing environment and those related functions to implement the test. For example, you can get detailed information when a transaction reverts to help you pinpoint the issue quickly.

Activate Brownie and Install Ganache Local Blockchain Installation

Step1: Brownie Installation

Take the MacOS environment for example. First thing first, we need to install a visual environment for Brownie by typing in the line of codes as follows in the terminal.

Python3 -m pip install –user pipx

Python3 -m pipx ensure path

After installation is done, be sure to close the terminal to avoid conflict or activate the visual environment.

Then, please type in the line of codes as follows to install Brownie

Pips install eth-brownie

To check if you have installed it on your Mac, we can type in the checker command, which is the same as any other package check.

Step 2: Create the first Brownie script folder and project

I assume you have created a new folder in your coding software, e.g: visual studio code. Now we can try to create a new brownie project. Here is the code for initiation as follows:

Brownie init

Once the initiation is done, as well as react installation, the Brownie folder comes up with a list of folders by default. They are the folders that have different functions as follows:

  • Build
  • Contracts
  • Interfaces
  • Reports
  • Scripts
  • Tests

Step 3: Install Local Ganache Cli

Brownie by default works with the Ganache CLI in the terminal, which is a local blockchain visual environment. In our previous article, the script directly connects to the web UI version. This time requires us to install Ganache CLI.

  • Download node.js (Here is the macOS version) and Install it on the device
  • Command: npm install –global yarn. We need to Install yarn, which is a package manager that actually allows us to download pieces and packages like the Ganache CLI. Be sure to add sudo if your device might respond you don’t have permission to install yarn.
  • Install Ganache CLI by using the commands: yarn global add ganache-cli. Again, be sure to add sudo if your device rejects again.

Create the 1st smart contract and deploy it onto Ganache CLI

Add the smart contract

In Brownie’s contract folders, it is for you to save the smart contract file, such as easy2digitalStore.sol. After you add the file under this folder, you can input this line of code as follows:

Brownie compile

Once you compile the given sol file, you can see all the compiled information saved under the build folder in the contract folder as a JSON format.

Deploy on the ETH Blockchain

First thing first, we need to create a new Python file under the script folder

Deployment.py

In the script, we need to use one brownie’s method which is the accounts(), which it’s for interacting with the local visual blockchain, Ganache CLI. Here are the code samples follows:

Import brownie import accounts

def single_contract():

         localGanacheAccount = accounts[0]

         print(localGanacheAccount)

Def main():

        single_contracct()

And we can use the brownie run to activate the python script in the terminal.

Add a new account and connect to the real test Network

We can create a new account instead of using the Ganache CLI one. Here are both the command and the code as follows:

In terminal:

Brownie accounts new easy2digitalAccount-account

Then, you need to add a private key for this new account. We can copy the MetaMask private key and paste it here.

Last but not least, Brownie would encrypt your private key using a password. Thus, just input a password as you like.

If you try to run using the load method to look up the new account you create just now, it will look like this result as follows:

Connect to a real test network

Brownie provides a list of networks declared. If you like to check, just input the command brownie networks list in the terminal.

In the previous article, we used Infura as a 3rd party platform to connect to Ethereum. So here we would use INFURA as well.

First thing first, we need an HTTP provider protocol from INFURA. In Brownie, we have a method to connect with Infura. Please add this to the .env

WEB3_INFURA_PROJECT_ID = your infura http endpoint unique ID.

Then, instead of getting the account from Ganache CLI, now we need to get the account from your MetaMask goerli test network.

Add the private key you wish to add, such as MetaMask wallet. Be sure to add the 0x before the MetaMask private key. Here we need to do three things

  • Create a get_account function which is to fetch the MetaMask wallet private key info
  • Add the brownie-config.yaml file in the main directory folder to connect with .env which includes the private key and INFURA endpoint unique ID
  • Replace account[0] with get_account() in the Ganache Cli testing script.

Lastly, we need to tell Infura which ETH test network the python script will use. As we used goerili in the previous article, here use the same one by inputting the command in the terminal

Brownie run scripts/deployment.py –network goerli

Once it’s connected, you can go to the deployment section under the build folder, there is a map JSON file generated.

Test and Debug your smart contracts

One of the powerful features of Brownie must be the testing environment. Smart contract developers can implement the UAT and fix the bugs before going live.

Let us create a script just as well as the deployment.py script we created in the test folder.

First thing first, Brownie is super convenient because it can allow developers directly import the smart contract into the python script.

Here is the code sample:

From brownie import easy2idigitalContract, accounts

So we can directly use the contract name with deploy() method to deploy the account on a blockchain. For example:

Def test_deployment():

##Arrange a specific smart contract address

walletAccount = accounts[0]

##Deploy the contract to the account

Single_contract = easy2idigitalContract.deploy({“from”: walletAccount})

Starting_value = single_contract.retrieve()

Secondly, as it is a test, we need to check if the value retrieved is equal to the expected value that it should be. So here is the sample of codes as follows:

Expected == 0

Assert starting_value == expected

Then, please input the command in the terminal to see if the value is equal. If the result is passed, it will show passed in the green color.

Brownie test

Brownie Console for any Adhoc function test and development

For the script testing, if you like to create a shell or console to test and run the script. You can try to input the code as follows, where you can directly update the script, and print it out to get the result immediately.

Brownie test –pdb

Full Python script of Brownie Web3.0 – Develop Smart Contracts and Activate Interaction with ETH Using Brownie and Python

If you are interested in Web3.0 tutorial 3 – Brownie in Web3.0 – Develop Ethereum Smart Contracts Interacting with Cryptocurrency Network Using Brownie and Python, please subscribe to our newsletter by adding the message “Web3.0 tutorial 3”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Web3.0 tutorial 3 – Brownie in Web3.0 – Develop Ethereum Smart Contracts Interacting with Cryptocurrency Network Using Brownie and Python If you did, please support us by doing one of the things listed below, because it always helps out our channel.

FAQ:

Q1: What is Brownie?

A: Brownie is a Python-based development framework and command-line tool that allows developers to write and deploy smart contracts on the Ethereum blockchain.

Q2: What are the benefits of using Brownie?

A: Brownie offers several benefits, including ease of use, a modular structure, built-in testing frameworks, and support for multiple networks.

Q3: How do I install Brownie?

A: To install Brownie, you can use the following command: pip install brownie.

Q4: How do I create a new Brownie project?

A: To create a new Brownie project, you can use the following command: brownie create myproject.

Q5: How do I write a smart contract in Brownie?

A: To write a smart contract in Brownie, you can create a new file in your project directory with a .sol extension and write your contract code in Solidity.

Q6: How do I compile a smart contract in Brownie?

A: To compile a smart contract in Brownie, you can use the following command: brownie compile.

Q7: How do I deploy a smart contract in Brownie?

A: To deploy a smart contract in Brownie, you can use the following command: brownie run scripts/deploy.py.

Q8: How do I test a smart contract in Brownie?

A: To test a smart contract in Brownie, you can use the following command: brownie test.

Q9: How do I interact with a smart contract in Brownie?

A: To interact with a smart contract in Brownie, you can use the following command: brownie call .

Q10: Where can I learn more about Brownie?

A: You can learn more about Brownie by visiting the official website: https://brownie.readthedocs.io/en/stable/.

3 thoughts on “Brownie in Web3.0 – Develop Ethereum Smart Contracts Interacting with Cryptocurrency Network Using Brownie and Python

  1. Simply want to say your article is as amazing. The clearness in your post is
    just spectacular and i can assume you’re an expert on this subject.
    Fine with your permission allow me to grab your feed to keep up to date
    with forthcoming post. Thanks a million and please carry on the gratifying work.

  2. I was curious if you ever considered changing the page layout
    of your site? Its very well written; I love what youve got to say.

    But maybe you could a little more in the way of content so people could
    connect with it better. Maybe you could space it out better?

Comments are closed.