资讯专栏INFORMATION COLUMN

eosjs 文档(读取区块链)

OpenDigg / 471人阅读

摘要:读取区块链读取区块链状态只需要连接到节点的实例。示例获取表格行获取帐户的前个代币余额。输出按索引获取一行输出通过二级索引获取一行输出获得货币余额输出获取帐户信息输出获取区块输出上一篇交易下一篇

读取区块链

读取区块链状态只需要连接到节点的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 }
上一篇:交易 下一篇:API

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/100509.html

相关文章

  • eosjs 文档(目录)

    摘要:文档用于使用与基于的区块链集成的。重要最近发布了针对的重大改写,一定要锁定你的依赖项。如果你正在寻找以前版本的,可以在这里找到它。指南介绍浏览器交易读取区块链参考接口接口类接口 eosjs 文档 用于使用EOSIO RPC API与基于EOSIO的区块链集成的Javascript API。 重要!最近发布了针对eosjs的重大改写,一定要锁定你的依赖项。 如果你正在寻找以前版本的eos...

    shleyZ 评论0 收藏0
  • eosjs 文档(交易)

    摘要:交易为了能够在区块链上发送交易和触发操作,你必须具有实例。签名提供程序必须包含与执行者和操作权限相对应的私钥。示例示例创建新帐户多个操作上一篇浏览器下一篇读取区块链 交易 为了能够在区块链上发送交易和触发操作,你必须具有Api实例。 签名提供程序必须包含与执行者和操作权限相对应的私钥。 const { Api, JsonRpc } = require(eosjs); const JsS...

    Pluser 评论0 收藏0
  • 使用EOSJS和scatter在EOS区块上开发dApp

    摘要:必备知识设置用于为区块链签署交易,并在不泄露密钥的情况下向应用程序提供个人信息。 由于我一直在深入研究EOS dApp的开发,我看了不少好文章。在这里,我汇总了下做一些研究后得到的所有知识。在本文中,我将解释如何使用EOSJS和scatter。我假设你对智能合约以及如何在EOS区块链上部署它们有基本的了解,因为我将在本文中跳过该部分。 我们在构建什么?  我们正在构建一个简单的todo...

    Mr_houzi 评论0 收藏0
  • 使用EOSJS和scatter在EOS区块上开发dApp

    摘要:必备知识设置用于为区块链签署交易,并在不泄露密钥的情况下向应用程序提供个人信息。 由于我一直在深入研究EOS dApp的开发,我看了不少好文章。在这里,我汇总了下做一些研究后得到的所有知识。在本文中,我将解释如何使用EOSJS和scatter。我假设你对智能合约以及如何在EOS区块链上部署它们有基本的了解,因为我将在本文中跳过该部分。 我们在构建什么?  我们正在构建一个简单的todo...

    mumumu 评论0 收藏0
  • 使用EOSJS和scatter在EOS区块上开发dApp

    摘要:必备知识设置用于为区块链签署交易,并在不泄露密钥的情况下向应用程序提供个人信息。 由于我一直在深入研究EOS dApp的开发,我看了不少好文章。在这里,我汇总了下做一些研究后得到的所有知识。在本文中,我将解释如何使用EOSJS和scatter。我假设你对智能合约以及如何在EOS区块链上部署它们有基本的了解,因为我将在本文中跳过该部分。 我们在构建什么?  我们正在构建一个简单的todo...

    xiaodao 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<