广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用Python的开发框架Brownie部署以太坊智能合约
  • 467
分享到

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

python部署智能合约pythonBrowniepython以太坊合约 2022-06-02 22:06:46 467人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录介绍为什么选择python?Brownie是什么?用Python部署您的第一个智能合约1. 安装 Brownie 和 bake2.设置环境变量3.部署您的智能合约4.读取您的智能合约结论介绍 我希望可以在任何开

目录
  • 介绍
  • 为什么选择python
  • Brownie是什么?
  • Python部署您的第一个智能合约
    • 1. 安装 Brownie 和 bake
    • 2.设置环境变量
    • 3.部署您的智能合约
    • 4.读取您的智能合约
  • 结论

    介绍

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

    
    pip install eth-brownie

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

    
    npm install -g ganache-cli

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

    
    brownie bake chainlink-mix
    cd chainlink

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

    
    build : This is where the project keeps track of your deployed smart contracts and compiled contracts
    contracts : The source code of your contracts, typically written in solidity or vyper
    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
    scripts : Scripts we create to automate processes of working with our contracts
    tests : Tests
    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,则可以看到所有命令的列表。

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

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

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

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

    4.读取您的智能合约

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

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

    得到类似以下的输出:

    
    Brownie v1.12.2 - Python development framework for Ethereum
    ChainlinkProject is the active project.
    Running 'scripts/price_feed_scripts/read_price_feed.py::main'...
    Reading data from 0x6B2305935DbC77662811ff817cF3Aa54fc585816
    62322000000
    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。

    
    pip install web3

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

    
    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部署智能合约的资料请关注编程网其它相关文章!

    --结束END--

    本文标题: 使用Python的开发框架Brownie部署以太坊智能合约

    本文链接: https://www.lsjlt.com/news/10797.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作