FDF/DAPP互助公排系統開發技術詳解丨FDF/DAPP互助公排開發原始碼模式

xiaofufu發表於2023-02-26

 Smart contracts are only programs stored on the blockchain,which will run when the predetermined conditions are met.They are often used to automate the execution of the agreement so that all participants can immediately determine the results without any middleman and without wasting time.They can also automatically complete the workflow and trigger the next operation when the conditions are met.

智慧合約是部署在區塊鏈的程式碼,區塊鏈本身不能執行程式碼,程式碼的執行是在本地的EVM中,實際上,部署在區塊鏈上程式碼是能夠在本地產生原智慧合約程式碼的程式碼,可以理解區塊鏈為一個資料庫,而客戶端從資料庫中讀取了儲存的執行程式碼,並在本地執行後,將結果寫入到了區塊鏈這個資料庫中。

 

  在區塊鏈網路上部署智慧合約

  a)建立專案:

  mkdir pythonDapp

  cd pythonDapp

  truffle init

  成功初始化專案後,轉到您的資料夾並在/contracts目錄中建立greeter.sol檔案。在網路上部署合約之前,我們必須編譯它並構建工件。

  b)智慧合約的編譯:

  因此,對於編譯,我們將使用Truffle solc編譯器。在您的主目錄中,執行以下命令:

  truffle compile

  (or)

  truffle.cmd compile#(for windows only)

  上面的命令將在/contracts目錄中編譯你的合約,並在/build目錄中建立二進位制工件檔案greeter.json。

  c)部署合約:
  開啟您的Python IDLE編輯器,並在主目錄deploy.py中使用以下程式碼建立一個新檔案,然後在您的目錄中執行py deploy.py。

  import json

  from web3 importWeb3,HTTPProvider

  from web3.contract importConciseContract

  #web3.py instance

  w3=Web3(HTTPProvider(" key>"))

  print(w3.isConnected())

  key="<Private Key here with 0x prefix>"

  acct=w3.eth.account.privateKeyToAccount(key)

  #compile your smart contract with truffle first

  truffleFile=json.load(open('./build/contracts/greeter.json'))

  abi=truffleFile['abi']

  bytecode=truffleFile['bytecode']

  contract=w3.eth.contract(bytecode=bytecode,abi=abi)

  #building transaction

  construct_txn=contract.constructor().buildTransaction({

  'from':acct.address,

  'nonce':w3.eth.getTransactionCount(acct.address),

  'gas':1728712,

  'gasPrice':w3.toWei('21','gwei')})

  signed=acct.signTransaction(construct_txn)

  tx_hash=w3.eth.sendRawTransaction(signed.rawTransaction)

  print(tx_hash.hex())

  tx_receipt=w3.eth.waitForTransactionReceipt(tx_hash)

  print("Contract Deployed At:",tx_receipt['contractAddress'])


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

相關文章