Search code examples
node.jspostmanblockchainethereumweb3js

How can I retrieve the data from Ethereum Blockchain


I am new using blockchain and node.js. How can I retrieve the data from ethereum blockchain using node get method.

Is it possible to retrieve the original saved data?


Solution

  • According to the Web3 API documentation, the way to retrieve a contract instance and to call a method is:

    1. Contract Definition

    var MyContract = web3.eth.contract(abi);
    

    2. Get the instance of the contract at the address

    var myContractInstance = MyContract .at('0x**********');
    

    3. Execute a call

    var owner = myContractInstance .owner.call();
    

    Full code:

    var abi = [
        {
          "constant": true,
          "inputs": [],
          "name": "owner",
          "outputs": [
            {
              "name": "",
              "type": "address"
            }
          ],
          "payable": false,
          "type": "function"
        },
        {
          "inputs": [],
          "payable": false,
          "type": "constructor"
        }
      ];
    
    
    var MyContract = web3.eth.contract(abi);
    
    // initiate contract for an address
    var myContractInstance = MyContract .at('0xa07ddaff6d8b7aabf91ac6f82bf89455eb9784f4');
    
    // call constant function (synchronous way)
    var owner = myContractInstance .owner.call();
    
    console.log("owner="+owner);
    

    Works fine:

    owner=0x13a0674c16f6a5789bff26188c63422a764d9a39