-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
executable file
·102 lines (88 loc) · 2.6 KB
/
helpers.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
const { massTransfer } = require('@waves/waves-transactions');
const AttachmentEnum = {
referral: 'sp',
direct: '',
};
const TRANSACTION_TYPE = {
ISSUE: 3,
TRANSFER: 4,
REISSUE: 5,
BURN: 6,
EXCHANGE: 7,
LEASE: 8,
CANCEL_LEASE: 9,
ALIAS: 10,
MASS_TRANSFER: 11,
DATA: 12,
SET_SCRIPT: 13,
SPONSORSHIP: 14,
SET_ASSET_SCRIPT: 15,
INVOKE_SCRIPT: 16,
};
function calculateRewardForHeightRange(
totalProfit,
currentHeight,
lastPaymentHeight,
balancesForHeight
) {
const blocksDiff = currentHeight - lastPaymentHeight;
const profitPerBlock = totalProfit / blocksDiff;
const rewards = {};
for (let i = 1; i <= blocksDiff; i++) {
const paymentHeight = currentHeight + i;
const balances = balancesForHeight[paymentHeight];
let totalBalance = Object.keys(balances).reduce(
(acc, address) => acc + balances[address],
0
);
for (let address in Object.keys(balances)) {
const amount = balances[address];
const share = amount / totalBalance;
const rewardOnBlock = share * profitPerBlock;
if (!rewards[address]) {
rewards[address] = rewardOnBlock;
} else {
rewards[address] += rewardOnBlock;
}
}
}
}
function createMassRewardTXs(balances, { attachment, assetId, seed }) {
const transfers = [];
// let total = 0;
for (const address in balances) {
const value = balances[address];
const roundedValue = Math.round(value);
if (roundedValue > 0) {
// total += roundedValue;
transfers.push({ amount: roundedValue, recipient: address });
}
}
const paymentsCount = 100;
const maxRewardTXsCount = Math.ceil(transfers.length / 100);
const rewardTXs = Array(maxRewardTXsCount);
const lenTransfers = transfers.length;
for (let i = 0; i < lenTransfers; i += paymentsCount) {
let endIndex = i + paymentsCount;
const isEndIndexBigger = endIndex > lenTransfers;
if (isEndIndexBigger) {
endIndex = lenTransfers;
}
const currentTransfers = transfers.slice(i, endIndex);
const rewardTx = massTransfer({
type: TRANSACTION_TYPE.MASS_TRANSFER,
transfers: currentTransfers,
attachment,
assetId,
});
rewardTXs.push(rewardTx);
if (isEndIndexBigger) {
break;
}
}
}
module.exports = {
calculateRewardForHeightRange,
createMassRewardTXs,
AttachmentEnum,
};