node.js - 如何从以太坊区块链中检索数据
问题描述
我是使用区块链和 node.js 的新手。如何使用节点获取方法从以太坊区块链中检索数据。
是否可以检索原始保存的数据?
解决方案
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
推荐阅读
- xcode - 当没有可用更新时,为什么 Homebrew 说我没有最新的 CLT?
- sql-server - 数据库目录级别的 Microsoft SqlServer 客户端 TDS(反向)代理
- javascript - 使用 CSV 在 D3 中嵌套数据后制作组条形图
- python - 如何在python中捕获字符串中的第一个数字元素?
- r - 在 Shiny 应用程序中增加残差图的高度
- pivot - 如何对度量进行分组并使列成为表源名称
- typescript - typescript 类型声明在 IDE 中显示错误
- arrays - 如何正确设置指向包含字符的数组的指针
- asp.net - ASP.NET.VB 将 GridView 单元格值从 [1] 值更改为 [High] 值的答案
- algorithm - 如果大 O 是可加的,并且 O(A) 和 O(B) 的两个函数加在一起将是 O(A + B),那么为什么两个循环不成立呢?