@arcblock/ocap-js

Javascript SDK for OCAP Service by ArcBlock

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
1521Aug 3, 2021Aug 17, 2018Minified + gzip package size for @arcblock/ocap-js in KB

Readme

@arcblock/ocap-js

build status code coverage styled with prettier license

基于 ArcBlock OCAP 服务为 Javascript 开发者定制的 SDK,可运行在多种 Javascript 环境中,比如 Node.js、浏览器、React-Native 中。

如果要在浏览器环境下使用该 SDK,必须在代码中引入 babel-polyfill

For English documentation,please look at README.md

Table of Contents

安装依赖

npm install @arcblock/ocap-js --save
// 或者
yarn add @arcblock/ocap-js

基本用法

const OCAPClient = require('@arcblock/ocap-js');

// 初始化 SDK
const client = new OCAPClient({
  dataSource: 'eth', // 目前仅支持 btc, eth
});

// 直接调用快捷查询方法
const account = await client.accountByAddress({
  address: '0xe65d3128feafd14d472442608daf94bceb91e333',
});
console.log('ShortcutQuery', account);

// 自己构造并发起查询
const result = await client.doRawQuery(`{
  blockByHeight(height:5027689) {
    time
    size
    gasUsed
    gasLimit
    nonce
    reward
    preHash
    size
  }
}`);
console.log('RawQuery', result);

// 查询结果翻页
const blocks = await client.listBlocks({
  timeFilter: {
    fromHeight: 1000000,
    toHeight: 1000020,
  },
  paging: { size: 5 },
});
console.log('PagedQuery.1', blocks.data.map(x => x.hash));
if (typeof blocks.next === 'function') {
  const blocks2 = await blocks.next();
  console.log('PagedQuery.2', blocks2.data.map(x => x.hash));
}

// 直接调用实时数据订阅方法
const subscription = await client.newBlockMined();
subscription.on('data', data => console.log('ShortcutSubscription', data));

// 自己构造并订阅实时数据
const rawSubscription = await client.doRawSubscription(`
  subscription {
    newBlockMined {
      height
      hash
    }
  }`);
rawSubscription.on('data', data => console.log('RawSubscription', data));

更多 API 调用代码示例:

接口文档

当前版本的接口、接口参数、接口响应列表参见:

种子项目

为节省开发者时间,我们准备了如下的种子项目,在其中集成了 OCAP JS SDK,仅需一条命令就可以开始构建 DAPP

如果你是在现有项目中集成 OCAP JS SDK,请参见集成文档:https://ocap.arcblock.io/playbooks/67df3512-c575-4751-8052-e3dcae3f3e1a

CodeSandbox 项目

如果你想直接在浏览器中开始开发 DApp,我们还准备了 CodeSandbox 上面的种子项目,可以 fork 后开始开发

OCAP 作弊条

OCAP 服务支持的所有 Query、Subscription 详细文档可以在 Playground 的实时文档生成的文档中看到。

License

MIT © wangshijun

Contributors

Name Website
wangshijun https://ocap.arcblock.io

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.