馬蹄鏈佛薩奇/泰山眾籌阿凡達(dá)/眾籌互助/智能合約流動(dòng)性挖礦分紅系統(tǒng)開(kāi)發(fā)(開(kāi)發(fā)詳情)
Smart contract,in short,is a technology that digitizes the contract in our life and can be automatically executed by the program when a condition is met.For example,you made an agreement with me.We decided on reward and punishment measures,and then entered the agreement into the blockchain in the form of code.Once the agreed conditions are triggered,a program will automatically execute it,which is smart contract
從部署的智能合約中讀取數(shù)據(jù)
在我們的合約中,有一個(gè)方法getGreeting()可以檢索我們?cè)趨^(qū)塊鏈中添加的問(wèn)候語(yǔ)。我們將使用web3.py調(diào)用此方法打開(kāi)您的Python IDLE編輯器并使用以下代碼創(chuàng)建一個(gè)新文件read.py。運(yùn)行py read.py讀取問(wèn)候語(yǔ)。
import json
from web3 importWeb3,HTTPProvider
from web3.contract importConciseContract
#compile your smart contract with truffle first
truffleFile=json.load(open('./build/contracts/greeter.json'))
abi=truffleFile['abi']
bytecode=truffleFile['bytecode']
#web3.py instance
w3=Web3(HTTPProvider("https://ropsten.infura.io/<ApI Key here>"))
print(w3.isConnected())開(kāi)發(fā)功能及流程I59模式2OO7開(kāi)發(fā)3O69
contract_address=Web3.toChecksumAddress("<Deployed Contract Address here>")
#Instantiate and deploy contract
contract=w3.eth.contract(abi=abi,bytecode=bytecode)
#Contract instance
contract_instance=w3.eth.contract(abi=abi,address=contract_address)
#Contract instance in concise mode
#contract_instance=w3.eth.contract(abi=abi,address=contract_address,ContractFactoryClass=ConciseContract)
#Getters+Setters for web3.eth.contract object ConciseContract
#print(format(contract_instance.getGreeting()))需求及案例:yy625019
print('Contract value:{}'.format(contract_instance.functions.getGreeting().call()))
導(dǎo)入的web3庫(kù)和所有其他必需的模塊
通過(guò)指向Ropsten Infura節(jié)點(diǎn)啟動(dòng)web3提供程序
通過(guò)指向abi和contract_Address創(chuàng)建合約實(shí)例(來(lái)自上一步)
調(diào)用getGreeting()方法并在控制臺(tái)打印結(jié)果