用基于 Python 的开发框架 Brownie 部署以太坊智能合约
https://remix.ethereum.org/
),这是一个强大的Web IDE,可让您进行智能合约可视化。Remix很棒,我现在仍然使用它,但是在单个IDE之外可以实现很多其他功能。后来我开始学习Truffle(https://www.trufflesuite.com/
)和HardHat(https://hardhat.org/guides/mainnet-forking.html
),它们是用于部署智能合约的Node.js
框架。yearn.finance
之类的项目使用python来部署其所有生产代码。Yearn.finance
由一群非常有才华的金融科技工程师经营,他们转向了区块链,带着他们熟悉和喜爱的Python工具。yearn.finance
团队用来部署和维护智能合约的工具。您可以使用简单的命令启动项目,然后立即开始使用代码。create-eth-app
。要开始使用,和其他所有Python软件包的安装方式一样。pip install eth-brownie
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
可以忽略,您将在后面了解它们的用途。WEB3_INFURA_PROJECT_ID
,可以通过创建Infura
帐户来检索该WEB3_INFURA_PROJECT_ID
。这就是我们用来连接到测试网络的东西。我们还将获得一个metamask
或其他web3以太坊钱包,并用一些ETH进行注资。对于这个demo,我们要使用Kovan
测试网络。https://gitter.im/kovan-testnet/faucet
获取一些Kovan Ether。PRIVATE_KEY
环境变量。在此处(https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html
)阅读有关设置环境变量的信息。如果这仍然使您感到困惑,并且这只是一个测试钱包,请随意将代码中的PRIVATE_KEY
替换为您的私钥和WEB3_INFURA_PROJECT_ID
。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
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 .
web3.py
的工具让您的开发更轻松,但是如果机智点,则我们始终可以直接在没有框架的情况下使用合约。Web3.py
是一个原始程序包,我们可以使用它来更直接地处理合同。为此,我们只需要上面的Kovan infura项目ID。请记住,要与任何智能合约进行交互,您需要做两件事:智能合约ABI 智能合约地址
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)
https://github.com/eth-brownie/brownie
https://github.com/ethereum/web3.py
点击下方阅读原文加入社区会员
评论