FDF公排互助系统开发技术分析

89 阅读1分钟

什么是DApp

  “DApp”代表去中心化应用程序。与传统应用程序一样,去中心化应用程序也有前端(客户端)和后端(服务器端)。DApp的用户界面可以用任何语言编写(就像传统应用程序一样),并且可以调用其后端。那么,Dapps与传统应用程序有何不同?DApp的后端代码运行在分散的对等网络(即区块链)上。您可能听说过BitTorrent、Tor、Popcorn Time——它们是在点对点网络上运行但不在区块链上运行的DApp。

  Dapps开发包括三个简单的步骤:系统开发180.3831.9724  在区块链网络上部署智能合约

  从部署的智能合约中读取数据

  将交易发送到部署的智能合约

  智能合约

  Solidity是编写智能合约最常用的语言,它编译为可以在节点上运行的以太坊虚拟机上执行的字节码。

  pragma solidity^0.5.7;

  contract greeter{
string greeting;

  function greet(string memory _greeting)public{

  greeting=_greeting;

  }

  function getGreeting()public view returns(string memory){

  return greeting;

  }
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("ropsten.infura.io/v3/"))#modify

  print(w3.isConnected())

  contract_address=Web3.toChecksumAddress("")#modify

  key="<Private key with 0x prefix here>"#modify

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

  account_address=acct.address

  #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)

  tx=contract_instance.functions.greet("Hello all my goody people").buildTransaction({'nonce':w3.eth.getTransactionCount(account_address)})

  #Get tx receipt to get contract address

  signed_tx=w3.eth.account.signTransaction(tx,key)

  #tx_receipt=w3.eth.getTransactionReceipt(tx_hash)

  hash=w3.eth.sendRawTransaction(signed_tx.rawTransaction)

  print(hash.hex())​​​​