Brownie: Ethereum's smart contract infrastructure
Editor's Note: This article comes fromQuantify Kicking Horse River (ID: gh_e9da2a6cbc62), reprinted by Odaily with authorization.
Editor's Note: This article comes from
Quantify Kicking Horse River (ID: gh_e9da2a6cbc62)
Quantify Kicking Horse River (ID: gh_e9da2a6cbc62)
, reprinted by Odaily with authorization.
When it comes to brownies, don’t think that they are delicious little cakes with a dark chocolate flavor. The brownies mentioned here refer to the basic framework of smart contracts called brownies on Ethereum. Brownie is used to develop Ethereum smart contracts. Its robustness and ease of use are very high, and it is often used for:
Deployment: Automatically deploy many contracts to the blockchain, initializing or integrating any transactions required for them.
Debugging: Get detailed information when transactions are recovering to help you pinpoint problems quickly.
set up
Testing: Write unit tests in Python and evaluate test coverage based on stack trace analysis.
Next, let's try to create a very simple smart contract in Solidity, which only exposes two external functions getValue and setValue. Save this smart contract in the file smart_contract.sol.
Next deploy and interact with this smart contract via Brownie.
set up
This attempt uses Python3.7 and virtualenv to isolate our environment. If Python3.7 is not installed, you can follow the steps below.
We will be using Ganache, which is a personal blockchain for Ethereum development. You can also install ethereum-testrpc instead, but that will make it impossible to run the GUI. When installing Ganache, follow the steps.
Create virtual environments for your Solidity projects. This has been created and named TestBrownie. Install Brownie.
To initialize an empty project, start by creating a new folder. In that folder, type:
project structure
Each brownie project contains the following folders:
contracts/: source of contracts
interfaces/: interface source
scripts/: scripts for deployment and interaction
tests/: scripts for testing the project
brownie-config.yaml: project configuration file
These folders below are also created by Brownie and are used internally for project management and cannot be edited or deleted.
build/: project data, such as compiler artifacts and unit test results
reports/: JSON report files used in the GUI
compile
smart_contract.sol copies your smart contract in the contracts directory. Compile all contract sources in the contracts/project subfolder:
Every time the compiler runs, Brownie compares the hash of each contract source to the hash of the existing compiled version. If the contract has not changed, it will not be recompiled. Use brownie compile --all if you wish to force the entire project to be recompiled.
If you want to compile a Solidity contract with another version, you just need to mention the contract in the usage instructions of the .sol file. If not present, it will be installed automatically. It should be noted here: the settings for the encoder are located in brownie-config.yaml.
Byzantium: Solidity <=0.5.4
Petersburg: Solidity >=0.5.5< =0.5.12
Istanbul: Solidity >=0.5.13, Vyper
Modifying any compiler settings will result in a complete recompilation of the project.
If a compiler version is set in the configuration file, that version will be used to compile all contracts in the project. Version should be given as a string in the format 0.xx
Regarding evm_version, Brownie sets the ruleset according to the compiler.
deploy
You can also manually set the EVM version. Valid options are byzantium, constantinople, petersburg, and istanbul. The Ethereum Classic rulesets atlantis and agharta are also available, which are converted to the ethereum equivalent before being passed to the compiler.
Note: Brownie supports Solidity version >= 0.4.22 and Vyper version 0.1.0-b16.
After a successful compilation, Brownie will create a SimpleContract.json file in the builds/contract folder.
deploy
Open brownie-config.yaml, it has a network section, you can customize an existing network, or create a new block under network. The Ganache GUI running on port 7545 is used here.
Create a private block under the network.
Save this file. Now deploy the compiled smart contract using the brownie console. It is very similar to the regular Python interpreter. From the project folder, load it by typing:
Cross-check the accounts in the output with the accounts visible on the Ganache GUI.
Each individual account is represented by an Account object on which actions can be performed, such as querying balances or sending ETH.
Every deployable contract and library has a ContractContainer for deploying new contracts and accessing existing ones. All compiled contracts are available as variables of the same name.
Deploy contract 0 with account:
If you type SimpleContract, you can see SimpleContract as a list of deployed instances of ContractContainer objects.
interaction
See the methods available on this contract:
Start by setting variables in the smart contract.


