-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpayouts.js
More file actions
106 lines (95 loc) · 4.14 KB
/
payouts.js
File metadata and controls
106 lines (95 loc) · 4.14 KB
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
const smartholdemApi = require("sthjs-wrapper")
const moment = require("moment")
const scheduler = require("node-schedule")
const util = require("./modules/util")
const request = require("request")
const axios = require("axios")
const jsonReader = require('jsonfile')
const rConfig = jsonReader.readFileSync("./config.json")
const sth = require("sthjs");
const PUB_KEY = sth.crypto.getKeys(rConfig.secret).publicKey;
const DELEGATE_ADDRESS = sth.crypto.getAddress(PUB_KEY);
function dbUpdate(key, value) {
request({
method: 'post',
json: true, // Use,If you are sending JSON data
url: 'http://127.0.0.1:' + rConfig.port + '/api/db/add',
body: {"key": key, "value": value},
headers: {
"accept": "application/json",
"x-api-key": rConfig.appKey
}
}, function (err, res, body) {
if (!err) {
console.log(body);
}
});
}
var doPayout = () => {
util.log("==Payout Begin==");
util.log("DateTime: " + moment().toISOString());
let transactions = [];
request({
method: 'get',
json: true, // Use,If you are sending JSON data
url: 'http://127.0.0.1:' + rConfig.port + '/api/voters/getFromDb',
headers: {
"accept": "application/json"
}
}, function (err, res, body) {
if (!err) {
let fee = 100000000;
let totalPayout = 0;
for (let i = 0; i < body.length; i++) {
let payoutSum = body[i].reward - fee;
if (payoutSum > 100000000) {
totalPayout = totalPayout + body[i].reward;
// create tx if not current delegate address
if (body[i].address !== DELEGATE_ADDRESS) {
transactions.push(smartholdemApi.createTransaction(rConfig.secret, body[i].address, payoutSum, {vendorField: "payout"}));
}
dbUpdate('0x' + body[i].address, {
"address": body[i].address,
"balance": body[i].balance,
"reward": 0,
"timestamp": body[i].timestamp,
"personalPercent": body[i].personalPercent
});
}
}
if (transactions) {
smartholdemApi.sendTransactions(transactions, (error, success, responseSend) => {
if (!error) {
if (responseSend.success === true) {
util.log("Total Payout:" + (totalPayout / 100000000));
for (let i = 0; i < transactions.length; i++) {
dbUpdate('2x' + transactions[i].id, {
"recipientId": transactions[i].recipientId,
"amount": transactions[i].amount,
"timestamp": Date.now()
});
}
request({
method: 'post',
json: true, // Use,If you are sending JSON data
url: 'http://127.0.0.1:' + rConfig.port + '/api/stats/cleanup',
body: {"totalPayout": totalPayout},
headers: {
"accept": "application/json",
"x-api-key": rConfig.appKey
}
});
}
}
});
}
}
});
};
exports.startScheduler = (cronJob, options) => {
console.log(`Automatic payouts scheduled: ${cronJob}`);
let paySchedule = scheduler.scheduleJob(cronJob, () => {
util.log("Payout on DateTime: " + moment().toISOString());
doPayout(options);
});
};