-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathappFastNG.js
323 lines (291 loc) · 11.1 KB
/
appFastNG.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//var request = require('sync-request');
var request = require('request');
var LineReaderSync = require("line-reader-sync")
var fs = require('fs');
/**
* Put your settings here:
* - address: the address of your node that you want to distribute from
* - alias: the alias of the node address
* - startBlockHeight: the block from which you want to start distribution for
* - endBlock: the block until you want to distribute the earnings
* - firstBlockWithLeases: the block where you received the first lease
* - distributableMRTPerBlock: amount of MRT distributed per forged block
* - filename: file to which the payments for the mass payment tool are written
* - node: address of your node in the form http://<ip>:<port
* - percentageOfFeesToDistribute: the percentage of Waves fees that you want to distribute
* - blockStorage: file for storing block history
*/
var config = {
address: '',
alias: '',
startBlockHeight: 462000,
endBlock: 462324,
firstBlockWithLeases: 462000,
distributableMrtPerBlock: 0,
filename: 'payments.json',
node: '',
percentageOfFeesToDistribute: 90,
blockStorage: 'blocks.json'
};
var payments = [];
var mrt = [];
var myLeases = {};
var myCanceledLeases = {};
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.
*/
async function start() {
console.log('getting blocks...');
var blocks = await getAllBlocks();
if (fs.existsSync(config.blockStorage)) {
fs.unlinkSync(config.blockStorage);
}
console.log('preparing datastructures...');
prepareDataStructure(blocks);
blocks.forEach(function(block) {
var transactions = [];
if (block.height < config.startBlockHeight) {
block.transactions.forEach(function(tx) {
if (tx.type === 8 || tx.type === 9) {
transactions.push(tx);
}
});
} else {
transactions = block.transactions;
}
var blockInfo = {
height: block.height,
generator: block.generator,
wavesFees: block.wavesFees,
previousBlockWavesFees: block.previousBlockWavesFees,
transactions: transactions
};
if (block.height >= 1740000) {
blockInfo.reward = block.reward;
}
fs.appendFileSync(config.blockStorage, JSON.stringify(blockInfo) + '\n');
});
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);
}
});
pay();
};
/**
* 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) {
var previousBlock;
blocks.forEach(function(block) {
var wavesFees = 0;
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.recipient === "address:" + config.address || transaction.recipient === 'alias:W:' + config.alias)) {
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 < 10 * Math.pow(10, 8)) {
wavesFees += transaction.fee;
}
} else if (block.height > 1090000 && (transaction.type === 4 || transaction.type === 16)) {
wavesFees += 100000;
}
});
if (previousBlock) {
block.previousBlockWavesFees = previousBlock.wavesFees;
}
block.wavesFees = wavesFees;
previousBlock = block;
});
};
async function getBlocks(from, to) {
return new Promise(function(resolve, reject){
request(config.node + '/blocks/seq/' + from + '/' + to, function (error, response, body) {
// in addition to parsing the value, deal with possible errors
if (error) return reject(error);
try {
// JSON.parse() can throw an exception if not valid JSON
resolve(JSON.parse(body));
} catch(e) {
reject(e);
}
});
});
};
/**
* Method that returns all relevant blocks.
*
* @returns {Array} all relevant blocks
*/
async function getAllBlocks() {
// leases have been resetted in block 462000, therefore, this is the first relevant block to be considered
var firstBlockWithLeases = config.firstBlockWithLeases;
var currentStartBlock = firstBlockWithLeases;
var blocks = [];
var steps = 100;
if (fs.existsSync(config.blockStorage)) {
lrs = new LineReaderSync(config.blockStorage);
var lineFound = true;
while(lineFound){
var line = lrs.readline()
if(line){
blocks.push(JSON.parse(line));
} else {
lineFound = false;
}
}
currentStartBlock = blocks[blocks.length - 1].height + 1;
console.log('retrieved blocks from ' + blocks[0].height + ' to ' + (currentStartBlock - 1));
}
while (currentStartBlock < config.endBlock) {
var currentBlocks;
if (currentStartBlock + (steps - 1) < config.endBlock) {
console.log('getting blocks from ' + currentStartBlock + ' to ' + (currentStartBlock + (steps - 1)));
//var res = request('GET', config.node + '/blocks/seq/' + currentStartBlock + '/' + (currentStartBlock + (steps - 1)));
/*, {
//'headers': {
// 'Connection': 'keep-alive'
//}
});*/
var blocksJSON = await getBlocks(currentStartBlock, currentStartBlock + (steps - 1));
currentBlocks = blocksJSON;
} else {
console.log('getting blocks from ' + currentStartBlock + ' to ' + config.endBlock);
currentBlocks = await getBlocks(currentStartBlock, config.endBlock);
/*currentBlocks = JSON.parse(request('GET', config.node + '/blocks/seq/' + currentStartBlock + '/' + config.endBlock, {
'headers': {
'Connection': 'keep-alive'
}
}).getBody('utf8'));*/
}
if (currentBlocks.length > 0) {
currentBlocks.forEach(function(block) {
if (block.height <= config.endBlock) {
blocks.push(block);
}
});
if (currentStartBlock + steps < config.endBlock) {
currentStartBlock += steps;
} 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, previousBlock) {
var fee;
if (block.height >= 1740000) {
fee = block.wavesFees * 0.4 + block.previousBlockWavesFees * 0.6 + block.reward;
} else if (block.height >= 805000) {
fee = block.wavesFees * 0.4 + block.previousBlockWavesFees * 0.6;
} else {
fee = block.wavesFees
}
for (var address in activeLeases) {
var share = (activeLeases[address] / amountTotalLeased)
var amount = fee * share;
var amountMRT = share * config.distributableMrtPerBlock;
if (payments[address]) {
payments[address] += amount * (config.percentageOfFeesToDistribute / 100);
mrt[address] += amountMRT;
} else {
payments[address] = amount * (config.percentageOfFeesToDistribute / 100);
mrt[address] = amountMRT;
}
}
};
/**
* Method that creates the concrete payment tx and writes it to the file
* configured in the config section.
*/
var pay = function() {
var transactions = [];
for (var address in payments) {
var payment = (payments[address] / Math.pow(10, 8));
if (payment > 0) {
transactions.push({
"amount": Number(Math.round(payments[address])),
"fee": 100000,
"sender": config.address,
"attachment": "",
"recipient": address
});
}
if (mrt[address] > 0) {
transactions.push({
"amount": Number(Math.round(mrt[address] * Math.pow(10, 2))),
"fee": 100000,
"assetId": "4uK8i4ThRGbehENwa6MxyLtxAjAo1Rj9fduborGExarC",
"sender": config.address,
"attachment": "",
"recipient": address
});
}
}
fs.writeFile(config.filename, JSON.stringify(transactions), {}, function(err) {
if (!err) {
console.log('payments written to ' + config.filename + '!');
} 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 };
};
start();