用基于 Python 的开发框架 Brownie 部署以太坊智能合约 - Go语言中文社区

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


在本文中,我们将使用Python部署智能合约。这篇文章可能是您走向智能合约和区块链开发的桥梁!

介绍

我希望可以在任何开发场景都尽量用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软件包的安装方式一样。

  1. pip install eth-brownie 

我们还需要安装ganache-cli一个用于部署本地区块链的软件包。为此,您需要安装npm和nodejs。

  1. npm install -g ganache-cli 

准备开始!我们将使用chainlink-mix入门,因为许多顶级defi项目都使用Chainlink来获取其资产数据。

  1. brownie bake chainlink-mix  
  2. cd chainlink 

通过ls命令将向我们展示项目的结构布局

Brownie项目布局

  1. build : This is where the project keeps track of your deployed smart contracts and compiled contracts  
  2. contracts : The source code of your contracts, typically written in solidity or vyper  
  3. interfaces : A layout of interfaces you’ll need to work with deployed contracts. Every interaction with a contract needs an ABI and an address. Interfaces are great ways to get a contract’s ABI 
  4. scripts : Scripts we create to automate processes of working with our contracts  
  5. tests : Tests  
  6. brownie-config.yaml : This is where we have all the information for brownie to understand how to work with our smart contract. What blockchain do we want to deploy to? Are there any special parameters we want to set? All these are set in the config file. 

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,则可以看到所有命令的列表。

  1. brownie run scripts/price_feed_scripts/deploy_price_consumer_v3.py --network kovan 

--network kovan允许我们设置要使用的网络。我们正在使用kovan testnet进行此演示。您将需要Kovan ETH来做到这一点!您将获得很多输出内容,但最终会得到类似以下结果:

  1. Running 'scripts/price_feed_scripts/deploy_price_consumer_v3.py::main'...  
  2. Transaction sent: 0x23d1dfa3937e0cfbab58f8d5ecabe2bfffc28bbe2349527dabe9289e747bac56  
  3. Gas price: 20.0 gwei   Gas limit: 145600   Nonce: 1339 
  4. PriceFeed.constructor confirmed - Block: 22721813   Gas used: 132364 (90.91%)  
  5. PriceFeed deployed at: 0x6B2305935DbC77662811ff817cF3Aa54fc585816 

如果此方法正常运行,我们可以转到kovan etherscan并找到我们部署的合约。上面的链接显示了此示例中部署的合约。

4.读取您的智能合约

现在我们已经部署了智能合约,我们可以从刚刚部署的合约中读取以太坊的价格。我们将运行另一个脚本:

  1. brownie run scripts/price_feed_scripts/read_price_feed.py --network kovan 

得到类似以下的输出:

  1. Brownie v1.12.2 - Python development framework for Ethereum  
  2. ChainlinkProject is the active project.  
  3. Running 'scripts/price_feed_scripts/read_price_feed.py::main'...  
  4. Reading data from 0x6B2305935DbC77662811ff817cF3Aa54fc585816  
  5. 62322000000  
  6. Where 62322000000 is the current price of ETH in USD! Solidity doesn’t understand decimals, and we know that this example has 8 decimals, so the price is $623.22 . 

您刚刚使用Python和Brownie部署了您的第一个智能合约!

使用web3.py

Brownie使用名为web3.py的工具让您的开发更轻松,但是如果机智点,则我们始终可以直接在没有框架的情况下使用合约。Web3.py是一个原始程序包,我们可以使用它来更直接地处理合同。为此,我们只需要上面的Kovan infura项目ID。请记住,要与任何智能合约进行交互,您需要做两件事:

智能合约ABI

智能合约地址

Brownie 会在后台处理很多此类工作,但我们也可以手动进行。这是通过web3.py从链上合同中读取的内容。首先,我们需要安装web3.py。

  1. pip install web3 

然后,我们可以在文件中运行以下内容。

  1. web3 = Web3(Web3.HTTPProvider('https://kovan.infura.io/v3/<infura_project_id>'))   
  2. 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"}]'  
  3. addr = '0x9326BFA02ADD2366b30bacB125260Af641031331'  
  4. contract = web3.eth.contract(addraddress=addr, abiabi=abi)   
  5. latestData = contract.functions.latestRoundData().call() print(latestData) 

运行上述操作后将在我们的控制台中打印以美元为单位的ETH的最新价格。请查看Chainlink文档以确定是否有问题。

结论

您可以从他们的文档中了解有关Web3.py和Brown的更多信息。这两个项目都是开源的,任何人都可以做出贡献!

https://github.com/eth-brownie/brownie

https://github.com/ethereum/web3.py

鸿蒙官方战略合作共建――HarmonyOS技术社区

【责任编辑:庞桂玉 TEL:(010)68476606】

点赞 0
版权声明:本文来源51CTO,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:http://developer.51cto.com/art/202105/664090.htm
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-28 21:48:44
  • 阅读 ( 1495 )
  • 分类:区块链

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