以太坊+ Python =布朗尼

敬礼,亲爱的加密爱好者!



今天,我们将讨论布朗尼-Truffle框架的类似物,该框架通常用于为Solidity开发智能合约,对其进行测试和部署(您可以在此处的一系列相关文章中了解到)。



那么,为什么我们需要另一个框架,它与Truffle的主要区别是什么?



  • 首先,他们使用不同的语言-虽然Truffle依赖JS,但并不是每个人都知道这种语言,也不是每个人都习惯使用它; 布朗尼使用Python 3。
  • 其次,由于集成了各种软件,因此核仁巧克力饼使开发更加方便:这是用于智能合约的ethpm软件包管理器,是用于部署本地测试链的ganache,并通过pytest和所有版本的solc编译器进行测试,甚至还绑定了MythX(用于自动搜索智能合约中的错误-换句话说,布朗尼为整个开发周期提供了工具。当然,Truffle还允许您使用所有这些工具,但是它们并未内置在框架中,因此必须另外安装。
  • 第三,布朗尼使您不仅可以在Solidity上使用智能合约,还可以在vyper上使用-vyper –一种用于开发智能合约的基于python的类型。


因此,如果您更喜欢使用Python并希望使自己更轻松地开发智能合约,那么布朗尼绝对值得一试。



布朗尼还能做什么?

如存储库本身所述,布朗尼是用于基于以太坊的平台的全周期智能合约开发框架,该框架支持:



  • 几种智能合约编程语言:Solidity和Vyper。
  • 建立合同。
  • 与合同的互动互动。
  • 使用pytest测试智能合约。
  • 与智能合约进行交互的脚本。
  • 使用智能合约模板。


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 .



综上所述,我们可以说布朗尼目前是一个相当成熟的企业级准备就绪的以太坊开发解决方案,并且能够满足其过程中产生的几乎所有需求。Pythonists不仅应该绝对应该尝试在其上进行下一个项目。




All Articles