-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc-testing.js
50 lines (36 loc) · 1.31 KB
/
rpc-testing.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
const WebSocket = require('ws');
const { decodeTxRaw } = require("@cosmjs/proto-signing");
const ws = new WebSocket('ws://gnfd-testnet-fullnode-tendermint-us.bnbchain.org/websocket');
ws.on('open', () => {
const subscriptionRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'subscribe',
params: ['tm.event = \'NewBlock\''],
id: 1,
});
ws.send(subscriptionRequest);
});
ws.on('message', (message) => {
const parsedMessage = JSON.parse(message);
if (parsedMessage.result && parsedMessage.result.data && parsedMessage.result.data.value) {
const blockHeight = parsedMessage.result.data.value.block.header.height;
const txs = parsedMessage.result.data.value.block.data.txs;
console.log('Block Height:', blockHeight);
if (txs) {
txs.forEach((tx, i) => {
let raw = Buffer.from(tx, 'base64');
let decodedTx = decodeTxRaw(raw);
console.log('Transaction', i + 1);
console.log(decodedTx.body.messages);
});
} else {
console.log('No transactions in this block.');
}
}
});
ws.on('close', function () {
console.log('WebSocket connection closed.');
});
ws.on('error', function (error) {
console.error('WebSocket error:', error);
});