-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
79 lines (61 loc) · 2.2 KB
/
example.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { XRouter } = require('./dist');
const timeout = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
(async function() {
try {
// create xrouter client
const client = new XRouter({
network: XRouter.networks.MAINNET,
});
// Listen for events
client.on(XRouter.events.INFO, message => {
console.log(message);
});
client.on(XRouter.events.ERROR, message => {
console.error(message);
});
// start the server & wait for it to find two or more EXR nodes
const started = await client.start();
if(!started)
return;
const wallet = 'BLOCK';
const queryNum = 3;
// make an spv call
const blockCountStr = await new Promise(resolve => {
const interval = setInterval(() => {
client.getBlockCount(wallet, queryNum)
.then(res => {
clearInterval(interval);
resolve(res);
})
.catch(() => {
// do nothing
});
}, 30000);
});
const blockCount = JSON.parse(blockCountStr);
console.log(`block count is ${blockCount}`);
await timeout(30000);
const blockHash = await client.getBlockHash(wallet, blockCount, queryNum);
console.log(`block hash is ${blockHash}`);
await timeout(30000);
const blockDataStr = await client.getBlock(wallet, blockHash, queryNum);
const blockData = JSON.parse(blockDataStr);
console.log('block data', blockData);
const blocksDataStr = await client.getBlocks(wallet, [blockData.hash, blockData.previousblockhash], queryNum);
const blocksData = JSON.parse(blocksDataStr);
console.log('blocks data', blocksData);
const txids = blocksData
.reduce((arr, data) => arr.concat(data.tx), []);
await timeout(30000);
const [ txid ] = txids;
const transactionStr = await client.getTransaction(wallet, txid, queryNum);
const transaction = JSON.parse(transactionStr);
console.log('transaction', transaction);
await timeout(30000);
const transactionsStr = await client.getTransactions(wallet, txids, queryNum);
const transactions = JSON.parse(transactionsStr);
console.log('transactions', transactions);
} catch(err) {
console.error(err);
}
})();