Marketplace has launched, further enabling blockchain developers! Learn more

How to deploy a smart contract with Brownie

September 23, 2022

Overview

Python is one of the most versatile programming languages; from researchers running their test models to developers using it in heavy production environments, it has use cases in every possible technical field. In today's guide, we will learn about Brownie, a Python-based tool used to write and deploy smart contracts.

Prerequisites
  • Python3 installed
  • Ethereum node
  • Text editor
  • Command-line
  • Love for brownies

What is Brownie?

Smart contract development is majorly dominated by JavaScript-based libraries like web3.js, ethers.js, Truffle, and Hardhat. Python is a versatile, highly used language and can also be used for smart contracts/web3 development; web3.py is a compelling Python library that fulfills web3 needs. Brownie framework is built on top of web3.py.

Brownies are small rectangular confectionary items loved by everyone, but the Brownie we are talking about today is a Python-based framework to develop and test smart contracts. Brownie has support for both Solidity and Vyper contracts, and it even provides contract testing via pytest.

To demonstrate the process of writing and deploying a smart contract with Brownie, we will use Brownie-mixes which are template projects. Specifically, we will use a token mix, which is a template of the ERC-20 implementation.

Installing dependencies

Brownie is built on top of python3, so we need it installed to work with brownie; let us check if we have python3 installed on our system. To do so, type the following in your terminal/cmd:

installing dependencies

Copy
python3 -V

This should return the version of python3 installed. If not installed, download and install it from the official python website.

Let us make a project directory before installing brownie, and make that project directory our current working directory:

installing dependencies

Copy
mkdir brownieDemo
cd brownieDemo

Now that you have installed python3 on your system let us install brownie using pip, Python's package manager. Pip is similar to what npm is for JavaScript. Type the following in your terminal/cmd:

installing dependencies

Copy
pip3 install eth-brownie
## If the install failS, use the following command for better luck.
sudo pip3 install eth-brownie

To check if Brownie was installed correctly, type brownie in your terminal/cmd, and it should give the following output:


To get the token mix, type the following in your terminal/cmd:

installing dependencies

Copy
brownie bake token

This will create a new directory token/ in our brownieDemo directory.

File Structure

First of all, lets cd into the token directory:

file structure

Copy
cd token

Now, open the token directory in your text editor. Under the contracts/ folder, you will find Token.sol, which is our main contract; you can write your own contracts or modify this. Under the scripts/ folder, you will find token.py python script; this script will be used to deploy the contract, and modifications are needed based on contracts.


The contract is an ERC-20 contract; you can learn more about the ERC-20 standards and contracts in this guide on ERC-20 tokens.

Booting our Ethereum node

We will deploy our contract on the Ropsten testnet instead of running our own node. It is more convenient to get a free trial endpoint from QuickNode. Make sure to select Ethereum as the chain and Ropsten as the network during checkout.

Screenshot of Quicknode Ropsten Endpoint

Save the HTTP URL. We will need it in the next step.

Network and Account Set Up.

We need to set up our QuickNode endpoint with Brownie. To do so, type the following in your terminal/cmd:

network and account set up

Copy
brownie networks add Ethereum ropstenquicknode host=YOUR_QUICKNODE_URL chainid=3

Replace YOUR_QUICKNODE_URL with the Ropsten URL we got in the last step.

In the above command, Ethereum is the name of the environment, and ropstenquicknode is the custom name of the network; you can give any name to your custom network.

The next thing we need to do here is to create a new wallet using Brownie. To do so, type the following in your terminal/cmd. You will be asked to set up a password for your account.

network and account set up

Copy
brownie accounts generate testac

This will generate an account along with a mnemonic phrase and save it offline. The name testac is the name for our account. You can choose any name that you would like.


Note: Mnemonic phrases can be used to recover an account or import the account to other non-custodial wallets. The account you see in the image above was just created for this guide. 

Copy the account address so that we can get some test ETH, which will be required to deploy our contract.

Getting test ETH

As our contract will be deployed on the Ropsten testnet, we will require some Ropsten test ETH to pay for the gas fee. Head over to the Ropsten faucet, paste your address in the field, and click on "Send me test Ether".

Deploying our contract

Before deploying the contract, we need to compile it using:

deploying our contract

Copy
brownie compile


Now open the scripts/token.py in your text editor, and make the following changes:

deploying our contract

Copy
#!/usr/bin/3

from brownie import Token, accounts

def main():
    acct = accounts.load('testac')
    return Token.deploy("Test Token", "TST", 18, 1e21, {'from': acct})

