forked from abrkn/tipmebch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
viacoinRpc.js
33 lines (27 loc) · 840 Bytes
/
viacoinRpc.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
const assert = require('assert');
const superagent = require('superagent');
const Redlock = require('redlock');
const createViacoinRpc = ({ redisClient, say, viacoindUrl }) => {
assert(redisClient, 'redisClient is required');
assert(viacoindUrl, 'viacoindUrl is required');
const redlock = new Redlock([redisClient]);
const lockViacoind = () =>
redlock.lock(`locks.viacoind.${viacoindUrl}.lock`, 10e3);
let fetchRpcCounter = 1;
const fetchRpc = (method, params) =>
superagent
.post(viacoindUrl)
.send({ id: (++fetchRpcCounter).toString(), method, params })
.then(_ => {
const { result, error } = _.body;
if (error) {
throw new Error(error);
}
return result;
});
return {
fetchRpc,
lockViacoind,
};
};
module.exports = createViacoinRpc;