我如何从以太坊区块链中检索数据

时间:2018-10-20 05:36:57

标签: node.js postman blockchain ethereum web3js

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

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

1 个答案:

答案 0 :(得分:0)

根据Web3 API文档,检索合同实例和调用方法的方法是:

1。合同定义

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

2。在该地址获取合同实例

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

3。执行通话

var owner = myContractInstance .owner.call();

完整代码:

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

工作正常:

owner=0x13a0674c16f6a5789bff26188c63422a764d9a39