-
Notifications
You must be signed in to change notification settings - Fork 2
/
obyte-client.js
47 lines (40 loc) · 1.19 KB
/
obyte-client.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
/*jslint node: true */
'use strict';
const obyte = require('obyte');
const { getEnvironment } = require("./environment.js");
let clients = {};
let watchedAAs = {
mainnet: {},
testnet: {},
};
/**
* Get obyte.js client created by the SDK
* @memberOf counterstake-sdk
* @param {boolean} testnet
* @return {Object}
* @example
* const obyteClient = getObyteClient(testnet);
*/
function getObyteClient(testnet) {
const environment = getEnvironment(testnet);
if (!clients[environment]) {
clients[environment] = new obyte.Client('wss://obyte.org/bb' + (testnet ? '-test' : ''), { testnet, reconnect: true });
setInterval(function () {
clients[environment].api.heartbeat();
}, 20 * 1000);
}
return clients[environment];
}
function watchAA(aa, client) {
client.justsaying("light/new_aa_to_watch", { aa });
watchedAAs[getEnvironment(client.options.testnet)][aa] = true;
}
function resumeWatchingAAs(client) {
for (let aa in watchedAAs[getEnvironment(client.options.testnet)]) {
console.log(`resubscribing to ${aa}`);
client.justsaying("light/new_aa_to_watch", { aa });
}
}
exports.getObyteClient = getObyteClient;
exports.watchAA = watchAA;
exports.resumeWatchingAAs = resumeWatchingAAs;