用基于 Python 的開發(fā)框架 Brownie 部署以太坊智能合約

https://remix.ethereum.org/),這是一個(gè)強(qiáng)大的Web IDE,可讓您進(jìn)行智能合約可視化。Remix很棒,我現(xiàn)在仍然使用它,但是在單個(gè)IDE之外可以實(shí)現(xiàn)很多其他功能。后來我開始學(xué)習(xí)Truffle(https://www.trufflesuite.com/)和HardHat(https://hardhat.org/guides/mainnet-forking.html),它們是用于部署智能合約的Node.js框架。yearn.finance之類的項(xiàng)目使用python來部署其所有生產(chǎn)代碼。Yearn.finance由一群非常有才華的金融科技工程師經(jīng)營,他們轉(zhuǎn)向了區(qū)塊鏈,帶著他們熟悉和喜愛的Python工具。
yearn.finance團(tuán)隊(duì)用來部署和維護(hù)智能合約的工具。您可以使用簡單的命令啟動項(xiàng)目,然后立即開始使用代碼。create-eth-app。要開始使用,和其他所有Python軟件包的安裝方式一樣。pip install eth-brownie
npm install -g ganache-cli
chainlink-mix入門,因?yàn)樵S多頂級defi項(xiàng)目都使用Chainlink來獲取其資產(chǎn)數(shù)據(jù)。brownie bake chainlink-mix
cd chainlink
ls命令將向我們展示項(xiàng)目的結(jié)構(gòu)布局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,可以通過創(chuàng)建Infura帳戶來檢索該WEB3_INFURA_PROJECT_ID。這就是我們用來連接到測試網(wǎng)絡(luò)的東西。我們還將獲得一個(gè)metamask或其他web3以太坊錢包,并用一些ETH進(jìn)行注資。對于這個(gè)demo,我們要使用Kovan測試網(wǎng)絡(luò)。https://gitter.im/kovan-testnet/faucet獲取一些Kovan Ether。
PRIVATE_KEY環(huán)境變量。在此處(https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html)閱讀有關(guān)設(shè)置環(huán)境變量的信息。如果這仍然使您感到困惑,并且這只是一個(gè)測試錢包,請隨意將代碼中的PRIVATE_KEY替換為您的私鑰和WEB3_INFURA_PROJECT_ID。deploy_price_consumer_v3.py的腳本,該腳本將部署我們的智能合約,該合約讀取以太坊的美元價(jià)格。如果您想更輕松地了解該合約的功能以及如何部署它,請隨時(shí)查看有關(guān)部署價(jià)格訂閱合同的Chainlink教程(https://docs.chain.link/docs/beginners-tutorial/)。brownie run是我們可以用來運(yùn)行腳本的命令。如果僅運(yùn)行brownie,則可以看到所有命令的列表。brownie run scripts/price_feed_scripts/deploy_price_consumer_v3.py --network kovan
--network kovan允許我們設(shè)置要使用的網(wǎng)絡(luò)。我們正在使用kovan testnet進(jìn)行此演示。您將需要Kovan ETH來做到這一點(diǎn)!您將獲得很多輸出內(nèi)容,但最終會得到類似以下結(jié)果: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的工具讓您的開發(fā)更輕松,但是如果機(jī)智點(diǎn),則我們始終可以直接在沒有框架的情況下使用合約。Web3.py是一個(gè)原始程序包,我們可以使用它來更直接地處理合同。為此,我們只需要上面的Kovan infura項(xiàng)目ID。請記住,要與任何智能合約進(jìn)行交互,您需要做兩件事:智能合約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/browniehttps://github.com/ethereum/web3.py
點(diǎn)擊下方閱讀原文加入社區(qū)會員
評論
圖片
表情
