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 | | let contractFilename = "boosting.ride" |
---|
13 | 13 | | |
---|
14 | 14 | | let SCALE18 = 18 |
---|
15 | 15 | | |
---|
16 | 16 | | let MULT18 = 1000000000000000000 |
---|
17 | 17 | | |
---|
18 | 18 | | let MULT18BI = toBigInt(MULT18) |
---|
19 | 19 | | |
---|
20 | 20 | | let blocksInDay = 1440 |
---|
21 | 21 | | |
---|
22 | 22 | | let blocksInMonth = 43800 |
---|
23 | 23 | | |
---|
24 | 24 | | func wrapErr (msg) = makeString([contractFilename, ": ", msg], "") |
---|
25 | 25 | | |
---|
26 | 26 | | |
---|
27 | 27 | | func throwErr (msg) = throw(wrapErr(msg)) |
---|
28 | 28 | | |
---|
29 | 29 | | |
---|
30 | 30 | | func getStringOrFail (address,key) = valueOrErrorMessage(getString(address, key), wrapErr((("mandatory this." + key) + " is not defined"))) |
---|
31 | 31 | | |
---|
32 | 32 | | |
---|
33 | 33 | | func getIntOrZero (address,key) = valueOrElse(getInteger(address, key), 0) |
---|
34 | 34 | | |
---|
35 | 35 | | |
---|
36 | 36 | | func getIntOrDefault (address,key,defaultVal) = valueOrElse(getInteger(address, key), defaultVal) |
---|
37 | 37 | | |
---|
38 | 38 | | |
---|
39 | 39 | | func getIntOrFail (address,key) = valueOrErrorMessage(getInteger(address, key), wrapErr((("mandatory this." + key) + " is not defined"))) |
---|
40 | 40 | | |
---|
41 | 41 | | |
---|
42 | 42 | | func abs (val) = if ((0 > val)) |
---|
43 | 43 | | then -(val) |
---|
44 | 44 | | else val |
---|
45 | 45 | | |
---|
46 | 46 | | |
---|
47 | 47 | | func ensurePositive (v,m) = if ((v >= 0)) |
---|
48 | 48 | | then v |
---|
49 | 49 | | else throwErr((valueOrElse(m, "value") + " should be positive")) |
---|
50 | 50 | | |
---|
51 | 51 | | |
---|
52 | 52 | | func keyReferralsContractAddress () = makeString(["%s%s", "config", "referralsContractAddress"], SEP) |
---|
53 | 53 | | |
---|
54 | 54 | | |
---|
55 | 55 | | let referralsContractAddressOrFail = addressFromStringValue(getStringOrFail(this, keyReferralsContractAddress())) |
---|
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 | | func keyFactoryAddress () = "%s%s__config__factoryAddress" |
---|
64 | 64 | | |
---|
65 | 65 | | |
---|
66 | 66 | | let IdxFactoryCfgStakingDapp = 1 |
---|
67 | 67 | | |
---|
68 | 68 | | let IdxFactoryCfgBoostingDapp = 2 |
---|
69 | 69 | | |
---|
70 | 70 | | let IdxFactoryCfgIdoDapp = 3 |
---|
71 | 71 | | |
---|
72 | 72 | | let IdxFactoryCfgTeamDapp = 4 |
---|
73 | 73 | | |
---|
74 | 74 | | let IdxFactoryCfgEmissionDapp = 5 |
---|
75 | 75 | | |
---|
76 | 76 | | let IdxFactoryCfgRestDapp = 6 |
---|
77 | 77 | | |
---|
78 | 78 | | let IdxFactoryCfgSlippageDapp = 7 |
---|
79 | 79 | | |
---|
80 | 80 | | let IdxFactoryCfgDaoDapp = 8 |
---|
81 | 81 | | |
---|
82 | 82 | | let IdxFactoryCfgMarketingDapp = 9 |
---|
83 | 83 | | |
---|
84 | 84 | | let IdxFactoryCfgGwxRewardDapp = 10 |
---|
85 | 85 | | |
---|
86 | 86 | | let IdxFactoryCfgBirdsDapp = 11 |
---|
87 | 87 | | |
---|
88 | 88 | | func keyFactoryCfg () = "%s__factoryConfig" |
---|
89 | 89 | | |
---|
90 | 90 | | |
---|
91 | 91 | | func keyFactoryLpAssetToPoolContractAddress (lpAssetStr) = makeString(["%s%s%s", lpAssetStr, "mappings__lpAsset2PoolContract"], SEP) |
---|
92 | 92 | | |
---|
93 | 93 | | |
---|
94 | 94 | | func keyFactoryPoolWeight (contractAddress) = makeString(["%s%s", "poolWeight", contractAddress], SEP) |
---|
95 | 95 | | |
---|
96 | 96 | | |
---|
97 | 97 | | func keyFactoryPoolWeightHistory (poolAddress,num) = ((("%s%s__poolWeight__" + poolAddress) + "__") + toString(num)) |
---|
98 | 98 | | |
---|
99 | 99 | | |
---|
100 | 100 | | func readFactoryAddressOrFail () = addressFromStringValue(getStringOrFail(this, keyFactoryAddress())) |
---|
101 | 101 | | |
---|
102 | 102 | | |
---|
103 | 103 | | func readFactoryCfgOrFail (factory) = split(getStringOrFail(factory, keyFactoryCfg()), SEP) |
---|
104 | 104 | | |
---|
105 | 105 | | |
---|
106 | 106 | | func getBoostingAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgBoostingDapp]) |
---|
107 | 107 | | |
---|
108 | 108 | | |
---|
109 | 109 | | func getEmissionAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgEmissionDapp]) |
---|
110 | 110 | | |
---|
111 | 111 | | |
---|
112 | 112 | | func getStakingAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgStakingDapp]) |
---|
113 | 113 | | |
---|
114 | 114 | | |
---|
115 | 115 | | func getGwxRewardAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgGwxRewardDapp]) |
---|
116 | 116 | | |
---|
117 | 117 | | |
---|
118 | 118 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
119 | 119 | | |
---|
120 | 120 | | |
---|
121 | 121 | | func keyManagerVaultAddress () = "%s__managerVaultAddress" |
---|
122 | 122 | | |
---|
123 | 123 | | |
---|
124 | 124 | | func keyEmissionRatePerBlockCurrent () = "%s%s__ratePerBlock__current" |
---|
125 | 125 | | |
---|
126 | 126 | | |
---|
127 | 127 | | func keyEmissionRatePerBlockMaxCurrent () = "%s%s__ratePerBlockMax__current" |
---|
128 | 128 | | |
---|
129 | 129 | | |
---|
130 | 130 | | func keyEmissionStartBlock () = "%s%s__emission__startBlock" |
---|
131 | 131 | | |
---|
132 | 132 | | |
---|
133 | 133 | | func keyBoostingV2LastUpdateHeight () = "%s%s__boostingV2__startBlock" |
---|
134 | 134 | | |
---|
135 | 135 | | |
---|
136 | 136 | | func keyBoostingV2Integral () = "%s%s__boostingV2__integral" |
---|
137 | 137 | | |
---|
138 | 138 | | |
---|
139 | 139 | | func keyEmissionDurationInBlocks () = "%s%s__emission__duration" |
---|
140 | 140 | | |
---|
141 | 141 | | |
---|
142 | 142 | | func keyEmissionEndBlock () = "%s%s__emission__endBlock" |
---|
143 | 143 | | |
---|
144 | 144 | | |
---|
145 | 145 | | func keyNextPergetIntOrDefault () = "%s__nextPeriod" |
---|
146 | 146 | | |
---|
147 | 147 | | |
---|
148 | 148 | | func keyGwxRewardEmissionStartHeight () = "%s%s__gwxRewardEmissionPart__startHeight" |
---|
149 | 149 | | |
---|
150 | 150 | | |
---|
151 | 151 | | let IdxCfgAssetId = 1 |
---|
152 | 152 | | |
---|
153 | 153 | | let IdxCfgMinLockAmount = 2 |
---|
154 | 154 | | |
---|
155 | 155 | | let IdxCfgMinLockDuration = 3 |
---|
156 | 156 | | |
---|
157 | 157 | | let IdxCfgMaxLockDuration = 4 |
---|
158 | 158 | | |
---|
159 | 159 | | let IdxCfgMathContract = 5 |
---|
160 | 160 | | |
---|
161 | 161 | | func keyConfig () = "%s__config" |
---|
162 | 162 | | |
---|
163 | 163 | | |
---|
164 | 164 | | func readConfigArrayOrFail () = split(getStringOrFail(this, keyConfig()), SEP) |
---|
165 | 165 | | |
---|
166 | 166 | | |
---|
167 | 167 | | let cfgArray = readConfigArrayOrFail() |
---|
168 | 168 | | |
---|
169 | 169 | | let assetId = fromBase58String(cfgArray[IdxCfgAssetId]) |
---|
170 | 170 | | |
---|
171 | 171 | | let minLockAmount = valueOrErrorMessage(parseInt(cfgArray[IdxCfgMinLockAmount]), wrapErr("invalid min lock amount")) |
---|
172 | 172 | | |
---|
173 | 173 | | let minLockDuration = valueOrErrorMessage(parseInt(cfgArray[IdxCfgMinLockDuration]), wrapErr("invalid min lock duration")) |
---|
174 | 174 | | |
---|
175 | 175 | | let maxLockDuration = valueOrErrorMessage(parseInt(cfgArray[IdxCfgMaxLockDuration]), wrapErr("invalid max lock duration")) |
---|
176 | 176 | | |
---|
177 | 177 | | let mathContract = valueOrErrorMessage(addressFromString(cfgArray[IdxCfgMathContract]), wrapErr("invalid math contract address")) |
---|
178 | 178 | | |
---|
179 | 179 | | func formatConfigS (assetId,minLockAmount,minLockDuration,maxLockDuration,mathContract) = makeString(["%s%d%d%d%s", assetId, minLockAmount, minLockDuration, maxLockDuration, mathContract], SEP) |
---|
180 | 180 | | |
---|
181 | 181 | | |
---|
182 | 182 | | func formatConfig (assetId,minLockAmount,minLockDuration,maxLockDuration,mathContract) = formatConfigS(assetId, toString(minLockAmount), toString(minLockDuration), toString(maxLockDuration), mathContract) |
---|
183 | 183 | | |
---|
184 | 184 | | |
---|
185 | 185 | | func getManagerVaultAddressOrThis () = match getString(keyManagerVaultAddress()) { |
---|
186 | 186 | | case s: String => |
---|
187 | 187 | | addressFromStringValue(s) |
---|
188 | 188 | | case _ => |
---|
189 | 189 | | this |
---|
190 | 190 | | } |
---|
191 | 191 | | |
---|
192 | 192 | | |
---|
193 | 193 | | func managerPublicKeyOrUnit () = { |
---|
194 | 194 | | let managerVaultAddress = getManagerVaultAddressOrThis() |
---|
195 | 195 | | match getString(managerVaultAddress, keyManagerPublicKey()) { |
---|
196 | 196 | | case s: String => |
---|
197 | 197 | | fromBase58String(s) |
---|
198 | 198 | | case _: Unit => |
---|
199 | 199 | | unit |
---|
200 | 200 | | case _ => |
---|
201 | 201 | | throw("Match error") |
---|
202 | 202 | | } |
---|
203 | 203 | | } |
---|
204 | 204 | | |
---|
205 | 205 | | |
---|
206 | 206 | | func mustManager (i) = { |
---|
207 | 207 | | let pd = throwErr("Permission denied") |
---|
208 | 208 | | match managerPublicKeyOrUnit() { |
---|
209 | 209 | | case pk: ByteVector => |
---|
210 | 210 | | if ((i.callerPublicKey == pk)) |
---|
211 | 211 | | then true |
---|
212 | 212 | | else pd |
---|
213 | 213 | | case _: Unit => |
---|
214 | 214 | | if ((i.caller == this)) |
---|
215 | 215 | | then true |
---|
216 | 216 | | else pd |
---|
217 | 217 | | case _ => |
---|
218 | 218 | | throw("Match error") |
---|
219 | 219 | | } |
---|
220 | 220 | | } |
---|
221 | 221 | | |
---|
222 | 222 | | |
---|
223 | 223 | | let IdxLockAmount = 1 |
---|
224 | 224 | | |
---|
225 | 225 | | let IdxLockStart = 2 |
---|
226 | 226 | | |
---|
227 | 227 | | let IdxLockDuration = 3 |
---|
228 | 228 | | |
---|
229 | 229 | | let IdxLockLastUpdateTimestamp = 4 |
---|
230 | 230 | | |
---|
231 | 231 | | let IdxLockGwxAmount = 5 |
---|
232 | 232 | | |
---|
233 | 233 | | let IdxLockWxClaimed = 6 |
---|
234 | 234 | | |
---|
235 | 235 | | func keyLockParamsRecord (userAddress,txId) = makeString(["%s%s%s__lock", toString(userAddress), match txId { |
---|
236 | 236 | | case b: ByteVector => |
---|
237 | 237 | | toBase58String(b) |
---|
238 | 238 | | case _: Unit => |
---|
239 | 239 | | "legacy" |
---|
240 | 240 | | case _ => |
---|
241 | 241 | | throw("Match error") |
---|
242 | 242 | | }], SEP) |
---|
243 | 243 | | |
---|
244 | 244 | | |
---|
245 | 245 | | func readLockParamsRecordOrFail (userAddress,txId) = split(getStringOrFail(this, keyLockParamsRecord(userAddress, txId)), SEP) |
---|
246 | 246 | | |
---|
247 | 247 | | |
---|
248 | 248 | | func keyUserGwxAmountTotal (userAddress) = makeString(["%s%s__gwxAmountTotal", toString(userAddress)], SEP) |
---|
249 | 249 | | |
---|
250 | 250 | | |
---|
251 | 251 | | func formatLockParamsRecord (amount,start,duration,gwxAmount,wxClaimed) = makeString(["%d%d%d%d%d%d%d", toString(amount), toString(start), toString(duration), toString(lastBlock.timestamp), toString(gwxAmount), toString(wxClaimed)], SEP) |
---|
252 | 252 | | |
---|
253 | 253 | | |
---|
254 | 254 | | func keyNextUserNum () = "%s__nextUserNum" |
---|
255 | 255 | | |
---|
256 | 256 | | |
---|
257 | 257 | | func keyUser2NumMapping (userAddress) = makeString(["%s%s%s__mapping__user2num", userAddress], SEP) |
---|
258 | 258 | | |
---|
259 | 259 | | |
---|
260 | 260 | | func keyNum2UserMapping (num) = makeString(["%s%s%s__mapping__num2user", num], SEP) |
---|
261 | 261 | | |
---|
262 | 262 | | |
---|
263 | 263 | | func keyLockParamTotalAmount () = "%s%s__stats__activeTotalLocked" |
---|
264 | 264 | | |
---|
265 | 265 | | |
---|
266 | 266 | | func keyStatsLocksDurationSumInBlocks () = "%s%s__stats__locksDurationSumInBlocks" |
---|
267 | 267 | | |
---|
268 | 268 | | |
---|
269 | 269 | | func keyStatsLocksCount () = "%s%s__stats__locksCount" |
---|
270 | 270 | | |
---|
271 | 271 | | |
---|
272 | 272 | | func keyStatsUsersCount () = "%s%s__stats__activeUsersCount" |
---|
273 | 273 | | |
---|
274 | 274 | | |
---|
275 | 275 | | func keyUserBoostEmissionLastINTEGRAL (userNum) = makeString(["%s%d__userBoostEmissionLastIntV2", toString(userNum)], SEP) |
---|
276 | 276 | | |
---|
277 | 277 | | |
---|
278 | 278 | | func keyUserLpBoostEmissionLastINTEGRAL (userNum,lpAssetId) = makeString(["%s%d__userBoostEmissionLastIntV2", toString(userNum), lpAssetId], SEP) |
---|
279 | 279 | | |
---|
280 | 280 | | |
---|
281 | 281 | | func keyUserMaxBoostINTEGRAL (userNum) = makeString(["%s%d__maxBoostInt", toString(userNum)], SEP) |
---|
282 | 282 | | |
---|
283 | 283 | | |
---|
284 | 284 | | func keyTotalMaxBoostINTEGRAL () = "%s%s__maxBoostInt__total" |
---|
285 | 285 | | |
---|
286 | 286 | | |
---|
287 | 287 | | func keyUserBoostAvalaibleToClaimTotal (userNum) = makeString(["%s%d__userBoostAvaliableToClaimTotal", toString(userNum)], SEP) |
---|
288 | 288 | | |
---|
289 | 289 | | |
---|
290 | 290 | | func keyUserBoostClaimed (userNum) = makeString(["%s%d__userBoostClaimed", toString(userNum)], SEP) |
---|
291 | 291 | | |
---|
292 | 292 | | |
---|
293 | 293 | | func keyGwxTotal () = "%s%s__gwx__total" |
---|
294 | 294 | | |
---|
295 | 295 | | |
---|
296 | 296 | | func keyVote (amountAssetId,priceAssetId,address,epoch) = makeString(["%s%s%s%s%d", "vote", amountAssetId, priceAssetId, toString(address), toString(epoch)], SEP) |
---|
297 | 297 | | |
---|
298 | 298 | | |
---|
299 | 299 | | func keyStartHeightByEpoch (epoch) = makeString(["%s%d", "startHeight", toString(epoch)], SEP) |
---|
300 | 300 | | |
---|
301 | 301 | | |
---|
302 | 302 | | func keyCurrentEpochUi () = makeString(["%s", "currentEpochUi"], SEP) |
---|
303 | 303 | | |
---|
304 | 304 | | |
---|
305 | 305 | | func keyVotingResultStaked (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStaked", lpAssetIdStr, toString(epoch)], SEP) |
---|
306 | 306 | | |
---|
307 | 307 | | |
---|
308 | 308 | | func keyVotingResultStakedIntegral (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStakedIntegral", lpAssetIdStr, toString(epoch)], SEP) |
---|
309 | 309 | | |
---|
310 | 310 | | |
---|
311 | 311 | | func keyVotingResultStakedLastUpdateHeight (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStakedIntegralLastUpdateHeight", lpAssetIdStr, toString(epoch)], SEP) |
---|
312 | 312 | | |
---|
313 | 313 | | |
---|
314 | 314 | | func keyVotingResultStakedIntegralLast (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "votingResultStakedIntegralLast", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
315 | 315 | | |
---|
316 | 316 | | |
---|
317 | 317 | | func keyVoteStakedIntegral (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "voteStakedIntegral", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
318 | 318 | | |
---|
319 | 319 | | |
---|
320 | 320 | | func keyVoteStakedLastUpdateHeight (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "voteStakedIntegralLastUpdateHeight", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
321 | 321 | | |
---|
322 | 322 | | |
---|
323 | 323 | | func keyVoteStakedIntegralLast (lpAssetIdStr,address,epoch) = makeString(["%s%s%s%d", "voteStakedIntegralLast", lpAssetIdStr, toString(address), toString(epoch)], SEP) |
---|
324 | 324 | | |
---|
325 | 325 | | |
---|
326 | 326 | | func keyStakedByUser (userAddressStr,lpAssetIdStr) = makeString(["%s%s%s", "staked", userAddressStr, lpAssetIdStr], SEP) |
---|
327 | 327 | | |
---|
328 | 328 | | |
---|
329 | 329 | | let factoryContract = readFactoryAddressOrFail() |
---|
330 | 330 | | |
---|
331 | 331 | | let factoryCfg = readFactoryCfgOrFail(factoryContract) |
---|
332 | 332 | | |
---|
333 | 333 | | let emissionContract = getEmissionAddressOrFail(factoryCfg) |
---|
334 | 334 | | |
---|
335 | 335 | | let stakingContract = getStakingAddressOrFail(factoryCfg) |
---|
336 | 336 | | |
---|
337 | 337 | | let gwxRewardContract = getGwxRewardAddressOrFail(factoryCfg) |
---|
338 | 338 | | |
---|
339 | 339 | | let lpStakingPoolsContract = valueOrErrorMessage(addressFromString(valueOrErrorMessage(getString(makeString(["%s", "lpStakingPoolsContract"], SEP)), wrapErr("lp_staking_pools contract address is undefined"))), wrapErr("invalid lp_staking_pools contract address")) |
---|
340 | 340 | | |
---|
341 | 341 | | let keyVotingEmissionContract = makeString(["%s", "votingEmissionContract"], SEP) |
---|
342 | 342 | | |
---|
343 | 343 | | let votingEmissionContract = addressFromStringValue(getStringValue(factoryContract, keyVotingEmissionContract)) |
---|
344 | 344 | | |
---|
345 | 345 | | let keyVotingEmissionRateContract = makeString(["%s", "votingEmissionRateContract"], SEP) |
---|
346 | 346 | | |
---|
347 | 347 | | let boostCoeff = { |
---|
348 | 348 | | let @ = invoke(emissionContract, "getBoostCoeffREADONLY", nil, nil) |
---|
349 | 349 | | if ($isInstanceOf(@, "Int")) |
---|
350 | 350 | | then @ |
---|
351 | 351 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
352 | 352 | | } |
---|
353 | 353 | | |
---|
354 | 354 | | func userNumberByAddressOrFail (userAddress) = match getString(this, keyUser2NumMapping(toString(userAddress))) { |
---|
355 | 355 | | case s: String => |
---|
356 | 356 | | valueOrErrorMessage(parseInt(s), wrapErr("invalid user number")) |
---|
357 | 357 | | case _: Unit => |
---|
358 | 358 | | throwErr("invalid user") |
---|
359 | 359 | | case _ => |
---|
360 | 360 | | throw("Match error") |
---|
361 | 361 | | } |
---|
362 | 362 | | |
---|
363 | 363 | | |
---|
364 | 364 | | func getGwxAmountTotal () = valueOrElse(getInteger(this, keyGwxTotal()), 0) |
---|
365 | 365 | | |
---|
366 | 366 | | |
---|
367 | 367 | | func getLockedGwxAmount (userAddress) = { |
---|
368 | 368 | | let functionName = "getLockedGwxAmount" |
---|
369 | 369 | | let votingEmissionRateContract = valueOrErrorMessage( match getString(votingEmissionContract, keyVotingEmissionRateContract) { |
---|
370 | 370 | | case _: Unit => |
---|
371 | 371 | | unit |
---|
372 | 372 | | case s: String => |
---|
373 | 373 | | addressFromString(s) |
---|
374 | 374 | | case _ => |
---|
375 | 375 | | throw("Match error") |
---|
376 | 376 | | }, wrapErr("invalid voting emission rate address")) |
---|
377 | 377 | | let lockedVotingEmissionRate = { |
---|
378 | 378 | | let @ = invoke(votingEmissionContract, functionName, [toString(userAddress)], nil) |
---|
379 | 379 | | if ($isInstanceOf(@, "Int")) |
---|
380 | 380 | | then @ |
---|
381 | 381 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
382 | 382 | | } |
---|
383 | 383 | | let lockedVotingEmission = { |
---|
384 | 384 | | let @ = invoke(votingEmissionRateContract, functionName, [toString(userAddress)], nil) |
---|
385 | 385 | | if ($isInstanceOf(@, "Int")) |
---|
386 | 386 | | then @ |
---|
387 | 387 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
388 | 388 | | } |
---|
389 | 389 | | let locked = max([lockedVotingEmissionRate, lockedVotingEmission]) |
---|
390 | 390 | | locked |
---|
391 | 391 | | } |
---|
392 | 392 | | |
---|
393 | 393 | | |
---|
394 | 394 | | func HistoryEntry (type,user,amount,lockStart,duration,gwxAmount,i) = { |
---|
395 | 395 | | let historyKEY = makeString(["%s%s%s%s__history", type, user, toBase58String(i.transactionId)], SEP) |
---|
396 | 396 | | let historyDATA = makeString(["%d%d%d%d%d%d%d", toString(lastBlock.height), toString(lastBlock.timestamp), toString(amount), toString(lockStart), toString(duration), toString(gwxAmount)], SEP) |
---|
397 | 397 | | StringEntry(historyKEY, historyDATA) |
---|
398 | 398 | | } |
---|
399 | 399 | | |
---|
400 | 400 | | |
---|
401 | 401 | | func StatsEntry (totalLockedInc,durationInc,lockCountInc,usersCountInc) = { |
---|
402 | 402 | | let locksDurationSumInBlocksKEY = keyStatsLocksDurationSumInBlocks() |
---|
403 | 403 | | let locksCountKEY = keyStatsLocksCount() |
---|
404 | 404 | | let usersCountKEY = keyStatsUsersCount() |
---|
405 | 405 | | let totalAmountKEY = keyLockParamTotalAmount() |
---|
406 | 406 | | let locksDurationSumInBlocks = getIntOrZero(this, locksDurationSumInBlocksKEY) |
---|
407 | 407 | | let locksCount = getIntOrZero(this, locksCountKEY) |
---|
408 | 408 | | let usersCount = getIntOrZero(this, usersCountKEY) |
---|
409 | 409 | | let totalAmount = getIntOrZero(this, totalAmountKEY) |
---|
410 | 410 | | [IntegerEntry(locksDurationSumInBlocksKEY, (locksDurationSumInBlocks + durationInc)), IntegerEntry(locksCountKEY, (locksCount + lockCountInc)), IntegerEntry(usersCountKEY, (usersCount + usersCountInc)), IntegerEntry(totalAmountKEY, (totalAmount + totalLockedInc))] |
---|
411 | 411 | | } |
---|
412 | 412 | | |
---|
413 | 413 | | |
---|
414 | 414 | | func LockParamsEntry (userAddress,txId,amount,start,duration,gwxAmount,wxClaimed) = [StringEntry(keyLockParamsRecord(userAddress, txId), formatLockParamsRecord(amount, start, duration, gwxAmount, wxClaimed))] |
---|
415 | 415 | | |
---|
416 | 416 | | |
---|
417 | 417 | | func extractOptionalPaymentAmountOrFail (i,expectedAssetId) = if ((size(i.payments) > 1)) |
---|
418 | 418 | | then throwErr("only one payment is allowed") |
---|
419 | 419 | | else if ((size(i.payments) == 0)) |
---|
420 | 420 | | then 0 |
---|
421 | 421 | | else { |
---|
422 | 422 | | let pmt = i.payments[0] |
---|
423 | 423 | | if ((value(pmt.assetId) != expectedAssetId)) |
---|
424 | 424 | | then throwErr("invalid asset id in payment") |
---|
425 | 425 | | else pmt.amount |
---|
426 | 426 | | } |
---|
427 | 427 | | |
---|
428 | 428 | | |
---|
429 | 429 | | func getUserGwxAmountTotal (userAddress) = valueOrElse(getInteger(this, keyUserGwxAmountTotal(userAddress)), 0) |
---|
430 | 430 | | |
---|
431 | 431 | | |
---|
432 | 432 | | func getVotingEmissionEpochInfo () = { |
---|
433 | 433 | | let $t01499515285 = { |
---|
434 | 434 | | let currentEpochUi = value(getInteger(votingEmissionContract, keyCurrentEpochUi())) |
---|
435 | 435 | | let lastFinalizedEpoch = (currentEpochUi - 1) |
---|
436 | 436 | | if ((0 > lastFinalizedEpoch)) |
---|
437 | 437 | | then throwErr("invalid epoch") |
---|
438 | 438 | | else $Tuple2(currentEpochUi, lastFinalizedEpoch) |
---|
439 | 439 | | } |
---|
440 | 440 | | let currentEpochUi = $t01499515285._1 |
---|
441 | 441 | | let lastFinalizedEpoch = $t01499515285._2 |
---|
442 | 442 | | let currentEpochStartHeight = value(getInteger(votingEmissionContract, keyStartHeightByEpoch(currentEpochUi))) |
---|
443 | 443 | | $Tuple2(lastFinalizedEpoch, currentEpochStartHeight) |
---|
444 | 444 | | } |
---|
445 | 445 | | |
---|
446 | 446 | | |
---|
447 | 447 | | func getPoolAssetsByLpAssetId (lpAssetIdStr) = { |
---|
448 | 448 | | let idxAmountAssetId = 4 |
---|
449 | 449 | | let idxPriceAssetId = 5 |
---|
450 | 450 | | let poolCfg = { |
---|
451 | 451 | | let @ = invoke(factoryContract, "getPoolConfigByLpAssetIdREADONLY", [lpAssetIdStr], nil) |
---|
452 | 452 | | if ($isInstanceOf(@, "List[Any]")) |
---|
453 | 453 | | then @ |
---|
454 | 454 | | else throw(($getType(@) + " couldn't be cast to List[Any]")) |
---|
455 | 455 | | } |
---|
456 | 456 | | let amountAssetId = { |
---|
457 | 457 | | let @ = poolCfg[idxAmountAssetId] |
---|
458 | 458 | | if ($isInstanceOf(@, "String")) |
---|
459 | 459 | | then @ |
---|
460 | 460 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
461 | 461 | | } |
---|
462 | 462 | | let priceAssetId = { |
---|
463 | 463 | | let @ = poolCfg[idxPriceAssetId] |
---|
464 | 464 | | if ($isInstanceOf(@, "String")) |
---|
465 | 465 | | then @ |
---|
466 | 466 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
467 | 467 | | } |
---|
468 | 468 | | $Tuple2(amountAssetId, priceAssetId) |
---|
469 | 469 | | } |
---|
470 | 470 | | |
---|
471 | 471 | | |
---|
472 | 472 | | func getUserVoteFinalized (lpAssetIdStr,userAddressStr) = { |
---|
473 | 473 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
474 | 474 | | let $t01597616056 = getVotingEmissionEpochInfo() |
---|
475 | 475 | | let lastFinalizedEpoch = $t01597616056._1 |
---|
476 | 476 | | let currentEpochStartHeight = $t01597616056._2 |
---|
477 | 477 | | let $t01605916134 = getPoolAssetsByLpAssetId(lpAssetIdStr) |
---|
478 | 478 | | let amountAssetId = $t01605916134._1 |
---|
479 | 479 | | let priceAssetId = $t01605916134._2 |
---|
480 | 480 | | let userVoteKey = keyVote(amountAssetId, priceAssetId, userAddress, lastFinalizedEpoch) |
---|
481 | 481 | | let userVote = valueOrElse(getInteger(votingEmissionContract, userVoteKey), 0) |
---|
482 | 482 | | userVote |
---|
483 | 483 | | } |
---|
484 | 484 | | |
---|
485 | 485 | | |
---|
486 | 486 | | func getUserVoteStaked (lpAssetIdStr,userAddressStr) = { |
---|
487 | 487 | | let stakedByUser = valueOrElse(getInteger(stakingContract, keyStakedByUser(userAddressStr, lpAssetIdStr)), 0) |
---|
488 | 488 | | let userVote = getUserVoteFinalized(lpAssetIdStr, userAddressStr) |
---|
489 | 489 | | if ((stakedByUser == 0)) |
---|
490 | 490 | | then 0 |
---|
491 | 491 | | else userVote |
---|
492 | 492 | | } |
---|
493 | 493 | | |
---|
494 | 494 | | |
---|
495 | 495 | | func getVotingResultStaked (lpAssetIdStr) = { |
---|
496 | 496 | | let $t01667816758 = getVotingEmissionEpochInfo() |
---|
497 | 497 | | let lastFinalizedEpoch = $t01667816758._1 |
---|
498 | 498 | | let currentEpochStartHeight = $t01667816758._2 |
---|
499 | 499 | | let votingResultStakedStart = valueOrElse(getInteger(votingEmissionContract, keyVotingResultStaked(lpAssetIdStr, lastFinalizedEpoch)), 0) |
---|
500 | 500 | | let votingResultStaked = valueOrElse(getInteger(this, keyVotingResultStaked(lpAssetIdStr, lastFinalizedEpoch)), votingResultStakedStart) |
---|
501 | 501 | | votingResultStaked |
---|
502 | 502 | | } |
---|
503 | 503 | | |
---|
504 | 504 | | |
---|
505 | 505 | | func getVotingResultStakedIntegral (lpAssetIdStr) = { |
---|
506 | 506 | | let $t01712017200 = getVotingEmissionEpochInfo() |
---|
507 | 507 | | let lastFinalizedEpoch = $t01712017200._1 |
---|
508 | 508 | | let currentEpochStartHeight = $t01712017200._2 |
---|
509 | 509 | | let votingResultStaked = getVotingResultStaked(lpAssetIdStr) |
---|
510 | 510 | | let votingResultStakedIntegralPrev = valueOrElse(getInteger(this, keyVotingResultStakedIntegral(lpAssetIdStr, lastFinalizedEpoch)), 0) |
---|
511 | 511 | | let votingResultStakedLastUpdateHeight = valueOrElse(getInteger(this, keyVotingResultStakedLastUpdateHeight(lpAssetIdStr, lastFinalizedEpoch)), currentEpochStartHeight) |
---|
512 | 512 | | let votingResultStakedIntegralDh = (height - votingResultStakedLastUpdateHeight) |
---|
513 | 513 | | let votingResultStakedIntegral = ((votingResultStakedIntegralDh * votingResultStaked) + votingResultStakedIntegralPrev) |
---|
514 | 514 | | votingResultStakedIntegral |
---|
515 | 515 | | } |
---|
516 | 516 | | |
---|
517 | 517 | | |
---|
518 | 518 | | func refreshVotingResultStakedIntegral (lpAssetIdStr,stakedVoteDelta) = { |
---|
519 | 519 | | let $t01803718117 = getVotingEmissionEpochInfo() |
---|
520 | 520 | | let lastFinalizedEpoch = $t01803718117._1 |
---|
521 | 521 | | let currentEpochStartHeight = $t01803718117._2 |
---|
522 | 522 | | let votingResultStaked = getVotingResultStaked(lpAssetIdStr) |
---|
523 | 523 | | let votingResultStakedNew = (votingResultStaked + stakedVoteDelta) |
---|
524 | 524 | | let votingResultStakedIntegral = getVotingResultStakedIntegral(lpAssetIdStr) |
---|
525 | 525 | | [IntegerEntry(keyVotingResultStaked(lpAssetIdStr, lastFinalizedEpoch), votingResultStakedNew), IntegerEntry(keyVotingResultStakedLastUpdateHeight(lpAssetIdStr, lastFinalizedEpoch), height), IntegerEntry(keyVotingResultStakedIntegral(lpAssetIdStr, lastFinalizedEpoch), votingResultStakedIntegral)] |
---|
526 | 526 | | } |
---|
527 | 527 | | |
---|
528 | 528 | | |
---|
529 | 529 | | func getUserVoteStakedIntegral (lpAssetIdStr,userAddressStr) = { |
---|
530 | 530 | | let $t01872818808 = getVotingEmissionEpochInfo() |
---|
531 | 531 | | let lastFinalizedEpoch = $t01872818808._1 |
---|
532 | 532 | | let currentEpochStartHeight = $t01872818808._2 |
---|
533 | 533 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
534 | 534 | | let userVoteStaked = getUserVoteStaked(lpAssetIdStr, userAddressStr) |
---|
535 | 535 | | let userVoteStakedIntegralPrev = valueOrElse(getInteger(this, keyVoteStakedIntegral(lpAssetIdStr, userAddress, lastFinalizedEpoch)), 0) |
---|
536 | 536 | | let userVoteStakedLastUpdateHeight = valueOrElse(getInteger(this, keyVoteStakedLastUpdateHeight(lpAssetIdStr, userAddress, lastFinalizedEpoch)), currentEpochStartHeight) |
---|
537 | 537 | | let userVoteStakedIntegralDh = (height - userVoteStakedLastUpdateHeight) |
---|
538 | 538 | | let userVoteStakedIntegral = ((userVoteStakedIntegralDh * userVoteStaked) + userVoteStakedIntegralPrev) |
---|
539 | 539 | | userVoteStakedIntegral |
---|
540 | 540 | | } |
---|
541 | 541 | | |
---|
542 | 542 | | |
---|
543 | 543 | | func refreshVoteStakedIntegral (lpAssetIdStr,userAddressStr,edge) = { |
---|
544 | 544 | | let $t01959619676 = getVotingEmissionEpochInfo() |
---|
545 | 545 | | let lastFinalizedEpoch = $t01959619676._1 |
---|
546 | 546 | | let currentEpochStartHeight = $t01959619676._2 |
---|
547 | 547 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
548 | 548 | | let userVoteFinalized = getUserVoteFinalized(lpAssetIdStr, userAddressStr) |
---|
549 | 549 | | let actions = if ((userVoteFinalized == 0)) |
---|
550 | 550 | | then nil |
---|
551 | 551 | | else { |
---|
552 | 552 | | let stakedVoteDelta = if (edge) |
---|
553 | 553 | | then userVoteFinalized |
---|
554 | 554 | | else -(userVoteFinalized) |
---|
555 | 555 | | let votingResultActions = refreshVotingResultStakedIntegral(lpAssetIdStr, stakedVoteDelta) |
---|
556 | 556 | | let userVoteStakedIntegral = getUserVoteStakedIntegral(lpAssetIdStr, userAddressStr) |
---|
557 | 557 | | let voteActions = [IntegerEntry(keyVoteStakedLastUpdateHeight(lpAssetIdStr, userAddress, lastFinalizedEpoch), height), IntegerEntry(keyVoteStakedIntegral(lpAssetIdStr, userAddress, lastFinalizedEpoch), userVoteStakedIntegral)] |
---|
558 | 558 | | (votingResultActions ++ voteActions) |
---|
559 | 559 | | } |
---|
560 | 560 | | actions |
---|
561 | 561 | | } |
---|
562 | 562 | | |
---|
563 | 563 | | |
---|
564 | 564 | | func getStakedVotesIntegralsDiff (lpAssetIdStr,userAddressStr) = { |
---|
565 | 565 | | let $t02053020610 = getVotingEmissionEpochInfo() |
---|
566 | 566 | | let lastFinalizedEpoch = $t02053020610._1 |
---|
567 | 567 | | let currentEpochStartHeight = $t02053020610._2 |
---|
568 | 568 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
569 | 569 | | let userVoteStakedIntegralLastKey = keyVoteStakedIntegralLast(lpAssetIdStr, userAddress, lastFinalizedEpoch) |
---|
570 | 570 | | let userVoteStakedIntegralLast = valueOrElse(getInteger(this, userVoteStakedIntegralLastKey), 0) |
---|
571 | 571 | | let votingResultStakedIntegralLastKey = keyVotingResultStakedIntegralLast(lpAssetIdStr, userAddress, lastFinalizedEpoch) |
---|
572 | 572 | | let votingResultStakedIntegralLast = valueOrElse(getInteger(this, votingResultStakedIntegralLastKey), 0) |
---|
573 | 573 | | let userVoteStakedIntegral = getUserVoteStakedIntegral(lpAssetIdStr, userAddressStr) |
---|
574 | 574 | | let votingResultStakedIntegral = getVotingResultStakedIntegral(lpAssetIdStr) |
---|
575 | 575 | | let userVoteStakedIntegralDiff = (userVoteStakedIntegral - userVoteStakedIntegralLast) |
---|
576 | 576 | | let votingResultStakedIntegralDiff = (votingResultStakedIntegral - votingResultStakedIntegralLast) |
---|
577 | 577 | | $Tuple3([IntegerEntry(userVoteStakedIntegralLastKey, userVoteStakedIntegral), IntegerEntry(votingResultStakedIntegralLastKey, votingResultStakedIntegral)], userVoteStakedIntegralDiff, votingResultStakedIntegralDiff) |
---|
578 | 578 | | } |
---|
579 | 579 | | |
---|
580 | 580 | | |
---|
581 | 581 | | func refreshBoostEmissionIntegral () = { |
---|
582 | 582 | | let wxEmissionPerBlock = getIntOrFail(emissionContract, keyEmissionRatePerBlockCurrent()) |
---|
583 | 583 | | let boostingV2LastUpdateHeightOption = getInteger(this, keyBoostingV2LastUpdateHeight()) |
---|
584 | 584 | | let boostingV2IngergalOption = getInteger(this, keyBoostingV2Integral()) |
---|
585 | 585 | | let emissionEnd = getIntOrFail(emissionContract, keyEmissionEndBlock()) |
---|
586 | 586 | | let h = if ((height > emissionEnd)) |
---|
587 | 587 | | then emissionEnd |
---|
588 | 588 | | else height |
---|
589 | 589 | | let dh = match boostingV2LastUpdateHeightOption { |
---|
590 | 590 | | case lastUpdateHeight: Int => |
---|
591 | 591 | | max([(h - lastUpdateHeight), 0]) |
---|
592 | 592 | | case _: Unit => |
---|
593 | 593 | | 0 |
---|
594 | 594 | | case _ => |
---|
595 | 595 | | throw("Match error") |
---|
596 | 596 | | } |
---|
597 | 597 | | let boostEmissionPerBlock = ((wxEmissionPerBlock * (boostCoeff - 1)) / boostCoeff) |
---|
598 | 598 | | let boostEmissionIntegralPrev = valueOrElse(boostingV2IngergalOption, 0) |
---|
599 | 599 | | let boostEmissionIntegral = ((boostEmissionPerBlock * dh) + boostEmissionIntegralPrev) |
---|
600 | 600 | | $Tuple2([IntegerEntry(keyBoostingV2Integral(), boostEmissionIntegral), IntegerEntry(keyBoostingV2LastUpdateHeight(), height)], boostEmissionIntegral) |
---|
601 | 601 | | } |
---|
602 | 602 | | |
---|
603 | 603 | | |
---|
604 | 604 | | func internalClaimWxBoost (lpAssetIdStr,userAddressStr,readOnly) = { |
---|
605 | 605 | | let userAddress = valueOrErrorMessage(addressFromString(userAddressStr), wrapErr("invalid user address")) |
---|
606 | 606 | | let userNum = userNumberByAddressOrFail(userAddress) |
---|
607 | 607 | | if ((userNum == userNum)) |
---|
608 | 608 | | then { |
---|
609 | 609 | | let EMPTYSTR = "empty" |
---|
610 | 610 | | let poolWeight = if ((lpAssetIdStr != EMPTYSTR)) |
---|
611 | 611 | | then { |
---|
612 | 612 | | let poolAddressStr = valueOrErrorMessage(getString(factoryContract, keyFactoryLpAssetToPoolContractAddress(lpAssetIdStr)), wrapErr(("unsupported lp asset " + lpAssetIdStr))) |
---|
613 | 613 | | getIntegerValue(factoryContract, keyFactoryPoolWeight(poolAddressStr)) |
---|
614 | 614 | | } |
---|
615 | 615 | | else if (readOnly) |
---|
616 | 616 | | then 0 |
---|
617 | 617 | | else throwErr(("not readonly mode: unsupported lp asset " + lpAssetIdStr)) |
---|
618 | 618 | | let userLpBoostEmissionLastIntegralKEY = keyUserLpBoostEmissionLastINTEGRAL(userNum, lpAssetIdStr) |
---|
619 | 619 | | let userBoostEmissionLastIntegralKEY = keyUserBoostEmissionLastINTEGRAL(userNum) |
---|
620 | 620 | | let userBoostEmissionLastIntegral = valueOrElse(getInteger(this, userLpBoostEmissionLastIntegralKEY), getIntOrZero(this, userBoostEmissionLastIntegralKEY)) |
---|
621 | 621 | | let boostEmissionIntegral = refreshBoostEmissionIntegral()._2 |
---|
622 | 622 | | let userBoostEmissionIntegral = (boostEmissionIntegral - userBoostEmissionLastIntegral) |
---|
623 | 623 | | if ((0 > userBoostEmissionIntegral)) |
---|
624 | 624 | | then throwErr("wrong calculations") |
---|
625 | 625 | | else { |
---|
626 | 626 | | let $t02415224291 = getStakedVotesIntegralsDiff(lpAssetIdStr, userAddressStr) |
---|
627 | 627 | | let stakedVotesIntegralsActions = $t02415224291._1 |
---|
628 | 628 | | let userVoteIntegralDiff = $t02415224291._2 |
---|
629 | 629 | | let totalVotesIntegralDiff = $t02415224291._3 |
---|
630 | 630 | | let poolUserBoostEmissionIntegral = fraction(userBoostEmissionIntegral, poolWeight, POOLWEIGHTMULT) |
---|
631 | 631 | | let userBoostAvaliableToClaimTotalNew = if ((totalVotesIntegralDiff == 0)) |
---|
632 | 632 | | then 0 |
---|
633 | 633 | | else fraction(poolUserBoostEmissionIntegral, userVoteIntegralDiff, totalVotesIntegralDiff) |
---|
634 | 634 | | let dataState = ([IntegerEntry(userLpBoostEmissionLastIntegralKEY, boostEmissionIntegral)] ++ stakedVotesIntegralsActions) |
---|
635 | 635 | | let debug = makeString([toString(userBoostEmissionLastIntegral), toString(userBoostEmissionIntegral), toString(poolWeight), toString(userVoteIntegralDiff), toString(totalVotesIntegralDiff)], ":") |
---|
636 | 636 | | $Tuple3(userBoostAvaliableToClaimTotalNew, dataState, debug) |
---|
637 | 637 | | } |
---|
638 | 638 | | } |
---|
639 | 639 | | else throw("Strict value is not equal to itself.") |
---|
640 | 640 | | } |
---|
641 | 641 | | |
---|
642 | 642 | | |
---|
643 | 643 | | func lockActions (i,durationMonths) = { |
---|
644 | 644 | | let durationMonthsAllowed = [1, 3, 6, 12, 24, 48] |
---|
645 | 645 | | if (!(containsElement(durationMonthsAllowed, durationMonths))) |
---|
646 | 646 | | then throwErr("invalid duration") |
---|
647 | 647 | | else { |
---|
648 | 648 | | let duration = (durationMonths * blocksInMonth) |
---|
649 | 649 | | let assetIdStr = toBase58String(assetId) |
---|
650 | 650 | | if ((size(i.payments) != 1)) |
---|
651 | 651 | | then throwErr("invalid payment - exact one payment must be attached") |
---|
652 | 652 | | else { |
---|
653 | 653 | | let pmt = i.payments[0] |
---|
654 | 654 | | let pmtAmount = pmt.amount |
---|
655 | 655 | | if ((assetId != value(pmt.assetId))) |
---|
656 | 656 | | then throwErr((("invalid asset is in payment - " + assetIdStr) + " is expected")) |
---|
657 | 657 | | else { |
---|
658 | 658 | | let nextUserNumKEY = keyNextUserNum() |
---|
659 | 659 | | let userAddress = i.caller |
---|
660 | 660 | | let userAddressStr = toString(userAddress) |
---|
661 | 661 | | let userIsExisting = isDefined(getString(keyUser2NumMapping(userAddressStr))) |
---|
662 | 662 | | let userNumStr = if (userIsExisting) |
---|
663 | 663 | | then value(getString(keyUser2NumMapping(userAddressStr))) |
---|
664 | 664 | | else toString(getIntOrFail(this, nextUserNumKEY)) |
---|
665 | 665 | | let userNum = parseIntValue(userNumStr) |
---|
666 | 666 | | let lockStart = height |
---|
667 | 667 | | if (if ((minLockAmount > pmtAmount)) |
---|
668 | 668 | | then (userAddress != lpStakingPoolsContract) |
---|
669 | 669 | | else false) |
---|
670 | 670 | | then throwErr(("amount is less then minLockAmount=" + toString(minLockAmount))) |
---|
671 | 671 | | else if ((minLockDuration > duration)) |
---|
672 | 672 | | then throwErr(("passed duration is less then minLockDuration=" + toString(minLockDuration))) |
---|
673 | 673 | | else if ((duration > maxLockDuration)) |
---|
674 | 674 | | then throwErr(("passed duration is greater then maxLockDuration=" + toString(maxLockDuration))) |
---|
675 | 675 | | else { |
---|
676 | 676 | | let coeffX8 = fraction(duration, MULT8, maxLockDuration) |
---|
677 | 677 | | let gWxAmountStart = fraction(pmtAmount, coeffX8, MULT8) |
---|
678 | 678 | | let gwxAmountTotal = getGwxAmountTotal() |
---|
679 | 679 | | let userBoostEmissionLastIntegralKEY = keyUserBoostEmissionLastINTEGRAL(userNum) |
---|
680 | 680 | | let boostEmissionIntegral = refreshBoostEmissionIntegral()._2 |
---|
681 | 681 | | let userGwxAmountTotal = getUserGwxAmountTotal(userAddress) |
---|
682 | 682 | | let gwxRewardInv = invoke(gwxRewardContract, "refreshUserReward", [userAddress.bytes], nil) |
---|
683 | 683 | | if ((gwxRewardInv == gwxRewardInv)) |
---|
684 | 684 | | then { |
---|
685 | 685 | | let arr = if (userIsExisting) |
---|
686 | 686 | | then nil |
---|
687 | 687 | | else [IntegerEntry(nextUserNumKEY, (userNum + 1)), StringEntry(keyUser2NumMapping(userAddressStr), userNumStr), StringEntry(keyNum2UserMapping(userNumStr), userAddressStr)] |
---|
688 | 688 | | $Tuple2(((((arr ++ LockParamsEntry(userAddress, i.transactionId, pmtAmount, lockStart, duration, gWxAmountStart, 0)) ++ StatsEntry(pmtAmount, duration, 1, if (userIsExisting) |
---|
689 | 689 | | then 0 |
---|
690 | 690 | | else 1)) :+ HistoryEntry("lock", userAddressStr, pmtAmount, lockStart, duration, gWxAmountStart, i)) ++ [IntegerEntry(userBoostEmissionLastIntegralKEY, boostEmissionIntegral), IntegerEntry(keyGwxTotal(), (gwxAmountTotal + gWxAmountStart)), IntegerEntry(keyUserGwxAmountTotal(userAddress), (userGwxAmountTotal + gWxAmountStart))]), gWxAmountStart) |
---|
691 | 691 | | } |
---|
692 | 692 | | else throw("Strict value is not equal to itself.") |
---|
693 | 693 | | } |
---|
694 | 694 | | } |
---|
695 | 695 | | } |
---|
696 | 696 | | } |
---|
697 | 697 | | } |
---|
698 | 698 | | |
---|
699 | 699 | | |
---|
700 | 700 | | @Callable(i) |
---|
701 | 701 | | func constructor (factoryAddressStr,lockAssetIdStr,minLockAmount,minDuration,maxDuration,mathContract) = { |
---|
702 | 702 | | let checkCaller = mustManager(i) |
---|
703 | 703 | | if ((checkCaller == checkCaller)) |
---|
704 | 704 | | then ([IntegerEntry(keyNextUserNum(), 0), StringEntry(keyConfig(), formatConfig(lockAssetIdStr, minLockAmount, minDuration, maxDuration, mathContract)), StringEntry(keyFactoryAddress(), factoryAddressStr)] ++ StatsEntry(0, 0, 0, 0)) |
---|
705 | 705 | | else throw("Strict value is not equal to itself.") |
---|
706 | 706 | | } |
---|
707 | 707 | | |
---|
708 | 708 | | |
---|
709 | 709 | | |
---|
710 | 710 | | @Callable(i) |
---|
711 | 711 | | func lockRef (duration,referrerAddress,signature) = { |
---|
712 | 712 | | let $t02826828333 = lockActions(i, duration) |
---|
713 | 713 | | let lockActionsResult = $t02826828333._1 |
---|
714 | 714 | | let gWxAmountStart = $t02826828333._2 |
---|
715 | 715 | | let referralAddress = toString(i.caller) |
---|
716 | 716 | | let refInv = if (if ((referrerAddress == "")) |
---|
717 | 717 | | then true |
---|
718 | 718 | | else (signature == base58'')) |
---|
719 | 719 | | then unit |
---|
720 | 720 | | else invoke(referralsContractAddressOrFail, "createPair", [referralProgramName, referrerAddress, referralAddress, signature], nil) |
---|
721 | 721 | | if ((refInv == refInv)) |
---|
722 | 722 | | then { |
---|
723 | 723 | | let updateRefActivity = invoke(mathContract, "updateReferralActivity", [toString(i.caller), gWxAmountStart], nil) |
---|
724 | 724 | | if ((updateRefActivity == updateRefActivity)) |
---|
725 | 725 | | then $Tuple2(lockActionsResult, unit) |
---|
726 | 726 | | else throw("Strict value is not equal to itself.") |
---|
727 | 727 | | } |
---|
728 | 728 | | else throw("Strict value is not equal to itself.") |
---|
729 | 729 | | } |
---|
730 | 730 | | |
---|
731 | 731 | | |
---|
732 | 732 | | |
---|
733 | 733 | | @Callable(i) |
---|
734 | 734 | | func lock (duration) = { |
---|
735 | 735 | | let $t02879128856 = lockActions(i, duration) |
---|
736 | 736 | | let lockActionsResult = $t02879128856._1 |
---|
737 | 737 | | let gWxAmountStart = $t02879128856._2 |
---|
738 | 738 | | let updateRefActivity = invoke(mathContract, "updateReferralActivity", [toString(i.caller), gWxAmountStart], nil) |
---|
739 | 739 | | if ((updateRefActivity == updateRefActivity)) |
---|
740 | 740 | | then $Tuple2(lockActionsResult, unit) |
---|
741 | 741 | | else throw("Strict value is not equal to itself.") |
---|
742 | 742 | | } |
---|
743 | 743 | | |
---|
744 | 744 | | |
---|
745 | 745 | | |
---|
746 | 746 | | @Callable(i) |
---|
747 | 747 | | func claimWxBoost (lpAssetIdStr,userAddressStr) = if ((stakingContract != i.caller)) |
---|
748 | 748 | | then throwErr("permissions denied") |
---|
749 | 749 | | else { |
---|
750 | 750 | | let $t02916629268 = internalClaimWxBoost(lpAssetIdStr, userAddressStr, false) |
---|
751 | 751 | | let userBoostAvailable = $t02916629268._1 |
---|
752 | 752 | | let dataState = $t02916629268._2 |
---|
753 | 753 | | let debug = $t02916629268._3 |
---|
754 | 754 | | $Tuple2(dataState, [userBoostAvailable]) |
---|
755 | 755 | | } |
---|
756 | 756 | | |
---|
757 | 757 | | |
---|
758 | 758 | | |
---|
759 | 759 | | @Callable(i) |
---|
760 | 760 | | func claimWxBoostREADONLY (lpAssetIdStr,userAddressStr) = { |
---|
761 | 761 | | let $t02940029501 = internalClaimWxBoost(lpAssetIdStr, userAddressStr, true) |
---|
762 | 762 | | let userBoostAvailable = $t02940029501._1 |
---|
763 | 763 | | let dataState = $t02940029501._2 |
---|
764 | 764 | | let debug = $t02940029501._3 |
---|
765 | 765 | | $Tuple2(nil, [userBoostAvailable, debug]) |
---|
766 | 766 | | } |
---|
767 | 767 | | |
---|
768 | 768 | | |
---|
769 | 769 | | |
---|
770 | 770 | | @Callable(i) |
---|
771 | 771 | | func unlock (txIdStr,amount) = { |
---|
772 | 772 | | let userAddress = i.caller |
---|
773 | 773 | | let userAddressStr = toString(userAddress) |
---|
774 | 774 | | let txId = fromBase58String(txIdStr) |
---|
775 | 775 | | let userRecordArray = readLockParamsRecordOrFail(userAddress, if ((txIdStr == "")) |
---|
776 | 776 | | then unit |
---|
777 | 777 | | else txId) |
---|
778 | 778 | | let userAmount = parseIntValue(userRecordArray[IdxLockAmount]) |
---|
779 | 779 | | let lockStart = parseIntValue(userRecordArray[IdxLockStart]) |
---|
780 | 780 | | let lockDuration = parseIntValue(userRecordArray[IdxLockDuration]) |
---|
781 | 781 | | let lockEnd = (lockStart + lockDuration) |
---|
782 | 782 | | let wxClaimed = parseIntValue(userRecordArray[IdxLockWxClaimed]) |
---|
783 | 783 | | let gwxAmount = parseIntValue(userRecordArray[IdxLockGwxAmount]) |
---|
784 | 784 | | let t = ((height - lockStart) / blocksInDay) |
---|
785 | 785 | | let exponent = fraction(toBigInt(t), (toBigInt((8 * blocksInDay)) * MULT18BI), toBigInt(lockDuration)) |
---|
786 | 786 | | let wxWithdrawable = if ((height > lockEnd)) |
---|
787 | 787 | | then (userAmount - wxClaimed) |
---|
788 | 788 | | else toInt(fraction(toBigInt(userAmount), (MULT18BI - pow(toBigInt(5), 1, exponent, SCALE18, SCALE18, DOWN)), MULT18BI)) |
---|
789 | 789 | | if ((amount > wxWithdrawable)) |
---|
790 | 790 | | then throwErr(("maximum amount to unlock: " + toString(wxWithdrawable))) |
---|
791 | 791 | | else { |
---|
792 | 792 | | let gwxBurned = max([amount, fraction((t * blocksInDay), userAmount, maxLockDuration)]) |
---|
793 | 793 | | let gwxRemaining = ensurePositive((gwxAmount - gwxBurned), "gwxRemaining") |
---|
794 | 794 | | let lockedGwxAmount = getLockedGwxAmount(userAddress) |
---|
795 | 795 | | if ((lockedGwxAmount > gwxRemaining)) |
---|
796 | 796 | | then throwErr(("locked gwx amount: " + toString(lockedGwxAmount))) |
---|
797 | 797 | | else if ((0 >= userAmount)) |
---|
798 | 798 | | then throwErr("nothing to unlock") |
---|
799 | 799 | | else { |
---|
800 | 800 | | let gwxAmountTotal = getGwxAmountTotal() |
---|
801 | 801 | | let userGwxAmountTotal = getUserGwxAmountTotal(userAddress) |
---|
802 | 802 | | let gwxRewardInv = reentrantInvoke(gwxRewardContract, "refreshUserReward", [userAddress.bytes], nil) |
---|
803 | 803 | | if ((gwxRewardInv == gwxRewardInv)) |
---|