blockchain - 如何从以太坊区 block 链中检索数据?

标签 blockchain ethereum

我做了一个小型事件注册智能合约(通过使用 Solidity()),因为我通过提供我的电子邮件 ID 的详细信息和我想要的门票数量购买了一张事件门票。最后,我想知道如何取回我收到的详细信息。我正在使用 testrpc、truffle 和专用网络。 点击购买后,我在 tetrpc 终端中获得了这些详细信息

Transaction: 0x35e92857102b0dbacd43234d1ea57790405eb9bef956b245c6b7737bc23d011b
Gas usage: 106532 Block Number: 5 Block Time: Sat Feb 03 2018 12:05:57 GMT+0530 (IST)

我对交易 ID 进行解码,如下所示:

gopi145@ubuntu:~/EventRegistration-POC/EventRegistration$ truffle console
truffle(development)> web3.eth.getTransaction('0x35e92857102b0dbacd43234d1ea57790405eb9bef956b245c6b7737bc23d011b')
{ hash: '0x35e92857102b0dbacd43234d1ea57790405eb9bef956b245c6b7737bc23d011b',
  nonce: 4,
  blockHash: '0x7c790dae57babfe40d68d8aad94913c2b748501c5734aec86cc3fcf0afc4f154',
  blockNumber: 5,
  transactionIndex: 0,
  from: '0x031e060414a0d2573f5b10bc75c0894d72288292',
  to: '0xa88a366e888bbccfb78092957ffc7760bc7c6db1',
  value: BigNumber { s: 1, e: 18, c: [ 60000 ] },
  gas: 200000,
  gasPrice: BigNumber { s: 1, e: 0, c: [ 1 ] },
  input: '0xa28f161c00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000b6d40676d61696c2e636f6d000000000000000000000000000000000000000000' }
truffle(development)> 

但我没有得到购买时提供的详细信息。请告诉我该怎么做?

最佳答案

web3.eth.getTransaction(txHash) 将返回交易详细信息,例如 blockHash、transactionIndex、from、to 等。

挖掘区 block 并添加到区 block 链需要一些时间。所以它不是即时的。

如果您想知道区 block 链中向外界添加/修改的任何存储值,那么我们需要在智能合约中引发事件。

使用Web3JS(JSON-RPC/WS-RPC),您需要注册并监听事件。当您的交易被开采时,您将收到一个事件。

事件将存储为日志,与存储相比,它非常便宜。没有人能够修改事件数据。

下面的代码相同。

可靠性代码:

pragma solidity ^0.4.17;
//Contract for storing ticket info
contract TicketRes {

  event on_success_booking(address userId, string bookingId, string emailId);
  //Ticket info having two storage values i.e email and userID
  struct BookingInfo{
     string emailId;
     address userId;
  }
  //Map for saving all the info, assuming all ticket has unique id as key. Value is ticket info
  mapping(bookingId=>BookingInfo) internal info;
  function Book() public {
  }
   //Method will save all basic info, and will raise event.
  function onBookingCompleted(address id, string bookingId, string emailId) public {
       info[bookingId] = BookingInfo(emailId,userId);

       on_success_booking(id, bookingId, emailId);
  }
 //You can get info by using bookingid at any point of time. 
 function getBookingInfo(string bookingId) public constant returns(string, address){
       return (info[bookingId].emailId, info[bookingId].userId);
 }

}

现在 Javascript 代码:

// Contract deployed address.
var contractAddress = "0x06433f4fc50423f71329597f50fb0a42cfecb11f"; 

if (typeof web3 !== 'undefined') {
     web3 = new Web3(web3.currentProvider);
} else {
     // set the provider you want from Web3.providers
     web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
}

//Open build folder and you will find contact json file copy the appropriate JSON and paste it there.
var contractABI = web3.eth.contract(/** ABI Here **/);

//Connected contract to your local network
var contract = contractABI.at(contractAddress);

//Loading booking event function.
var booking_event = web3.sha3('on_success_booking(address,string,string)');

//Watching events, when onBookingCompleted() tran's mined then event get triggered. You can get all previous events also. for that need to apply filters.
booking_event.watch((error, result) => {
   if(error){
       console.log("error",error);
   }
   console.log("Result", result); //result.args holds values, result.args.id, result.args.bookingId and result.args.emailid
 });

关于blockchain - 如何从以太坊区 block 链中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594840/

相关文章:

blockchain - Hyperledger fabcar 示例结构显示连接失败错误

docker - 区 block 链 : Hyper Ledger Fabric Installations in Windows 10

bitcoin - 区 block 链是去中心化数据库吗?

ethereum - 如何从 Metamask 获取帐户地址?

algorithm - 根据需要转移值(value)的算法[保留]

cryptography - Solidity:插入符号 ^ 运算符有什么作用?

blockchain - Composer playground 无法加载示例业务网络

go - bufio.NewReader ReadBytes-同时读取多个消息

performance - 我们可以在 Hyperledger Fabric 中的单个事务中发送多大的有效负载。我们可以在一次交易中投入多少 Assets ?

go - 从 go-ethereum 实现 Ethereum personal_sign (EIP-191) 给出了与 ethers.js 不同的签名