forked from bcoin-org/bcash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-tx-from-chain.js
85 lines (63 loc) · 1.75 KB
/
get-tx-from-chain.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
80
81
82
83
84
85
'use strict';
const bcoin = require('../..');
const Logger = require('blgr');
// Setup logger to see what's Bcoin doing.
const logger = new Logger({
level: 'debug'
});
// Create an in-memory blockchain.
const chain = new bcoin.Chain({
logger: logger,
memory: true,
network: 'testnet'
});
const mempool = new bcoin.Mempool({ chain: chain });
// Create a network pool of peers with a limit of 8 peers.
const pool = new bcoin.Pool({
logger: logger,
chain: chain,
mempool: mempool,
maxPeers: 8
});
// Create a chain indexer which indexes tx by hash
const indexer = new bcoin.TXIndexer({
logger: logger,
memory: true,
network: 'testnet',
chain: chain
});
// Open the chain, pool and indexer
(async function() {
await logger.open();
await pool.open();
// Connect, start retrieving and relaying txs
await pool.connect();
// Start the blockchain sync.
pool.startSync();
await chain.open();
await indexer.open();
console.log('Current height:', chain.height);
// Watch the action
chain.on('block', (block) => {
console.log('block: %s', block.rhash());
});
mempool.on('tx', (tx) => {
console.log('tx: %s', tx.rhash);
});
pool.on('tx', (tx) => {
console.log('tx: %s', tx.rhash);
});
await new Promise(r => setTimeout(r, 300));
await pool.stopSync();
const tip = await indexer.getTip();
const block = await chain.getBlock(tip.hash);
const meta = await indexer.getMeta(block.txs[0].hash());
const tx = meta.tx;
const view = await indexer.getSpentView(tx);
console.log(`Tx with hash ${tx.rhash()}:`, meta);
console.log(`Tx input: ${tx.getInputValue(view)},` +
` output: ${tx.getOutputValue()}, fee: ${tx.getFee(view)}`);
await indexer.close();
await chain.close();
await pool.close();
})();