什麼是Brownie?
使用Python部署您的第一個智慧合約
1.安裝 Brownie
pip install eth-brownie
npm install -g ganache-cli
browniebake chainlink-mix
cdchainlink
:
build :這是專案跟蹤已部署的智慧合約和已編譯合約的地方
contracts:合同的原始碼,通常以solidity或vyper
interfaces:處理已部署合同所需的介面布局。每次與合同的互動都需要一個ABI和一個地址。介面是獲取合同的ABI的好方法
scripts :我們建立的指令碼可自動執行合同工作
tests :測試
brownie-config.yaml:這是我們擁有布朗尼所有資訊的地方,以瞭解如何使用我們的智慧合約。我們想部署到什麼區塊鏈?我們是否需要設定任何特殊引數?所有這些都在配置檔案中設定。
2.設定環境變數
3.部署您的智慧合約
brownie run scripts/price_feed_scripts/deploy_price_consumer_v3.py --network kovan
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
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 from0x6B2305935DbC77662811ff817cF3Aa54fc585816
62322000000
5.使用web3.py
合同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)
作者:鏈三豐,來源:區塊鏈研究實驗室