首页 > 解决方案 > 如何从以太坊区块链中检索数据

问题描述

我是使用区块链和 node.js 的新手。如何使用节点获取方法从以太坊区块链中检索数据。

是否可以检索原始保存的数据?

标签: node.jspostmanblockchainethereumweb3js

解决方案


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

推荐阅读