馬蹄鏈佛薩奇/泰山眾籌阿凡達/眾籌互助/智慧合約流動性挖礦分紅系統開發技術案例

xiaofufu發表於2023-03-11

  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


  從部署的智慧合約中讀取資料


  在我們的合約中,有一個方法getGreeting()可以檢索我們在區塊鏈中新增的問候語。我們將使用web3.py呼叫此方法開啟您的Python IDLE編輯器並使用以下程式碼建立一個新檔案read.py。執行py read.py讀取問候語。


  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(" Key here>"))


  print(w3.isConnected())

  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()))需求及案例:MrsFu123


  print('Contract value:{}'.format(contract_instance.functions.getGreeting().call()))


  匯入的web3庫和所有其他必需的模組


  透過指向Ropsten Infura節點啟動web3提供程式


  透過指向abi和contract_Address建立合約例項(來自上一步)


  呼叫getGreeting()方法並在控制檯列印結果


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69956839/viewspace-2939179/,如需轉載,請註明出處,否則將追究法律責任。

相關文章