1 | 1 | | {-# STDLIB_VERSION 6 #-} |
---|
2 | 2 | | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | 3 | | {-# CONTENT_TYPE DAPP #-} |
---|
4 | 4 | | let SEP = "__" |
---|
5 | 5 | | |
---|
6 | 6 | | let SCALE8 = 8 |
---|
7 | 7 | | |
---|
8 | 8 | | let MULT8 = 100000000 |
---|
9 | 9 | | |
---|
10 | 10 | | let POOLWEIGHTMULT = MULT8 |
---|
11 | 11 | | |
---|
12 | 12 | | func wrapErr (msg) = makeString(["boosting.ride:", msg], " ") |
---|
13 | 13 | | |
---|
14 | 14 | | |
---|
15 | 15 | | func throwErr (msg) = throw(wrapErr(msg)) |
---|
16 | 16 | | |
---|
17 | 17 | | |
---|
18 | 18 | | func strf (address,key) = valueOrErrorMessage(getString(address, key), (("mandatory this." + key) + " is not defined")) |
---|
19 | 19 | | |
---|
20 | 20 | | |
---|
21 | 21 | | func ioz (address,key) = valueOrElse(getInteger(address, key), 0) |
---|
22 | 22 | | |
---|
23 | 23 | | |
---|
24 | 24 | | func iod (address,key,defaultVal) = valueOrElse(getInteger(address, key), defaultVal) |
---|
25 | 25 | | |
---|
26 | 26 | | |
---|
27 | 27 | | func iof (address,key) = valueOrErrorMessage(getInteger(address, key), (("mandatory this." + key) + " is not defined")) |
---|
28 | 28 | | |
---|
29 | 29 | | |
---|
30 | 30 | | func abs (val) = if ((0 > val)) |
---|
31 | 31 | | then -(val) |
---|
32 | 32 | | else val |
---|
33 | 33 | | |
---|
34 | 34 | | |
---|
35 | 35 | | func aal (val) = match val { |
---|
36 | 36 | | case valAnyLyst: List[Any] => |
---|
37 | 37 | | valAnyLyst |
---|
38 | 38 | | case _ => |
---|
39 | 39 | | throw("fail to cast into List[Any]") |
---|
40 | 40 | | } |
---|
41 | 41 | | |
---|
42 | 42 | | |
---|
43 | 43 | | func ai (val) = match val { |
---|
44 | 44 | | case valInt: Int => |
---|
45 | 45 | | valInt |
---|
46 | 46 | | case _ => |
---|
47 | 47 | | throw("fail to cast into Int") |
---|
48 | 48 | | } |
---|
49 | 49 | | |
---|
50 | 50 | | |
---|
51 | 51 | | func keyReferralsContractAddress () = makeString(["%s%s", "config", "referralsContractAddress"], SEP) |
---|
52 | 52 | | |
---|
53 | 53 | | |
---|
54 | 54 | | let referralsContractAddressOrFail = addressFromStringValue(strf(this, keyReferralsContractAddress())) |
---|
55 | 55 | | |
---|
56 | 56 | | let keyReferralProgramName = makeString(["%s%s", "referral", "programName"], SEP) |
---|
57 | 57 | | |
---|
58 | 58 | | let referralProgramNameDefault = "wxlock" |
---|
59 | 59 | | |
---|
60 | 60 | | let referralProgramName = valueOrElse(getString(this, keyReferralProgramName), referralProgramNameDefault) |
---|
61 | 61 | | |
---|
62 | 62 | | func keyFactoryAddress () = "%s%s__config__factoryAddress" |
---|
63 | 63 | | |
---|
64 | 64 | | |
---|
65 | 65 | | let IdxFactoryCfgStakingDapp = 1 |
---|
66 | 66 | | |
---|
67 | 67 | | let IdxFactoryCfgBoostingDapp = 2 |
---|
68 | 68 | | |
---|
69 | 69 | | let IdxFactoryCfgIdoDapp = 3 |
---|
70 | 70 | | |
---|
71 | 71 | | let IdxFactoryCfgTeamDapp = 4 |
---|
72 | 72 | | |
---|
73 | 73 | | let IdxFactoryCfgEmissionDapp = 5 |
---|
74 | 74 | | |
---|
75 | 75 | | let IdxFactoryCfgRestDapp = 6 |
---|
76 | 76 | | |
---|
77 | 77 | | let IdxFactoryCfgSlippageDapp = 7 |
---|
78 | 78 | | |
---|
79 | 79 | | let IdxFactoryCfgDaoDapp = 8 |
---|
80 | 80 | | |
---|
81 | 81 | | let IdxFactoryCfgMarketingDapp = 9 |
---|
82 | 82 | | |
---|
83 | 83 | | let IdxFactoryCfgGwxRewardDapp = 10 |
---|
84 | 84 | | |
---|
85 | 85 | | let IdxFactoryCfgBirdsDapp = 11 |
---|
86 | 86 | | |
---|
87 | 87 | | func keyFactoryCfg () = "%s__factoryConfig" |
---|
88 | 88 | | |
---|
89 | 89 | | |
---|
90 | 90 | | func keyFactoryLp2AssetsMapping (lpAssetStr) = makeString(["%s%s%s", lpAssetStr, "mappings__lpAsset2PoolContract"], SEP) |
---|
91 | 91 | | |
---|
92 | 92 | | |
---|
93 | 93 | | func keyFactoryLpList () = "%s__lpTokensList" |
---|
94 | 94 | | |
---|
95 | 95 | | |
---|
96 | 96 | | func keyFactoryLpAssetToPoolContractAddress (lpAssetStr) = makeString(["%s%s%s", lpAssetStr, "mappings__lpAsset2PoolContract"], SEP) |
---|
97 | 97 | | |
---|
98 | 98 | | |
---|
99 | 99 | | func keyFactoryPoolWeight (contractAddress) = makeString(["%s%s", "poolWeight", contractAddress], SEP) |
---|
100 | 100 | | |
---|
101 | 101 | | |
---|
102 | 102 | | func keyFactoryPoolWeightHistory (poolAddress,num) = ((("%s%s__poolWeight__" + poolAddress) + "__") + toString(num)) |
---|
103 | 103 | | |
---|
104 | 104 | | |
---|
105 | 105 | | func readFactoryAddressOrFail () = addressFromStringValue(strf(this, keyFactoryAddress())) |
---|
106 | 106 | | |
---|
107 | 107 | | |
---|
108 | 108 | | func readLpList () = split(valueOrElse(getString(readFactoryAddressOrFail(), keyFactoryLpList()), ""), SEP) |
---|
109 | 109 | | |
---|
110 | 110 | | |
---|
111 | 111 | | func readFactoryCfgOrFail (factory) = split(strf(factory, keyFactoryCfg()), SEP) |
---|
112 | 112 | | |
---|
113 | 113 | | |
---|
114 | 114 | | func getBoostingAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgBoostingDapp]) |
---|
115 | 115 | | |
---|
116 | 116 | | |
---|
117 | 117 | | func getEmissionAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgEmissionDapp]) |
---|
118 | 118 | | |
---|
119 | 119 | | |
---|
120 | 120 | | func getStakingAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgStakingDapp]) |
---|
121 | 121 | | |
---|
122 | 122 | | |
---|
123 | 123 | | func getGwxRewardAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgGwxRewardDapp]) |
---|
124 | 124 | | |
---|
125 | 125 | | |
---|
126 | 126 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
127 | 127 | | |
---|
128 | 128 | | |
---|
129 | 129 | | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" |
---|
130 | 130 | | |
---|
131 | 131 | | |
---|
132 | 132 | | func keyEmissionRatePerBlockCurrent () = "%s%s__ratePerBlock__current" |
---|
133 | 133 | | |
---|
134 | 134 | | |
---|
135 | 135 | | func keyEmissionRatePerBlockMaxCurrent () = "%s%s__ratePerBlockMax__current" |
---|
136 | 136 | | |
---|
137 | 137 | | |
---|
138 | 138 | | func keyEmissionStartBlock () = "%s%s__emission__startBlock" |
---|
139 | 139 | | |
---|
140 | 140 | | |
---|
141 | 141 | | func keyBoostingV2LastUpdateHeight () = "%s%s__boostingV2__startBlock" |
---|
142 | 142 | | |
---|
143 | 143 | | |
---|
144 | 144 | | func keyBoostingV2Integral () = "%s%s__boostingV2__integral" |
---|
145 | 145 | | |
---|
146 | 146 | | |
---|
147 | 147 | | func keyEmissionDurationInBlocks () = "%s%s__emission__duration" |
---|
148 | 148 | | |
---|
149 | 149 | | |
---|
150 | 150 | | func keyEmissionEndBlock () = "%s%s__emission__endBlock" |
---|
151 | 151 | | |
---|
152 | 152 | | |
---|
153 | 153 | | func keyNextPeriod () = "%s__nextPeriod" |
---|
154 | 154 | | |
---|
155 | 155 | | |
---|
156 | 156 | | func keyGwxRewardEmissionStartHeight () = "%s%s__gwxRewardEmissionPart__startHeight" |
---|
157 | 157 | | |
---|
158 | 158 | | |
---|
159 | 159 | | let IdxCfgAssetId = 1 |
---|
160 | 160 | | |
---|
161 | 161 | | let IdxCfgMinLockAmount = 2 |
---|
162 | 162 | | |
---|
163 | 163 | | let IdxCfgMinLockDuration = 3 |
---|
164 | 164 | | |
---|
165 | 165 | | let IdxCfgMaxLockDuration = 4 |
---|
166 | 166 | | |
---|
167 | 167 | | let IdxCfgMathContract = 5 |
---|
168 | 168 | | |
---|
169 | 169 | | func keyConfig () = "%s__config" |
---|
170 | 170 | | |
---|
171 | 171 | | |
---|
172 | 172 | | func readConfigArrayOrFail () = split(strf(this, keyConfig()), SEP) |
---|
173 | 173 | | |
---|
174 | 174 | | |
---|
175 | 175 | | let mathContract = addressFromStringValue(readConfigArrayOrFail()[IdxCfgMathContract]) |
---|
176 | 176 | | |
---|
177 | 177 | | func formatConfigS (assetId,minLockAmount,minLockDuration,maxLockDuration,mathContract) = makeString(["%s%d%d%d", assetId, minLockAmount, minLockDuration, maxLockDuration, mathContract], SEP) |
---|
178 | 178 | | |
---|
179 | 179 | | |
---|
180 | 180 | | func formatConfig (assetId,minLockAmount,minLockDuration,maxLockDuration,mathContract) = formatConfigS(assetId, toString(minLockAmount), toString(minLockDuration), toString(maxLockDuration), mathContract) |
---|
181 | 181 | | |
---|
182 | 182 | | |
---|
183 | 183 | | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { |
---|
184 | 184 | | case s: String => |
---|
185 | 185 | | fromBase58String(s) |
---|
186 | 186 | | case _: Unit => |
---|
187 | 187 | | unit |
---|
188 | 188 | | case _ => |
---|
189 | 189 | | throw("Match error") |
---|
190 | 190 | | } |
---|
191 | 191 | | |
---|
192 | 192 | | |
---|
193 | 193 | | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { |
---|
194 | 194 | | case s: String => |
---|
195 | 195 | | fromBase58String(s) |
---|
196 | 196 | | case _: Unit => |
---|
197 | 197 | | unit |
---|
198 | 198 | | case _ => |
---|
199 | 199 | | throw("Match error") |
---|
200 | 200 | | } |
---|
201 | 201 | | |
---|
202 | 202 | | |
---|
203 | 203 | | func mustManager (i) = { |
---|
204 | 204 | | let pd = throw("Permission denied") |
---|
205 | 205 | | match managerPublicKeyOrUnit() { |
---|
206 | 206 | | case pk: ByteVector => |
---|
207 | 207 | | if ((i.callerPublicKey == pk)) |
---|
208 | 208 | | then true |
---|
209 | 209 | | else pd |
---|
210 | 210 | | case _: Unit => |
---|
211 | 211 | | if ((i.caller == this)) |
---|
212 | 212 | | then true |
---|
213 | 213 | | else pd |
---|
214 | 214 | | case _ => |
---|
215 | 215 | | throw("Match error") |
---|
216 | 216 | | } |
---|
217 | 217 | | } |
---|
218 | 218 | | |
---|
219 | 219 | | |
---|
220 | 220 | | let IdxLockUserNum = 1 |
---|
221 | 221 | | |
---|
222 | 222 | | let IdxLockAmount = 2 |
---|
223 | 223 | | |
---|
224 | 224 | | let IdxLockStart = 3 |
---|
225 | 225 | | |
---|
226 | 226 | | let IdxLockDuration = 4 |
---|
227 | 227 | | |
---|
228 | 228 | | let IdxLockParamK = 5 |
---|
229 | 229 | | |
---|
230 | 230 | | let IdxLockParamB = 6 |
---|
231 | 231 | | |
---|
232 | 232 | | func keyLockParamsRecord (userAddress) = makeString(["%s%s__lock", userAddress], SEP) |
---|
233 | 233 | | |
---|
234 | 234 | | |
---|
235 | 235 | | func readLockParamsRecordOrFail (userAddress) = split(strf(this, keyLockParamsRecord(userAddress)), SEP) |
---|
236 | 236 | | |
---|
237 | 237 | | |
---|
238 | 238 | | func formatLockParamsRecordS (userNum,amount,start,duration,paramK,paramB,lastUpdTimestamp,gwxAmount) = makeString(["%d%d%d%d%d%d%d%d", userNum, amount, start, duration, paramK, paramB, lastUpdTimestamp, gwxAmount], SEP) |
---|
239 | 239 | | |
---|
240 | 240 | | |
---|
241 | 241 | | func formatLockParamsRecord (userNum,amount,start,duration,paramK,paramB,gwxAmount) = formatLockParamsRecordS(userNum, toString(amount), toString(start), toString(duration), toString(paramK), toString(paramB), toString(lastBlock.timestamp), toString(gwxAmount)) |
---|
242 | 242 | | |
---|
243 | 243 | | |
---|
244 | 244 | | func keyNextUserNum () = "%s__nextUserNum" |
---|
245 | 245 | | |
---|
246 | 246 | | |
---|
247 | 247 | | func keyUser2NumMapping (userAddress) = makeString(["%s%s%s__mapping__user2num", userAddress], SEP) |
---|
248 | 248 | | |
---|
249 | 249 | | |
---|
250 | 250 | | func keyNum2UserMapping (num) = makeString(["%s%s%s__mapping__num2user", num], SEP) |
---|
251 | 251 | | |
---|
252 | 252 | | |
---|
253 | 253 | | func keyLockParamUserAmount (userNum) = makeString(["%s%d%s__paramByUserNum", userNum, "amount"], SEP) |
---|
254 | 254 | | |
---|
255 | 255 | | |
---|
256 | 256 | | func keyLockParamStartBlock (userNum) = makeString(["%s%d%s__paramByUserNum", userNum, "start"], SEP) |
---|
257 | 257 | | |
---|
258 | 258 | | |
---|
259 | 259 | | func keyLockParamDuration (userNum) = makeString(["%s%d%s__paramByUserNum", userNum, "duration"], SEP) |
---|
260 | 260 | | |
---|
261 | 261 | | |
---|
262 | 262 | | func keyLockParamK (userNum) = makeString(["%s%d%s__paramByUserNum", userNum, "k"], SEP) |
---|
263 | 263 | | |
---|
264 | 264 | | |
---|
265 | 265 | | func keyLockParamB (userNum) = makeString(["%s%d%s__paramByUserNum", userNum, "b"], SEP) |
---|
266 | 266 | | |
---|
267 | 267 | | |
---|
268 | 268 | | func keyLockParamByPeriodK (userNum,period) = makeString(["%s%d%s%d__paramByPeriod", userNum, "k", period], SEP) |
---|
269 | 269 | | |
---|
270 | 270 | | |
---|
271 | 271 | | func keyLockParamByPeriodB (userNum,period) = makeString(["%s%d%s%d__paramByPeriod", userNum, "b", period], SEP) |
---|
272 | 272 | | |
---|
273 | 273 | | |
---|
274 | 274 | | func keyLockParamTotalAmount () = "%s%s__stats__activeTotalLocked" |
---|
275 | 275 | | |
---|
276 | 276 | | |
---|
277 | 277 | | func keyStatsLocksDurationSumInBlocks () = "%s%s__stats__locksDurationSumInBlocks" |
---|
278 | 278 | | |
---|
279 | 279 | | |
---|
280 | 280 | | func keyStatsLocksCount () = "%s%s__stats__locksCount" |
---|
281 | 281 | | |
---|
282 | 282 | | |
---|
283 | 283 | | func keyStatsUsersCount () = "%s%s__stats__activeUsersCount" |
---|
284 | 284 | | |
---|
285 | 285 | | |
---|
286 | 286 | | func keyUserBoostEmissionLastINTEGRAL (userNum) = makeString(["%s%d__userBoostEmissionLastIntV2", userNum], SEP) |
---|
287 | 287 | | |
---|
288 | 288 | | |
---|
289 | 289 | | func keyUserLpBoostEmissionLastINTEGRAL (userNum,lpAssetId) = makeString(["%s%d__userBoostEmissionLastIntV2", userNum, lpAssetId], SEP) |
---|
290 | 290 | | |
---|
291 | 291 | | |
---|
292 | 292 | | func keyUserMaxBoostINTEGRAL (userNum) = makeString(["%s%d__maxBoostInt", userNum], SEP) |
---|
293 | 293 | | |
---|
294 | 294 | | |
---|
295 | 295 | | func keyTotalMaxBoostINTEGRAL () = "%s%s__maxBoostInt__total" |
---|
296 | 296 | | |
---|
297 | 297 | | |
---|
298 | 298 | | func keyUserBoostAvalaibleToClaimTotal (userNum) = makeString(["%s%d__userBoostAvaliableToClaimTotal", userNum], SEP) |
---|
299 | 299 | | |
---|
300 | 300 | | |
---|
301 | 301 | | func keyUserBoostClaimed (userNum) = makeString(["%s%d__userBoostClaimed", userNum], SEP) |
---|
302 | 302 | | |
---|
303 | 303 | | |
---|
304 | 304 | | func keyTotalCachedGwx () = "%s%s__gwxCached__total" |
---|
305 | 305 | | |
---|
306 | 306 | | |
---|
307 | 307 | | func keyTotalCachedGwxCorrective () = "%s__gwxCachedTotalCorrective" |
---|
308 | 308 | | |
---|
309 | 309 | | |
---|
310 | 310 | | let factoryContract = readFactoryAddressOrFail() |
---|
311 | 311 | | |
---|
312 | 312 | | let factoryCfg = readFactoryCfgOrFail(factoryContract) |
---|
313 | 313 | | |
---|
314 | 314 | | let emissionContract = getEmissionAddressOrFail(factoryCfg) |
---|
315 | 315 | | |
---|
316 | 316 | | let stakingContract = getStakingAddressOrFail(factoryCfg) |
---|
317 | 317 | | |
---|
318 | 318 | | let gwxRewardContract = getGwxRewardAddressOrFail(factoryCfg) |
---|
319 | 319 | | |
---|
320 | 320 | | let keyVotingEmissionContract = makeString(["%s", "votingEmissionContract"], SEP) |
---|
321 | 321 | | |
---|
322 | 322 | | let votingEmissionContract = addressFromStringValue(getStringValue(factoryContract, keyVotingEmissionContract)) |
---|
323 | 323 | | |
---|
324 | 324 | | let boostCoeff = { |
---|
325 | 325 | | let @ = invoke(emissionContract, "getBoostCoeffREADONLY", nil, nil) |
---|
326 | 326 | | if ($isInstanceOf(@, "Int")) |
---|
327 | 327 | | then @ |
---|
328 | 328 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
329 | 329 | | } |
---|
330 | 330 | | |
---|
331 | 331 | | func getTotalCachedGwx (correct) = { |
---|
332 | 332 | | let keyCurrentEpochUi = makeString(["%s", "currentEpochUi"], SEP) |
---|
333 | 333 | | let currentEpochUi = getIntegerValue(votingEmissionContract, keyCurrentEpochUi) |
---|
334 | 334 | | let keyTargetEpoch = makeString(["%s%s", "totalCachedGwxCorrection__activationEpoch"], SEP) |
---|
335 | 335 | | let targetEpochOption = getInteger(this, keyTargetEpoch) |
---|
336 | 336 | | let totalCachedGwxRaw = valueOrElse(getInteger(this, keyTotalCachedGwx()), 0) |
---|
337 | 337 | | let isCorrectionActivated = if (isDefined(targetEpochOption)) |
---|
338 | 338 | | then (currentEpochUi >= value(targetEpochOption)) |
---|
339 | 339 | | else false |
---|
340 | 340 | | let corrective = if (if (isCorrectionActivated) |
---|
341 | 341 | | then correct |
---|
342 | 342 | | else false) |
---|
343 | 343 | | then valueOrElse(getInteger(this, keyTotalCachedGwxCorrective()), 0) |
---|
344 | 344 | | else 0 |
---|
345 | 345 | | max([0, (totalCachedGwxRaw + corrective)]) |
---|
346 | 346 | | } |
---|
347 | 347 | | |
---|
348 | 348 | | |
---|
349 | 349 | | func HistoryEntry (type,user,amount,lockStart,duration,k,b,i) = { |
---|
350 | 350 | | let historyKEY = makeString(["%s%s%s%s__history", type, user, toBase58String(i.transactionId)], SEP) |
---|
351 | 351 | | let historyDATA = makeString(["%d%d%d%d%d%d%d", toString(lastBlock.height), toString(lastBlock.timestamp), toString(amount), toString(lockStart), toString(duration), toString(k), toString(b)], SEP) |
---|
352 | 352 | | StringEntry(historyKEY, historyDATA) |
---|
353 | 353 | | } |
---|
354 | 354 | | |
---|
355 | 355 | | |
---|
356 | 356 | | func StatsEntry (totalLockedInc,durationInc,lockCountInc,usersCountInc) = { |
---|
357 | 357 | | let locksDurationSumInBlocksKEY = keyStatsLocksDurationSumInBlocks() |
---|
358 | 358 | | let locksCountKEY = keyStatsLocksCount() |
---|
359 | 359 | | let usersCountKEY = keyStatsUsersCount() |
---|
360 | 360 | | let totalAmountKEY = keyLockParamTotalAmount() |
---|
361 | 361 | | let locksDurationSumInBlocks = ioz(this, locksDurationSumInBlocksKEY) |
---|
362 | 362 | | let locksCount = ioz(this, locksCountKEY) |
---|
363 | 363 | | let usersCount = ioz(this, usersCountKEY) |
---|
364 | 364 | | let totalAmount = ioz(this, totalAmountKEY) |
---|
365 | 365 | | [IntegerEntry(locksDurationSumInBlocksKEY, (locksDurationSumInBlocks + durationInc)), IntegerEntry(locksCountKEY, (locksCount + lockCountInc)), IntegerEntry(usersCountKEY, (usersCount + usersCountInc)), IntegerEntry(totalAmountKEY, (totalAmount + totalLockedInc))] |
---|
366 | 366 | | } |
---|
367 | 367 | | |
---|
368 | 368 | | |
---|
369 | 369 | | func calcGwxAmount (kRaw,bRaw,h) = { |
---|
370 | 370 | | let SCALE = 1000 |
---|
371 | 371 | | (((kRaw * h) + bRaw) / SCALE) |
---|
372 | 372 | | } |
---|
373 | 373 | | |
---|
374 | 374 | | |
---|
375 | 375 | | func LockParamsEntry (userAddress,userNum,amount,start,duration,k,b,period) = { |
---|
376 | 376 | | let userAmountKEY = keyLockParamUserAmount(userNum) |
---|
377 | 377 | | let startBlockKEY = keyLockParamStartBlock(userNum) |
---|
378 | 378 | | let durationKEY = keyLockParamDuration(userNum) |
---|
379 | 379 | | let kKEY = keyLockParamK(userNum) |
---|
380 | 380 | | let bKEY = keyLockParamB(userNum) |
---|
381 | 381 | | let kByPeriodKEY = keyLockParamByPeriodK(userNum, period) |
---|
382 | 382 | | let bByPeriodKEY = keyLockParamByPeriodB(userNum, period) |
---|
383 | 383 | | let gwxAmount = calcGwxAmount(k, b, height) |
---|
384 | 384 | | [IntegerEntry(userAmountKEY, amount), IntegerEntry(startBlockKEY, start), IntegerEntry(durationKEY, duration), IntegerEntry(kKEY, k), IntegerEntry(bKEY, b), IntegerEntry(kByPeriodKEY, k), IntegerEntry(bByPeriodKEY, b), StringEntry(keyLockParamsRecord(userAddress), formatLockParamsRecord(userNum, amount, start, duration, k, b, gwxAmount))] |
---|
385 | 385 | | } |
---|
386 | 386 | | |
---|
387 | 387 | | |
---|
388 | 388 | | func extractOptionalPaymentAmountOrFail (i,expectedAssetId) = if ((size(i.payments) > 1)) |
---|
389 | 389 | | then throw("only one payment is allowed") |
---|
390 | 390 | | else if ((size(i.payments) == 0)) |
---|
391 | 391 | | then 0 |
---|
392 | 392 | | else { |
---|
393 | 393 | | let pmt = i.payments[0] |
---|
394 | 394 | | if ((value(pmt.assetId) != expectedAssetId)) |
---|
395 | 395 | | then throw("invalid asset id in payment") |
---|
396 | 396 | | else pmt.amount |
---|
397 | 397 | | } |
---|
398 | 398 | | |
---|
399 | 399 | | |
---|
400 | 400 | | func calcUserGwxAmountAtHeight (userAddress,targetHeight) = { |
---|
401 | 401 | | let EMPTY = "empty" |
---|
402 | 402 | | let user2NumMappingKEY = keyUser2NumMapping(userAddress) |
---|
403 | 403 | | let userNum = valueOrElse(getString(user2NumMappingKEY), EMPTY) |
---|
404 | 404 | | let k = valueOrElse(getInteger(keyLockParamK(userNum)), 0) |
---|
405 | 405 | | let b = valueOrElse(getInteger(keyLockParamB(userNum)), 0) |
---|
406 | 406 | | let gwxAmountCalc = calcGwxAmount(k, b, targetHeight) |
---|
407 | 407 | | let gwxAmount = if ((0 > gwxAmountCalc)) |
---|
408 | 408 | | then 0 |
---|
409 | 409 | | else gwxAmountCalc |
---|
410 | 410 | | gwxAmount |
---|
411 | 411 | | } |
---|
412 | 412 | | |
---|
413 | 413 | | |
---|
414 | 414 | | func calcCurrentGwxAmount (userAddress) = calcUserGwxAmountAtHeight(userAddress, height) |
---|
415 | 415 | | |
---|
416 | 416 | | |
---|
417 | 417 | | func getVotingEmissionEpochInfo () = { |
---|
418 | 418 | | let keyCurrentEpochUi = makeString(["%s", "currentEpochUi"], SEP) |
---|
419 | 419 | | let $t01483815126 = { |
---|
420 | 420 | | let currentEpochUi = value(getInteger(votingEmissionContract, keyCurrentEpochUi)) |
---|
421 | 421 | | let lastFinalizedEpoch = (currentEpochUi - 1) |
---|
422 | 422 | | if ((0 > lastFinalizedEpoch)) |
---|
423 | 423 | | then throwErr("invalid epoch") |
---|
424 | 424 | | else $Tuple2(currentEpochUi, lastFinalizedEpoch) |
---|
425 | 425 | | } |
---|
426 | 426 | | let currentEpochUi = $t01483815126._1 |
---|
427 | 427 | | let lastFinalizedEpoch = $t01483815126._2 |
---|
428 | 428 | | func keyStartHeightByEpoch (epoch) = makeString(["%s%d", "startHeight", toString(epoch)], SEP) |
---|
429 | 429 | | |
---|
430 | 430 | | let currentEpochStartHeight = value(getInteger(votingEmissionContract, keyStartHeightByEpoch(currentEpochUi))) |
---|
431 | 431 | | $Tuple2(lastFinalizedEpoch, currentEpochStartHeight) |
---|
432 | 432 | | } |
---|
433 | 433 | | |
---|
434 | 434 | | |
---|
435 | 435 | | func getPoolAssetsByLpAssetId (lpAssetIdStr) = { |
---|
436 | 436 | | let idxAmountAssetId = 4 |
---|
437 | 437 | | let idxPriceAssetId = 5 |
---|
438 | 438 | | let poolCfg = { |
---|
439 | 439 | | let @ = invoke(factoryContract, "getPoolConfigByLpAssetId", [lpAssetIdStr], nil) |
---|
440 | 440 | | if ($isInstanceOf(@, "List[Any]")) |
---|
441 | 441 | | then @ |
---|
442 | 442 | | else throw(($getType(@) + " couldn't be cast to List[Any]")) |
---|
443 | 443 | | } |
---|
444 | 444 | | let amountAssetId = { |
---|
445 | 445 | | let @ = poolCfg[idxAmountAssetId] |
---|
446 | 446 | | if ($isInstanceOf(@, "String")) |
---|
447 | 447 | | then @ |
---|
448 | 448 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
449 | 449 | | } |
---|
450 | 450 | | let priceAssetId = { |
---|
451 | 451 | | let @ = poolCfg[idxPriceAssetId] |
---|
452 | 452 | | if ($isInstanceOf(@, "String")) |
---|
453 | 453 | | then @ |
---|
454 | 454 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
455 | 455 | | } |
---|
456 | 456 | | $Tuple2(amountAssetId, priceAssetId) |
---|
457 | 457 | | } |
---|
458 | 458 | | |
---|
459 | 459 | | |
---|
460 | 460 | | func keyVote (amountAssetId,priceAssetId,address,epoch) = makeString(["%s%s%s%s%d", "vote", amountAssetId, priceAssetId, toString(address), toString(epoch)], SEP) |
---|
461 | 461 | | |
---|
462 | 462 | | |
---|
463 | 463 | | func keyVotingResultStaked (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStaked", lpAssetIdStr, toString(epoch)], SEP) |
---|
464 | 464 | | |
---|
465 | 465 | | |
---|
466 | 466 | | func keyVotingResultStakedIntegral (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStakedIntegral", lpAssetIdStr, toString(epoch)], SEP) |
---|
467 | 467 | | |
---|
468 | 468 | | |
---|
469 | 469 | | func keyVotingResultStakedLastUpdateHeight (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStakedIntegralLastUpdateHeight", lpAssetIdStr, toString(epoch)], SEP) |
---|
470 | 470 | | |
---|
471 | 471 | | |
---|
472 | 472 | | func keyVotingResultStakedIntegralLast (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "votingResultStakedIntegralLast", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
473 | 473 | | |
---|
474 | 474 | | |
---|
475 | 475 | | func keyVoteStakedIntegral (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "voteStakedIntegral", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
476 | 476 | | |
---|
477 | 477 | | |
---|
478 | 478 | | func keyVoteStakedLastUpdateHeight (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "voteStakedIntegralLastUpdateHeight", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
479 | 479 | | |
---|
480 | 480 | | |
---|
481 | 481 | | func keyVoteStakedIntegralLast (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "voteStakedIntegralLast", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
482 | 482 | | |
---|
483 | 483 | | |
---|
484 | 484 | | func keyStakedByUser (userAddressStr,lpAssetIdStr) = makeString(["%s%s%s", "staked", userAddressStr, lpAssetIdStr], SEP) |
---|
485 | 485 | | |
---|
486 | 486 | | |
---|
487 | 487 | | func getUserVoteFinalized (lpAssetIdStr,userAddressStr) = { |
---|
488 | 488 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
489 | 489 | | let $t01791617996 = getVotingEmissionEpochInfo() |
---|
490 | 490 | | let lastFinalizedEpoch = $t01791617996._1 |
---|
491 | 491 | | let currentEpochStartHeight = $t01791617996._2 |
---|
492 | 492 | | let $t01799918074 = getPoolAssetsByLpAssetId(lpAssetIdStr) |
---|
493 | 493 | | let amountAssetId = $t01799918074._1 |
---|
494 | 494 | | let priceAssetId = $t01799918074._2 |
---|
495 | 495 | | let userVoteKey = keyVote(amountAssetId, priceAssetId, userAddress, lastFinalizedEpoch) |
---|
496 | 496 | | let userVote = valueOrElse(getInteger(votingEmissionContract, userVoteKey), 0) |
---|
497 | 497 | | userVote |
---|
498 | 498 | | } |
---|
499 | 499 | | |
---|
500 | 500 | | |
---|
501 | 501 | | func getUserVoteStaked (lpAssetIdStr,userAddressStr) = { |
---|
502 | 502 | | let stakedByUser = valueOrElse(getInteger(stakingContract, keyStakedByUser(userAddressStr, lpAssetIdStr)), 0) |
---|
503 | 503 | | let userVote = getUserVoteFinalized(lpAssetIdStr, userAddressStr) |
---|
504 | 504 | | if ((stakedByUser == 0)) |
---|
505 | 505 | | then 0 |
---|
506 | 506 | | else userVote |
---|
507 | 507 | | } |
---|
508 | 508 | | |
---|
509 | 509 | | |
---|
510 | 510 | | func getVotingResultStaked (lpAssetIdStr) = { |
---|
511 | 511 | | let $t01861818698 = getVotingEmissionEpochInfo() |
---|
512 | 512 | | let lastFinalizedEpoch = $t01861818698._1 |
---|
513 | 513 | | let currentEpochStartHeight = $t01861818698._2 |
---|
514 | 514 | | let votingResultStakedStart = valueOrElse(getInteger(votingEmissionContract, keyVotingResultStaked(lpAssetIdStr, lastFinalizedEpoch)), 0) |
---|
515 | 515 | | let votingResultStaked = valueOrElse(getInteger(this, keyVotingResultStaked(lpAssetIdStr, lastFinalizedEpoch)), votingResultStakedStart) |
---|
516 | 516 | | votingResultStaked |
---|
517 | 517 | | } |
---|
518 | 518 | | |
---|
519 | 519 | | |
---|
520 | 520 | | func refreshVotingResultStakedIntegral (lpAssetIdStr,stakedVoteDelta) = { |
---|
521 | 521 | | let $t01921919299 = getVotingEmissionEpochInfo() |
---|
522 | 522 | | let lastFinalizedEpoch = $t01921919299._1 |
---|
523 | 523 | | let currentEpochStartHeight = $t01921919299._2 |
---|
524 | 524 | | let votingResultStaked = getVotingResultStaked(lpAssetIdStr) |
---|
525 | 525 | | let votingResultStakedNew = (votingResultStaked + stakedVoteDelta) |
---|
526 | 526 | | let votingResultStakedIntegralKey = keyVotingResultStakedIntegral(lpAssetIdStr, lastFinalizedEpoch) |
---|
527 | 527 | | let votingResultStakedIntegralPrev = valueOrElse(getInteger(this, votingResultStakedIntegralKey), 0) |
---|
528 | 528 | | let votingResultLastUpdateHeightKey = keyVotingResultStakedLastUpdateHeight(lpAssetIdStr, lastFinalizedEpoch) |
---|
529 | 529 | | let votingResultStakedLastUpdateHeight = valueOrElse(getInteger(this, votingResultLastUpdateHeightKey), currentEpochStartHeight) |
---|
530 | 530 | | let votingResultStakedIntegralDh = (height - votingResultStakedLastUpdateHeight) |
---|
531 | 531 | | let votingResultStakedIntegral = ((votingResultStakedIntegralDh * votingResultStaked) + votingResultStakedIntegralPrev) |
---|
532 | 532 | | $Tuple2([IntegerEntry(keyVotingResultStaked(lpAssetIdStr, lastFinalizedEpoch), votingResultStakedNew), IntegerEntry(votingResultLastUpdateHeightKey, height), IntegerEntry(votingResultStakedIntegralKey, votingResultStakedIntegral)], votingResultStakedIntegral) |
---|
533 | 533 | | } |
---|
534 | 534 | | |
---|
535 | 535 | | |
---|
536 | 536 | | func refreshVoteStakedIntegral (lpAssetIdStr,userAddressStr,edge) = { |
---|
537 | 537 | | let $t02049920579 = getVotingEmissionEpochInfo() |
---|
538 | 538 | | let lastFinalizedEpoch = $t02049920579._1 |
---|
539 | 539 | | let currentEpochStartHeight = $t02049920579._2 |
---|
540 | 540 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
541 | 541 | | let idxAmountAssetId = 4 |
---|
542 | 542 | | let idxPriceAssetId = 5 |
---|
543 | 543 | | let poolCfg = { |
---|
544 | 544 | | let @ = invoke(factoryContract, "getPoolConfigByLpAssetId", [lpAssetIdStr], nil) |
---|
545 | 545 | | if ($isInstanceOf(@, "List[Any]")) |
---|
546 | 546 | | then @ |
---|
547 | 547 | | else throw(($getType(@) + " couldn't be cast to List[Any]")) |
---|
548 | 548 | | } |
---|
549 | 549 | | let amountAssetId = { |
---|
550 | 550 | | let @ = poolCfg[idxAmountAssetId] |
---|
551 | 551 | | if ($isInstanceOf(@, "String")) |
---|
552 | 552 | | then @ |
---|
553 | 553 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
554 | 554 | | } |
---|
555 | 555 | | let priceAssetId = { |
---|
556 | 556 | | let @ = poolCfg[idxPriceAssetId] |
---|
557 | 557 | | if ($isInstanceOf(@, "String")) |
---|
558 | 558 | | then @ |
---|
559 | 559 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
560 | 560 | | } |
---|
561 | 561 | | let userVote = valueOrElse(getInteger(votingEmissionContract, keyVote(amountAssetId, priceAssetId, userAddress, lastFinalizedEpoch)), 0) |
---|
562 | 562 | | let actions = if ((userVote == 0)) |
---|
563 | 563 | | then nil |
---|
564 | 564 | | else { |
---|
565 | 565 | | let stakedVoteDelta = if (edge) |
---|
566 | 566 | | then userVote |
---|
567 | 567 | | else -(userVote) |
---|
568 | 568 | | let $t02118421303 = refreshVotingResultStakedIntegral(lpAssetIdStr, stakedVoteDelta) |
---|
569 | 569 | | let votingResultActions = $t02118421303._1 |
---|
570 | 570 | | let votingResultStakedIntegral = $t02118421303._2 |
---|
571 | 571 | | let userVoteStaked = if (edge) |
---|
572 | 572 | | then 0 |
---|
573 | 573 | | else userVote |
---|
574 | 574 | | let userVoteStakedIntegralKey = keyVoteStakedIntegral(lpAssetIdStr, userAddress, lastFinalizedEpoch) |
---|
575 | 575 | | let userVoteStakedIntegralPrev = valueOrElse(getInteger(this, userVoteStakedIntegralKey), 0) |
---|
576 | 576 | | let userVoteStakedLastUpdateHeightKey = keyVoteStakedLastUpdateHeight(lpAssetIdStr, userAddress, lastFinalizedEpoch) |
---|
577 | 577 | | let userVoteStakedLastUpdateHeight = valueOrElse(getInteger(this, userVoteStakedLastUpdateHeightKey), currentEpochStartHeight) |
---|
578 | 578 | | let userVoteStakedIntegralDh = (height - userVoteStakedLastUpdateHeight) |
---|
579 | 579 | | let userVoteStakedIntegral = ((userVoteStakedIntegralDh * userVoteStaked) + userVoteStakedIntegralPrev) |
---|
580 | 580 | | let voteActions = [IntegerEntry(userVoteStakedLastUpdateHeightKey, height), IntegerEntry(userVoteStakedIntegralKey, userVoteStakedIntegral)] |
---|
581 | 581 | | (votingResultActions ++ voteActions) |
---|
582 | 582 | | } |
---|
583 | 583 | | actions |
---|
584 | 584 | | } |
---|
585 | 585 | | |
---|
586 | 586 | | |
---|
587 | 587 | | func getStakedVotesIntegralsDiff (lpAssetIdStr,userAddressStr) = { |
---|
588 | 588 | | let $t02229522375 = getVotingEmissionEpochInfo() |
---|
589 | 589 | | let lastFinalizedEpoch = $t02229522375._1 |
---|
590 | 590 | | let currentEpochStartHeight = $t02229522375._2 |
---|
591 | 591 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
592 | 592 | | let userVoteStaked = getUserVoteStaked(lpAssetIdStr, userAddressStr) |
---|
593 | 593 | | let votingResultStaked = getVotingResultStaked(lpAssetIdStr) |
---|
594 | 594 | | let userVoteStakedIntegralPrev = valueOrElse(getInteger(this, keyVoteStakedIntegral(lpAssetIdStr, userAddress, lastFinalizedEpoch)), 0) |
---|
595 | 595 | | let votingResultStakedIntegralPrev = valueOrElse(getInteger(this, keyVotingResultStakedIntegral(lpAssetIdStr, lastFinalizedEpoch)), 0) |
---|
596 | 596 | | let userVoteStakedLastUpdateHeight = valueOrElse(getInteger(this, keyVoteStakedLastUpdateHeight(lpAssetIdStr, userAddress, lastFinalizedEpoch)), currentEpochStartHeight) |
---|
597 | 597 | | let votingResultStakedLastUpdateHeight = valueOrElse(getInteger(this, keyVotingResultStakedLastUpdateHeight(lpAssetIdStr, lastFinalizedEpoch)), currentEpochStartHeight) |
---|
598 | 598 | | let userVoteStakedIntegralKey = keyVoteStakedIntegralLast(lpAssetIdStr, userAddress, lastFinalizedEpoch) |
---|
599 | 599 | | let userVoteStakedIntegralLast = valueOrElse(getInteger(this, userVoteStakedIntegralKey), 0) |
---|
600 | 600 | | let votingResultStakedIntegralKey = keyVotingResultStakedIntegralLast(lpAssetIdStr, userAddress, lastFinalizedEpoch) |
---|
601 | 601 | | let votingResultStakedIntegralLast = valueOrElse(getInteger(this, votingResultStakedIntegralKey), 0) |
---|
602 | 602 | | let userVoteStakedIntegralDh = (height - userVoteStakedLastUpdateHeight) |
---|
603 | 603 | | let votingResultStakedIntegralDh = (height - votingResultStakedLastUpdateHeight) |
---|
604 | 604 | | let userVoteStakedIntegral = ((userVoteStakedIntegralDh * userVoteStaked) + userVoteStakedIntegralPrev) |
---|
605 | 605 | | let votingResultStakedIntegral = ((votingResultStakedIntegralDh * votingResultStaked) + votingResultStakedIntegralPrev) |
---|
606 | 606 | | let userVoteStakedIntegralDiff = (userVoteStakedIntegral - userVoteStakedIntegralLast) |
---|
607 | 607 | | let votingResultStakedIntegralDiff = (votingResultStakedIntegral - votingResultStakedIntegralLast) |
---|
608 | 608 | | $Tuple3([IntegerEntry(userVoteStakedIntegralKey, userVoteStakedIntegral), IntegerEntry(votingResultStakedIntegralKey, votingResultStakedIntegral)], userVoteStakedIntegralDiff, votingResultStakedIntegralDiff) |
---|
609 | 609 | | } |
---|
610 | 610 | | |
---|
611 | 611 | | |
---|
612 | 612 | | func refreshBoostEmissionIntegral () = { |
---|
613 | 613 | | let wxEmissionPerBlock = iof(emissionContract, keyEmissionRatePerBlockCurrent()) |
---|
614 | 614 | | let boostingV2LastUpdateHeightOption = getInteger(emissionContract, keyBoostingV2LastUpdateHeight()) |
---|
615 | 615 | | let boostingV2IngergalOption = getInteger(this, keyBoostingV2Integral()) |
---|
616 | 616 | | let emissionEnd = iof(emissionContract, keyEmissionEndBlock()) |
---|
617 | 617 | | let h = if ((height > emissionEnd)) |
---|
618 | 618 | | then emissionEnd |
---|
619 | 619 | | else height |
---|
620 | 620 | | let dh = match boostingV2LastUpdateHeightOption { |
---|
621 | 621 | | case lastUpdateHeight: Int => |
---|
622 | 622 | | max([(h - lastUpdateHeight), 0]) |
---|
623 | 623 | | case _: Unit => |
---|
624 | 624 | | 0 |
---|
625 | 625 | | case _ => |
---|
626 | 626 | | throw("Match error") |
---|
627 | 627 | | } |
---|
628 | 628 | | let boostEmissionPerBlock = ((wxEmissionPerBlock * (boostCoeff - 1)) / boostCoeff) |
---|
629 | 629 | | let boostEmissionIntegralPrev = valueOrElse(boostingV2IngergalOption, 0) |
---|
630 | 630 | | let boostEmissionIntegral = ((boostEmissionPerBlock * dh) + boostEmissionIntegralPrev) |
---|
631 | 631 | | $Tuple2([IntegerEntry(keyBoostingV2Integral(), boostEmissionIntegral), IntegerEntry(keyBoostingV2LastUpdateHeight(), height)], boostEmissionIntegral) |
---|
632 | 632 | | } |
---|
633 | 633 | | |
---|
634 | 634 | | |
---|
635 | 635 | | func internalClaimWxBoost (lpAssetIdStr,userAddressStr,readOnly) = { |
---|
636 | 636 | | let userRecordOption = getString(this, keyLockParamsRecord(userAddressStr)) |
---|
637 | 637 | | if ((userRecordOption == unit)) |
---|
638 | 638 | | then $Tuple3(0, nil, "userRecord::is::empty") |
---|
639 | 639 | | else { |
---|
640 | 640 | | let userRecordArray = split(value(userRecordOption), SEP) |
---|
641 | 641 | | let userNumStr = userRecordArray[IdxLockUserNum] |
---|
642 | 642 | | let EMPTYSTR = "empty" |
---|
643 | 643 | | let poolWeight = if ((lpAssetIdStr != EMPTYSTR)) |
---|
644 | 644 | | then { |
---|
645 | 645 | | let poolAddressStr = valueOrErrorMessage(getString(factoryContract, keyFactoryLp2AssetsMapping(lpAssetIdStr)), ("unsupported lp asset " + lpAssetIdStr)) |
---|
646 | 646 | | getIntegerValue(factoryContract, keyFactoryPoolWeight(poolAddressStr)) |
---|
647 | 647 | | } |
---|
648 | 648 | | else if (readOnly) |
---|
649 | 649 | | then 0 |
---|
650 | 650 | | else throw(("not readonly mode: unsupported lp asset " + lpAssetIdStr)) |
---|
651 | 651 | | let userLpBoostEmissionLastIntegralKEY = keyUserLpBoostEmissionLastINTEGRAL(userNumStr, lpAssetIdStr) |
---|
652 | 652 | | let userBoostEmissionLastIntegralKEY = keyUserBoostEmissionLastINTEGRAL(userNumStr) |
---|
653 | 653 | | let userBoostEmissionLastIntegral = valueOrElse(getInteger(this, userLpBoostEmissionLastIntegralKEY), ioz(this, userBoostEmissionLastIntegralKEY)) |
---|
654 | 654 | | let boostEmissionIntegral = refreshBoostEmissionIntegral()._2 |
---|
655 | 655 | | let userBoostEmissionIntegral = (boostEmissionIntegral - userBoostEmissionLastIntegral) |
---|
656 | 656 | | if ((0 > userBoostEmissionIntegral)) |
---|
657 | 657 | | then throw("wrong calculations") |
---|
658 | 658 | | else { |
---|
659 | 659 | | let $t02690027039 = getStakedVotesIntegralsDiff(lpAssetIdStr, userAddressStr) |
---|
660 | 660 | | let stakedVotesIntegralsActions = $t02690027039._1 |
---|
661 | 661 | | let userVoteIntegralDiff = $t02690027039._2 |
---|
662 | 662 | | let totalVotesIntegralDiff = $t02690027039._3 |
---|
663 | 663 | | let poolUserBoostEmissionIntegral = fraction(userBoostEmissionIntegral, poolWeight, POOLWEIGHTMULT) |
---|
664 | 664 | | let userBoostAvaliableToClaimTotalNew = if ((totalVotesIntegralDiff == 0)) |
---|
665 | 665 | | then 0 |
---|
666 | 666 | | else fraction(poolUserBoostEmissionIntegral, userVoteIntegralDiff, totalVotesIntegralDiff) |
---|
667 | 667 | | let dataState = ([IntegerEntry(userLpBoostEmissionLastIntegralKEY, boostEmissionIntegral)] ++ stakedVotesIntegralsActions) |
---|
668 | 668 | | let debug = makeString([toString(userBoostEmissionLastIntegral), toString(userBoostEmissionIntegral), toString(poolWeight), toString(userVoteIntegralDiff), toString(totalVotesIntegralDiff)], ":") |
---|
669 | 669 | | $Tuple3(userBoostAvaliableToClaimTotalNew, dataState, debug) |
---|
670 | 670 | | } |
---|
671 | 671 | | } |
---|
672 | 672 | | } |
---|
673 | 673 | | |
---|
674 | 674 | | |
---|
675 | 675 | | func lockActions (i,duration) = { |
---|
676 | 676 | | let cfgArray = readConfigArrayOrFail() |
---|
677 | 677 | | let assetIdStr = cfgArray[IdxCfgAssetId] |
---|
678 | 678 | | let assetId = fromBase58String(assetIdStr) |
---|
679 | 679 | | let minLockAmount = parseIntValue(cfgArray[IdxCfgMinLockAmount]) |
---|
680 | 680 | | let minLockDuration = parseIntValue(cfgArray[IdxCfgMinLockDuration]) |
---|
681 | 681 | | let maxLockDuration = parseIntValue(cfgArray[IdxCfgMaxLockDuration]) |
---|
682 | 682 | | if ((size(i.payments) != 1)) |
---|
683 | 683 | | then throw("invalid payment - exact one payment must be attached") |
---|
684 | 684 | | else { |
---|
685 | 685 | | let pmt = i.payments[0] |
---|
686 | 686 | | let pmtAmount = pmt.amount |
---|
687 | 687 | | if ((assetId != value(pmt.assetId))) |
---|
688 | 688 | | then throw((("invalid asset is in payment - " + assetIdStr) + " is expected")) |
---|
689 | 689 | | else { |
---|
690 | 690 | | let nextUserNumKEY = keyNextUserNum() |
---|
691 | 691 | | let userAddressStr = toString(i.caller) |
---|
692 | 692 | | let userIsExisting = isDefined(getString(keyUser2NumMapping(userAddressStr))) |
---|
693 | 693 | | let userNumStr = if (userIsExisting) |
---|
694 | 694 | | then value(getString(keyUser2NumMapping(userAddressStr))) |
---|
695 | 695 | | else toString(iof(this, nextUserNumKEY)) |
---|
696 | 696 | | let userNum = parseIntValue(userNumStr) |
---|
697 | 697 | | let lockStart = height |
---|
698 | 698 | | let startBlockKEY = keyLockParamStartBlock(userNumStr) |
---|
699 | 699 | | let durationKEY = keyLockParamDuration(userNumStr) |
---|
700 | 700 | | let userAmountKEY = keyLockParamUserAmount(userNumStr) |
---|
701 | 701 | | if ((minLockAmount > pmtAmount)) |
---|
702 | 702 | | then throw(("amount is less then minLockAmount=" + toString(minLockAmount))) |
---|
703 | 703 | | else if ((minLockDuration > duration)) |
---|
704 | 704 | | then throw(("passed duration is less then minLockDuration=" + toString(minLockDuration))) |
---|
705 | 705 | | else if ((duration > maxLockDuration)) |
---|
706 | 706 | | then throw(("passed duration is greater then maxLockDuration=" + toString(maxLockDuration))) |
---|
707 | 707 | | else if (if (userIsExisting) |
---|
708 | 708 | | then ((iof(this, startBlockKEY) + iof(this, durationKEY)) >= lockStart) |
---|
709 | 709 | | else false) |
---|
710 | 710 | | then throw("there is an active lock - consider to use increaseLock") |
---|
711 | 711 | | else if ((ioz(this, userAmountKEY) > 0)) |
---|
712 | 712 | | then throw(("there are locked WXs - consider to use increaseLock " + userAmountKEY)) |
---|
713 | 713 | | else { |
---|
714 | 714 | | let coeffX8 = fraction(duration, MULT8, maxLockDuration) |
---|
715 | 715 | | let gWxAmountStart = fraction(pmtAmount, coeffX8, MULT8) |
---|
716 | 716 | | let gWxParamsResultList = aal(invoke(mathContract, "calcGwxParamsREADONLY", [gWxAmountStart, lockStart, duration], nil)) |
---|
717 | 717 | | let k = ai(gWxParamsResultList[0]) |
---|
718 | 718 | | let b = ai(gWxParamsResultList[1]) |
---|
719 | 719 | | let period = toString(ai(gWxParamsResultList[2])) |
---|
720 | 720 | | let totalCachedGwxRaw = getTotalCachedGwx(false) |
---|
721 | 721 | | let userBoostEmissionLastIntegralKEY = keyUserBoostEmissionLastINTEGRAL(userNumStr) |
---|
722 | 722 | | let boostEmissionIntegral = refreshBoostEmissionIntegral()._2 |
---|
723 | 723 | | let arr = if (userIsExisting) |
---|
724 | 724 | | then nil |
---|
725 | 725 | | else [IntegerEntry(nextUserNumKEY, (userNum + 1)), StringEntry(keyUser2NumMapping(userAddressStr), userNumStr), StringEntry(keyNum2UserMapping(userNumStr), userAddressStr)] |
---|
726 | 726 | | $Tuple2(((((arr ++ LockParamsEntry(userAddressStr, userNumStr, pmtAmount, lockStart, duration, k, b, period)) ++ StatsEntry(pmtAmount, duration, 1, if (userIsExisting) |
---|
727 | 727 | | then 0 |
---|
728 | 728 | | else 1)) :+ HistoryEntry("lock", userAddressStr, pmtAmount, lockStart, duration, k, b, i)) ++ [IntegerEntry(userBoostEmissionLastIntegralKEY, boostEmissionIntegral), IntegerEntry(keyTotalCachedGwx(), (totalCachedGwxRaw + gWxAmountStart))]), gWxAmountStart) |
---|
729 | 729 | | } |
---|
730 | 730 | | } |
---|
731 | 731 | | } |
---|
732 | 732 | | } |
---|
733 | 733 | | |
---|
734 | 734 | | |
---|
735 | 735 | | @Callable(i) |
---|
736 | 736 | | func constructor (factoryAddressStr,lockAssetIdStr,minLockAmount,minDuration,maxDuration,mathContract) = { |
---|
737 | 737 | | let checkCaller = mustManager(i) |
---|
738 | 738 | | if ((checkCaller == checkCaller)) |
---|
739 | 739 | | then ([IntegerEntry(keyNextUserNum(), 0), StringEntry(keyConfig(), formatConfig(lockAssetIdStr, minLockAmount, minDuration, maxDuration, mathContract)), StringEntry(keyFactoryAddress(), factoryAddressStr)] ++ StatsEntry(0, 0, 0, 0)) |
---|
740 | 740 | | else throw("Strict value is not equal to itself.") |
---|
741 | 741 | | } |
---|
742 | 742 | | |
---|
743 | 743 | | |
---|
744 | 744 | | |
---|
745 | 745 | | @Callable(i) |
---|
746 | 746 | | func lockRef (duration,referrerAddress,signature) = { |
---|
747 | 747 | | let $t03158931654 = lockActions(i, duration) |
---|
748 | 748 | | let lockActionsResult = $t03158931654._1 |
---|
749 | 749 | | let gWxAmountStart = $t03158931654._2 |
---|
750 | 750 | | let referralAddress = toString(i.caller) |
---|
751 | 751 | | let refInv = if (if ((referrerAddress == "")) |
---|
752 | 752 | | then true |
---|
753 | 753 | | else (signature == base58'')) |
---|
754 | 754 | | then unit |
---|
755 | 755 | | else invoke(referralsContractAddressOrFail, "createPair", [referralProgramName, referrerAddress, referralAddress, signature], nil) |
---|
756 | 756 | | if ((refInv == refInv)) |
---|
757 | 757 | | then { |
---|
758 | 758 | | let updateRefActivity = invoke(mathContract, "updateReferralActivity", [toString(i.caller), gWxAmountStart], nil) |
---|
759 | 759 | | if ((updateRefActivity == updateRefActivity)) |
---|
760 | 760 | | then $Tuple2(lockActionsResult, unit) |
---|
761 | 761 | | else throw("Strict value is not equal to itself.") |
---|
762 | 762 | | } |
---|
763 | 763 | | else throw("Strict value is not equal to itself.") |
---|
764 | 764 | | } |
---|
765 | 765 | | |
---|
766 | 766 | | |
---|
767 | 767 | | |
---|
768 | 768 | | @Callable(i) |
---|
769 | 769 | | func lock (duration) = { |
---|
770 | 770 | | let $t03211232177 = lockActions(i, duration) |
---|
771 | 771 | | let lockActionsResult = $t03211232177._1 |
---|
772 | 772 | | let gWxAmountStart = $t03211232177._2 |
---|
773 | 773 | | let updateRefActivity = invoke(mathContract, "updateReferralActivity", [toString(i.caller), gWxAmountStart], nil) |
---|
774 | 774 | | if ((updateRefActivity == updateRefActivity)) |
---|
775 | 775 | | then $Tuple2(lockActionsResult, unit) |
---|
776 | 776 | | else throw("Strict value is not equal to itself.") |
---|
777 | 777 | | } |
---|
778 | 778 | | |
---|
779 | 779 | | |
---|
780 | 780 | | |
---|
781 | 781 | | @Callable(i) |
---|
782 | 782 | | func increaseLock (deltaDuration) = { |
---|
783 | 783 | | let cfgArray = readConfigArrayOrFail() |
---|
784 | 784 | | let assetIdStr = cfgArray[IdxCfgAssetId] |
---|
785 | 785 | | let assetId = fromBase58String(assetIdStr) |
---|
786 | 786 | | let minLockDuration = parseIntValue(cfgArray[IdxCfgMinLockDuration]) |
---|
787 | 787 | | let maxLockDuration = parseIntValue(cfgArray[IdxCfgMaxLockDuration]) |
---|
788 | 788 | | let pmtAmount = extractOptionalPaymentAmountOrFail(i, assetId) |
---|
789 | 789 | | let userAddressStr = toString(i.caller) |
---|
790 | 790 | | let userRecordArray = readLockParamsRecordOrFail(userAddressStr) |
---|
791 | 791 | | let userNumStr = userRecordArray[IdxLockUserNum] |
---|
792 | 792 | | let userAmount = parseIntValue(userRecordArray[IdxLockAmount]) |
---|
793 | 793 | | let lockStart = parseIntValue(userRecordArray[IdxLockStart]) |
---|
794 | 794 | | let lockDuration = parseIntValue(userRecordArray[IdxLockDuration]) |
---|
795 | 795 | | let lockEnd = (lockStart + lockDuration) |
---|
796 | 796 | | let remainingDuration = max([(lockEnd - height), 0]) |
---|
797 | 797 | | let userAmountNew = (userAmount + pmtAmount) |
---|
798 | 798 | | let lockDurationNew = (remainingDuration + deltaDuration) |
---|
799 | 799 | | if ((0 > deltaDuration)) |
---|
800 | 800 | | then throw("duration is less then zero") |
---|
801 | 801 | | else if ((minLockDuration > lockDurationNew)) |
---|
802 | 802 | | then throw(("lockDurationNew is less then minLockDuration=" + toString(minLockDuration))) |
---|
803 | 803 | | else if ((lockDurationNew > maxLockDuration)) |
---|
804 | 804 | | then throw(("deltaDuration + existedLockDuration is greater then maxLockDuration=" + toString(maxLockDuration))) |
---|
805 | 805 | | else { |
---|
806 | 806 | | let coeffX8 = fraction(lockDurationNew, MULT8, maxLockDuration) |
---|
807 | 807 | | let gWxAmountStart = fraction(userAmountNew, coeffX8, MULT8) |
---|
808 | 808 | | let updateRefActivity = invoke(mathContract, "updateReferralActivity", [toString(i.caller), gWxAmountStart], nil) |
---|
809 | 809 | | if ((updateRefActivity == updateRefActivity)) |
---|
810 | 810 | | then { |
---|
811 | 811 | | let lockStartNew = height |
---|
812 | 812 | | let gWxParamsResultList = aal(invoke(mathContract, "calcGwxParamsREADONLY", [gWxAmountStart, lockStartNew, lockDurationNew], nil)) |
---|
813 | 813 | | let k = ai(gWxParamsResultList[0]) |
---|
814 | 814 | | let b = ai(gWxParamsResultList[1]) |
---|
815 | 815 | | let period = toString(ai(gWxParamsResultList[2])) |
---|
816 | 816 | | let currUserGwx = calcCurrentGwxAmount(userAddressStr) |
---|
817 | 817 | | let gwxDiff = (gWxAmountStart - currUserGwx) |
---|
818 | 818 | | if ((0 > gwxDiff)) |
---|
819 | 819 | | then throw(("gwxDiff is less then 0: " + toString(gwxDiff))) |
---|
820 | 820 | | else { |
---|
821 | 821 | | let totalCachedGwxRaw = getTotalCachedGwx(false) |
---|
822 | 822 | | let totalCachedGwxCorrected = getTotalCachedGwx(true) |
---|
823 | 823 | | (((LockParamsEntry(userAddressStr, userNumStr, userAmountNew, lockStartNew, lockDurationNew, k, b, period) ++ StatsEntry(pmtAmount, deltaDuration, 0, 0)) :+ HistoryEntry("lock", userAddressStr, pmtAmount, lockStart, lockDurationNew, k, b, i)) ++ [IntegerEntry(keyTotalCachedGwx(), (totalCachedGwxRaw + gwxDiff))]) |
---|
824 | 824 | | } |
---|
825 | 825 | | } |
---|
826 | 826 | | else throw("Strict value is not equal to itself.") |
---|
827 | 827 | | } |
---|
828 | 828 | | } |
---|
829 | 829 | | |
---|
830 | 830 | | |
---|
831 | 831 | | |
---|
832 | 832 | | @Callable(i) |
---|
833 | 833 | | func claimWxBoost (lpAssetIdStr,userAddressStr) = if ((stakingContract != i.caller)) |
---|
834 | 834 | | then throw("permissions denied") |
---|
835 | 835 | | else { |
---|
836 | 836 | | let $t03526435366 = internalClaimWxBoost(lpAssetIdStr, userAddressStr, false) |
---|
837 | 837 | | let userBoostAvailable = $t03526435366._1 |
---|
838 | 838 | | let dataState = $t03526435366._2 |
---|
839 | 839 | | let debug = $t03526435366._3 |
---|
840 | 840 | | $Tuple2(dataState, [userBoostAvailable]) |
---|
841 | 841 | | } |
---|
842 | 842 | | |
---|
843 | 843 | | |
---|
844 | 844 | | |
---|
845 | 845 | | @Callable(i) |
---|
846 | 846 | | func claimWxBoostREADONLY (lpAssetIdStr,userAddressStr) = { |
---|
847 | 847 | | let $t03549835599 = internalClaimWxBoost(lpAssetIdStr, userAddressStr, true) |
---|
848 | 848 | | let userBoostAvailable = $t03549835599._1 |
---|
849 | 849 | | let dataState = $t03549835599._2 |
---|
850 | 850 | | let debug = $t03549835599._3 |
---|
851 | 851 | | $Tuple2(nil, [userBoostAvailable, debug]) |
---|
852 | 852 | | } |
---|
853 | 853 | | |
---|
854 | 854 | | |
---|
855 | 855 | | |
---|
856 | 856 | | @Callable(i) |
---|
857 | 857 | | func unlock (userAddress) = { |
---|
858 | 858 | | let userRecordArray = readLockParamsRecordOrFail(userAddress) |
---|
859 | 859 | | let userNumStr = userRecordArray[IdxLockUserNum] |
---|
860 | 860 | | let userAmount = parseIntValue(userRecordArray[IdxLockAmount]) |
---|
861 | 861 | | let lockStart = parseIntValue(userRecordArray[IdxLockStart]) |
---|
862 | 862 | | let lockDuration = parseIntValue(userRecordArray[IdxLockDuration]) |
---|
863 | 863 | | let lockEnd = (lockStart + lockDuration) |
---|
864 | 864 | | let cfgArray = readConfigArrayOrFail() |
---|
865 | 865 | | let assetId = fromBase58String(cfgArray[IdxCfgAssetId]) |
---|
866 | 866 | | if ((lockEnd >= height)) |
---|
867 | 867 | | then throw((("wait " + toString(lockEnd)) + " to unlock")) |
---|
868 | 868 | | else if ((0 >= userAmount)) |
---|
869 | 869 | | then throw("nothing to unlock") |
---|
870 | 870 | | else { |
---|
871 | 871 | | let period = valueOrElse(getInteger(mathContract, keyNextPeriod()), 0) |
---|
872 | 872 | | (((LockParamsEntry(userAddress, userNumStr, 0, lockStart, lockDuration, 0, 0, toString(period)) ++ StatsEntry(-(userAmount), 0, 0, -1)) :+ HistoryEntry("unlock", userAddress, userAmount, lockStart, lockDuration, 0, 0, i)) :+ ScriptTransfer(addressFromStringValue(userAddress), userAmount, assetId)) |
---|
873 | 873 | | } |
---|
874 | 874 | | } |
---|
875 | 875 | | |
---|
876 | 876 | | |
---|
877 | 877 | | |
---|
878 | 878 | | @Callable(i) |
---|
879 | 879 | | func gwxUserInfoREADONLY (userAddress) = { |
---|
880 | 880 | | let gwxAmount = calcCurrentGwxAmount(userAddress) |
---|
881 | 881 | | $Tuple2(nil, [gwxAmount]) |
---|
882 | 882 | | } |
---|
883 | 883 | | |
---|
884 | 884 | | |
---|
885 | 885 | | |
---|
886 | 886 | | @Callable(i) |
---|
887 | 887 | | func getUserGwxAmountAtHeightREADONLY (userAddress,targetHeight) = { |
---|
888 | 888 | | let gwxAmount = calcUserGwxAmountAtHeight(userAddress, targetHeight) |
---|
889 | 889 | | $Tuple2(nil, gwxAmount) |
---|
890 | 890 | | } |
---|
891 | 891 | | |
---|
892 | 892 | | |
---|
893 | 893 | | |
---|
894 | 894 | | @Callable(i) |
---|
895 | 895 | | func getTotalCachedGwxREADONLY () = $Tuple2(nil, getTotalCachedGwx(true)) |
---|
896 | 896 | | |
---|
897 | 897 | | |
---|
898 | 898 | | |
---|
899 | 899 | | @Callable(i) |
---|
900 | 900 | | func onBoostEmissionUpdate () = { |
---|
901 | 901 | | let checkCaller = if ((i.caller == emissionContract)) |
---|
902 | 902 | | then true |
---|
903 | 903 | | else mustManager(i) |
---|
904 | 904 | | if ((checkCaller == checkCaller)) |
---|
905 | 905 | | then refreshBoostEmissionIntegral() |
---|
906 | 906 | | else throw("Strict value is not equal to itself.") |
---|
907 | 907 | | } |
---|
908 | 908 | | |
---|
909 | 909 | | |
---|
910 | 910 | | |
---|
911 | 911 | | @Callable(i) |
---|
912 | 912 | | func onStakedVoteUpdate (lpAssetIdStr,userAddressStr,edge) = { |
---|
913 | 913 | | let checkCaller = if ((i.caller == stakingContract)) |
---|
914 | 914 | | then true |
---|
915 | 915 | | else mustManager(i) |
---|
916 | 916 | | if ((checkCaller == checkCaller)) |
---|
917 | 917 | | then { |
---|
918 | 918 | | let actions = refreshVoteStakedIntegral(lpAssetIdStr, userAddressStr, edge) |
---|