forked from w0utje/WavesLPoSDistributer
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.js
More file actions
478 lines (408 loc) · 17.4 KB
/
Copy pathapp.js
File metadata and controls
478 lines (408 loc) · 17.4 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
var request = require('sync-request');
var fs = require('fs');
/**
* w0utje's edit of Hawky's LPoSDistributor
* added mercury and ripto bux fee calculation
* added (cancelled) leases infomation to be re-used next payout
* added HTML payout overview generation
* added feeAssetId to the payments for sending payments with a custom assetFee
* Removed 0.003 Waves Fee substraction, because of the custom assetFee adding
* if you don't want to use an assetFee, remember that you'll need to substract the waves fee for each transaction.
*
* Planned MRT distribution, ToDo
* 425419 - 475418 -> 50 MRT / block
* 475419 - 525418 -> 40 MRT / block
* 525419 - 575418 -> 30 MRT / block
* 575419 - 625418 -> 20 MRT / block
* 625419 - 925418 -> 10 MRT / block
*
* Put your settings here:
* - address: the address of your node that you want to distribute from
* - startBlockHeight: the block from which you want to start distribution for
* - endBlock: the block until you want to distribute the earnings
* - distributableMRTPerBlock: amount of MRT distributed per forged block
* - filename: file to which the payments for the mass payment tool are written
* - paymentid: used to create payment and html file with this id.
* - node: address of your node in the form http://<ip>:<port
* - percentageOfFeesToDistribute: the percentage of Waves fees that you want to distribute
*/
var config = {
address: '',
startBlockHeight: 559474,
endBlock: 567751,
distributableMrtPerBlock: 30,
filename: 'payment', //.json added automatically
paymentid: 11,
//node: 'http://127.0.0.1:6869',
node: 'http://nodes.wavesnodes.com',
percentageOfFeesToDistribute: 100
};
var myLeases = {};
var myCanceledLeases = {};
var currentStartBlock = 462000;
var fs=require('fs');
var prevleaseinfofile = config.startBlockHeight + "_" + config.address + ".json";
if (fs.existsSync(prevleaseinfofile))
{
console.log("reading" + prevleaseinfofile + " file");
var data=fs.readFileSync(prevleaseinfofile);
var prevleaseinfo=JSON.parse(data);
myLeases = prevleaseinfo["leases"];
myCanceledLeases = prevleaseinfo["canceledleases"];
currentStartBlock = config.startBlockHeight;
}
var payments = [];
var mrt = [];
var merfees=[];
var rbxfees=[];
var assetfees = [];
var allfees = [assetfees];
var BlockCount = 0;
var LastBlock = {};
var myForgedBlocks = [];
/**
* This method starts the overall process by first downloading the blocks,
* preparing the necessary datastructures and finally preparing the payments
* and serializing them into a file that could be used as input for the
* masspayment tool.
*/
var start = function() {
console.log('getting blocks...');
var blocks = getAllBlocks();
console.log('preparing datastructures...');
prepareDataStructure(blocks);
console.log('preparing payments...');
myForgedBlocks.forEach(function(block) {
if (block.height >= config.startBlockHeight && block.height <= config.endBlock) {
var blockLeaseData = getActiveLeasesAtBlock(block);
var activeLeasesForBlock = blockLeaseData.activeLeases;
var amountTotalLeased = blockLeaseData.totalLeased;
distribute(activeLeasesForBlock, amountTotalLeased, block);
BlockCount++;
}
});
//Get last block
LastBlock = blocks.slice(-1)[0] ;
pay();
console.log("blocks forged: " + BlockCount);
};
/**
* This method organizes the datastructures that are later on necessary
* for the block-exact analysis of the leases.
*
* @param blocks all blocks that should be considered
*/
var prepareDataStructure = function(blocks) {
blocks.forEach(function(block) {
var wavesFees = 0;
var merFees = 0;
var rbxFees = 0;
var assetFees = [];
if (block.generator === config.address) {
myForgedBlocks.push(block);
}
block.transactions.forEach(function(transaction) {
// type 8 are leasing tx
if (transaction.type === 8 && transaction.recipient === config.address) {
transaction.block = block.height;
myLeases[transaction.id] = transaction;
} else if (transaction.type === 9 && myLeases[transaction.leaseId]) { // checking for lease cancel tx
transaction.block = block.height;
myCanceledLeases[transaction.leaseId] = transaction;
}
// considering Waves fees
if (!transaction.feeAsset || transaction.feeAsset === '' || transaction.feeAsset === null) {
if(transaction.fee < 200000000) // if tx waves fee is more dan 2 waves, filter it. probably a mistake by someone
{
wavesFees += transaction.fee;
} else {
console.log("Filter TX at block: " + block.height + " Amount: " + transaction.fee)
}
} else {
// assetfee dynamic testing
assetFees = InsertOrUpdateArray(assetFees,transaction.feeAsset,transaction.fee); //test for combined assetsfees array
}
if (transaction.feeAsset === 'HzfaJp8YQWLvQG4FkUxq2Q7iYWMYQ2k8UF89vVJAjWPj') { //Mercury
merFees += transaction.fee;
}
if (transaction.feeAsset === 'AnERqFRffNVrCbviXbDEdzrU6ipXCP5Y1PKpFdRnyQAy') { //Ripto Bux
rbxFees += transaction.fee;
}
});
block.wavesFees = wavesFees;
block.merFees = merFees;
block.rbxFees = rbxFees;
block.assetFees = assetFees;
});
};
/**
* Method that returns all relevant blocks.
*
* @returns {Array} all relevant blocks
*/
var getAllBlocks = function() {
// leases have been resetted in block 462000, therefore, this is the first relevant block to be considered
//var firstBlockWithLeases=462000;
//var currentStartBlock = firstBlockWithLeases;
var blocks = [];
while (currentStartBlock < config.endBlock) {
var currentBlocks;
if (currentStartBlock + 99 < config.endBlock) {
console.log('getting blocks from ' + currentStartBlock + ' to ' + (currentStartBlock + 99));
currentBlocks = JSON.parse(request('GET', config.node + '/blocks/seq/' + currentStartBlock + '/' + (currentStartBlock + 99), {
'headers': {
'Connection': 'keep-alive'
}
}).getBody('utf8'));
} else {
console.log('getting blocks from ' + currentStartBlock + ' to ' + config.endBlock);
currentBlocks = JSON.parse(request('GET', config.node + '/blocks/seq/' + currentStartBlock + '/' + config.endBlock, {
'headers': {
'Connection': 'keep-alive'
}
}).getBody('utf8'));
}
currentBlocks.forEach(function(block) {
if (block.height <= config.endBlock) {
blocks.push(block);
}
});
if (currentStartBlock + 100 < config.endBlock) {
currentStartBlock += 100;
} else {
currentStartBlock = config.endBlock;
}
}
return blocks;
};
/**
* This method distributes either Waves fees and MRT to the active leasers for
* the given block.
*
* @param activeLeases active leases for the block in question
* @param amountTotalLeased total amount of leased waves in this particular block
* @param block the block to consider
*/
var distribute = function(activeLeases, amountTotalLeased, block) {
var fee = block.wavesFees;
var merfee = block.merFees;
var rbxfee = block.rbxFees;
var assetFees = block.assetFees;
for (var address in activeLeases) {
var share = (activeLeases[address] / amountTotalLeased)
var amount = fee * share;
var meramount = merfee * share;
var rbxamount = rbxfee * share;
var assetamounts = [];
for(var i in assetFees)
{
assetamounts[i]=assetFees[i] * share;
}
var amountMRT = share * config.distributableMrtPerBlock;
if (address in payments) {
payments[address] += amount * (config.percentageOfFeesToDistribute / 100);
mrt[address] += amountMRT;
merfees[address] += meramount * (config.percentageOfFeesToDistribute / 100);
rbxfees[address] += rbxamount * (config.percentageOfFeesToDistribute / 100);
allfees[address]=assetamounts; //toch 100% hier loop door assets
} else {
payments[address] = amount * (config.percentageOfFeesToDistribute / 100);
mrt[address] = amountMRT;
merfees[address] = meramount * (config.percentageOfFeesToDistribute / 100);
rbxfees[address] = rbxamount * (config.percentageOfFeesToDistribute / 100);
allfees[address]=assetamounts; //toch 100%
}
console.log(address + ' will receive ' + amount + ' of(' + fee + ') and Mer amount: ' + meramount + ' (' + merfee + ') and ' + amountMRT + ' MRT for block: ' + block.height + ' share: ' + share);
}
};
/**
* Method that creates the concrete payment tx and writes it to the file
* configured in the config section.
*/
var pay = function() {
var transactions = [];
var totalMRT = 0;
var totalfees =0;
var totalmerfees=0;
var totalrbxfees=0;
var totalassetsfees=[];
var html = "";
var html = "<!DOCTYPE html>" +
"<html lang=\"en\">" +
"<head>" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" +
" <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">" +
" <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script>" +
" <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>" +
"</head>" +
"<body>" +
"<div class=\"container\">" +
" <h3>Fee's between blocks " + config.startBlockHeight + " - " + config.endBlock + ", Payout #" + config.paymentid + "</h3>" +
" <h4>(LPOS address: " + config.address + ")</h4>" +
" <h5>29-06-2017: Hi all, again a short update of the fee's earned by the bearwaves node. Automated distribution, riding on BearWaves $BEAR. Cheers!</h5> " +
" <h5>You can always contact us by <a href=\"mailto:bearwaves@outlook.com\">E-mail</a> or in Waves Slack @w0utje <img src=\"https://www.bearwaves.nl/wp-content/uploads/2017/08/banksy.jpg\" style=\"width:50px;height:50px;\"></h5>" +
" <h5>Blocks forged: " + BlockCount + "</h5>" +
" <table class=\"table table-striped table-hover\">" +
" <thead> " +
" <tr>" +
" <th>Address</th>" +
" <th>Waves</th>" +
" <th>MRT</th>" +
" <th>Mercury</th>" +
" <th>Ripto Bux</th>" +
" </tr>" +
" </thead>" +
" <tbody>";
for (var address in payments) {
var payment = (payments[address] / Math.pow(10, 8));
console.log(address + ' will receive ' + parseFloat(payment).toFixed(8) + ' and ' + parseFloat(mrt[address]).toFixed(2) + ' MRT and ' + parseFloat(merfees[address]).toFixed(8) + ' Mercury!');
//send Waves fee
if (Number(Math.round(payments[address])) > 0) {
transactions.push({
"amount": Number(Math.round(payments[address])),
"fee": 1, //bearwaves 0.01
"feeAssetId": "9gnc5UCY6RxtSi9FEJkcD57r5NBgdr45DVYtunyDLrgC",
"sender": config.address,
"attachment": "DVCsMf2Av2pvvM8GNzzP1tQKZtd4jWfcHJQj9bky32RR6janfLK2",
"recipient": address
});
}
//send MRT
if (Number(Math.round(mrt[address] * Math.pow(10, 2))) > 0) {
transactions.push({
"amount": Number(Math.round(mrt[address] * Math.pow(10, 2))),
"fee": 1, //bearwaves 0.01
"feeAssetId": "9gnc5UCY6RxtSi9FEJkcD57r5NBgdr45DVYtunyDLrgC",
"assetId": "4uK8i4ThRGbehENwa6MxyLtxAjAo1Rj9fduborGExarC",
"sender": config.address,
"attachment": "DVCsMf2Av2pvvM8GNzzP1tQKZtd4jWfcHJQj9bky32RR6janfLK2",
"recipient": address
});
}
//send mercury fee
if (Number(Math.round(merfees[address])) > 0) {
transactions.push({
"amount": Number(Math.round(merfees[address])),
"fee": 1, //bearwaves 0.01
"feeAssetId": "9gnc5UCY6RxtSi9FEJkcD57r5NBgdr45DVYtunyDLrgC",
"assetId": "HzfaJp8YQWLvQG4FkUxq2Q7iYWMYQ2k8UF89vVJAjWPj",
"sender": config.address,
"attachment": "DVCsMf2Av2pvvM8GNzzP1tQKZtd4jWfcHJQj9bky32RR6janfLK2",
"recipient": address
});
}
//this will send one BearWaves token to every leaser
transactions.push({
"amount": 100,
"fee": 1, //bearwaves 0.01
"feeAssetId": "9gnc5UCY6RxtSi9FEJkcD57r5NBgdr45DVYtunyDLrgC",
"assetId": "9gnc5UCY6RxtSi9FEJkcD57r5NBgdr45DVYtunyDLrgC",
"sender": config.address,
"attachment": "DVCsMf2Av2pvvM8GNzzP1tQKZtd4jWfcHJQj9bky32RR6janfLK2",
"recipient": address
});
totalMRT += mrt[address];
totalfees += payments[address];
totalmerfees += merfees[address];
totalrbxfees += rbxfees[address];
for (var i in allfees[address])
{
totalassetsfees[i] += allfees[address][i];
}
//html += "<tr><td>" + address + "</td><td>" + ((payments[address]/100000000).toPrecision(8) - 0.002) + "</td><td>" + (merfees[address]/100000000).toPrecision(8) + "</td><td>" + mrt[address].toPrecision(8) + "</td><td>" + (upfees[address]/100000000) + "</td></tr>\r\n";
html += "<tr><td>" + address + "</td><td>" + //address column
((payments[address]/100000000).toFixed(8)) + "</td><td>" + //Waves fee's
mrt[address].toFixed(2) + "</td><td>" + //MRT
(merfees[address]/100000000).toFixed(8) + "</td><td>" + //Mercury fee's
(rbxfees[address]/100000000).toFixed(8) + "</td></tr>" + //Ripto Bux fee's
"\r\n";
}
html += "<tr><td><b>Total</b></td><td><b>" + ((totalfees/100000000).toFixed(8)) +
"</b></td><td><b>" + totalMRT.toFixed(2) + "</b></td><td><b>" +
(totalmerfees/100000000).toFixed(8) + "</b></td><td><b>" +
(totalrbxfees/100000000).toFixed(8) + "</b></td></tr>" +
"\r\n";
html += "</tbody>" +
" </table>" +
"</div>" +
"</body>" +
"</html>";
console.log("total fees: " + (totalfees/100000000) + " total MRT: " + totalMRT + " total Mer: " + (totalmerfees/100000000) + " total Up: " + (totalrbxfees/100000000) );
console.log(totalassetsfees);
var paymentfile = config.filename + config.paymentid + ".json";
var htmlfile = config.filename + config.paymentid + ".html";
fs.writeFile(paymentfile, JSON.stringify(transactions), {}, function(err) {
if (!err) {
console.log('payments written to ' + paymentfile + '!');
} else {
console.log(err);
}
});
fs.writeFile(htmlfile, html, {}, function(err) {
if (!err) {
console.log('html written!');
} else {
console.log(err);
}
});
var latestblockinfo = {};
latestblockinfo["leases"]=myLeases;
latestblockinfo["canceledleases"]=myCanceledLeases;
var blockleases = config.endBlock + "_" + config.address + ".json" ;
fs.writeFile(blockleases, JSON.stringify(latestblockinfo), {}, function(err) {
if (!err) {
console.log('leaseinfo written to ' + blockleases + '!');
} else {
console.log(err);
}
});
var ActiveLeaseData = getActiveLeasesAtBlock(LastBlock);
fs.writeFile("LastBlockLeasers.json", JSON.stringify(ActiveLeaseData), {}, function(err) {
if (!err) {
console.log('ActiveLeasers written to LastBlockLeasers.json!');
} else {
console.log(err);
}
});
};
/**
* This method returns (block-exact) the active leases and the total amount
* of leased Waves for a given block.
*
* @param block the block to consider
* @returns {{totalLeased: number, activeLeases: {}}} total amount of leased waves and active leases for the given block
*/
var getActiveLeasesAtBlock = function(block) {
var activeLeases = [];
var totalLeased = 0;
var activeLeasesPerAddress = {};
for (var leaseId in myLeases) {
var currentLease = myLeases[leaseId];
if (!myCanceledLeases[leaseId] || myCanceledLeases[leaseId].block > block.height) {
activeLeases.push(currentLease);
}
}
activeLeases.forEach(function (lease) {
if (block.height > lease.block + 1000) {
if (!activeLeasesPerAddress[lease.sender]) {
activeLeasesPerAddress[lease.sender] = lease.amount;
} else {
activeLeasesPerAddress[lease.sender] += lease.amount;
}
totalLeased += lease.amount;
}
});
return { totalLeased: totalLeased, activeLeases: activeLeasesPerAddress };
};
var InsertOrUpdateArray = function(MyArray,Key,Value)
{
if(Key in MyArray)
{
MyArray[Key] += Value;
} else
{
MyArray[Key] = Value;
}
return MyArray;
};
start();