Line 6: We added this line to import the testac account we created earlier and stored it in the acct variable.

Line 7: On this line, we edited the 'From': part to have our acct variable.

FINALLY, we will deploy our contract using the deployment script (scripts/token.py here):

deploying our contract

Copy
brownie run token.py --network ropstenquicknode

In the above command, ropstenquicknode is the name of the custom network which we created earlier. The prompt will ask you for the password which we set earlier while making the account. After running the above command, you must get the transaction hash, and Brownie will wait for the transaction to get confirmed. Once the transaction is confirmed, it will return the address at which our contract is deployed on the Ropsten testnet.


You can check out the deployed contract by copy-pasting the contract address at Ropsten etherscan.

Conclusion

So, today we learned brownies are good, but Brownie the framework is the best. We learned how to import a Brownie-mix, add a custom network, create an account, and compile and deploy a contract, and we used Brownie for the entire process!

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

Related articles 18

Solidity vs Vyper
Originally Published On: Aug 18, 2021
Updated On: Sep 23, 2022

With the introduction to smart... When we think of blockchains,... Introduction IntroductionVyper is... Now, the million dollar question...

Continue reading
How to write an Ethereum smart contract using Solidity
Originally Published On: Apr 5, 2020
Updated On: Sep 23, 2022

This article is intended for... Before getting started with... The smart contract term was... As we saw that smart contracts... Now, let's write a simple smart... After writing a smart contract... So we have successfully created...

Continue reading
How to Setup Local Development Environment for Solidity
Originally Published On: Jul 10, 2021
Updated On: Sep 23, 2022

The success story of blockchain... According to the Now that you are geared up with... Installation Now that our contract is ready... Now, let's write some test... The last thing, we need to do is... For deploying using hardhat, you... Here we saw how to work with...

Continue reading
How to Deploy a Smart Contract on MATIC/Polygon
Originally Published On: Oct 27, 2021
Updated On: Sep 23, 2022

Ethereum is a very lovely... With the rapid adoption of... We'll deploy our contract on... After creating your QuickNode... We will need some test MATIC to... Time to write our smart... Now click on the Solidity logo... A big thank you and kudos if you...

Continue reading
How to Create a BEP20 Token
Originally Published On: Jul 3, 2021
Updated On: Sep 23, 2022

BEP20 is the Binance Smart Chain... The first thing you will want to... The Next thing we will need to... Because BSC uses the EVM, the... With the initial BEP20.sol... In order to view this in your... Congratulations! You made it to...

Continue reading
What is an ABI?
Originally Published On: Mar 20, 2021
Updated On: Sep 23, 2022

While interacting with a smart... ABI (Application Binary... The JSON format of a contract’s... One of the most common ways is... Now that you know what ABI is,...

Continue reading
How to call another smart contract from your solidity code
Originally Published On: Jan 30, 2021
Updated On: Sep 23, 2022

Smart contracts are the most... Smart contracts are computer... We’ll deploy our contracts on... Now to achieve our goal we'll... Now you will see two contracts... Now click on count button under... Now let's update the value of... Let's check if the INTERFACE... Here we successfully saw how we...

Continue reading
How to create and deploy an ERC-721 (NFT)
Originally Published On: Mar 6, 2021
Updated On: Sep 23, 2022

Digital collectibles compatible... Fungible means to be the same or... ERC stands for Ethereum Request... Digital art (or... Now that we know what ERC-721... Before writing our NFT contract,... For ease and security, we’ll use... Congratulations on creating your...

Continue reading
How to Write a Secure Smart Contract in Vyper
Originally Published On: Feb 2, 2022
Updated On: Sep 23, 2022

Hey readers! If you are familiar... In this project we will be... First things first, if we are... We can begin by writing our... Here we will write the code... Congrats on making it to the...

Continue reading
Como crear un Token BEP20
Originally Published On: Jan 10, 2022
Updated On: Sep 23, 2022

BEP20 es en Binance Smart Chain... La primera cosa que hay que... El próximo paso que deberás... Como BSC usa la EVM, el código... Con el contrato BEP20.sol... Para poder ver esto en la... ¡Felicidades! Acabas de terminar...

Continue reading
How to Create a Dutch Auction Smart Contract
Originally Published On: Feb 11, 2022
Updated On: Sep 23, 2022

Often to perform any transaction... Auctions are platforms for... A Dutch auction, also known as... We will deploy our contract on... Before moving further, it is... Now that our Dutch auction... If you made it here,...

Continue reading