1 | 1 | | {-# STDLIB_VERSION 6 #-} |
---|
2 | 2 | | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | 3 | | {-# CONTENT_TYPE DAPP #-} |
---|
4 | 4 | | let SEP = "__" |
---|
5 | 5 | | |
---|
6 | 6 | | let SCALE = 1000 |
---|
7 | 7 | | |
---|
8 | 8 | | let MULT8 = 100000000 |
---|
9 | 9 | | |
---|
10 | 10 | | let zeroBigInt = toBigInt(0) |
---|
11 | 11 | | |
---|
12 | 12 | | let processingStageTotal = 0 |
---|
13 | 13 | | |
---|
14 | 14 | | let processingStageShares = 1 |
---|
15 | 15 | | |
---|
16 | 16 | | func getNumberByKey (key) = valueOrElse(getInteger(this, key), 0) |
---|
17 | 17 | | |
---|
18 | 18 | | |
---|
19 | 19 | | func getNumberOrFail (key) = valueOrErrorMessage(getInteger(this, key), (("mandatory this." + key) + " is not defined")) |
---|
20 | 20 | | |
---|
21 | 21 | | |
---|
22 | 22 | | func getStringByKey (key) = valueOrElse(getString(this, key), "") |
---|
23 | 23 | | |
---|
24 | 24 | | |
---|
25 | 25 | | func getStringOrFail (key) = valueOrErrorMessage(getString(this, key), (("mandatory this." + key) + " is not defined")) |
---|
26 | 26 | | |
---|
27 | 27 | | |
---|
28 | 28 | | func abs (val) = if ((0 > val)) |
---|
29 | 29 | | then -(val) |
---|
30 | 30 | | else val |
---|
31 | 31 | | |
---|
32 | 32 | | |
---|
33 | 33 | | func absBigInt (val) = if ((zeroBigInt > val)) |
---|
34 | 34 | | then -(val) |
---|
35 | 35 | | else val |
---|
36 | 36 | | |
---|
37 | 37 | | |
---|
38 | 38 | | let keyMaxDepth = "%s__maxDepth" |
---|
39 | 39 | | |
---|
40 | 40 | | let maxDepthDefault = 30 |
---|
41 | 41 | | |
---|
42 | 42 | | let maxDepth = valueOrElse(getInteger(this, keyMaxDepth), maxDepthDefault) |
---|
43 | 43 | | |
---|
44 | 44 | | func keyFactoryAddress () = "%s%s__config__factoryAddress" |
---|
45 | 45 | | |
---|
46 | 46 | | |
---|
47 | 47 | | let factoryAddressStr = getStringOrFail(keyFactoryAddress()) |
---|
48 | 48 | | |
---|
49 | 49 | | let factoryContract = addressFromStringValue(factoryAddressStr) |
---|
50 | 50 | | |
---|
51 | 51 | | func keyEmissionAddress () = "%s%s__config__emissionAddress" |
---|
52 | 52 | | |
---|
53 | 53 | | |
---|
54 | 54 | | func keyNumToUserMapping (num) = makeString(["%s%s%s", "mapping", "num2user", toString(num)], SEP) |
---|
55 | 55 | | |
---|
56 | 56 | | |
---|
57 | 57 | | let keyReferralProgramName = makeString(["%s%s", "referral", "programName"], SEP) |
---|
58 | 58 | | |
---|
59 | 59 | | let referralProgramNameDefault = "wxlock" |
---|
60 | 60 | | |
---|
61 | 61 | | let referralProgramName = valueOrElse(getString(this, keyReferralProgramName), referralProgramNameDefault) |
---|
62 | 62 | | |
---|
63 | 63 | | let keyReferralMinGWxAmount = makeString(["%s%s", "referral", "minGWxAmount"], SEP) |
---|
64 | 64 | | |
---|
65 | 65 | | let referralMinGWxAmountDefault = (500 * MULT8) |
---|
66 | 66 | | |
---|
67 | 67 | | let referralMinGWxAmount = valueOrElse(getInteger(this, keyReferralMinGWxAmount), referralMinGWxAmountDefault) |
---|
68 | 68 | | |
---|
69 | 69 | | let keyReferrerRewardPermille = makeString(["%s%s", "referral", "referrerRewardPermille"], SEP) |
---|
70 | 70 | | |
---|
71 | 71 | | let referrerRewardPermilleDefault = 50 |
---|
72 | 72 | | |
---|
73 | 73 | | let referrerRewardPermille = valueOrElse(getInteger(this, keyReferrerRewardPermille), referrerRewardPermilleDefault) |
---|
74 | 74 | | |
---|
75 | 75 | | let keyReferralRewardPermille = makeString(["%s%s", "referral", "referralRewardPermille"], SEP) |
---|
76 | 76 | | |
---|
77 | 77 | | let referralRewardPermilleDefault = 50 |
---|
78 | 78 | | |
---|
79 | 79 | | let referralRewardPermille = valueOrElse(getInteger(this, keyReferralRewardPermille), referralRewardPermilleDefault) |
---|
80 | 80 | | |
---|
81 | 81 | | func keyReferrer (referralAddress) = makeString(["%s%s%s", "referrer", referralProgramName, referralAddress], SEP) |
---|
82 | 82 | | |
---|
83 | 83 | | |
---|
84 | 84 | | func keyUnclaimedReferral (programName,claimerAddress) = makeString(["%s%s%s%s", "unclaimedReferral", programName, claimerAddress], SEP) |
---|
85 | 85 | | |
---|
86 | 86 | | |
---|
87 | 87 | | let emissionAddressStr = getStringOrFail(keyEmissionAddress()) |
---|
88 | 88 | | |
---|
89 | 89 | | let emissionContract = addressFromStringValue(emissionAddressStr) |
---|
90 | 90 | | |
---|
91 | 91 | | let IdxCfgAssetId = 1 |
---|
92 | 92 | | |
---|
93 | 93 | | let IdxCfgPacemakerAddress = 2 |
---|
94 | 94 | | |
---|
95 | 95 | | let IdxCfgBoostingContract = 3 |
---|
96 | 96 | | |
---|
97 | 97 | | let IdxCfgMaxDepth = 4 |
---|
98 | 98 | | |
---|
99 | 99 | | func keyConfig () = "%s__config" |
---|
100 | 100 | | |
---|
101 | 101 | | |
---|
102 | 102 | | func readConfigArrayOrFail () = split(getStringOrFail(keyConfig()), SEP) |
---|
103 | 103 | | |
---|
104 | 104 | | |
---|
105 | 105 | | func formatConfig (wxAssetIdStr,matcherPacemakerAddressStr,boostingContractAddressStr,maxDepth) = makeString(["%s%s%s%d", wxAssetIdStr, matcherPacemakerAddressStr, boostingContractAddressStr, toString(maxDepth)], SEP) |
---|
106 | 106 | | |
---|
107 | 107 | | |
---|
108 | 108 | | func boostingContractOrFail () = { |
---|
109 | 109 | | let cfgArray = readConfigArrayOrFail() |
---|
110 | 110 | | valueOrErrorMessage(addressFromString(cfgArray[IdxCfgBoostingContract]), "boosting contract address is not defined") |
---|
111 | 111 | | } |
---|
112 | 112 | | |
---|
113 | 113 | | |
---|
114 | 114 | | func keyGwxRewardEmissionStartHeight () = "%s%s__gwxRewardEmissionPart__startHeight" |
---|
115 | 115 | | |
---|
116 | 116 | | |
---|
117 | 117 | | func keyUsersCount () = "%s__nextUserNum" |
---|
118 | 118 | | |
---|
119 | 119 | | |
---|
120 | 120 | | func keyRatePerBlockCurrent () = "%s%s__ratePerBlock__current" |
---|
121 | 121 | | |
---|
122 | 122 | | |
---|
123 | 123 | | func keyPoolWeightVirtual () = "%s%s__poolWeight__GWXvirtualPOOL" |
---|
124 | 124 | | |
---|
125 | 125 | | |
---|
126 | 126 | | func keyNextProcessedUser () = "%s__nextProcessedUser" |
---|
127 | 127 | | |
---|
128 | 128 | | |
---|
129 | 129 | | func keyLatestPeriod () = "%s__latestPeriod" |
---|
130 | 130 | | |
---|
131 | 131 | | |
---|
132 | 132 | | func keyNextPeriod () = "%s__nextPeriod" |
---|
133 | 133 | | |
---|
134 | 134 | | |
---|
135 | 135 | | func keyProcessingStage () = "%s__processingStage" |
---|
136 | 136 | | |
---|
137 | 137 | | |
---|
138 | 138 | | func keyNextProcessedPeriod () = "%s__nextProcessedPeriod" |
---|
139 | 139 | | |
---|
140 | 140 | | |
---|
141 | 141 | | func keyUserUnclaimed (userIndex) = makeString(["%s%d", "userUnclaimed", toString(userIndex)], SEP) |
---|
142 | 142 | | |
---|
143 | 143 | | |
---|
144 | 144 | | func keyNextUnlaimedPeriodOfUser (userIndex) = makeString(["%s%d__nextClaimedPeriod", toString(userIndex)], SEP) |
---|
145 | 145 | | |
---|
146 | 146 | | |
---|
147 | 147 | | func keyLastProcessedPeriodOfUser (userIndex) = makeString(["%s%d__lastProcessedPeriod", toString(userIndex)], SEP) |
---|
148 | 148 | | |
---|
149 | 149 | | |
---|
150 | 150 | | func keyHeightForPeriod (period) = makeString(["%s%d__startHeightForPeriod", toString(period)], SEP) |
---|
151 | 151 | | |
---|
152 | 152 | | |
---|
153 | 153 | | func keyAuxEmissionRewardForPeriod (period) = makeString(["%s%d__auxEmissionReward", toString(period)], SEP) |
---|
154 | 154 | | |
---|
155 | 155 | | |
---|
156 | 156 | | func keyTotalAmountForPeriod (period) = makeString(["%s%d__totalAmountForPeriod", toString(period)], SEP) |
---|
157 | 157 | | |
---|
158 | 158 | | |
---|
159 | 159 | | func keyLastPayoutInfo () = "%s__lastPayoutInfo" |
---|
160 | 160 | | |
---|
161 | 161 | | |
---|
162 | 162 | | func PeriodPayoutInfo (period,matcherReward,emissionReward) = makeString(["%d%d%d", toString(period), toString(matcherReward), toString(emissionReward)], SEP) |
---|
163 | 163 | | |
---|
164 | 164 | | |
---|
165 | 165 | | func keyPayoutHistoryInfo (period) = makeString(["%s%s%d__payouts__history", toString(period)], SEP) |
---|
166 | 166 | | |
---|
167 | 167 | | |
---|
168 | 168 | | func keyTotalWeightForPeriod (period) = makeString(["%s%d__totalWeightForPeriod", toString(period)], SEP) |
---|
169 | 169 | | |
---|
170 | 170 | | |
---|
171 | 171 | | func keyUserKValueForPeriod (period,userIndex) = makeString(["%s%d%s%d__paramByPeriod", toString(userIndex), "k", toString(period)], SEP) |
---|
172 | 172 | | |
---|
173 | 173 | | |
---|
174 | 174 | | func keyUserBValueForPeriod (period,userIndex) = makeString(["%s%d%s%d__paramByPeriod", toString(userIndex), "b", toString(period)], SEP) |
---|
175 | 175 | | |
---|
176 | 176 | | |
---|
177 | 177 | | func keyUserWeightForPeriod (period,userIndex) = makeString(["%s%d%s%d__paramByPeriod", toString(userIndex), "weight", toString(period)], SEP) |
---|
178 | 178 | | |
---|
179 | 179 | | |
---|
180 | 180 | | func keyReferralsContractAddress () = makeString(["%s%s", "config", "referralsContractAddress"], SEP) |
---|
181 | 181 | | |
---|
182 | 182 | | |
---|
183 | 183 | | let referralsContractAddressOrFail = addressFromStringValue(getStringOrFail(keyReferralsContractAddress())) |
---|
184 | 184 | | |
---|
185 | 185 | | func HistoryEntry (type,user,amount,i) = { |
---|
186 | 186 | | let historyKEY = makeString(["%s%s%s%s__history", type, user, toBase58String(i.transactionId)], SEP) |
---|
187 | 187 | | let historyDATA = makeString(["%d%d%d%d%d%d", toString(lastBlock.height), toString(lastBlock.timestamp), toString(amount)], SEP) |
---|
188 | 188 | | StringEntry(historyKEY, historyDATA) |
---|
189 | 189 | | } |
---|
190 | 190 | | |
---|
191 | 191 | | |
---|
192 | 192 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
193 | 193 | | |
---|
194 | 194 | | |
---|
195 | 195 | | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" |
---|
196 | 196 | | |
---|
197 | 197 | | |
---|
198 | 198 | | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { |
---|
199 | 199 | | case s: String => |
---|
200 | 200 | | fromBase58String(s) |
---|
201 | 201 | | case _: Unit => |
---|
202 | 202 | | unit |
---|
203 | 203 | | case _ => |
---|
204 | 204 | | throw("Match error") |
---|
205 | 205 | | } |
---|
206 | 206 | | |
---|
207 | 207 | | |
---|
208 | 208 | | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { |
---|
209 | 209 | | case s: String => |
---|
210 | 210 | | fromBase58String(s) |
---|
211 | 211 | | case _: Unit => |
---|
212 | 212 | | unit |
---|
213 | 213 | | case _ => |
---|
214 | 214 | | throw("Match error") |
---|
215 | 215 | | } |
---|
216 | 216 | | |
---|
217 | 217 | | |
---|
218 | 218 | | func mustManager (i) = { |
---|
219 | 219 | | let pd = throw("Permission denied") |
---|
220 | 220 | | match managerPublicKeyOrUnit() { |
---|
221 | 221 | | case pk: ByteVector => |
---|
222 | 222 | | if ((i.callerPublicKey == pk)) |
---|
223 | 223 | | then true |
---|
224 | 224 | | else pd |
---|
225 | 225 | | case _: Unit => |
---|
226 | 226 | | if ((i.caller == this)) |
---|
227 | 227 | | then true |
---|
228 | 228 | | else pd |
---|
229 | 229 | | case _ => |
---|
230 | 230 | | throw("Match error") |
---|
231 | 231 | | } |
---|
232 | 232 | | } |
---|
233 | 233 | | |
---|
234 | 234 | | |
---|
235 | 235 | | func calcUserWeight (boostingContractAddress,heightForPeriod,period,userIndex) = { |
---|
236 | 236 | | let kLast = keyLastProcessedPeriodOfUser(userIndex) |
---|
237 | 237 | | let kKey = keyUserKValueForPeriod(period, userIndex) |
---|
238 | 238 | | let kRaw = getInteger(boostingContractAddress, kKey) |
---|
239 | 239 | | let kUserWeight = keyUserWeightForPeriod(period, userIndex) |
---|
240 | 240 | | if (isDefined(kRaw)) |
---|
241 | 241 | | then { |
---|
242 | 242 | | let k = value(kRaw) |
---|
243 | 243 | | let b = value(getInteger(boostingContractAddress, keyUserBValueForPeriod(period, userIndex))) |
---|
244 | 244 | | let w = ((k * heightForPeriod) + b) |
---|
245 | 245 | | if ((w > 0)) |
---|
246 | 246 | | then $Tuple2((w / SCALE), [IntegerEntry(kLast, period), IntegerEntry(kUserWeight, w)]) |
---|
247 | 247 | | else $Tuple2(0, nil) |
---|
248 | 248 | | } |
---|
249 | 249 | | else { |
---|
250 | 250 | | let p = getInteger(this, kLast) |
---|
251 | 251 | | if (if (isDefined(p)) |
---|
252 | 252 | | then (period >= value(p)) |
---|
253 | 253 | | else false) |
---|
254 | 254 | | then { |
---|
255 | 255 | | let pv = value(p) |
---|
256 | 256 | | let k = value(getInteger(boostingContractAddress, keyUserKValueForPeriod(pv, userIndex))) |
---|
257 | 257 | | let b = value(getInteger(boostingContractAddress, keyUserBValueForPeriod(pv, userIndex))) |
---|
258 | 258 | | let w = ((k * heightForPeriod) + b) |
---|
259 | 259 | | if ((w > 0)) |
---|
260 | 260 | | then $Tuple2((w / SCALE), [IntegerEntry(kUserWeight, w)]) |
---|
261 | 261 | | else $Tuple2(0, nil) |
---|
262 | 262 | | } |
---|
263 | 263 | | else $Tuple2(0, nil) |
---|
264 | 264 | | } |
---|
265 | 265 | | } |
---|
266 | 266 | | |
---|
267 | 267 | | |
---|
268 | 268 | | func calcUserWeightForClaim (boostingContractAddress,heightForPeriod,period,userIndex) = { |
---|
269 | 269 | | let kUserWeight = keyUserWeightForPeriod(period, userIndex) |
---|
270 | 270 | | let userWeightOrUnit = getInteger(kUserWeight) |
---|
271 | 271 | | match userWeightOrUnit { |
---|
272 | 272 | | case _: Unit => |
---|
273 | 273 | | 0 |
---|
274 | 274 | | case w: Int => |
---|
275 | 275 | | (w / SCALE) |
---|
276 | 276 | | case _ => |
---|
277 | 277 | | throw("Match error") |
---|
278 | 278 | | } |
---|
279 | 279 | | } |
---|
280 | 280 | | |
---|
281 | 281 | | |
---|
282 | 282 | | func getUserIndexByAddress (boostingContractAddressStr,userAddress) = { |
---|
283 | 283 | | let key = makeString(["%s%s%s", "mapping", "user2num", userAddress], SEP) |
---|
284 | 284 | | parseIntValue(valueOrErrorMessage(getString(Address(fromBase58String(boostingContractAddressStr)), key), ((("User address " + userAddress) + " is not found in boosting contract data, key=") + key))) |
---|
285 | 285 | | } |
---|
286 | 286 | | |
---|
287 | 287 | | |
---|
288 | 288 | | func nextPeriod () = getNumberByKey(keyNextPeriod()) |
---|
289 | 289 | | |
---|
290 | 290 | | |
---|
291 | 291 | | func commonClaimReward (userAddress) = { |
---|
292 | 292 | | let cfgArray = readConfigArrayOrFail() |
---|
293 | 293 | | let userIdx = getUserIndexByAddress(cfgArray[IdxCfgBoostingContract], userAddress) |
---|
294 | 294 | | let userUnclaimedOption = getInteger(keyUserUnclaimed(userIdx)) |
---|
295 | 295 | | match userUnclaimedOption { |
---|
296 | 296 | | case _: Unit => |
---|
297 | 297 | | $Tuple2(0, nil) |
---|
298 | 298 | | case u: Int => |
---|
299 | 299 | | $Tuple2(u, [IntegerEntry(keyUserUnclaimed(userIdx), 0)]) |
---|
300 | 300 | | case _ => |
---|
301 | 301 | | throw("Match error") |
---|
302 | 302 | | } |
---|
303 | 303 | | } |
---|
304 | 304 | | |
---|
305 | 305 | | |
---|
306 | 306 | | @Callable(i) |
---|
307 | 307 | | func updateReferralActivity (userAddress,gWxAmountStart) = { |
---|
308 | 308 | | let referrer = getString(referralsContractAddressOrFail, keyReferrer(userAddress)) |
---|
309 | 309 | | let activeReferralInv = if ((referrer == unit)) |
---|
310 | 310 | | then unit |
---|
311 | 311 | | else invoke(referralsContractAddressOrFail, "updateReferralActivity", [referralProgramName, userAddress, (gWxAmountStart >= referralMinGWxAmount)], nil) |
---|
312 | 312 | | if ((activeReferralInv == activeReferralInv)) |
---|
313 | 313 | | then $Tuple2(nil, unit) |
---|
314 | 314 | | else throw("Strict value is not equal to itself.") |
---|
315 | 315 | | } |
---|
316 | 316 | | |
---|
317 | 317 | | |
---|
318 | 318 | | |
---|
319 | 319 | | @Callable(i) |
---|
320 | 320 | | func finalizeHelper () = { |
---|
321 | 321 | | let processingStage = valueOrElse(getInteger(keyProcessingStage()), processingStageTotal) |
---|
322 | 322 | | let currentPeriod = getNumberByKey(keyNextProcessedPeriod()) |
---|
323 | 323 | | let currentUser = getNumberByKey(keyNextProcessedUser()) |
---|
324 | 324 | | let latestPeriod = getNumberByKey(keyLatestPeriod()) |
---|
325 | 325 | | let usersCount = valueOrElse(getInteger(boostingContractOrFail(), keyUsersCount()), 0) |
---|
326 | 326 | | let totalWeightKey = keyTotalWeightForPeriod(currentPeriod) |
---|
327 | 327 | | let totalWeight = getNumberByKey(keyTotalWeightForPeriod(currentPeriod)) |
---|
328 | 328 | | let heightForPeriod = getNumberByKey(keyHeightForPeriod(currentPeriod)) |
---|
329 | 329 | | if ((currentPeriod > latestPeriod)) |
---|
330 | 330 | | then $Tuple2(nil, false) |
---|
331 | 331 | | else if ((processingStage == processingStageTotal)) |
---|
332 | 332 | | then { |
---|
333 | 333 | | let $t01208412200 = calcUserWeight(boostingContractOrFail(), heightForPeriod, currentPeriod, currentUser) |
---|
334 | 334 | | let userWeight = $t01208412200._1 |
---|
335 | 335 | | let userActions = $t01208412200._2 |
---|
336 | 336 | | let totalWeightNew = (totalWeight + userWeight) |
---|
337 | 337 | | let processingActions = if (((usersCount - 1) > currentUser)) |
---|
338 | 338 | | then [IntegerEntry(keyNextProcessedUser(), (currentUser + 1))] |
---|
339 | 339 | | else [IntegerEntry(keyProcessingStage(), processingStageShares), IntegerEntry(keyNextProcessedUser(), 0)] |
---|
340 | 340 | | $Tuple2((([IntegerEntry(totalWeightKey, totalWeightNew)] ++ processingActions) ++ userActions), true) |
---|
341 | 341 | | } |
---|
342 | 342 | | else if ((processingStage == processingStageShares)) |
---|
343 | 343 | | then { |
---|
344 | 344 | | let userWeight = calcUserWeightForClaim(boostingContractOrFail(), heightForPeriod, currentPeriod, currentUser) |
---|
345 | 345 | | let userAmountMatcherForPeriod = fraction(getNumberByKey(keyTotalAmountForPeriod(currentPeriod)), userWeight, totalWeight) |
---|
346 | 346 | | let userAmountEmissionForPeriod = fraction(getNumberByKey(keyAuxEmissionRewardForPeriod(currentPeriod)), userWeight, totalWeight) |
---|
347 | 347 | | let userTotalAmount = (userAmountEmissionForPeriod + userAmountMatcherForPeriod) |
---|
348 | 348 | | let userUnclaimedOption = getInteger(keyUserUnclaimed(currentUser)) |
---|
349 | 349 | | let userAddress = getStringValue(boostingContractOrFail(), keyNumToUserMapping(currentUser)) |
---|
350 | 350 | | let referrer = getString(referralsContractAddressOrFail, keyReferrer(userAddress)) |
---|
351 | 351 | | let activeReferralInv = if ((referrer == unit)) |
---|
352 | 352 | | then unit |
---|
353 | 353 | | else invoke(referralsContractAddressOrFail, "updateReferralActivity", [referralProgramName, userAddress, (userWeight >= referralMinGWxAmount)], nil) |
---|
354 | 354 | | if ((activeReferralInv == activeReferralInv)) |
---|
355 | 355 | | then { |
---|
356 | 356 | | let referralInv = if (if ((referrer == unit)) |
---|
357 | 357 | | then true |
---|
358 | 358 | | else (referralMinGWxAmount > userWeight)) |
---|
359 | 359 | | then unit |
---|
360 | 360 | | else { |
---|
361 | 361 | | let referrerReward = fraction(userTotalAmount, referrerRewardPermille, SCALE) |
---|
362 | 362 | | let referralReward = fraction(userTotalAmount, referralRewardPermille, SCALE) |
---|
363 | 363 | | invoke(referralsContractAddressOrFail, "incUnclaimed", [referralProgramName, userAddress, referrerReward, referralReward], nil) |
---|
364 | 364 | | } |
---|
365 | 365 | | if ((referralInv == referralInv)) |
---|
366 | 366 | | then { |
---|
367 | 367 | | let unclaimedActions = [IntegerEntry(keyUserUnclaimed(currentUser), (valueOrElse(userUnclaimedOption, 0) + userTotalAmount))] |
---|
368 | 368 | | let processingActions = if (((usersCount - 1) > currentUser)) |
---|
369 | 369 | | then [IntegerEntry(keyNextProcessedUser(), (currentUser + 1))] |
---|
370 | 370 | | else [IntegerEntry(keyNextProcessedPeriod(), (currentPeriod + 1)), IntegerEntry(keyNextProcessedUser(), 0), DeleteEntry(keyProcessingStage())] |
---|
371 | 371 | | $Tuple2((unclaimedActions ++ processingActions), true) |
---|
372 | 372 | | } |
---|
373 | 373 | | else throw("Strict value is not equal to itself.") |
---|
374 | 374 | | } |
---|
375 | 375 | | else throw("Strict value is not equal to itself.") |
---|
376 | 376 | | } |
---|
377 | 377 | | else throw("invalid processing stage") |
---|
378 | 378 | | } |
---|
379 | 379 | | |
---|
380 | 380 | | |
---|
381 | 381 | | |
---|
382 | 382 | | @Callable(i) |
---|
383 | 383 | | func finalizeWrapper (counter) = { |
---|
384 | 384 | | let result = { |
---|
385 | 385 | | let @ = invoke(this, "finalizeHelper", nil, nil) |
---|
386 | 386 | | if ($isInstanceOf(@, "Boolean")) |
---|
387 | 387 | | then @ |
---|
388 | 388 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
389 | 389 | | } |
---|
390 | 390 | | if ((result == result)) |
---|
391 | 391 | | then if (!(result)) |
---|
392 | 392 | | then if ((counter == maxDepth)) |
---|
393 | 393 | | then throw("Nothing to process") |
---|
394 | 394 | | else $Tuple2(nil, unit) |
---|
395 | 395 | | else if ((counter > 0)) |
---|
396 | 396 | | then $Tuple2(nil, invoke(this, "finalizeWrapper", [(counter - 1)], nil)) |
---|
397 | 397 | | else $Tuple2(nil, unit) |
---|
398 | 398 | | else throw("Strict value is not equal to itself.") |
---|
399 | 399 | | } |
---|
400 | 400 | | |
---|
401 | 401 | | |
---|
402 | 402 | | |
---|
403 | 403 | | @Callable(i) |
---|
404 | 404 | | func processPendingPeriodsAndUsers () = $Tuple2(nil, invoke(this, "finalizeWrapper", [maxDepth], nil)) |
---|
405 | 405 | | |
---|
406 | 406 | | |
---|
407 | 407 | | |
---|
408 | 408 | | @Callable(i) |
---|
409 | 409 | | func deposit () = { |
---|
410 | 410 | | let cfgArray = readConfigArrayOrFail() |
---|
411 | 411 | | if ((i.caller != Address(fromBase58String(cfgArray[IdxCfgPacemakerAddress])))) |
---|
412 | 412 | | then throw("Wrong caller address") |
---|
413 | 413 | | else { |
---|
414 | 414 | | let assetId = value(value(i.payments[0]).assetId) |
---|
415 | 415 | | if ((assetId != fromBase58String(cfgArray[IdxCfgAssetId]))) |
---|
416 | 416 | | then throw("Wrong payment asset") |
---|
417 | 417 | | else { |
---|
418 | 418 | | let period = nextPeriod() |
---|
419 | 419 | | let deltaH = (height - getNumberOrFail(keyGwxRewardEmissionStartHeight())) |
---|
420 | 420 | | let emissionRate = valueOrErrorMessage(getInteger(emissionContract, keyRatePerBlockCurrent()), (("mandatory emission_contract." + keyRatePerBlockCurrent()) + " is not defined")) |
---|
421 | 421 | | let weight = valueOrErrorMessage(getInteger(factoryContract, keyPoolWeightVirtual()), (("mandatory factory_contract." + keyPoolWeightVirtual()) + " is not defined")) |
---|
422 | 422 | | let auxAmount = fraction((deltaH * weight), emissionRate, MULT8) |
---|
423 | 423 | | let em = invoke(emissionContract, "emit", [auxAmount], nil) |
---|
424 | 424 | | if ((em == em)) |
---|
425 | 425 | | then { |
---|
426 | 426 | | let matcherPart = value(i.payments[0]).amount |
---|
427 | 427 | | let payoutInfo = PeriodPayoutInfo(period, matcherPart, auxAmount) |
---|
428 | 428 | | [IntegerEntry(keyLatestPeriod(), period), IntegerEntry(keyHeightForPeriod(period), height), IntegerEntry(keyAuxEmissionRewardForPeriod(period), auxAmount), IntegerEntry(keyGwxRewardEmissionStartHeight(), height), IntegerEntry(keyTotalAmountForPeriod(period), matcherPart), IntegerEntry(keyNextPeriod(), (period + 1)), StringEntry(keyLastPayoutInfo(), payoutInfo), StringEntry(keyPayoutHistoryInfo(period), payoutInfo)] |
---|
429 | 429 | | } |
---|
430 | 430 | | else throw("Strict value is not equal to itself.") |
---|
431 | 431 | | } |
---|
432 | 432 | | } |
---|
433 | 433 | | } |
---|
434 | 434 | | |
---|
435 | 435 | | |
---|
436 | 436 | | |
---|
437 | 437 | | @Callable(i) |
---|
438 | 438 | | func claimReward () = { |
---|
439 | 439 | | let cfgArray = readConfigArrayOrFail() |
---|
440 | 440 | | let address = toString(i.caller) |
---|
441 | 441 | | let $t01754217592 = commonClaimReward(address) |
---|
442 | 442 | | let amount = $t01754217592._1 |
---|
443 | 443 | | let actions = $t01754217592._2 |
---|
444 | 444 | | let checkAmount = if ((amount > 0)) |
---|
445 | 445 | | then true |
---|
446 | 446 | | else throw("Nothing to claim") |
---|
447 | 447 | | if ((checkAmount == checkAmount)) |
---|
448 | 448 | | then { |
---|
449 | 449 | | let amountFromEmission = 0 |
---|
450 | 450 | | let claimedReferral = { |
---|
451 | 451 | | let @ = invoke(referralsContractAddressOrFail, "claim", [referralProgramName], nil) |
---|
452 | 452 | | if ($isInstanceOf(@, "Int")) |
---|
453 | 453 | | then @ |
---|
454 | 454 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
455 | 455 | | } |
---|
456 | 456 | | let totalAmount = (amount + claimedReferral) |
---|
457 | 457 | | $Tuple2(([ScriptTransfer(i.caller, totalAmount, fromBase58String(cfgArray[IdxCfgAssetId])), HistoryEntry("claim", address, amount, i)] ++ actions), [totalAmount, amountFromEmission]) |
---|
458 | 458 | | } |
---|
459 | 459 | | else throw("Strict value is not equal to itself.") |
---|
460 | 460 | | } |
---|
461 | 461 | | |
---|
462 | 462 | | |
---|
463 | 463 | | |
---|
464 | 464 | | @Callable(i) |
---|
465 | 465 | | func claimRewardREADONLY (address) = { |
---|
466 | 466 | | let $t01816518215 = commonClaimReward(address) |
---|
467 | 467 | | let amount = $t01816518215._1 |
---|
468 | 468 | | let actions = $t01816518215._2 |
---|
469 | 469 | | let referralUnclaimed = valueOrElse(getInteger(referralsContractAddressOrFail, keyUnclaimedReferral(referralProgramName, address)), 0) |
---|
470 | 470 | | let totalAmount = (amount + referralUnclaimed) |
---|
471 | 471 | | $Tuple2(nil, totalAmount) |
---|
472 | 472 | | } |
---|
473 | 473 | | |
---|
474 | 474 | | |
---|
475 | 475 | | |
---|
476 | 476 | | @Callable(i) |
---|
477 | 477 | | func latestFinalizedPeriodREADONLY (address) = $Tuple2(nil, valueOrElse(getInteger(this, keyLatestPeriod()), -1)) |
---|
478 | 478 | | |
---|
479 | 479 | | |
---|
480 | 480 | | |
---|
481 | 481 | | @Callable(i) |
---|
482 | 482 | | func latestFinalizedPeriodInfoREADONLY (address) = $Tuple2(nil, getStringByKey(keyLastPayoutInfo())) |
---|
483 | 483 | | |
---|
484 | 484 | | |
---|
485 | 485 | | |
---|
486 | 486 | | @Callable(i) |
---|
487 | 487 | | func calcGwxParamsREADONLY (gwxAmountStart,lockStartHeight,lockDurationBlocks) = { |
---|
488 | 488 | | let lockEndHeight = (lockStartHeight + lockDurationBlocks) |
---|
489 | 489 | | let scale8ParamK = -(fraction(gwxAmountStart, SCALE, lockDurationBlocks)) |
---|
490 | 490 | | let scale8ParamB = (fraction(gwxAmountStart, SCALE, lockDurationBlocks) * lockEndHeight) |
---|
491 | 491 | | $Tuple2(nil, [scale8ParamK, scale8ParamB, nextPeriod()]) |
---|
492 | 492 | | } |
---|
493 | 493 | | |
---|
494 | 494 | | |
---|
495 | 495 | | |
---|
496 | 496 | | @Callable(i) |
---|
497 | 497 | | func calcGwxAmountStartREADONLY (wxLockAmount,lockDuration,maxLockDuration) = { |
---|
498 | 498 | | let coeffX8 = fraction(lockDuration, MULT8, maxLockDuration) |
---|
499 | 499 | | let gWxAmountStart = fraction(wxLockAmount, coeffX8, MULT8) |
---|
500 | 500 | | $Tuple2(nil, [gWxAmountStart]) |
---|
501 | 501 | | } |
---|
502 | 502 | | |
---|
503 | 503 | | |
---|
504 | 504 | | |
---|
505 | 505 | | @Callable(i) |
---|
506 | 506 | | func onEmissionForGwxStart () = if ((i.caller != factoryContract)) |
---|
507 | 507 | | then throw("permissions denied") |
---|
508 | 508 | | else [IntegerEntry(keyGwxRewardEmissionStartHeight(), height)] |
---|
509 | 509 | | |
---|
510 | 510 | | |
---|
511 | 511 | | |
---|
512 | 512 | | @Callable(i) |
---|
513 | 513 | | func latestPeriodEmissionRewardsREADONLY (address) = { |
---|
514 | 514 | | let period = nextPeriod() |
---|
515 | 515 | | $Tuple2(nil, [getNumberByKey(keyAuxEmissionRewardForPeriod(period))]) |
---|
516 | 516 | | } |
---|
517 | 517 | | |
---|
518 | 518 | | |
---|
519 | 519 | | |
---|
520 | 520 | | @Callable(i) |
---|
521 | 521 | | func calcD (x1BigIntStr,x2BigIntStr,ampBigIntStr,aPrecisionBigIntStr,targetPrecisionBigIntStr) = { |
---|
522 | 522 | | let nCoins = toBigInt(2) |
---|
523 | 523 | | let aPrecision = parseBigIntValue(aPrecisionBigIntStr) |
---|
524 | 524 | | let targetPrecision = parseBigIntValue(targetPrecisionBigIntStr) |
---|
525 | 525 | | let x1 = parseBigIntValue(x1BigIntStr) |
---|
526 | 526 | | let x2 = parseBigIntValue(x2BigIntStr) |
---|
527 | 527 | | let amp = (parseBigIntValue(ampBigIntStr) * aPrecision) |
---|
528 | 528 | | let s = (x1 + x2) |
---|
529 | 529 | | if ((s == zeroBigInt)) |
---|
530 | 530 | | then $Tuple2(nil, toString(zeroBigInt)) |
---|
531 | 531 | | else { |
---|
532 | 532 | | let ann = (amp * nCoins) |
---|
533 | 533 | | let arr = [0, 1, 2, 3, 4, 5, 6] |
---|
534 | 534 | | func calc (acc,cur) = { |
---|
535 | 535 | | let $t02084820875 = acc |
---|
536 | 536 | | let d = $t02084820875._1 |
---|
537 | 537 | | let dPrev = $t02084820875._2 |
---|
538 | 538 | | let found = $t02084820875._3 |
---|
539 | 539 | | if ((found != unit)) |
---|
540 | 540 | | then acc |
---|
541 | 541 | | else { |
---|
542 | 542 | | let dp = (((d * d) * d) / (((x1 * x2) * nCoins) * nCoins)) |
---|
543 | 543 | | let dNext = (((((ann * s) / aPrecision) + (dp * nCoins)) * d) / ((((ann - aPrecision) * d) / aPrecision) + ((nCoins + toBigInt(1)) * dp))) |
---|
544 | 544 | | let dDiff = absBigInt((dNext - value(d))) |
---|
545 | 545 | | if ((targetPrecision >= dDiff)) |
---|
546 | 546 | | then $Tuple3(dNext, d, cur) |
---|
547 | 547 | | else $Tuple3(dNext, d, unit) |
---|
548 | 548 | | } |
---|
549 | 549 | | } |
---|
550 | 550 | | |
---|
551 | 551 | | let $t02147321536 = { |
---|
552 | 552 | | let $l = arr |
---|
553 | 553 | | let $s = size($l) |
---|
554 | 554 | | let $acc0 = $Tuple3(s, unit, unit) |
---|
555 | 555 | | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
556 | 556 | | then $a |
---|
557 | 557 | | else calc($a, $l[$i]) |
---|
558 | 558 | | |
---|
559 | 559 | | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
560 | 560 | | then $a |
---|
561 | 561 | | else throw("List size exceeds 7") |
---|
562 | 562 | | |
---|
563 | 563 | | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7) |
---|
564 | 564 | | } |
---|
565 | 565 | | let dNext = $t02147321536._1 |
---|
566 | 566 | | let dPrev = $t02147321536._2 |
---|
567 | 567 | | let found = $t02147321536._3 |
---|
568 | 568 | | if ((found != unit)) |
---|
569 | 569 | | then $Tuple2(nil, toString(dNext)) |
---|
570 | 570 | | else { |
---|
571 | 571 | | let dDiff = absBigInt((dNext - value(dPrev))) |
---|
572 | 572 | | throw(("D calculation error, dDiff = " + toString(dDiff))) |
---|
573 | 573 | | } |
---|
574 | 574 | | } |
---|
575 | 575 | | } |
---|
576 | 576 | | |
---|
577 | 577 | | |
---|
578 | 578 | | |
---|
579 | 579 | | @Callable(i) |
---|
580 | 580 | | func setManager (pendingManagerPublicKey) = { |
---|
581 | 581 | | let checkCaller = mustManager(i) |
---|
582 | 582 | | if ((checkCaller == checkCaller)) |
---|
583 | 583 | | then { |
---|
584 | 584 | | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
585 | 585 | | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
586 | 586 | | then [StringEntry(keyPendingManagerPublicKey(), pendingManagerPublicKey)] |
---|
587 | 587 | | else throw("Strict value is not equal to itself.") |
---|
588 | 588 | | } |
---|
589 | 589 | | else throw("Strict value is not equal to itself.") |
---|
590 | 590 | | } |
---|
591 | 591 | | |
---|
592 | 592 | | |
---|
593 | 593 | | |
---|
594 | 594 | | @Callable(i) |
---|
595 | 595 | | func confirmManager () = { |
---|
596 | 596 | | let pm = pendingManagerPublicKeyOrUnit() |
---|
597 | 597 | | let hasPM = if (isDefined(pm)) |
---|
598 | 598 | | then true |
---|
599 | 599 | | else throw("No pending manager") |
---|
600 | 600 | | if ((hasPM == hasPM)) |
---|
601 | 601 | | then { |
---|
602 | 602 | | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
603 | 603 | | then true |
---|
604 | 604 | | else throw("You are not pending manager") |
---|
605 | 605 | | if ((checkPM == checkPM)) |
---|
606 | 606 | | then [StringEntry(keyManagerPublicKey(), toBase58String(value(pm))), DeleteEntry(keyPendingManagerPublicKey())] |
---|
607 | 607 | | else throw("Strict value is not equal to itself.") |
---|
608 | 608 | | } |
---|
609 | 609 | | else throw("Strict value is not equal to itself.") |
---|
610 | 610 | | } |
---|
611 | 611 | | |
---|
612 | 612 | | |
---|
613 | 613 | | @Verifier(tx) |
---|
614 | 614 | | func verify () = { |
---|
615 | 615 | | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
616 | 616 | | case pk: ByteVector => |
---|
617 | 617 | | pk |
---|
618 | 618 | | case _: Unit => |
---|
619 | 619 | | tx.senderPublicKey |
---|
620 | 620 | | case _ => |
---|
621 | 621 | | throw("Match error") |
---|
622 | 622 | | } |
---|
623 | 623 | | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
624 | 624 | | } |
---|
625 | 625 | | |
---|