How to get the data of an ethereum block using web3

Viewed 1368

This is my contract:

pragma solidity ^0.4.18;
contract Signup {
   string fStudentId;
   string fLocation;

   function setInfo(string _fStudentId, string _fLocation) public  {
       fStudentId = _fStudentId;
       fLocation = _fLocation;
   }
   function getInfo() public constant returns (string, string)    {
       return (fStudentId, fLocation);
   }
}

I put studentId & location in to block, and how do I get this info by using web3? I try to use: web3.eth.getBlock(7).then(console.log);

but get: enter image description here

Where is my studentId & location? thanks!!!!!

1 Answers

Not sure what you mean when you say "I put studentId & location in to block" but I will try to answer.

You are probably setting state by doing something like

contract.functions.setInfo(1, "here")

To retrieve that data you will do

contract.functions.getInfo()


The actual state of the ethereum blockchain is composed into a merkle trie and referenced by stateRoot. So as you can see, there is not an easy way to find this data based on the block header you have.

To retrieve the data from your contract you have a few options:

  1. Use your getInfo method as I described above
  2. Look up the transaction where you execute setInfo and parse the inputs from the transaction receipt
  3. Emit an event from your contract whenever setInfo is called and set up a listener for a filter that will be called any time a transaction that emits your contract's event occurs
  4. Run a node and get the raw data from the block instead of just the block header
Related