Ethereum + Python = Brownie

Salute, dear crypto enthusiast!



Today we will talk about Brownie - an analogue of the Truffle framework , which is often used to develop smart contracts for Solidity, test and deploy them (which you can read about in a series of related articles here ).



So why do we need another framework, and what is its key difference from Truffle?



  • First, they use different languages ​​- while Truffle relies on JS, not everyone knows this language and not everyone is comfortable using it; brownie uses Python 3.
  • Secondly, due to the integration of various software, brownie makes development more convenient: here is the ethpm package manager for smart contracts, and ganache for deploying a local test chain, and tests through pytest, and all versions of solc compilers, and even bindings to MythX - a tool for automatic search for errors in smart contracts - in other words, brownie offers tools for the entire development cycle. Of course Truffle also allows you to use all these tools, but they are not built into the framework and have to be installed additionally.
  • Thirdly, brownie allows you to work not only with smart contracts on Solidity, but also on vyper - a typed python-based one for developing smart contracts.


Thus, if you prefer to work with Python and want to make it easier for yourself to develop smart contracts, then brownie is definitely worth a try.



What else can brownie do?

As stated in the repository itself, brownie is a full cycle smart contract development framework for Ethereum-based platforms that supports:



  • Several smart contract programming languages: Solidity and Vyper.
  • Build contracts.
  • Interactive interaction with contracts.
  • Testing smart contracts with pytest.
  • Scripts for interacting with smart contracts.
  • Working with smart contract templates.


brownie , brownie . pip:



pip install eth-brownie



brownie .



help brownie
brownie --help
Brownie v1.6.9 - Python development framework for Ethereum

Usage:  brownie <command> [<args>...] [options <args>]

Commands:
  init               Initialize a new brownie project
  bake               Initialize from a brownie-mix template
  ethpm              Commands related to the ethPM package manager
  compile            Compiles the contract source files
  console            Load the console
  test               Run test cases in the tests/ folder
  run                Run a script in the scripts/ folder
  accounts           Manage local accounts
  gui                Load the GUI to view opcodes and test coverage
  analyze            Find security vulnerabilities using the MythX API

Options:
  --help -h          Display this message

Type 'brownie <command> --help' for specific options and more information about
each command.


brownie



brownie brownie-config.yaml. brownie init



init
brownie init salut_habr
Brownie v1.6.9 - Python development framework for Ethereum

SUCCESS: Brownie environment has been initiated at salut_habr


brownie bake template_name



brownie bake token
Brownie v1.6.9 - Python development framework for Ethereum

Downloading from https://github.com/brownie-mix/token-mix/archive/master.zip...
5.62kiB [00:00, 2.82MiB/s]
SUCCESS: Brownie mix 'token' has been initiated at token


ERC-20 ( token).

:



├───build #   ,      .
│   ├───contracts #    ,  ABI  .
│   └───deployments #         .
├───contracts #   (  ).
├───interfaces #   .
├───reports #  .
├───scripts # Python           run.
└───tests #    pytest   .


, brownie , brownie-config.yaml — , .



brownie



brownie , , : compile, console, test run.



brownie compile



, contracts . , , "_",- brownie ( ).



./build/contracts/ json-, ABI , - -.



brownie , , . , -all.



brownie compile --all
Brownie v1.6.9 - Python development framework for Ethereum

Compiling contracts...
  Solc version: 0.5.17+commit.d19bba13.Windows.msvc
  Optimizer: Enabled  Runs: 200
  EVM Version: Istanbul
Generating build data...
 - Token...
 - SafeMath...

Brownie project has been compiled at C:\Users\Default\Documents\token\build\contracts


, , brownie-config.yaml



brownie test



pytest, , , CI/CD.



brownie test
Brownie v1.6.9 - Python development framework for Ethereum

==================================================================================================== test session starts =====================================================================================================
platform win32 -- Python 3.8.3, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Default\Documents\token
plugins: eth-brownie-1.6.9, hypothesis-5.5.4, forked-1.1.3, xdist-1.31.0, web3-5.5.1
collecting ... Launching 'ganache-cli.cmd --port 8545 --gasLimit 6721975 --accounts 10 --hardfork istanbul --mnemonic brownie'...
collected 7 items

tests\test_approve_transferFrom.py ......                                                                                                                                                                               [ 85%]
tests\test_transfer.py .                                                                                                                                                                                                [100%]

===================================================================================================== 7 passed in 9.35s ====================================================================================================== 
Terminating local RPC client...


tests/



brownie run



scripts. brownie 2.x, CI/CD ( ).



brownie run
brownie run token
Brownie v1.6.9 - Python development framework for Ethereum

TokenProject is the active project.
Launching 'ganache-cli.cmd --port 8545 --gasLimit 6721975 --accounts 10 --hardfork istanbul --mnemonic brownie'...

Running 'scripts.token.main'...
Transaction sent: 0xe36fbf7d93c1c91bde5e9290128999ed06ea54eb68352fb477fa91ce8072f472
  Gas price: 0.0 gwei   Gas limit: 549953
  Token.constructor confirmed - Block: 1   Gas used: 549953 (100.00%)
  Token deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

Terminating local RPC client...


brownie console



brownie: , . :



brownie console
Brownie v1.6.9 - Python development framework for Ethereum

TokenProject is the active project.
Launching 'ganache-cli.cmd --port 8545 --gasLimit 6721975 --accounts 10 --hardfork istanbul --mnemonic brownie'...
Brownie environment is ready.
>>> dir()
[Fixed, Gui, SafeMath, Token, Wei, a, accounts, alert, compile_source, config, dir, history, network, project, rpc, run, web3]


brownie, , web3py.



/



brownie ganache ( Ethereum) , ( Quorum!). --network network_name console run, network_name brownie-config.yaml. ETH ETC, . , yaml . develop, test master ( ), Azure .



Summing up, we can say that brownie is currently already a fairly mature Enterpise-ready solution for development for Ethereum and is able to satisfy almost all needs arising in its process. Pythonists and not only should definitely try to make their next project on it.




All Articles