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