使用Python的开发框架Brownie部署以太坊智能合约
介绍
我希望可以在任何开发场景都尽量用Python。在区块链开发中,常用的是以太坊虚拟机智能合约语言Solidity,它具有许多不错的功能,并且仍然可以使用 Python 进行部署。刚开始使用Solidity时,我使用了Remix(https://remix.ethereum.org/),这是一个强大的Web IDE,可让您进行智能合约可视化。Remix很棒,我现在仍然使用它,但是在单个IDE之外可以实现很多其他功能。后来我开始学习Truffle(https://www.trufflesuite.com/)和HardHat(https://hardhat.org/guides/mainnet-forking.html),它们是用于部署智能合约的Node.js框架。
这些是到目前为止我所见过的主要框架,这些框架都不错,但是我更喜欢Python。所以当我发现Brownie 和web3.py:一个用于部署智能合约的Python框架和一个用于区块链开发的开源协议之后非常兴奋。我们将在本文中同时介绍Brownie和Web3.py。
为什么选择Python?
有这么多数据科学家、学者和金融科技机构使用Python是有原因的。它用途广泛,具有轻松的开发体验,并且与各种第三方库紧密结合在一起。顶级 defi 项目开始意识到这一点,诸如yearn.finance之类的项目使用python来部署其所有生产代码。Yearn.finance由一群非常有才华的金融科技工程师经营,他们转向了区块链,带着他们熟悉和喜爱的Python工具。
Brownie是什么?
Brownie是由Ben Hauser创建的Python智能合约开源框架,又名“iamdefinitelyahuman”(中文意思“非绝对人类”),是一件艺术品。这就是yearn.finance团队用来部署和维护智能合约的工具。您可以使用简单的命令启动项目,然后立即开始使用代码。
用Python部署您的第一个智能合约
1. 安装 Brownie 和 bake
Brownie具有“baking”功能,可让您使用一些基础代码启动存储库,因为大多数项目都需要很多相同的部分,类似于create-eth-app。要开始使用,和其他所有Python软件包的安装方式一样。
pipinstalleth-brownie
我们还需要安装ganache-cli一个用于部署本地区块链的软件包。为此,您需要安装npm和nodejs。
npminstall-gganache-cli
准备开始!我们将使用chainlink-mix入门,因为许多顶级defi项目都使用Chainlink来获取其资产数据。
browniebakechainlink-mix cdchainlink
通过ls命令将向我们展示项目的结构布局
Brownie项目布局
build:Thisiswheretheprojectkeepstrackofyourdeployedsmartcontractsandcompiledcontracts contracts:Thesourcecodeofyourcontracts,typicallywritteninsolidityorvyper interfaces:Alayoutofinterfacesyou'llneedtoworkwithdeployedcontracts.EveryinteractionwithacontractneedsanABIandanaddress.Interfacesaregreatwaystogetacontract'sABI scripts:Scriptswecreatetoautomateprocessesofworkingwithourcontracts tests:Tests brownie-config.yaml:Thisiswherewehavealltheinformationforbrownietounderstandhowtoworkwithoursmartcontract.Whatblockchaindowewanttodeployto?Arethereanyspecialparameterswewanttoset?Allthesearesetintheconfigfile.
requirements.txt,README.md,LICENSE和.gitignore可以忽略,您将在后面了解它们的用途。
2.设置环境变量
如果您熟悉区块链开发,就会知道本地区块链,测试网区块链和主网区块链都是不同的东西。我们将部署到测试网,以便我们可以与真实的实时区块链网络进行交互。您需要一个WEB3_INFURA_PROJECT_ID,可以通过创建Infura帐户来检索该WEB3_INFURA_PROJECT_ID。这就是我们用来连接到测试网络的东西。我们还将获得一个metamask或其他web3以太坊钱包,并用一些ETH进行注资。对于这个demo,我们要使用Kovan测试网络。
您可以跳过有关LINK资金的部分,我们只需要testnet ETH。我们也不会使用Ropsten,而是使用Kovan。如果您已经有了钱包,请从https://gitter.im/kovan-testnet/faucet获取一些Kovan Ether。
安装,配置和Metamask
一旦有了Metamask钱包,就可以将私钥导出到PRIVATE_KEY环境变量。在此处(https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html)阅读有关设置环境变量的信息。如果这仍然使您感到困惑,并且这只是一个测试钱包,请随意将代码中的PRIVATE_KEY替换为您的私钥和WEB3_INFURA_PROJECT_ID。
3.部署您的智能合约
在我们的脚本文件夹中,我们有一个名为deploy_price_consumer_v3.py的脚本,该脚本将部署我们的智能合约,该合约读取以太坊的美元价格。如果您想更轻松地了解该合约的功能以及如何部署它,请随时查看有关部署价格订阅合同的Chainlink教程(https://docs.chain.link/docs/beginners-tutorial/)。brownie run是我们可以用来运行脚本的命令。如果仅运行brownie,则可以看到所有命令的列表。
brownierunscripts/price_feed_scripts/deploy_price_consumer_v3.py--networkkovan
--network kovan允许我们设置要使用的网络。我们正在使用kovan testnet进行此演示。您将需要Kovan ETH来做到这一点!您将获得很多输出内容,但最终会得到类似以下结果:
Running'scripts/price_feed_scripts/deploy_price_consumer_v3.py::main'... Transactionsent:0x23d1dfa3937e0cfbab58f8d5ecabe2bfffc28bbe2349527dabe9289e747bac56 Gasprice:20.0gweiGaslimit:145600Nonce:1339 PriceFeed.constructorconfirmed-Block:22721813Gasused:132364(90.91%) PriceFeeddeployedat:0x6B2305935DbC77662811ff817cF3Aa54fc585816
如果此方法正常运行,我们可以转到kovan etherscan并找到我们部署的合约。上面的链接显示了此示例中部署的合约。
4.读取您的智能合约
现在我们已经部署了智能合约,我们可以从刚刚部署的合约中读取以太坊的价格。我们将运行另一个脚本:
brownierunscripts/price_feed_scripts/read_price_feed.py--networkkovan
得到类似以下的输出:
Browniev1.12.2-PythondevelopmentframeworkforEthereum ChainlinkProjectistheactiveproject. Running'scripts/price_feed_scripts/read_price_feed.py::main'... Readingdatafrom0x6B2305935DbC77662811ff817cF3Aa54fc585816 62322000000 Where62322000000isthecurrentpriceofETHinUSD!Soliditydoesn'tunderstanddecimals,andweknowthatthisexamplehas8decimals,sothepriceis$623.22.
您刚刚使用Python和Brownie部署了您的第一个智能合约!
使用web3.py
Brownie使用名为web3.py的工具让您的开发更轻松,但是如果机智点,则我们始终可以直接在没有框架的情况下使用合约。Web3.py是一个原始程序包,我们可以使用它来更直接地处理合同。为此,我们只需要上面的Kovan infura项目ID。请记住,要与任何智能合约进行交互,您需要做两件事:
- 智能合约ABI
- 智能合约地址
Brownie 会在后台处理很多此类工作,但我们也可以手动进行。这是通过web3.py从链上合同中读取的内容。首先,我们需要安装web3.py。
pipinstallweb3
然后,我们可以在文件中运行以下内容。
web3=Web3(Web3.HTTPProvider('https://kovan.infura.io/v3/<infura_project_id>')) abi='[{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"description","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint80","name":"_roundId","type":"uint80"}],"name":"getRoundData","outputs":[{"internalType":"uint80","name":"roundId","type":"uint80"},{"internalType":"int256","name":"answer","type":"int256"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"},{"internalType":"uint80","name":"answeredInRound","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"latestRoundData","outputs":[{"internalType":"uint80","name":"roundId","type":"uint80"},{"internalType":"int256","name":"answer","type":"int256"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"},{"internalType":"uint80","name":"answeredInRound","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]' addr='0x9326BFA02ADD2366b30bacB125260Af641031331' contract=web3.eth.contract(address=addr,abi=abi) latestData=contract.functions.latestRoundData().call()print(latestData)
运行上述操作后将在我们的控制台中打印以美元为单位的ETH的最新价格。请查看Chainlink文档以确定是否有问题。
结论
您可以从他们的文档中了解有关Web3.py和Brown的更多信息。这两个项目都是开源的,任何人都可以做出贡献!
https://github.com/eth-brownie/brownie
https://github.com/ethereum/web3.py
以上就是使用Python的开发框架Brownie部署以太坊智能合约的详细内容,更多关于Python部署智能合约的资料请关注本站其它相关文章!
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。