-
Notifications
You must be signed in to change notification settings - Fork 101
/
ParaMetrics.java
279 lines (225 loc) · 9.32 KB
/
ParaMetrics.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prizm;
import java.io.Serializable;
import java.math.BigInteger;
/**
*
* @author zoi
*/
public class ParaMetrics implements Serializable {
public static long getParataxPercent(long genesisAmount) {
long ams = (Math.abs(genesisAmount) * 100L) / Math.abs(ParaEngine.MAXIMUM_PARAMINING_AMOUNT);
if (ams > Constants.MAX_PARATAX_PERCENT) return Constants.MAX_PARATAX_PERCENT;
if (ams < 0) return 0;
return ams;
}
public static void setParataxPercent(ParaMetrics paraMetrics, int height, long genesisAmount, boolean isOnHold) {
paraMetrics.setGenesisEmission(genesisAmount);
if (height >= Constants.ENABLE_COMPOUND_AND_2X_PARATAX) {
long percent = getParataxPercent(genesisAmount) * 2;
if (isOnHold) {
if (percent > Constants.MAX_PARATAX_PERCENT_ON_HOLD) percent = Constants.MAX_PARATAX_PERCENT_ON_HOLD;
} else {
if (percent > Constants.MAX_PARATAX_PERCENT) percent = Constants.MAX_PARATAX_PERCENT;
}
paraMetrics.setParaTax(percent);
return;
}
paraMetrics.setParaTax(getParataxPercent(genesisAmount));
}
public static long getPercentAmount(long amount, long percent) {
if (isOverflow(amount, percent)) {
BigInteger bigAmount = BigInteger.valueOf(amount);
BigInteger bigPercent = BigInteger.valueOf(percent);
try {
return bigAmount.multiply(bigPercent).divide(BigInteger.valueOf(100L)).longValueExact();
} catch (ArithmeticException e) { // if bigAmount > Long.MAX_VALUE
return (Long.MAX_VALUE / 100L) * percent;
}
}
return (amount * percent) / 100l;
}
public static long getAmountMinusPercent(long amount, long percent) {
return amount - getPercentAmount(amount, percent);
}
private static final double ORDINARY_DIVIDER = 86400d;
private static final double COMPOUND_DIVIDER = 50d;
private long balance = 0l;
private long amount = 0l;
private long payout = 0l;
private int beforeStamp = 0;
private int afterStamp = 0;
private double multiplier = 0;
private long paraTax = 0l;
private long paraTaxAmount = 0l;
private long genesisEmission = 0l;
private int lastForgedBlockHeight = 0;
private long hold = 0l;
public boolean calculateOrdinaryInterest() {
double multi = 1d;
double percent = 0d;
if (balance>=100l && balance<=9999l) percent = 0.12d;
if (balance>=10000l && balance<=99999l) percent = 0.14d;
if (balance>=100000l && balance<=999999l) percent = 0.18d;
if (balance>=1000000l && balance<=4999999l) percent = 0.21d;
if (balance>=5000000l && balance<=9999999l) percent = 0.25d;
if (balance>=10000000l && balance<=49999999l) percent = 0.28d;
if (balance>=50000000l && balance<100000000l) percent = 0.33d;
if (amount>=100000l && amount<=999999l) multi = 2.18d;
if (amount>=1000000l && amount<=9999999l) multi = 2.36d;
if (amount>=10000000l && amount<=99999999l) multi = 2.77d;
if (amount>=100000000l && amount<=999999999l) multi = 3.05d;
if (amount>=1000000000l && amount<=9999999999l) multi = 3.36d;
if (amount>=10000000000l && amount<=99999999999l) multi = 3.88d;
if (amount>=100000000000l) multi = 4.37d;
this.multiplier = (multi * percent) / 100d;
double days = (afterStamp - beforeStamp) / ORDINARY_DIVIDER;
payout = (long) Math.floor((double) balance * (days * this.multiplier));
// ParaTax calculation
if (paraTax > 0) {
paraTaxAmount = getPercentAmount(payout, paraTax);
payout = getAmountMinusPercent(payout, paraTax);
}
if (payout < 0) payout = 0;
return true;
}
public void decreaseAmountByGenesis() {
double mtx = ((double)Math.abs(genesisEmission)/(double)Math.abs(ParaEngine.MAXIMUM_PARAMINING_AMOUNT));
if (mtx>1d) mtx = 1d;
mtx = 1 - mtx;
if (mtx < 0.1) mtx = 0.1;
payout *= mtx;
}
public boolean calculateCompoundInterest(boolean isOnHold) {
setParataxPercent(this, 0, this.genesisEmission, isOnHold);
this.calculateOrdinaryInterest();
long ordinaryPayout = this.payout;
setParataxPercent(this, Constants.ENABLE_COMPOUND_AND_2X_PARATAX, this.genesisEmission, isOnHold);
this.calculateCompoundInterestInternal();
long compoundPayout = this.payout;
if (ordinaryPayout < compoundPayout) {
setParataxPercent(this, 0, this.genesisEmission, isOnHold);
this.calculateOrdinaryInterest();
}
decreaseAmountByGenesis();
if (payout > Constants.MAX_BALANCE_AFTER_PARAMINING_PAYOUT_NQT)
this.payout = Constants.MAX_BALANCE_AFTER_PARAMINING_PAYOUT_NQT;
if (payout > 0 && this.payout + this.balance > Constants.MAX_BALANCE_AFTER_PARAMINING_PAYOUT_NQT) {
this.payout = Constants.MAX_BALANCE_AFTER_PARAMINING_PAYOUT_NQT - this.balance;
}
return true;
}
private boolean calculateCompoundInterestInternal() {
double multi = 1d;
double percent = 0d;
if (balance>=100l && balance<=9999l) percent = 0.12d;
if (balance>=10000l && balance<=99999l) percent = 0.14d;
if (balance>=100000l && balance<=999999l) percent = 0.18d;
if (balance>=1000000l && balance<=4999999l) percent = 0.21d;
if (balance>=5000000l && balance<=9999999l) percent = 0.25d;
if (balance>=10000000l && balance<=49999999l) percent = 0.28d;
if (balance>=50000000l && balance<100000000l) percent = 0.33d;
if (amount>=100000l && amount<=999999l) multi = 2.18d;
if (amount>=1000000l && amount<=9999999l) multi = 2.36d;
if (amount>=10000000l && amount<=99999999l) multi = 2.77d;
if (amount>=100000000l && amount<=999999999l) multi = 3.05d;
if (amount>=1000000000l && amount<=9999999999l) multi = 3.36d;
if (amount>=10000000000l && amount<=99999999999l) multi = 3.88d;
if (amount>=100000000000l) multi = 4.37d;
this.multiplier = ((multi * percent) / 100d) / (ORDINARY_DIVIDER/COMPOUND_DIVIDER);
double periods = (afterStamp - beforeStamp) / COMPOUND_DIVIDER;
payout = (long) (Math.floor((double) balance * Math.pow(1d + this.multiplier, periods)) - balance);
// ParaTax calculation
if (paraTax > 0) {
paraTaxAmount = getPercentAmount(payout, paraTax);
payout = getAmountMinusPercent(payout, paraTax);
}
if (payout < 0) payout = 0;
return true;
}
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
this.balance = balance;
}
public long getAmount() {
return amount;
}
public void setAmount(long amount) {
this.amount = amount;
}
public long getPayout() {
return payout;
}
public int getBeforeStamp() {
return beforeStamp;
}
public void setBeforeStamp(int beforeStamp) {
this.beforeStamp = beforeStamp;
}
public int getAfterStamp() {
return afterStamp;
}
public void setAfterStamp(int afterStamp) {
this.afterStamp = afterStamp;
}
public double getMultiplier() {
return multiplier;
}
public void setPayout(long payout) {
this.payout = payout;
}
public void setMultiplier(double multiplier) {
this.multiplier = multiplier;
}
public long getParaTax() {
return paraTax;
}
public void setParaTax(long paraTax) {
this.paraTax = paraTax;
}
public long getParaTaxAmount() {
return paraTaxAmount;
}
public void setParaTaxAmount(long paraTaxAmount) {
this.paraTaxAmount = paraTaxAmount;
}
public boolean isParaTaxed() {
return paraTax > 0;
}
public int getLastForgedBlockHeight() {
return lastForgedBlockHeight;
}
public void setLastForgedBlockHeight(int lastForgedBlockHeight) {
this.lastForgedBlockHeight = lastForgedBlockHeight;
}
public long getHold() {
return hold;
}
public void setHold(long hold) {
this.hold = hold;
}
public boolean isOnHoldAtHeight(int height) {
return getLastForgedBlockHeight() >= height - Constants.HOLD_RANGE
&& balance >= Constants.HOLD_BALANCE_MIN
&& balance <= Constants.HOLD_BALANCE_MAX;
}
private void setGenesisEmission(long genesisEmission) {
this.genesisEmission = genesisEmission;
}
@Override
public String toString() {
return "ParaMetrics{" + "balance=" + balance + ", amount=" + amount + ", payout=" + payout + ", beforeStamp=" + beforeStamp + ", afterStamp=" + afterStamp + ", multiplier=" + multiplier + ", paraTax=" + paraTax + ", paraTaxAmount=" + paraTaxAmount + '}';
}
public static boolean isOverflow( long a, long b) {
if (a == 0 || b == 0)
return false;
long result = a * b;
return a != result / b;
}
}