Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine (EVM).
Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine (EVM). So, when we are thinking about dApp development in Python, our choice of smart contract programming language will be vyper, which is similar to solidity in many aspects. Brownie fully supports both vyper and solidity.
Similarly, we can use Brownie as a testing framework, it provides developers with the ability to use and write pytest test cases for smart contracts.
Brownie takes care of managing contract artifacts, allowing us to write simple, manageable contracts and deployment scripts for custom deployments, library linking, and complex Ethereum applications.
Getting Started
Install Brownie - High-level framework for smart contracts development.
I will be using Ubuntu 22.04 - Jammy Jellyfish and make sure Python3 is installed.
The recommended way of installing brownie is through pipx.
To install pipx on Ubuntu:
# sudo apt install pipx
Next, Install Ganache (it's a framework to run a local blockchain - similar to Remix but in your local environment)
We will need NPM to install the ganache.
# npm install ganache –global
Once installed, if you launch ganache. You will be able to see a local blockchain network running & listening on RPC 127.0.0.1:8545, you will also find the chain id, available accounts, private keys, mnemonic phase, gas fee etc…now, we have a full up-and-running local blockchain network.
Install Brownie
# pipx install eth-brownie
# brownie --version
When we Initiate the Brownie environment, Brownie scaffold will create various folders in the path, such as build, contracts, interfaces, scripts, tests, etc.
Compiling a Contract
Open the contract folder and create a new file called StoreValue.sol, this is going to be our first smart contract – which is storing a value in the blockchain.
// SPDX-License-Identifier: MIT
/*
This contract stores a value in the blockchain.
*/
pragma solidity ^0.8.0;
contract StoreValue {
uint256 myValue;
function store(uint256 _myValue) public {
myValue = _myValue;
}
function retrieve() public view returns (uint256) {
return myValue;
}
}
Once we have the contract file, we need to compile the contract.
# brownie compile
Now, let's set up our brownie environment.
Previously when we installed ganache, we have the private keys displayed for our local environment. We need the private key to create the brownie account.
# brownie accounts new my-goerli-account
Deploying the Smart Contract locally
Open the scripts folder and create a new file called deploy.py
from brownie import accounts, StoreValue, network
def deploy_store_value():
account = get_account()
storeValue = StoreValue.deploy({"from": account})
value = storeValue.retrieve()
print(value)
transaction = storeValue.store(15, {"from": account})
transaction.wait(1)
value = storeValue.retrieve()
print(value)
def get_account():
if network.show_active() == "development":
return accounts[0]
else:
return accounts.load("my-goerli-account")
def main():
deploy_store_value()
Execute the code in your local environment (we are using Ganache behind the scenes)
# brownie run scripts/deploy.py
Smart Contract Testing:
Open the test folder and create a new file called test_store_value.py
We are testing two test cases here, the first being a test case for the actual smart contract deployment and the second being the actual value stored in the blockchain.
from brownie import StoreValue, accounts
def test_deploy():
account = accounts[0]
simple_storage = StoreValue.deploy({"from": account})
value = simple_storage.retrieve()
expected = 0
assert value == expected
def test_store():
account = accounts[0]
store_value = StoreValue.deploy({"from": account})
expected = 99
txn = store_value.store(expected, {"from": account})
txn.wait(1)
assert expected == store_value.retrieve()
Execute the testing script
# brownie test -s
Comments