乐趣区

eosjs 文档(读取区块链)

读取区块链
读取区块链状态只需要连接到节点的 JsonRpc 实例。
const {JsonRpc} = require(‘eosjs’);
const fetch = require(‘node-fetch’); // node only; not needed in browsers
const rpc = new JsonRpc(‘http://127.0.0.1:8888’, { fetch});
示例
获取表格行
获取帐户 testacc 的前 10 个代币余额。
const resp = await rpc.get_table_rows({
json: true, // Get the response as json
code: ‘eosio.token’, // Contract that we target
scope: ‘testacc’ // Account that owns the data
table: ‘accounts’ // Table name
limit: 10, // maximum number of rows that we want to get
});

console.log(resp.rows);
输出:
{
“rows”: [{
“balance”: “100.0000 HAK”
}
],
“more”: false
}
按索引获取一行
const resp = await rpc.get_table_rows({
json: true, // Get the response as json
code: ‘contract’, // Contract that we target
scope: ‘contract’ // Account that owns the data
table: ‘profiles’ // Table name
lower_bound: ‘testacc’ // Table primary key value
limit: 1, // Here we limit to 1 to get only the
});
console.log(resp.rows);
输出:
{
“rows”: [{
“user”: “testacc”,
“age”: 21,
“surname”: “Martin”
}
],
“more”: false
}
通过二级索引获取一行
const resp = await rpc.get_table_rows({
json: true, // Get the response as json
code: ‘contract’, // Contract that we target
scope: ‘contract’ // Account that owns the data
table: ‘profiles’ // Table name
table_key: ‘age’ // Table secondaray key name
lower_bound: 21 // Table secondary key value
limit: 1, // Here we limit to 1 to get only the
});
console.log(resp.rows);
输出:
{
“rows”: [{
“user”: “testacc”,
“age”: 21,
“surname”: “Martin”
}
],
“more”: false
}
获得货币余额
console.log(await rpc.get_currency_balance(‘eosio.token’, ‘testacc’, ‘HAK’));
输出:
[“1000000000.0000 HAK”]
获取帐户信息
console.log(await rpc.get_account(‘testacc’));
输出:
{“account_name”: “testacc”,
“head_block_num”: 1079,
“head_block_time”: “2018-11-10T00:45:53.500”,
“privileged”: false,
“last_code_update”: “1970-01-01T00:00:00.000”,
“created”: “2018-11-10T00:37:05.000”,
“ram_quota”: -1,
“net_weight”: -1,
“cpu_weight”: -1,
“net_limit”: {“used”: -1, “available”: -1, “max”: -1},
“cpu_limit”: {“used”: -1, “available”: -1, “max”: -1},
“ram_usage”: 2724,
“permissions”:
[{ “perm_name”: “active”, “parent”: “owner”, “required_auth”: [] },
{“perm_name”: “owner”, “parent”: “”, “required_auth”: [] } ],
“total_resources”: null,
“self_delegated_bandwidth”: null,
“refund_request”: null,
“voter_info”: null }
获取区块
console.log(await rpc.get_block(1));
输出:
{“timestamp”: “2018-06-01T12:00:00.000”,
“producer”: “”,
“confirmed”: 1,
“previous”: “0000000000000000000000000000000000000000000000000000000000000000”,
“transaction_mroot”: “0000000000000000000000000000000000000000000000000000000000000000”,
“action_mroot”: “cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f”,
“schedule_version”: 0,
“new_producers”: null,
“header_extensions”: [],
“producer_signature”: “SIG_K1_111111111111111111111111111111111111111111111111111111111111111116uk5ne”,
“transactions”: [],
“block_extensions”: [],
“id”: “00000001bcf2f448225d099685f14da76803028926af04d2607eafcf609c265c”,
“block_num”: 1,
“ref_block_prefix”: 2517196066 }

上一篇:交易

退出移动版