-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegate_event.js
299 lines (274 loc) · 10.6 KB
/
delegate_event.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const rp = require('request-promise');
var JSONbig = require('json-bigint');
const fs = require('fs');
var hex64 = require('hex64');
const events = require('./out.json')
const BigNumber = require('bignumber.js');
const config = {
apiURL: "http://dev-api.loom.games/delegation/stake?action=unbound",
loomURL: "http://basechain.dappchains.com",
startPage: 1,
endPage: 10000,
}
function niceNumber(num) {
try{
var sOut = num.toString();
if ( sOut.length >=17 || sOut.indexOf("e") > 0){
sOut=parseFloat(num).toPrecision(5)+"";
sOut = sOut.replace("e","x10<sup>")+"</sup>";
}
return sOut;
}
catch ( e) {
return num;
}
}
function bigIntToString(num) {
let rawBigint = num.toString(10);
return Number(rawBigint.substring(0, rawBigint.length - 18))
}
async function getBlockTime(block) {
var options = {
method: 'GET',
uri: config.loomURL + '/rpc/block?height=' + block,
};
try {
const res = await rp(options);
const data = JSONbig.parse(res);
if (data.error != undefined) {
console.log(data.error)
return 0
}
return data.result.block_meta.header.time;
} catch (err) {
console.log(err)
return 0
}
}
function base64ToHexAddr(str) {
return "default:0x" + hex64.transform(str)
}
async function readJSON(filename) {
let rawdata = fs.readFileSync(filename);
let delegations = JSONbig.parse(rawdata);
return delegations.result
}
async function getDelegationEvents(page) {
var options = {
method: 'GET',
uri: config.apiURL + "&page=" + page,
};
try {
const res = await rp(options);
const data = JSONbig.parse(res);
return data.data
} catch (err) {
console.log(err)
return 0
}
}
const payoutBonus = {
6: 5, // June 5%
7: 6, // July 6%
9: 2, // September 2
10: 3, // October 3
11: 5, // November 5
}
async function getPaidRewards(currentMonthFile, nextMonthFile, fromBlock, toBlock) {
const paidRewards = {}
const diffRewards = {}
// read delegations from files
let delegations = await readJSON(nextMonthFile)
for (response of delegations.list_responses) {
//console.log(response.delegations)
const validatorDelegations = response.delegations;
if (validatorDelegations == undefined) {
continue;
}
for (delegation of validatorDelegations) {
// Skip normal delegation
if (delegation.index) {
continue;
}
const d = base64ToHexAddr(delegation.delegator.local)
const v = base64ToHexAddr(delegation.validator.local)
//const amount = bigIntToString(delegation.amount.Value)
//console.log(d, v, amount)
paidRewards[d + v] = delegation.amount
}
}
delegations = await readJSON(currentMonthFile)
for (response of delegations.list_responses) {
const validatorDelegations = response.delegations;
if (validatorDelegations == undefined) {
continue;
}
for (delegation of validatorDelegations) {
// Skip normal delegation
if (delegation.index) {
continue;
}
const d = base64ToHexAddr(delegation.delegator.local)
const v = base64ToHexAddr(delegation.validator.local)
if (typeof paidRewards[d + v] !== 'undefined') {
diffRewards[d + v] = {
"amount": paidRewards[d + v].Value - delegation.amount.Value,
"delegator" : d,
"validator" : v,
}
diffRewards[d + v].amount = getUnboundRewardEventAmount(d,v,fromBlock, toBlock).plus(diffRewards[d + v].amount)
}
}
}
return diffRewards
}
function getUnboundRewardEventAmount(delegator, validator, fromBlock, toBlock) {
let unboundAmount = new BigNumber(0)
for (page in events) {
let unboundEvents = events[page];
for (event of unboundEvents) {
if(event.validator.toLowerCase() == validator.toLowerCase() &&
event.delegator.toLowerCase() == delegator.toLowerCase() &&
event.index == 0 && event.block_height >= fromBlock && event.block_height < toBlock){
let amount = BigNumber(event.amount)
unboundAmount = unboundAmount.plus(amount)
}
}
}
//console.log("unbound number", unboundAmount.toString(10))
return unboundAmount
}
async function scanUnboundEvent() {
let allPages = {}
// get rewards unbound events
for (i = config.startPage; i <= config.endPage; i++) {
console.log("page", i)
const data = await getDelegationEvents(i)
allPages[i] = data
if (data.length < 100) {
break;
}
// if (data.length < 100) {
// break;
// }
// for (delegation of data) {
// const blocktime = new Date(delegation.block_time * 1000).toISOString();
// console.log(delegation.delegator.toLowerCase(),
// delegation.validator.toLowerCase(),
// delegation.amount.toString(10),
// delegation.index, blocktime, delegation.block_height)
// }
}
console.log(JSON.stringify(allPages, null, 2))
}
function sortObject(obj) {
var arr = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
arr.push({
'key': prop,
'value': obj[prop]
});
}
}
arr.sort(function(a, b) { return a.value - b.value; });
let result = {}
let totalCompensationAmount = 0
for(obj of arr) {
totalCompensationAmount += obj.value
result[obj.key] = obj.value
}
result["total_refund_amount"] = totalCompensationAmount
//arr.sort(function(a, b) { a.value.toLowerCase().localeCompare(b.value.toLowerCase()); }); //use this to sort as strings
return result; // returns array
}
async function calculateMissingRewards() {
let monthlyPaidRewards = {}
try {
//await scanUnboundEvent()
//console.log(eventsByBlock)
// 5700000 -> June *
// 7250000 -> July
// 9250000 -> August
// 11000000 -> September
// 12700000 -> October
// 14550000 -> November
// 16150000 -> End of problem
let rewards = await getPaidRewards("june_delegations.json", "july_delegations.json", 5700000, 7250000)
let currentMonthRewardsPaid = {}
let bonusPercent = 100/95 - 1
for (var key in rewards) {
if (typeof currentMonthRewardsPaid[rewards[key].delegator] === 'undefined') {
currentMonthRewardsPaid[rewards[key].delegator] = (rewards[key].amount/1000000000000000000) * bonusPercent
} else {
currentMonthRewardsPaid[rewards[key].delegator] += (rewards[key].amount/1000000000000000000) * bonusPercent
}
}
monthlyPaidRewards["June"] = currentMonthRewardsPaid
rewards = await getPaidRewards("july_delegations.json", "august_delegations.json", 7250000, 9250000)
currentMonthRewardsPaid = {}
bonusPercent = 100/94 - 1
for (var key in rewards) {
if (typeof currentMonthRewardsPaid[rewards[key].delegator] === 'undefined') {
currentMonthRewardsPaid[rewards[key].delegator] = (rewards[key].amount/1000000000000000000) * bonusPercent
} else {
currentMonthRewardsPaid[rewards[key].delegator] += (rewards[key].amount/1000000000000000000) * bonusPercent
}
}
monthlyPaidRewards["July"] = currentMonthRewardsPaid
rewards = await getPaidRewards("september_delegations.json", "october_delegations.json", 11000000, 12700000)
currentMonthRewardsPaid = {}
bonusPercent = 100/98 - 1
for (var key in rewards) {
if (typeof currentMonthRewardsPaid[rewards[key].delegator] === 'undefined') {
currentMonthRewardsPaid[rewards[key].delegator] = (rewards[key].amount/1000000000000000000) * bonusPercent
} else {
currentMonthRewardsPaid[rewards[key].delegator] += (rewards[key].amount/1000000000000000000) * bonusPercent
}
}
monthlyPaidRewards["September"] = currentMonthRewardsPaid
rewards = await getPaidRewards("october_delegations.json", "november_delegations.json", 12700000, 14550000)
currentMonthRewardsPaid = {}
bonusPercent = 100/97 - 1
for (var key in rewards) {
if (typeof currentMonthRewardsPaid[rewards[key].delegator] === 'undefined') {
currentMonthRewardsPaid[rewards[key].delegator] = (rewards[key].amount/1000000000000000000) * bonusPercent
} else {
currentMonthRewardsPaid[rewards[key].delegator] += (rewards[key].amount/1000000000000000000) * bonusPercent
}
}
monthlyPaidRewards["October"] = currentMonthRewardsPaid
rewards = await getPaidRewards("november_delegations.json", "november_end_delegations.json", 14550000, 16150000)
currentMonthRewardsPaid = {}
bonusPercent = 100/95 - 1
for (var key in rewards) {
if (typeof currentMonthRewardsPaid[rewards[key].delegator] === 'undefined') {
currentMonthRewardsPaid[rewards[key].delegator] = (rewards[key].amount/1000000000000000000) * bonusPercent
} else {
currentMonthRewardsPaid[rewards[key].delegator] += (rewards[key].amount/1000000000000000000) * bonusPercent
}
}
monthlyPaidRewards["November"] = currentMonthRewardsPaid
let totalPaidRewards = {}
for(month in monthlyPaidRewards) {
let monthlyPayout = monthlyPaidRewards[month]
for(delegator in monthlyPayout) {
if (typeof totalPaidRewards[delegator] === 'undefined') {
if (monthlyPayout[delegator] > 0) {
totalPaidRewards[delegator] = monthlyPayout[delegator]
}
} else {
if (monthlyPayout[delegator] > 0) {
totalPaidRewards[delegator] += monthlyPayout[delegator]
}
}
}
}
console.log(JSON.stringify(sortObject(totalPaidRewards), null, 2))
} catch (err) {
console.log("should not revert", err);
}
}
(async function () {
await calculateMissingRewards();
})();