当前位置: 首页 > 科技观察

使用基于Python的开发框架Brownie部署以太坊智能合约

时间:2023-03-14 20:36:47 科技观察

在本文中,我们将使用Python来部署智能合约。本文可能是您通向智能合约和区块链开发的桥梁!简介希望在任何开发场景中尽可能多地使用Python。在区块链开发中,常用的是以太坊虚拟机智能合约语言Solidity,它有很多不错的特性,仍然可以使用Python部署。当我第一次开始使用Solidity时,我使用了Remix(https://remix.ethereum.org/),这是一个功能强大的WebIDE,可让您可视化智能合约。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是BenHauser创建的Python智能合约开源框架,又名“iamdefinitelyahuman”(中文意思是“非绝对人”),堪称艺术品。这是yearn.finance团队用来部署和维护智能合约的工具。您可以使用一个简单的命令启动一个项目,并立即开始使用代码。使用Python1部署您的第一个智能合约。安装Brownie和bakeBrownie具有“烘焙”功能,允许您使用一些基本代码启动存储库,因为大多数项目将需要许多相同的部分,类似于create-eth-app。首先,像安装任何其他Python包一样安装它。pipinstalleth-brownie我们还需要安装ganache-cli一个用于部署本地区块链的包。为此,您需要安装npm和nodejs。npminstall-gganache-cli准备就绪!我们将使用chainlink-mix开始,因为许多顶级defi项目都使用Chainlink作为其资产数据。browniebakechainlink-mixcdchainlink通过ls命令将向我们展示项目的结构布局Brownie项目布局build:Thisiswheretheprojectkeepstrackofyourdeployedsmartcontractsandcompiledcontractscontracts:Thesourcecodeofyourcontracts,typicallywritteninsolidityorvyperinterfaces:Alayoutofinterfacesyou'llneedtoworkwithdeployedcontracts.EveryinteractionwithacontractneedsanABIandanaddress.Interfacesaregreatwaystogetacontract'sABIscripts:Scriptswecreatetoautomateprocessesofworkingwithourcontractstests:Testsbrownie-config.yaml:Thisiswherewehavealltheinformationforbrownietounderstandhowtoworkwithoursmartcontract.Whatblockchaindowewanttodeployto?Arethereanyspecialparameterswewanttoset?Allthesearesetintheconfigfile.requirements.txt、README.md、LICENSE和.gitignore可以忽略,稍后你会了解它们的用途。2.设置环境变量如果熟悉区块链开发,就会知道本地区块链、测试网区块链和主网区块链都是不同的东西。我们将部署到测试网,以便我们可以与真实的实时区块链网络进行交互。您将需要一个WEB3_INFURA_PROJECT_ID,可以通过创建Infura帐户来检索它。这就是我们用来连接到测试网络的。我们还将获得一个元掩码或其他web3以太坊钱包,并用一些ETH为其提供资金。对于这个演示,我们将使用Kovan测试网络。您可以跳过有关LINK资金的部分,我们只需要测试网ETH。我们也不会使用Ropsten,而是Kovan。如果您已经有了钱包,请从https://gitter.im/kovan-testnet/faucet获取一些KovanEther。安装、配置和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/)。brownierun是我们可以用来运行脚本的命令。如果您只运行brownie,您可以看到所有命令的列表。brownierunscripts/price_feed_scripts/deploy_price_consumer_v3.py--networkkovan--networkkovan允许我们设置要使用的网络。我们在此演示中使用kovan测试网。YouwillneedKovanETHtodothis!您将获得很多输出内容,但最终会得到类似以下结果:Running'scripts/price_feed_scripts/deploy_price_consumer_v3.py::main'...Transactionsent:0x23d1dfa3937e0cfbab58f8d5ecabe2bfffc28bbe2349527dabe9289e747bac56Gasprice:20.0gweiGaslimit:145600Nonce:1339PriceFeed.constructorconfirmed-Block:22721813Gasused:132364(90.91%)PriceFeeddeployedat:0x6B2305935DbC77662811ff817cF3Aa54fc585816Ifthismethodworks,wecangotokovanetherscanandfindourdeployedcontract.Thelinkaboveshowsthecontractdeployedinthisexample.4.ReadyoursmartcontractNowthatwehavedeployedoursmartcontract,wecanreadthepriceofEthereumfromthecontractwejustdeployed.我们将运行另一个脚本:brownierunscripts/price_feed_scripts/read_price_feed.py--networkkovan得到类似以下的输出:Browniev1.12.2-PythondevelopmentframeworkforEthereumChainlinkProjectistheactiveproject.Running'scripts/price_feed_scripts/read_price_feed.py::main'...Readingdatafrom0x6B2305935DbC77662811ff817cF3Aa54fc58581662322000000Where62322000000isthecurrentpriceofETHinUSD!Soliditydoesn'tunderstanddecimals,我们知道这个例子有8位小数,所以价格是623.22美元。您刚刚使用Python和Brownie部署了您的第一个智能合约!使用web3.pyBrownie使用名为web3.py的工具可以使您的开发更轻松,但如果我们聪明的话,我们总是可以在没有框架的情况下直接使用合同。web3.py是一个原始包,我们可以用它来更直接的处理合约。为此,我们只需要上面的Kovaninfura项目ID。请记住,要与任何智能合约交互,您需要两件事:智能合约ABI智能合约地址Brownie在后台处理了很多这样的事情,但我们也可以手动完成。这是通过web3.py从链上合约读取的内容。首先,我们需要安装web3.py。pipinstallweb3然后我们可以在文件中运行以下命令。web3=Web3(Web3.HTTPProvider('https://kovan.infura.io/v3/'))abi='[{"输入":[],"名称":"小数","输出":[{"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",“输出”:[{“内部类型”:“uint80”,“名称”:“roundId”,“类型”:“你int80"},{"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"},{“内部类型”:“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(addraddress=addr,abiabi=abi)latestData=contract.functions.latestRoundData().call()print(latestData)在运行上面的命令后将在我们的控制中操作请查看Chainlink文档,看是否有问题。结论您可以从他们的文档中了解有关Web3.py和Brown的更多信息。这两个项目都是开源的,任何人都可以做出贡献!https://github.com/eth-brownie/browniehttps://github.com/ethereum/web3.py