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
/
sending.js
114 lines (86 loc) · 2.51 KB
/
sending.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const sending = require('debug')('zfaucet:sending');
// internal libs
const db = require('./lib/db');
const config = require('./config');
const utils = require('./lib/utils');
const rpc = require('./lib/rpc');
async function findInputs() {
// get balance from rpc daemon
const balance = await rpc.getbalance();
sending(`balance: ${balance}`);
// check if we have enought money to send
const balMinusSend = balance -
(config.sendingAmount * config.dripsPerSend) - config.sendingFee;
if (balMinusSend <= 0) throw new Error('Not enough to send.');
// get inputs and make sure its not empty
const inputs = await rpc.listunspent();
if (inputs.length === 0) throw new Error('No inputs.');
// get and return largest input address
const large = utils.indexOfMax(inputs);
return inputs[large].address;
}
module.exports.findInputs = findInputs;
async function sendDrip(sendingAddress) {
// get pending drips and make sure its not empty
const rows = await db.pendingDrips();
if (rows.length === 0) {
sending('sending list empty');
return 0;
}
// make transaction list
const sendList = [
{
address: rows[0].payoutAddress,
amount: typeof rows[0].amount === 'number' ? rows[0].amount : config.sendingAmount
}
];
sending('sendList: %o', sendList);
// send payment
const opid = await rpc.zSendmany(
sendingAddress,
sendList,
1,
config.sendingFee
);
// change drips to processed and return opid
for (const row of rows) {
row.processed = Date.now();
row.operationId = opid;
await db.payouts.update(row);
}
return opid;
}
module.exports.sendDrip = sendDrip;
async function updateDrips() {
const operations = await rpc.zGetoperationresult();
// update drips
await Promise.all(operations.map(async transaction => {
const results = await db.payouts.find(1000, {operationId: transaction.id});
for (const result of results) {
result.transactionId = transaction.result.txid;
await db.payouts.update(result);
}
}));
return 1; // signal finished without error
}
module.exports.updateDrips = updateDrips;
async function main() {
sending('running sending script');
try {
const sendingAddress = await findInputs();
await sendDrip(sendingAddress);
await updateDrips();
sending('sending script done');
return 1;
} catch (err) {
sending(`error: %s`, err.message);
return 0;
}
}
module.exports.main = main;
/* istanbul ignore next */
// send the drips, if running this script alone
if (require.main === module) {
main();
setInterval(main, config.sendingIntervalMin * 60 * 1000);
}