-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveBlockWebSocket.js
98 lines (81 loc) · 2.26 KB
/
liveBlockWebSocket.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
86
87
88
89
90
91
92
93
94
95
96
97
98
const { TezosToolkit } = require('@taquito/taquito');
const RPC_MAP = {
mainnet: 'https://mainnet.api.tez.ie',
ghostnet: 'https://ghostnet.ecadinfra.com',
kathmandunet: 'https://kathmandunet.ecadinfra.com',
};
const tezos = new TezosToolkit(RPC_MAP[process.env.REACT_APP_TEZOS_NETWORK]);
let IO;
let INTERVAL = 30;
// GLOBAL VARIABLES
let head;
let currentCycle = 0;
let currentLevel = 0;
/**
* @description Runs the main process which monitors the head of the chain and acts on new levels and cycles
* @param {Io} io The socket io instance
*/
const main = async (io) => {
try {
head = await tezos.rpc.getBlock();
// Bootstrap the block monitor
if (io) {
IO = io;
const constants = await tezos.rpc.getConstants();
await initialize(head, constants);
return loop(INTERVAL);
}
// Set new current level and cycle
checkNewLevel(head);
// Sleep and repeat
return loop(INTERVAL);
} catch (e) {
return loop(INTERVAL);
}
};
/**
* @description Initilizes the variables used to track the current state of the chain
* @param {Object} headBlock The latest block
* @param {Object} constants The protocol constants
*/
const initialize = async (headBlock, constants) => {
INTERVAL = parseInt(constants.minimal_block_delay, 10) / 2;
const {
metadata: {
level_info: { cycle, level },
},
} = headBlock;
IO.on('connection', (socket) => {
socket.emit('block', head);
socket.emit('constants', constants);
});
currentLevel = level;
currentCycle = cycle;
console.log('Initializing block data system');
console.log(`Level: ${level}`);
console.log(`Cycle: ${cycle}`);
};
/**
* @description Increments the current level and pays out any generated rewards
* @param {Number} level The level of the current block
*/
const checkNewLevel = (headBlock) => {
const {
metadata: {
level_info: { cycle, level },
},
} = headBlock;
if (currentCycle !== cycle) {
currentCycle = cycle;
}
if (currentLevel !== level) {
currentLevel = level;
IO.emit('block', headBlock);
}
};
/**
* @description Runs the loop function
* @param {Number} seconds How often (in seconds) to run the loop function
*/
const loop = (seconds) => setTimeout(main, seconds * 1000);
module.exports = main;