-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclaimer.js
100 lines (94 loc) · 2.82 KB
/
claimer.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 Eos = require('eosjs');
const config = require('./config.json');
const httpEndPoint = config.httpEndPoint;
const chainId = config.chainId;
const wif = config.wif;
const producerName = config.producerName;
const permission = config.permission;
var eos = Eos({
httpEndpoint: httpEndPoint, chainId: chainId,
keyProvider: wif
});
cacheRewards();
//try every 10 min
setInterval(cacheRewards, 10 * 60 * 1000 + 5000);
//////////////////////////
function cacheRewards() {
Promise.all([getGlobal(), getProducer(producerName)]).then(([global, producer]) => {
let bpay = (global.perblock_bucket * producer.unpaid_blocks) / global.total_unpaid_blocks / 10000;
let vpay = (global.pervote_bucket * producer.total_votes) / (1 * global.total_producer_vote_weight) / 10000;
if (vpay < 100) {
vpay = 0;
}
let next_claim_time = 1 * producer.last_claim_time / 1000 + 24 * 60 * 60 * 1000;
if (next_claim_time > Date.now()) {
return 0;
}
return bpay + vpay;
}, errs => {
console.error(errs);
//retry
cacheRewards();
}).then(rewards => {
console.log("current rewards:", rewards);
if (rewards > 0) {
eos.transaction({
// ...headers,
actions: [
{
account: 'eosio',
name: 'claimrewards',
authorization: [{
actor: producerName,
permission: permission
}],
data: {
owner: producerName
}
}
]
}).then(res => {
console.log(res);
}, err => {
console.error(err);
//retry
cacheRewards();
});
}
});
}
function getGlobal() {
return new Promise((resolve, reject) => {
eos.getTableRows({
"scope": "eosio",
"code": "eosio",
"table": "global",
"json": true
}).then(res => {
resolve(res.rows[0]);
}, err => {
console.error(err);
reject(err);
});
});
}
function getProducer(name) {
return new Promise((resolve, reject) => {
eos.getTableRows({
"scope": "eosio",
"code": "eosio",
"table": "producers",
"lower_bound": name,
"limit": 1,
"json": true
}).then(res => {
if (!res.rows[0] || name != res.rows[0].owner) {
reject("producer not exist!");
}
resolve(res.rows[0]);
}, err => {
console.error(err);
reject(err);
});
});
}