This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathipayouts-daemon.js
99 lines (76 loc) · 1.93 KB
/
ipayouts-daemon.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
99
const rpc = require('./lib/rpc');
const ipayouts = require('./lib/ipayouts');
const config = require('./config');
const sending = require('./sending');
const findInput = sending.findInputs;
function buildSendList(payouts) {
const sendMap = {};
for (const {address, amount} of payouts) {
if (!(address in sendMap))
sendMap[address] = 0;
sendMap[address] += amount;
}
const sendList = [];
for (const address in sendMap) {
if ({}.hasOwnProperty.call(sendMap, address)) {
sendList.push({
address,
amount: sendMap[address].toFixed(8)
});
}
}
return sendList;
}
async function sendPayouts() {
const inputAddress = await findInput();
const payouts = await ipayouts.getUnpaid();
const sendList = buildSendList(payouts);
for (const send of sendList)
send.amount -= config.sendingFee / sendList.length;
const operationId = await rpc.zSendmany(
inputAddress,
sendList,
1,
config.sendingFee
);
await Promise.all(payouts.map(async payout => {
payout.processed = Date.now();
payout.operationId = operationId;
await ipayouts.update(payout);
}));
}
async function updatePayouts() {
const operations = await rpc.zGetoperationresult();
// update drips
await Promise.all(operations.map(async transaction => {
const results = await ipayouts.find(1000, {operationId: transaction.id});
await Promise.all(results.map(async result => {
result.transactionId = transaction.result.txid;
await ipayouts.update(result);
}));
}));
}
module.exports = {
findInput,
buildSendList,
sendPayouts,
updatePayouts
};
/* istanbul ignore next */
if (require.main === module) {
(async () => {
async function job() {
await sendPayouts();
await updatePayouts();
}
const interval = 2.5 * 60 * 1000;
for (;;) {
const startTime = Date.now();
await job();
const delta = Date.now() - startTime;
await new Promise(resolve => {
setTimeout(resolve, Math.min(0, interval - delta));
});
}
})();
}