-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbitcoinRpc.js
More file actions
33 lines (27 loc) · 840 Bytes
/
Copy pathbitcoinRpc.js
File metadata and controls
33 lines (27 loc) · 840 Bytes
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 createBitcoinRpc = ({ redisClient, say, bitcoindUrl }) => {
assert(redisClient, 'redisClient is required');
assert(bitcoindUrl, 'bitcoindUrl is required');
const redlock = new Redlock([redisClient]);
const lockBitcoind = () =>
redlock.lock(`locks.bitcoind.${bitcoindUrl}.lock`, 10e3);
let fetchRpcCounter = 1;
const fetchRpc = (method, params) =>
superagent
.post(bitcoindUrl)
.send({ id: (++fetchRpcCounter).toString(), method, params })
.then(_ => {
const { result, error } = _.body;
if (error) {
throw new Error(error);
}
return result;
});
return {
fetchRpc,
lockBitcoind,
};
};
module.exports = createBitcoinRpc;