主页 > 比特派钱包和imtoken哪个安全 > 使用Python的开发框架Brownie部署以太坊智能合约

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

介绍

我希望在任何开发场景下都能尽可能多地使用Python。 在区块链开发中,常用的是以太坊虚拟机智能合约语言Solidity,它有很多不错的特性以太坊部署合约,仍然可以使用Python部署。 当我第一次开始使用 Solidity 时,我使用了 Remix(),这是一个功能强大的 Web IDE,可让您可视化智能合约。 Remix 很棒,我仍在使用它,但它比单个 IDE 的功能要多得多。 后来我开始学习 Truffle() 和 HardHat(),它们是用于部署智能合约的 Node.js 框架。

这些是我目前看到的主要框架,都不错,但我更喜欢Python。 所以当我发现 Brownie 和 web3.py 时,我非常兴奋:一个用于部署智能合约的 Python 框架和一个用于区块链开发的开源协议。 我们将在本文中介绍 Brownie 和 Web3.py。

为什么选择 Python?

这么多数据科学家、学者和金融科技机构使用 Python 是有原因的。 用途广泛,开发体验简单,与各种第三方库紧密耦合。 顶级 DeFi 项目开始意识到这一点,像 yearn.finance 这样的项目使用 python 来部署他们所有的生产代码。 Yearn.finance 由一群才华横溢的金融科技工程师运营,他们已经转向区块链,并带来了他们熟悉和喜爱的 Python 工具。

什么是布朗尼?

以太坊智能合约安全性_以太坊部署合约_以太坊智能合约代码

Brownie 是 Ben Hauser 创建的 Python 智能合约开源框架,又名“iamdefinitelyahuman”(中文意思是“非绝对人”),堪称艺术品。 这是 yearn.finance 团队用来部署和维护智能合约的工具。 您可以使用一个简单的命令启动一个项目,并立即开始使用代码。

使用 Python 部署您的第一个智能合约

1.安装布朗尼并烘烤

Brownie 有一个“烘焙”功能,可以让你用一些基本代码启动一个存储库,因为大多数项目将需要许多相同的部分,类似于 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 命令将向我们展示项目的结构布局

布朗尼项目布局

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 以太坊钱包,并用一些 ETH 为其提供资金。 对于这个演示,我们将使用 Kovan 测试网络。

您可以跳过有关 LINK 资金的部分,我们只需要测试网 ETH。 我们也不会使用 Ropsten,而是 Kovan。 如果您已经有了钱包,请从这里获取一些 Kovan Ether。

安装、配置和 Metamask

拥有 Metamask 钱包后,您可以将私钥导出到 PRIVATE_KEY 环境变量。 在此处阅读有关设置环境变量的信息()。 如果这仍然让您感到困惑,并且这只是一个测试钱包,请随时将代码中的 PRIVATE_KEY 替换为您的私钥和 WEB3_INFURA_PROJECT_ID。

3. 部署你的智能合约

在我们的脚本文件夹中,我们有一个名为 deploy_price_consumer_v3.py 的脚本,它将部署我们的智能合约,该合约读取以太坊的美元价格。 如果您想更轻松地了解此合约的作用以及如何部署它,请随时查看有关部署价格订阅合约的 Chainlink 教程()。 brownie run 是我们可以用来运行脚本的命令。 如果您只运行 brownie,您可以看到所有命令的列表。

以太坊智能合约安全性_以太坊智能合约代码_以太坊部署合约

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

--network kovan 允许我们设置要使用的网络。 我们在此演示中使用 kovan 测试网。 你需要 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。 请记住,要与任何智能合约交互,您需要两件事:

以太坊智能合约安全性_以太坊部署合约_以太坊智能合约代码

Brownie 在后台处理了很多这样的事情,但我们也可以手动完成。 这是通过 web3.py 从链上合约读取的内容。 首先,我们需要安装 web3.py。

pip install web3

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

web3 = Web3(Web3.HTTPProvider('https://kovan.infura.io/v3/')) 
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 的更多信息。 这两个项目都是开源的,任何人都可以做出贡献!

以上就是使用Python开发框架Brownie部署以太坊智能合约的详细内容。 更多Python智能合约部署,请关注Scenario的其他相关文章!