Query Data With Javascript

Getting the query result with OCAP playground or postman is cool, but we want to use it in our own application.

We provide several SDKs so that you can retrieve data easily with your favorite language.

Here we use OCAP Javascript SDK as an example. Let's create a new project:

Requirements

The simple demo project assumes you already have node.js installed.

mkdir ocap-js-test
cd ocap-js-test
npm init -y
1
2
3

Then add @arcblock/ocap-js as dependency:

npm install @arcblock/ocap-js --save
touch index.js
1
2

Then open index.js and add these contents:

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

// init client
const client = new OCAPClient({
  dataSource: 'btc', // currently `btc` and `eth` is supported
});

const queries = client.getQueries();

console.log(queries);

client
  .transactionsByAddress({ receiver: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' })
  .then(data => console.log(data));
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Then run the script with:

node index.js
1

You will get the following outout:

[ 'accountByAddress',
  'blockByHash',
  'blockByHeight',
  'blockchainInfo',
  'blocksByHeight',
  'cryptoHistoryPrice',
  'emptyBlocks',
  'genesisBlock',
  'richestAccounts',
  'transactionByHash',
  'transactionByIndex',
  'transactionsByAddress',
  'transactionsByIndex',
  'zeroFeesBlocks' ]
{ transactionsByAddress:
   { page:
      { total: 1462,
        next: true,
        cursor: 'cK1GAXeWPHkBuMksAIvI4w%3D%3D' },
     data:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     next: [AsyncFunction] } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Want to see a recording for this demo project, click the asciinema cast below!

asciicast

Last Updated: 11/30/2018, 10:39:27 AM