1 | 1 | | {-# STDLIB_VERSION 6 #-} |
---|
2 | 2 | | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | 3 | | {-# CONTENT_TYPE DAPP #-} |
---|
4 | 4 | | let separator = "__" |
---|
5 | 5 | | |
---|
6 | 6 | | let poolWeightMult = 100000000 |
---|
7 | 7 | | |
---|
8 | 8 | | let maxDepthDefault = 10 |
---|
9 | 9 | | |
---|
10 | 10 | | let finalizationStageBalances = 0 |
---|
11 | 11 | | |
---|
12 | 12 | | let finalizationStageTotal = 1 |
---|
13 | 13 | | |
---|
14 | 14 | | let finalizationStageShares = 2 |
---|
15 | 15 | | |
---|
16 | 16 | | let resumptionFeeDefault = 500000000000 |
---|
17 | 17 | | |
---|
18 | 18 | | let keyEpochLength = makeString(["%s", "epochLength"], separator) |
---|
19 | 19 | | |
---|
20 | 20 | | let keyEpochLengthNew = makeString(["%s%s", "epochLength__new"], separator) |
---|
21 | 21 | | |
---|
22 | 22 | | func keyEpochLengthByEpoch (epoch) = makeString(["%s%d", "epochLength", toString(epoch)], separator) |
---|
23 | 23 | | |
---|
24 | 24 | | |
---|
25 | 25 | | let keyCurrentEpoch = makeString(["%s", "currentEpoch"], separator) |
---|
26 | 26 | | |
---|
27 | 27 | | let keyMaxDepth = makeString(["%s", "maxDepth"], separator) |
---|
28 | 28 | | |
---|
29 | 29 | | let keyResumptionFee = makeString(["%s", "resumptionFee"], separator) |
---|
30 | 30 | | |
---|
31 | 31 | | let keyVotingEmissionCandidateContract = makeString(["%s", "votingEmissionCandidateContract"], separator) |
---|
32 | 32 | | |
---|
33 | 33 | | let keyVotingEmissionRateContract = makeString(["%s", "votingEmissionRateContract"], separator) |
---|
34 | 34 | | |
---|
35 | 35 | | let keyFactoryContract = makeString(["%s", "factoryContract"], separator) |
---|
36 | 36 | | |
---|
37 | 37 | | let keyBoostingContract = makeString(["%s", "boostingContract"], separator) |
---|
38 | 38 | | |
---|
39 | 39 | | let keyStakingContract = makeString(["%s", "stakingContract"], separator) |
---|
40 | 40 | | |
---|
41 | 41 | | let keyAssetsStoreContract = makeString(["%s", "assetsStoreContract"], separator) |
---|
42 | 42 | | |
---|
43 | 43 | | let keyFinalizationStage = makeString(["%s", "finalizationStage"], separator) |
---|
44 | 44 | | |
---|
45 | 45 | | let keyNextPool = makeString(["%s", "nextPool"], separator) |
---|
46 | 46 | | |
---|
47 | 47 | | let keyNextUser = makeString(["%s", "nextUser"], separator) |
---|
48 | 48 | | |
---|
49 | 49 | | let keyStartHeight = makeString(["%s", "startHeight"], separator) |
---|
50 | 50 | | |
---|
51 | 51 | | let keyCurrentEpochUi = makeString(["%s", "currentEpochUi"], separator) |
---|
52 | 52 | | |
---|
53 | 53 | | let keyStartHeightUi = makeString(["%s", "startHeightUi"], separator) |
---|
54 | 54 | | |
---|
55 | 55 | | let keyFinalizationShouldBeForced = makeString(["%s", "force"], separator) |
---|
56 | 56 | | |
---|
57 | 57 | | func keyStartHeightByEpoch (epoch) = makeString(["%s%d", "startHeight", toString(epoch)], separator) |
---|
58 | 58 | | |
---|
59 | 59 | | |
---|
60 | 60 | | func keyFinalized (epoch) = makeString(["%s%d", "finalized", toString(epoch)], separator) |
---|
61 | 61 | | |
---|
62 | 62 | | |
---|
63 | 63 | | func keyInList (pool) = { |
---|
64 | 64 | | let $t020132053 = pool |
---|
65 | 65 | | let amountAssetId = $t020132053._1 |
---|
66 | 66 | | let priceAssetId = $t020132053._2 |
---|
67 | 67 | | makeString(["%s%s%s", "inList", amountAssetId, priceAssetId], separator) |
---|
68 | 68 | | } |
---|
69 | 69 | | |
---|
70 | 70 | | |
---|
71 | 71 | | func keyInsufficientBalances (pool,epoch) = { |
---|
72 | 72 | | let $t022022242 = pool |
---|
73 | 73 | | let amountAssetId = $t022022242._1 |
---|
74 | 74 | | let priceAssetId = $t022022242._2 |
---|
75 | 75 | | makeString(["%s%s%s%d", "insufficientBalances", amountAssetId, priceAssetId, toString(epoch)], separator) |
---|
76 | 76 | | } |
---|
77 | 77 | | |
---|
78 | 78 | | |
---|
79 | 79 | | func keyUsed (address,epoch) = makeString(["%s%s%d", "used", toString(address), toString(epoch)], separator) |
---|
80 | 80 | | |
---|
81 | 81 | | |
---|
82 | 82 | | func keyVote (pool,address,epoch) = { |
---|
83 | 83 | | let $t025502590 = pool |
---|
84 | 84 | | let amountAssetId = $t025502590._1 |
---|
85 | 85 | | let priceAssetId = $t025502590._2 |
---|
86 | 86 | | makeString(["%s%s%s%s%d", "vote", amountAssetId, priceAssetId, toString(address), toString(epoch)], separator) |
---|
87 | 87 | | } |
---|
88 | 88 | | |
---|
89 | 89 | | |
---|
90 | 90 | | func keyVotingResult (pool,epoch) = { |
---|
91 | 91 | | let $t027722812 = pool |
---|
92 | 92 | | let amountAssetId = $t027722812._1 |
---|
93 | 93 | | let priceAssetId = $t027722812._2 |
---|
94 | 94 | | makeString(["%s%s%s%d", "votingResult", amountAssetId, priceAssetId, toString(epoch)], separator) |
---|
95 | 95 | | } |
---|
96 | 96 | | |
---|
97 | 97 | | |
---|
98 | 98 | | func keyVotingResultStaked (lpAssetIdStr,epoch) = makeString(["%s%s%d", "votingResultStaked", lpAssetIdStr, toString(epoch)], separator) |
---|
99 | 99 | | |
---|
100 | 100 | | |
---|
101 | 101 | | func keyPoolShare (pool,epoch) = { |
---|
102 | 102 | | let $t031333173 = pool |
---|
103 | 103 | | let amountAssetId = $t031333173._1 |
---|
104 | 104 | | let priceAssetId = $t031333173._2 |
---|
105 | 105 | | makeString(["%s%s%s%d", "poolShare", amountAssetId, priceAssetId, toString(epoch)], separator) |
---|
106 | 106 | | } |
---|
107 | 107 | | |
---|
108 | 108 | | |
---|
109 | 109 | | func keyTotalVotes (epoch) = makeString(["%s%d", "totalVotes", toString(epoch)], separator) |
---|
110 | 110 | | |
---|
111 | 111 | | |
---|
112 | 112 | | func keyStakedByUser (lpAssetIdStr,userAddressStr) = makeString(["%s%s%s", "staked", userAddressStr, lpAssetIdStr], separator) |
---|
113 | 113 | | |
---|
114 | 114 | | |
---|
115 | 115 | | func wrapErr (msg) = makeString(["voting_emission.ride:", msg], " ") |
---|
116 | 116 | | |
---|
117 | 117 | | |
---|
118 | 118 | | func throwErr (msg) = throw(wrapErr(msg)) |
---|
119 | 119 | | |
---|
120 | 120 | | |
---|
121 | 121 | | func getValueOrFail (address,key,type) = { |
---|
122 | 122 | | let error = wrapErr(makeString(["mandatory ", toString(address), ".", key, " is not defined"], "")) |
---|
123 | 123 | | valueOrErrorMessage( match type { |
---|
124 | 124 | | case str: String => |
---|
125 | 125 | | getString(address, key) |
---|
126 | 126 | | case int: Int => |
---|
127 | 127 | | getInteger(address, key) |
---|
128 | 128 | | case _ => |
---|
129 | 129 | | throwErr("invalid entry type") |
---|
130 | 130 | | }, error) |
---|
131 | 131 | | } |
---|
132 | 132 | | |
---|
133 | 133 | | |
---|
134 | 134 | | func getStrOrFail (address,key) = { |
---|
135 | 135 | | let @ = getValueOrFail(address, key, "") |
---|
136 | 136 | | if ($isInstanceOf(@, "String")) |
---|
137 | 137 | | then @ |
---|
138 | 138 | | else throw(($getType(@) + " couldn't be cast to String")) |
---|
139 | 139 | | } |
---|
140 | 140 | | |
---|
141 | 141 | | |
---|
142 | 142 | | func getIntOrFail (address,key) = { |
---|
143 | 143 | | let @ = getValueOrFail(address, key, 0) |
---|
144 | 144 | | if ($isInstanceOf(@, "Int")) |
---|
145 | 145 | | then @ |
---|
146 | 146 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
147 | 147 | | } |
---|
148 | 148 | | |
---|
149 | 149 | | |
---|
150 | 150 | | func poolToString (pool) = ((pool._1 + separator) + pool._2) |
---|
151 | 151 | | |
---|
152 | 152 | | |
---|
153 | 153 | | func stringToPool (str) = { |
---|
154 | 154 | | let parts = split(str, separator) |
---|
155 | 155 | | if ((size(parts) == 2)) |
---|
156 | 156 | | then $Tuple2(parts[0], parts[1]) |
---|
157 | 157 | | else throwErr("invalid pool string") |
---|
158 | 158 | | } |
---|
159 | 159 | | |
---|
160 | 160 | | |
---|
161 | 161 | | let factoryContract = addressFromStringValue(getStrOrFail(this, keyFactoryContract)) |
---|
162 | 162 | | |
---|
163 | 163 | | let IdxFactoryCfgGwxRewardDapp = 10 |
---|
164 | 164 | | |
---|
165 | 165 | | func keyFactoryCfg () = "%s__factoryConfig" |
---|
166 | 166 | | |
---|
167 | 167 | | |
---|
168 | 168 | | func readFactoryCfgOrFail (factory) = split(getStrOrFail(factory, keyFactoryCfg()), separator) |
---|
169 | 169 | | |
---|
170 | 170 | | |
---|
171 | 171 | | func getGwxRewardAddressOrFail (factoryCfg) = addressFromStringValue(factoryCfg[IdxFactoryCfgGwxRewardDapp]) |
---|
172 | 172 | | |
---|
173 | 173 | | |
---|
174 | 174 | | func getPoolInfo (amountAssetId,priceAssetId) = { |
---|
175 | 175 | | let poolInfoOption = { |
---|
176 | 176 | | let @ = invoke(factoryContract, "poolInfoREADONLY", [amountAssetId, priceAssetId], nil) |
---|
177 | 177 | | if ($isInstanceOf(@, "(Address, ByteVector)")) |
---|
178 | 178 | | then @ |
---|
179 | 179 | | else unit |
---|
180 | 180 | | } |
---|
181 | 181 | | poolInfoOption |
---|
182 | 182 | | } |
---|
183 | 183 | | |
---|
184 | 184 | | |
---|
185 | 185 | | func getLpAssetByPoolAssets (amountAssetId,priceAssetId) = { |
---|
186 | 186 | | func keyMappingsBaseAsset2internalId (baseAssetStr) = ("%s%s%s__mappings__baseAsset2internalId__" + baseAssetStr) |
---|
187 | 187 | | |
---|
188 | 188 | | func keyMappingPoolAssetsToPoolContractAddress (internalAmountAssetIdStr,internalPriceAssetIdStr) = (((("%d%d%s%s__" + toString(internalAmountAssetIdStr)) + "__") + toString(internalPriceAssetIdStr)) + "__mappings__poolAssets2PoolContract") |
---|
189 | 189 | | |
---|
190 | 190 | | func keyMappingPoolContractToLPAsset (poolContractAddress) = (("%s%s%s__" + poolContractAddress) + "__mappings__poolContract2LpAsset") |
---|
191 | 191 | | |
---|
192 | 192 | | let amountAssetInternalId = getIntOrFail(factoryContract, keyMappingsBaseAsset2internalId(amountAssetId)) |
---|
193 | 193 | | let priceAssetInternalId = getIntOrFail(factoryContract, keyMappingsBaseAsset2internalId(priceAssetId)) |
---|
194 | 194 | | let poolContractAddress = getStrOrFail(factoryContract, keyMappingPoolAssetsToPoolContractAddress(amountAssetInternalId, priceAssetInternalId)) |
---|
195 | 195 | | let lpAssetId = getStrOrFail(factoryContract, keyMappingPoolContractToLPAsset(poolContractAddress)) |
---|
196 | 196 | | lpAssetId |
---|
197 | 197 | | } |
---|
198 | 198 | | |
---|
199 | 199 | | |
---|
200 | 200 | | func checkWxEmissionPoolLabel (pool) = { |
---|
201 | 201 | | let $t062996339 = pool |
---|
202 | 202 | | let amountAssetId = $t062996339._1 |
---|
203 | 203 | | let priceAssetId = $t062996339._2 |
---|
204 | 204 | | let @ = invoke(factoryContract, "checkWxEmissionPoolLabel", [amountAssetId, priceAssetId], nil) |
---|
205 | 205 | | if ($isInstanceOf(@, "Boolean")) |
---|
206 | 206 | | then @ |
---|
207 | 207 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
208 | 208 | | } |
---|
209 | 209 | | |
---|
210 | 210 | | |
---|
211 | 211 | | func gwxRewardDeposit () = { |
---|
212 | 212 | | let factoryCfg = readFactoryCfgOrFail(factoryContract) |
---|
213 | 213 | | let gwxRewardsContract = getGwxRewardAddressOrFail(factoryCfg) |
---|
214 | 214 | | invoke(gwxRewardsContract, "deposit", nil, nil) |
---|
215 | 215 | | } |
---|
216 | 216 | | |
---|
217 | 217 | | |
---|
218 | 218 | | let poolsListName = "pools" |
---|
219 | 219 | | |
---|
220 | 220 | | func getVotesListName (pool) = { |
---|
221 | 221 | | let $t067566796 = pool |
---|
222 | 222 | | let amountAssetId = $t067566796._1 |
---|
223 | 223 | | let priceAssetId = $t067566796._2 |
---|
224 | 224 | | makeString(["votes", amountAssetId, priceAssetId], separator) |
---|
225 | 225 | | } |
---|
226 | 226 | | |
---|
227 | 227 | | |
---|
228 | 228 | | func keyListHead (listName) = { |
---|
229 | 229 | | let meta = if ((listName == poolsListName)) |
---|
230 | 230 | | then "%s%s" |
---|
231 | 231 | | else "%s%s%s%s" |
---|
232 | 232 | | makeString([meta, listName, "head"], separator) |
---|
233 | 233 | | } |
---|
234 | 234 | | |
---|
235 | 235 | | |
---|
236 | 236 | | func keyListSize (listName) = { |
---|
237 | 237 | | let meta = if ((listName == poolsListName)) |
---|
238 | 238 | | then "%s%s" |
---|
239 | 239 | | else "%s%s%s%s" |
---|
240 | 240 | | makeString([meta, listName, "size"], separator) |
---|
241 | 241 | | } |
---|
242 | 242 | | |
---|
243 | 243 | | |
---|
244 | 244 | | func keyListPrev (listName,id) = { |
---|
245 | 245 | | let meta = if ((listName == poolsListName)) |
---|
246 | 246 | | then "%s%s%s%s" |
---|
247 | 247 | | else "%s%s%s%s%s" |
---|
248 | 248 | | makeString([meta, listName, id, "prev"], separator) |
---|
249 | 249 | | } |
---|
250 | 250 | | |
---|
251 | 251 | | |
---|
252 | 252 | | func keyListNext (listName,id) = { |
---|
253 | 253 | | let meta = if ((listName == poolsListName)) |
---|
254 | 254 | | then "%s%s%s%s" |
---|
255 | 255 | | else "%s%s%s%s%s" |
---|
256 | 256 | | makeString([meta, listName, id, "next"], separator) |
---|
257 | 257 | | } |
---|
258 | 258 | | |
---|
259 | 259 | | |
---|
260 | 260 | | func containsNode (listName,id) = { |
---|
261 | 261 | | let headOrUnit = getString(this, keyListHead(listName)) |
---|
262 | 262 | | let prevOrUnit = getString(this, keyListPrev(listName, id)) |
---|
263 | 263 | | let nextOrUnit = getString(this, keyListNext(listName, id)) |
---|
264 | 264 | | if (if ((id == valueOrElse(headOrUnit, ""))) |
---|
265 | 265 | | then true |
---|
266 | 266 | | else (prevOrUnit != unit)) |
---|
267 | 267 | | then true |
---|
268 | 268 | | else (nextOrUnit != unit) |
---|
269 | 269 | | } |
---|
270 | 270 | | |
---|
271 | 271 | | |
---|
272 | 272 | | func insertNodeActions (listName,id) = { |
---|
273 | 273 | | let headOrUnit = getString(this, keyListHead(listName)) |
---|
274 | 274 | | let listSize = valueOrElse(getInteger(this, keyListSize(listName)), 0) |
---|
275 | 275 | | let checkNode = if (!(containsNode(listName, id))) |
---|
276 | 276 | | then true |
---|
277 | 277 | | else throwErr("Node exists") |
---|
278 | 278 | | if ((checkNode == checkNode)) |
---|
279 | 279 | | then (([IntegerEntry(keyListSize(listName), (listSize + 1))] ++ (if ((headOrUnit != unit)) |
---|
280 | 280 | | then [StringEntry(keyListNext(listName, id), value(headOrUnit)), StringEntry(keyListPrev(listName, value(headOrUnit)), id)] |
---|
281 | 281 | | else nil)) ++ [StringEntry(keyListHead(listName), id)]) |
---|
282 | 282 | | else throw("Strict value is not equal to itself.") |
---|
283 | 283 | | } |
---|
284 | 284 | | |
---|
285 | 285 | | |
---|
286 | 286 | | func deleteNodeActions (listName,id) = { |
---|
287 | 287 | | let headOrUnit = getString(this, keyListHead(listName)) |
---|
288 | 288 | | let listSize = valueOrElse(getInteger(this, keyListSize(listName)), 0) |
---|
289 | 289 | | let prevOrUnit = getString(this, keyListPrev(listName, id)) |
---|
290 | 290 | | let nextOrUnit = getString(this, keyListNext(listName, id)) |
---|
291 | 291 | | ([IntegerEntry(keyListSize(listName), (listSize - 1))] ++ (if (if ((prevOrUnit != unit)) |
---|
292 | 292 | | then (nextOrUnit != unit) |
---|
293 | 293 | | else false) |
---|
294 | 294 | | then [StringEntry(keyListNext(listName, value(prevOrUnit)), value(nextOrUnit)), StringEntry(keyListPrev(listName, value(nextOrUnit)), value(prevOrUnit)), DeleteEntry(keyListPrev(listName, id)), DeleteEntry(keyListNext(listName, id))] |
---|
295 | 295 | | else if ((nextOrUnit != unit)) |
---|
296 | 296 | | then [StringEntry(keyListHead(listName), value(nextOrUnit)), DeleteEntry(keyListNext(listName, id)), DeleteEntry(keyListPrev(listName, value(nextOrUnit)))] |
---|
297 | 297 | | else if ((prevOrUnit != unit)) |
---|
298 | 298 | | then [DeleteEntry(keyListPrev(listName, id)), DeleteEntry(keyListNext(listName, value(prevOrUnit)))] |
---|
299 | 299 | | else if ((id == valueOrElse(headOrUnit, ""))) |
---|
300 | 300 | | then [DeleteEntry(keyListHead(listName))] |
---|
301 | 301 | | else throwErr(((("invalid node: " + listName) + ".") + id)))) |
---|
302 | 302 | | } |
---|
303 | 303 | | |
---|
304 | 304 | | |
---|
305 | 305 | | func keyManagerVaultAddress () = "%s__managerVaultAddress" |
---|
306 | 306 | | |
---|
307 | 307 | | |
---|
308 | 308 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
309 | 309 | | |
---|
310 | 310 | | |
---|
311 | 311 | | func getManagerVaultAddressOrThis () = match getString(keyManagerVaultAddress()) { |
---|
312 | 312 | | case s: String => |
---|
313 | 313 | | addressFromStringValue(s) |
---|
314 | 314 | | case _ => |
---|
315 | 315 | | this |
---|
316 | 316 | | } |
---|
317 | 317 | | |
---|
318 | 318 | | |
---|
319 | 319 | | func managerPublicKeyOrUnit () = { |
---|
320 | 320 | | let managerVaultAddress = getManagerVaultAddressOrThis() |
---|
321 | 321 | | match getString(managerVaultAddress, keyManagerPublicKey()) { |
---|
322 | 322 | | case s: String => |
---|
323 | 323 | | fromBase58String(s) |
---|
324 | 324 | | case _: Unit => |
---|
325 | 325 | | unit |
---|
326 | 326 | | case _ => |
---|
327 | 327 | | throw("Match error") |
---|
328 | 328 | | } |
---|
329 | 329 | | } |
---|
330 | 330 | | |
---|
331 | 331 | | |
---|
332 | 332 | | func isManager (i) = match managerPublicKeyOrUnit() { |
---|
333 | 333 | | case pk: ByteVector => |
---|
334 | 334 | | (i.callerPublicKey == pk) |
---|
335 | 335 | | case _: Unit => |
---|
336 | 336 | | (i.caller == this) |
---|
337 | 337 | | case _ => |
---|
338 | 338 | | throw("Match error") |
---|
339 | 339 | | } |
---|
340 | 340 | | |
---|
341 | 341 | | |
---|
342 | 342 | | func mustManager (i) = if (isManager(i)) |
---|
343 | 343 | | then true |
---|
344 | 344 | | else throw("permission denied") |
---|
345 | 345 | | |
---|
346 | 346 | | |
---|
347 | 347 | | func mustThis (i) = if ((i.caller == this)) |
---|
348 | 348 | | then true |
---|
349 | 349 | | else throw("permission denied") |
---|
350 | 350 | | |
---|
351 | 351 | | |
---|
352 | 352 | | @Callable(i) |
---|
353 | 353 | | func getUserGwxAmountAtHeight (userAddress,targetHeight) = { |
---|
354 | 354 | | let boostingContractAddress = valueOrErrorMessage(addressFromString(getStrOrFail(this, keyBoostingContract)), wrapErr("invalid boosting contract address")) |
---|
355 | 355 | | $Tuple2(nil, { |
---|
356 | 356 | | let @ = invoke(boostingContractAddress, "getUserGwxAmountAtHeightREADONLY", [userAddress, targetHeight], nil) |
---|
357 | 357 | | if ($isInstanceOf(@, "Int")) |
---|
358 | 358 | | then @ |
---|
359 | 359 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
360 | 360 | | }) |
---|
361 | 361 | | } |
---|
362 | 362 | | |
---|
363 | 363 | | |
---|
364 | 364 | | |
---|
365 | 365 | | @Callable(i) |
---|
366 | 366 | | func constructor (factoryContract,votingEmissionCandidateContract,boostingContract,stakingContract,epochLength) = { |
---|
367 | 367 | | let checks = [mustManager(i), if ((addressFromString(factoryContract) != unit)) |
---|
368 | 368 | | then true |
---|
369 | 369 | | else "invalid factory contract address", if ((addressFromString(votingEmissionCandidateContract) != unit)) |
---|
370 | 370 | | then true |
---|
371 | 371 | | else "invalid voting emission candidate contract address", if ((addressFromString(boostingContract) != unit)) |
---|
372 | 372 | | then true |
---|
373 | 373 | | else "invalid boosting contract address", if ((addressFromString(stakingContract) != unit)) |
---|
374 | 374 | | then true |
---|
375 | 375 | | else "invalid staking contract address", if ((epochLength > 0)) |
---|
376 | 376 | | then true |
---|
377 | 377 | | else throwErr("invalid epoch length")] |
---|
378 | 378 | | if ((checks == checks)) |
---|
379 | 379 | | then $Tuple2([StringEntry(keyFactoryContract, factoryContract), StringEntry(keyVotingEmissionCandidateContract, votingEmissionCandidateContract), StringEntry(keyBoostingContract, boostingContract), StringEntry(keyStakingContract, stakingContract), IntegerEntry(keyEpochLength, epochLength)], unit) |
---|
380 | 380 | | else throw("Strict value is not equal to itself.") |
---|
381 | 381 | | } |
---|
382 | 382 | | |
---|
383 | 383 | | |
---|
384 | 384 | | |
---|
385 | 385 | | @Callable(i) |
---|
386 | 386 | | func create (amountAssetId,priceAssetId) = { |
---|
387 | 387 | | let checks = [if ((toBase58String(i.caller.bytes) == valueOrElse(getString(this, keyVotingEmissionCandidateContract), ""))) |
---|
388 | 388 | | then true |
---|
389 | 389 | | else mustManager(i)] |
---|
390 | 390 | | if ((checks == checks)) |
---|
391 | 391 | | then { |
---|
392 | 392 | | let pool = $Tuple2(amountAssetId, priceAssetId) |
---|
393 | 393 | | let inListActions = ([BooleanEntry(keyInList(pool), true)] ++ insertNodeActions(poolsListName, poolToString(pool))) |
---|
394 | 394 | | let currentEpochIsNotDefined = (getInteger(this, keyCurrentEpoch) == unit) |
---|
395 | 395 | | let startHeightActions = if (currentEpochIsNotDefined) |
---|
396 | 396 | | then { |
---|
397 | 397 | | let epoch = 0 |
---|
398 | 398 | | [IntegerEntry(keyCurrentEpoch, epoch), IntegerEntry(keyStartHeightByEpoch(epoch), height), IntegerEntry(keyStartHeight, height), IntegerEntry(keyCurrentEpochUi, epoch), IntegerEntry(keyStartHeightUi, height)] |
---|
399 | 399 | | } |
---|
400 | 400 | | else nil |
---|
401 | 401 | | $Tuple2((inListActions ++ startHeightActions), unit) |
---|
402 | 402 | | } |
---|
403 | 403 | | else throw("Strict value is not equal to itself.") |
---|
404 | 404 | | } |
---|
405 | 405 | | |
---|
406 | 406 | | |
---|
407 | 407 | | |
---|
408 | 408 | | @Callable(i) |
---|
409 | 409 | | func vote (amountAssetId,priceAssetId,amount) = { |
---|
410 | 410 | | let pool = $Tuple2(amountAssetId, priceAssetId) |
---|
411 | 411 | | let epoch = valueOrElse(getInteger(this, keyCurrentEpoch), 0) |
---|
412 | 412 | | let startHeight = getIntOrFail(this, keyStartHeightByEpoch(epoch)) |
---|
413 | 413 | | let epochLength = getIntOrFail(this, keyEpochLength) |
---|
414 | 414 | | let endHeight = (startHeight + epochLength) |
---|
415 | 415 | | let finalizationStageOrUnit = getInteger(this, keyFinalizationStage) |
---|
416 | 416 | | let used = valueOrElse(getInteger(this, keyUsed(i.caller, epoch)), 0) |
---|
417 | 417 | | let vote = valueOrElse(getInteger(this, keyVote(pool, i.caller, epoch)), 0) |
---|
418 | 418 | | let poolResult = valueOrElse(getInteger(this, keyVotingResult(pool, epoch)), 0) |
---|
419 | 419 | | let totalVotes = valueOrElse(getInteger(this, keyTotalVotes(epoch)), 0) |
---|
420 | 420 | | let gwxAmountAtEndTotal = { |
---|
421 | 421 | | let @ = invoke(this, "getUserGwxAmountAtHeight", [toBase58String(i.caller.bytes), endHeight], nil) |
---|
422 | 422 | | if ($isInstanceOf(@, "Int")) |
---|
423 | 423 | | then @ |
---|
424 | 424 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
425 | 425 | | } |
---|
426 | 426 | | let available = (gwxAmountAtEndTotal - used) |
---|
427 | 427 | | let newVote = (vote + amount) |
---|
428 | 428 | | let wxEmission = checkWxEmissionPoolLabel(pool) |
---|
429 | 429 | | let checks = [if ((getBoolean(keyInList(pool)) != unit)) |
---|
430 | 430 | | then true |
---|
431 | 431 | | else throwErr("invalid assets"), if ((endHeight > height)) |
---|
432 | 432 | | then true |
---|
433 | 433 | | else throwErr("invalid height"), if ((finalizationStageOrUnit == unit)) |
---|
434 | 434 | | then true |
---|
435 | 435 | | else throwErr("finalization in progress"), if ((gwxAmountAtEndTotal > 0)) |
---|
436 | 436 | | then true |
---|
437 | 437 | | else throwErr("you do not have gWX"), if (if ((amount > 0)) |
---|
438 | 438 | | then (available >= amount) |
---|
439 | 439 | | else false) |
---|
440 | 440 | | then true |
---|
441 | 441 | | else throwErr("invalid amount"), if (wxEmission) |
---|
442 | 442 | | then true |
---|
443 | 443 | | else throwErr("pool hasn't WX_EMISSION label")] |
---|
444 | 444 | | if ((checks == checks)) |
---|
445 | 445 | | then { |
---|
446 | 446 | | let votesListName = getVotesListName(pool) |
---|
447 | 447 | | let userAddressStr = toString(i.caller) |
---|
448 | 448 | | let votesListActions = if (containsNode(votesListName, userAddressStr)) |
---|
449 | 449 | | then nil |
---|
450 | 450 | | else insertNodeActions(votesListName, userAddressStr) |
---|
451 | 451 | | $Tuple2(([IntegerEntry(keyUsed(i.caller, epoch), (used + amount)), IntegerEntry(keyVote(pool, i.caller, epoch), newVote), IntegerEntry(keyVotingResult(pool, epoch), (poolResult + amount)), IntegerEntry(keyTotalVotes(epoch), (totalVotes + amount))] ++ votesListActions), unit) |
---|
452 | 452 | | } |
---|
453 | 453 | | else throw("Strict value is not equal to itself.") |
---|
454 | 454 | | } |
---|
455 | 455 | | |
---|
456 | 456 | | |
---|
457 | 457 | | |
---|
458 | 458 | | @Callable(i) |
---|
459 | 459 | | func cancelVote (amountAssetId,priceAssetId) = { |
---|
460 | 460 | | let pool = $Tuple2(amountAssetId, priceAssetId) |
---|
461 | 461 | | let epoch = valueOrElse(getInteger(this, keyCurrentEpoch), 0) |
---|
462 | 462 | | let startHeight = getIntOrFail(this, keyStartHeightByEpoch(epoch)) |
---|
463 | 463 | | let epochLength = getIntOrFail(this, keyEpochLength) |
---|
464 | 464 | | let endHeight = (startHeight + epochLength) |
---|
465 | 465 | | let finalizationStageOrUnit = getInteger(this, keyFinalizationStage) |
---|
466 | 466 | | let used = valueOrElse(getInteger(this, keyUsed(i.caller, epoch)), 0) |
---|
467 | 467 | | let vote = valueOrElse(getInteger(this, keyVote(pool, i.caller, epoch)), 0) |
---|
468 | 468 | | let poolResult = valueOrElse(getInteger(this, keyVotingResult(pool, epoch)), 0) |
---|
469 | 469 | | let totalVotes = valueOrElse(getInteger(this, keyTotalVotes(epoch)), 0) |
---|
470 | 470 | | let checks = [if ((getBoolean(keyInList(pool)) != unit)) |
---|
471 | 471 | | then true |
---|
472 | 472 | | else throwErr("invalid assets"), if ((endHeight > height)) |
---|
473 | 473 | | then true |
---|
474 | 474 | | else throwErr("invalid height"), if ((finalizationStageOrUnit == unit)) |
---|
475 | 475 | | then true |
---|
476 | 476 | | else throwErr("finalization in progress"), if ((vote > 0)) |
---|
477 | 477 | | then true |
---|
478 | 478 | | else throwErr("no vote")] |
---|
479 | 479 | | if ((checks == checks)) |
---|
480 | 480 | | then { |
---|
481 | 481 | | let votesListName = getVotesListName(pool) |
---|
482 | 482 | | let userAddressStr = toString(i.caller) |
---|
483 | 483 | | $Tuple2(([IntegerEntry(keyUsed(i.caller, epoch), max([(used - vote), 0])), DeleteEntry(keyVote(pool, i.caller, epoch)), IntegerEntry(keyVotingResult(pool, epoch), (poolResult - vote)), IntegerEntry(keyTotalVotes(epoch), (totalVotes - vote))] ++ deleteNodeActions(votesListName, userAddressStr)), unit) |
---|
484 | 484 | | } |
---|
485 | 485 | | else throw("Strict value is not equal to itself.") |
---|
486 | 486 | | } |
---|
487 | 487 | | |
---|
488 | 488 | | |
---|
489 | 489 | | |
---|
490 | 490 | | @Callable(i) |
---|
491 | 491 | | func setEpochLength (newEpochLength) = { |
---|
492 | 492 | | let checks = [mustManager(i), if ((newEpochLength > 0)) |
---|
493 | 493 | | then true |
---|
494 | 494 | | else throwErr("invalid epoch length")] |
---|
495 | 495 | | if ((checks == checks)) |
---|
496 | 496 | | then $Tuple2([IntegerEntry(keyEpochLengthNew, newEpochLength)], unit) |
---|
497 | 497 | | else throw("Strict value is not equal to itself.") |
---|
498 | 498 | | } |
---|
499 | 499 | | |
---|
500 | 500 | | |
---|
501 | 501 | | |
---|
502 | 502 | | @Callable(i) |
---|
503 | 503 | | func setMaxDepth (newMaxDepth) = { |
---|
504 | 504 | | let checks = [mustManager(i), if ((newMaxDepth > 0)) |
---|
505 | 505 | | then true |
---|
506 | 506 | | else throwErr("invalid max depth")] |
---|
507 | 507 | | if ((checks == checks)) |
---|
508 | 508 | | then $Tuple2([IntegerEntry(keyMaxDepth, newMaxDepth)], unit) |
---|
509 | 509 | | else throw("Strict value is not equal to itself.") |
---|
510 | 510 | | } |
---|
511 | 511 | | |
---|
512 | 512 | | |
---|
513 | 513 | | |
---|
514 | 514 | | @Callable(i) |
---|
515 | 515 | | func processPoolBalanceINTERNAL (poolStr) = { |
---|
516 | 516 | | let checkCaller = mustThis(i) |
---|
517 | 517 | | if ((checkCaller == checkCaller)) |
---|
518 | 518 | | then { |
---|
519 | 519 | | let epoch = getIntOrFail(this, keyCurrentEpoch) |
---|
520 | 520 | | let epochPrevious = (epoch - 1) |
---|
521 | 521 | | let pool = stringToPool(poolStr) |
---|
522 | 522 | | let $t01702017060 = pool |
---|
523 | 523 | | let amountAssetId = $t01702017060._1 |
---|
524 | 524 | | let priceAssetId = $t01702017060._2 |
---|
525 | 525 | | let lpAssetId = getLpAssetByPoolAssets(amountAssetId, priceAssetId) |
---|
526 | 526 | | let balanceIsOkCurrent = { |
---|
527 | 527 | | let @ = invoke(factoryContract, "checkBalance", [lpAssetId], nil) |
---|
528 | 528 | | if ($isInstanceOf(@, "Boolean")) |
---|
529 | 529 | | then @ |
---|
530 | 530 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
531 | 531 | | } |
---|
532 | 532 | | let balanceIsOkPrevious = !(valueOrElse(getBoolean(this, keyInsufficientBalances(pool, (epochPrevious - 1))), false)) |
---|
533 | 533 | | let actions = if (!(balanceIsOkCurrent)) |
---|
534 | 534 | | then if (!(balanceIsOkPrevious)) |
---|
535 | 535 | | then { |
---|
536 | 536 | | let deleteWxEmissionInv = invoke(factoryContract, "deleteWxEmissionPoolLabel", [amountAssetId, priceAssetId], nil) |
---|
537 | 537 | | if ((deleteWxEmissionInv == deleteWxEmissionInv)) |
---|
538 | 538 | | then { |
---|
539 | 539 | | let modifyWeightInv = invoke(factoryContract, "modifyWeight", [lpAssetId, 0], nil) |
---|
540 | 540 | | if ((modifyWeightInv == modifyWeightInv)) |
---|
541 | 541 | | then { |
---|
542 | 542 | | let poolAddress = valueOrErrorMessage(getPoolInfo(amountAssetId, priceAssetId), wrapErr("invalid assets"))._1 |
---|
543 | 543 | | let newStatus = 3 |
---|
544 | 544 | | let setPoolStatusInv = invoke(factoryContract, "managePool", [toString(poolAddress), newStatus], nil) |
---|
545 | 545 | | if ((setPoolStatusInv == setPoolStatusInv)) |
---|
546 | 546 | | then { |
---|
547 | 547 | | let listActions = ([DeleteEntry(keyInList(pool))] ++ deleteNodeActions(poolsListName, poolToString(pool))) |
---|
548 | 548 | | ([IntegerEntry(keyPoolShare(pool, epochPrevious), 0)] ++ listActions) |
---|
549 | 549 | | } |
---|
550 | 550 | | else throw("Strict value is not equal to itself.") |
---|
551 | 551 | | } |
---|
552 | 552 | | else throw("Strict value is not equal to itself.") |
---|
553 | 553 | | } |
---|
554 | 554 | | else throw("Strict value is not equal to itself.") |
---|
555 | 555 | | } |
---|
556 | 556 | | else [BooleanEntry(keyInsufficientBalances(pool, epochPrevious), true)] |
---|
557 | 557 | | else nil |
---|
558 | 558 | | $Tuple2(actions, unit) |
---|
559 | 559 | | } |
---|
560 | 560 | | else throw("Strict value is not equal to itself.") |
---|
561 | 561 | | } |
---|
562 | 562 | | |
---|
563 | 563 | | |
---|
564 | 564 | | |
---|
565 | 565 | | @Callable(i) |
---|
566 | 566 | | func processVoteINTERNAL (poolStr,userAddressStr) = { |
---|
567 | 567 | | let checkCaller = mustThis(i) |
---|
568 | 568 | | if ((checkCaller == checkCaller)) |
---|
569 | 569 | | then { |
---|
570 | 570 | | let userAddress = valueOrErrorMessage(addressFromString(userAddressStr), wrapErr(("processVoteINTERNAL: invalid user address " + userAddressStr))) |
---|
571 | 571 | | let epoch = getIntOrFail(this, keyCurrentEpoch) |
---|
572 | 572 | | let epochPrevious = (epoch - 1) |
---|
573 | 573 | | let epochLength = getIntOrFail(this, keyEpochLength) |
---|
574 | 574 | | let startHeight = getIntOrFail(this, keyStartHeightByEpoch(epoch)) |
---|
575 | 575 | | let endHeight = (startHeight + epochLength) |
---|
576 | 576 | | let startHeightPrevious = getIntOrFail(this, keyStartHeightByEpoch(epochPrevious)) |
---|
577 | 577 | | let epochLengthPrevious = getIntOrFail(this, keyEpochLengthByEpoch(epochPrevious)) |
---|
578 | 578 | | let endHeightPrevious = (startHeightPrevious + epochLengthPrevious) |
---|
579 | 579 | | let checkTargetEpoch = if ((epochPrevious >= 0)) |
---|
580 | 580 | | then true |
---|
581 | 581 | | else throwErr("processVoteINTERNAL: invalid previous epoch") |
---|
582 | 582 | | if ((checkTargetEpoch == checkTargetEpoch)) |
---|
583 | 583 | | then { |
---|
584 | 584 | | let pool = stringToPool(poolStr) |
---|
585 | 585 | | let $t01941319453 = pool |
---|
586 | 586 | | let amountAssetId = $t01941319453._1 |
---|
587 | 587 | | let priceAssetId = $t01941319453._2 |
---|
588 | 588 | | let gwxAmountAtEndTotal = { |
---|
589 | 589 | | let @ = invoke(this, "getUserGwxAmountAtHeight", [userAddressStr, endHeight], nil) |
---|
590 | 590 | | if ($isInstanceOf(@, "Int")) |
---|
591 | 591 | | then @ |
---|
592 | 592 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
593 | 593 | | } |
---|
594 | 594 | | let gwxAmountAtEndTotalPrevious = { |
---|
595 | 595 | | let @ = invoke(this, "getUserGwxAmountAtHeight", [userAddressStr, endHeightPrevious], nil) |
---|
596 | 596 | | if ($isInstanceOf(@, "Int")) |
---|
597 | 597 | | then @ |
---|
598 | 598 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
599 | 599 | | } |
---|
600 | 600 | | let totalVotes = valueOrElse(getInteger(keyTotalVotes(epoch)), 0) |
---|
601 | 601 | | let votingResult = valueOrElse(getInteger(keyVotingResult(pool, epoch)), 0) |
---|
602 | 602 | | let votePrevious = valueOrErrorMessage(getInteger(keyVote(pool, userAddress, epochPrevious)), wrapErr((((("processVoteINTERNAL " + poolStr) + " ") + userAddressStr) + ": no previous vote"))) |
---|
603 | 603 | | let used = valueOrElse(getInteger(this, keyUsed(userAddress, epoch)), 0) |
---|
604 | 604 | | let lpAssetIdStr = getLpAssetByPoolAssets(amountAssetId, priceAssetId) |
---|
605 | 605 | | let votingResultStakedPrevious = valueOrElse(getInteger(keyVotingResultStaked(lpAssetIdStr, epochPrevious)), 0) |
---|
606 | 606 | | let stakingContract = addressFromStringValue(getStrOrFail(this, keyStakingContract)) |
---|
607 | 607 | | let stakedByUser = valueOrElse(getInteger(stakingContract, keyStakedByUser(lpAssetIdStr, userAddressStr)), 0) |
---|
608 | 608 | | let votingResultStakedActions = if ((stakedByUser == 0)) |
---|
609 | 609 | | then nil |
---|
610 | 610 | | else [IntegerEntry(keyVotingResultStaked(lpAssetIdStr, epochPrevious), (votingResultStakedPrevious + votePrevious))] |
---|
611 | 611 | | let lpAssetId = getLpAssetByPoolAssets(amountAssetId, priceAssetId) |
---|
612 | 612 | | let newVote = if ((gwxAmountAtEndTotalPrevious > 0)) |
---|
613 | 613 | | then fraction(votePrevious, gwxAmountAtEndTotal, gwxAmountAtEndTotalPrevious) |
---|
614 | 614 | | else 0 |
---|
615 | 615 | | let actions = if ((newVote > 0)) |
---|
616 | 616 | | then [IntegerEntry(keyVote(pool, userAddress, epoch), newVote), IntegerEntry(keyTotalVotes(epoch), (totalVotes + newVote)), IntegerEntry(keyVotingResult(pool, epoch), (votingResult + newVote)), IntegerEntry(keyUsed(userAddress, epoch), (used + newVote))] |
---|
617 | 617 | | else deleteNodeActions(getVotesListName(pool), userAddressStr) |
---|
618 | 618 | | $Tuple2((actions ++ votingResultStakedActions), unit) |
---|
619 | 619 | | } |
---|
620 | 620 | | else throw("Strict value is not equal to itself.") |
---|
621 | 621 | | } |
---|
622 | 622 | | else throw("Strict value is not equal to itself.") |
---|
623 | 623 | | } |
---|
624 | 624 | | |
---|
625 | 625 | | |
---|
626 | 626 | | |
---|
627 | 627 | | @Callable(i) |
---|
628 | 628 | | func processPoolINTERNAL (poolStr,force) = { |
---|
629 | 629 | | let checkCaller = mustThis(i) |
---|
630 | 630 | | if ((checkCaller == checkCaller)) |
---|
631 | 631 | | then { |
---|
632 | 632 | | let targetEpoch = { |
---|
633 | 633 | | let currentEpoch = getIntOrFail(this, keyCurrentEpoch) |
---|
634 | 634 | | if (force) |
---|
635 | 635 | | then currentEpoch |
---|
636 | 636 | | else (currentEpoch - 1) |
---|
637 | 637 | | } |
---|
638 | 638 | | let checkTargetEpoch = if ((targetEpoch >= 0)) |
---|
639 | 639 | | then true |
---|
640 | 640 | | else throwErr("processPoolINTERNAL: invalid target epoch") |
---|
641 | 641 | | if ((checkTargetEpoch == checkTargetEpoch)) |
---|
642 | 642 | | then { |
---|
643 | 643 | | let pool = stringToPool(poolStr) |
---|
644 | 644 | | let $t02181621856 = pool |
---|
645 | 645 | | let amountAssetId = $t02181621856._1 |
---|
646 | 646 | | let priceAssetId = $t02181621856._2 |
---|
647 | 647 | | let stakingContract = addressFromStringValue(getStrOrFail(this, keyStakingContract)) |
---|
648 | 648 | | let lpAssetId = getLpAssetByPoolAssets(amountAssetId, priceAssetId) |
---|
649 | 649 | | let r = { |
---|
650 | 650 | | let @ = invoke(stakingContract, "usersListTraversal", [lpAssetId], nil) |
---|
651 | 651 | | if ($isInstanceOf(@, "Boolean")) |
---|
652 | 652 | | then @ |
---|
653 | 653 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
654 | 654 | | } |
---|
655 | 655 | | if ((r == r)) |
---|
656 | 656 | | then if (r) |
---|
657 | 657 | | then $Tuple2(nil, true) |
---|
658 | 658 | | else { |
---|
659 | 659 | | let assetsStoreContract = addressFromStringValue(valueOrErrorMessage(getString(factoryContract, keyAssetsStoreContract), "invalid assets store contract")) |
---|
660 | 660 | | let totalVotes = valueOrElse(getInteger(this, keyTotalVotes(targetEpoch)), 0) |
---|
661 | 661 | | let votingResult = valueOrElse(getInteger(this, keyVotingResult(pool, targetEpoch)), 0) |
---|
662 | 662 | | let share = if ((totalVotes == 0)) |
---|
663 | 663 | | then 0 |
---|
664 | 664 | | else fraction(votingResult, poolWeightMult, totalVotes) |
---|
665 | 665 | | let modifyWeightInv = invoke(factoryContract, "modifyWeight", [lpAssetId, share], nil) |
---|
666 | 666 | | if ((modifyWeightInv == modifyWeightInv)) |
---|
667 | 667 | | then $Tuple2([IntegerEntry(keyPoolShare(pool, targetEpoch), share)], false) |
---|
668 | 668 | | else throw("Strict value is not equal to itself.") |
---|
669 | 669 | | } |
---|
670 | 670 | | else throw("Strict value is not equal to itself.") |
---|
671 | 671 | | } |
---|
672 | 672 | | else throw("Strict value is not equal to itself.") |
---|
673 | 673 | | } |
---|
674 | 674 | | else throw("Strict value is not equal to itself.") |
---|
675 | 675 | | } |
---|
676 | 676 | | |
---|
677 | 677 | | |
---|
678 | 678 | | |
---|
679 | 679 | | @Callable(i) |
---|
680 | 680 | | func finalizeHelper () = { |
---|
681 | 681 | | let force = valueOrElse(getBoolean(keyFinalizationShouldBeForced), false) |
---|
682 | 682 | | let epoch = valueOrElse(getInteger(this, keyCurrentEpoch), 0) |
---|
683 | 683 | | let previousEpoch = (epoch - 1) |
---|
684 | 684 | | let startHeight = getIntOrFail(this, keyStartHeightByEpoch(epoch)) |
---|
685 | 685 | | let epochLength = getIntOrFail(this, keyEpochLength) |
---|
686 | 686 | | let endHeight = (startHeight + epochLength) |
---|
687 | 687 | | let finalizationStageOrUnit = getInteger(this, keyFinalizationStage) |
---|
688 | 688 | | if (if (if ((height >= endHeight)) |
---|
689 | 689 | | then (finalizationStageOrUnit == unit) |
---|
690 | 690 | | else false) |
---|
691 | 691 | | then !(force) |
---|
692 | 692 | | else false) |
---|
693 | 693 | | then { |
---|
694 | 694 | | let newEpoch = (epoch + 1) |
---|
695 | 695 | | let newEpochLengthOption = getInteger(this, keyEpochLengthNew) |
---|
696 | 696 | | let newEpochLengthActions = match newEpochLengthOption { |
---|
697 | 697 | | case newEpochLength: Int => |
---|
698 | 698 | | [IntegerEntry(keyEpochLength, newEpochLength), DeleteEntry(keyEpochLengthNew)] |
---|
699 | 699 | | case _: Unit => |
---|
700 | 700 | | nil |
---|
701 | 701 | | case _ => |
---|
702 | 702 | | throw("Match error") |
---|
703 | 703 | | } |
---|
704 | 704 | | $Tuple2(([IntegerEntry(keyStartHeightByEpoch(newEpoch), height), IntegerEntry(keyStartHeight, height), IntegerEntry(keyCurrentEpoch, newEpoch), IntegerEntry(keyFinalizationStage, finalizationStageBalances), IntegerEntry(keyEpochLengthByEpoch(epoch), epochLength)] ++ newEpochLengthActions), true) |
---|
705 | 705 | | } |
---|
706 | 706 | | else if (if (force) |
---|
707 | 707 | | then (finalizationStageOrUnit == unit) |
---|
708 | 708 | | else false) |
---|
709 | 709 | | then $Tuple2([IntegerEntry(keyFinalizationStage, finalizationStageShares)], true) |
---|
710 | 710 | | else if ((finalizationStageOrUnit == unit)) |
---|
711 | 711 | | then $Tuple2(nil, false) |
---|
712 | 712 | | else if ((finalizationStageOrUnit == finalizationStageBalances)) |
---|
713 | 713 | | then { |
---|
714 | 714 | | let poolOrUnit = getString(keyNextPool) |
---|
715 | 715 | | match poolOrUnit { |
---|
716 | 716 | | case _: Unit => |
---|
717 | 717 | | match getString(keyListHead(poolsListName)) { |
---|
718 | 718 | | case _: Unit => |
---|
719 | 719 | | $Tuple2([IntegerEntry(keyFinalizationStage, finalizationStageTotal), DeleteEntry(keyNextPool)], true) |
---|
720 | 720 | | case nextPoolStr: String => |
---|
721 | 721 | | $Tuple2([StringEntry(keyNextPool, nextPoolStr)], true) |
---|
722 | 722 | | case _ => |
---|
723 | 723 | | throw("Match error") |
---|
724 | 724 | | } |
---|
725 | 725 | | case poolStr: String => |
---|
726 | 726 | | let nextPoolOrUnit = getString(keyListNext(poolsListName, poolStr)) |
---|
727 | 727 | | if ((nextPoolOrUnit == nextPoolOrUnit)) |
---|
728 | 728 | | then { |
---|
729 | 729 | | let r = invoke(this, "processPoolBalanceINTERNAL", [poolStr], nil) |
---|
730 | 730 | | if ((r == r)) |
---|
731 | 731 | | then match nextPoolOrUnit { |
---|
732 | 732 | | case _: Unit => |
---|
733 | 733 | | $Tuple2([IntegerEntry(keyFinalizationStage, finalizationStageTotal), DeleteEntry(keyNextPool)], true) |
---|
734 | 734 | | case nextPoolStr: String => |
---|
735 | 735 | | $Tuple2([StringEntry(keyNextPool, nextPoolStr)], true) |
---|
736 | 736 | | case _ => |
---|
737 | 737 | | throw("Match error") |
---|
738 | 738 | | } |
---|
739 | 739 | | else throw("Strict value is not equal to itself.") |
---|
740 | 740 | | } |
---|
741 | 741 | | else throw("Strict value is not equal to itself.") |
---|
742 | 742 | | case _ => |
---|
743 | 743 | | throw("Match error") |
---|
744 | 744 | | } |
---|
745 | 745 | | } |
---|
746 | 746 | | else if ((finalizationStageOrUnit == finalizationStageTotal)) |
---|
747 | 747 | | then { |
---|
748 | 748 | | let poolOrUnit = getString(keyNextPool) |
---|
749 | 749 | | let userOrUnit = getString(keyNextUser) |
---|
750 | 750 | | match poolOrUnit { |
---|
751 | 751 | | case _: Unit => |
---|
752 | 752 | | match getString(keyListHead(poolsListName)) { |
---|
753 | 753 | | case _: Unit => |
---|
754 | 754 | | $Tuple2([IntegerEntry(keyFinalizationStage, finalizationStageShares), DeleteEntry(keyNextPool), DeleteEntry(keyNextUser)], true) |
---|
755 | 755 | | case poolsHeadStr: String => |
---|
756 | 756 | | $Tuple2([StringEntry(keyNextPool, poolsHeadStr)], true) |
---|
757 | 757 | | case _ => |
---|
758 | 758 | | throw("Match error") |
---|
759 | 759 | | } |
---|
760 | 760 | | case poolStr: String => |
---|
761 | 761 | | let pool = stringToPool(poolStr) |
---|
762 | 762 | | let nextUserOrUnit = match userOrUnit { |
---|
763 | 763 | | case _: Unit => |
---|
764 | 764 | | getString(keyListHead(getVotesListName(pool))) |
---|
765 | 765 | | case user: String => |
---|
766 | 766 | | let next = getString(keyListNext(getVotesListName(pool), user)) |
---|
767 | 767 | | if ((next == next)) |
---|
768 | 768 | | then { |
---|
769 | 769 | | let processVoteInv = invoke(this, "processVoteINTERNAL", [poolStr, user], nil) |
---|
770 | 770 | | if ((processVoteInv == processVoteInv)) |
---|
771 | 771 | | then next |
---|
772 | 772 | | else throw("Strict value is not equal to itself.") |
---|
773 | 773 | | } |
---|
774 | 774 | | else throw("Strict value is not equal to itself.") |
---|
775 | 775 | | case _ => |
---|
776 | 776 | | throw("Match error") |
---|
777 | 777 | | } |
---|
778 | 778 | | match nextUserOrUnit { |
---|
779 | 779 | | case _: Unit => |
---|
780 | 780 | | let nextPoolOrUnit = getString(keyListNext(poolsListName, poolStr)) |
---|
781 | 781 | | match nextPoolOrUnit { |
---|
782 | 782 | | case _: Unit => |
---|
783 | 783 | | $Tuple2([IntegerEntry(keyFinalizationStage, finalizationStageShares), DeleteEntry(keyNextPool), DeleteEntry(keyNextUser)], true) |
---|
784 | 784 | | case s: String => |
---|
785 | 785 | | $Tuple2([StringEntry(keyNextPool, s), DeleteEntry(keyNextUser)], true) |
---|
786 | 786 | | case _ => |
---|
787 | 787 | | throw("Match error") |
---|
788 | 788 | | } |
---|
789 | 789 | | case nextUser: String => |
---|
790 | 790 | | $Tuple2([StringEntry(keyNextUser, nextUser)], true) |
---|
791 | 791 | | case _ => |
---|
792 | 792 | | throw("Match error") |
---|
793 | 793 | | } |
---|
794 | 794 | | case _ => |
---|
795 | 795 | | throw("Match error") |
---|
796 | 796 | | } |
---|
797 | 797 | | } |
---|
798 | 798 | | else if ((finalizationStageOrUnit == finalizationStageShares)) |
---|
799 | 799 | | then { |
---|
800 | 800 | | let poolOrUnit = getString(keyNextPool) |
---|
801 | 801 | | match poolOrUnit { |
---|
802 | 802 | | case _: Unit => |
---|
803 | 803 | | match getString(keyListHead(poolsListName)) { |
---|
804 | 804 | | case _: Unit => |
---|
805 | 805 | | let actions = if (force) |
---|
806 | 806 | | then [DeleteEntry(keyFinalizationStage), DeleteEntry(keyFinalizationShouldBeForced)] |
---|
807 | 807 | | else [DeleteEntry(keyFinalizationStage), BooleanEntry(keyFinalized(previousEpoch), true), IntegerEntry(keyCurrentEpochUi, epoch), IntegerEntry(keyStartHeightUi, startHeight)] |
---|
808 | 808 | | let gwxRewardDepositInv = gwxRewardDeposit() |
---|
809 | 809 | | if ((gwxRewardDepositInv == gwxRewardDepositInv)) |
---|
810 | 810 | | then $Tuple2(actions, true) |
---|
811 | 811 | | else throw("Strict value is not equal to itself.") |
---|
812 | 812 | | case nextPoolStr: String => |
---|
813 | 813 | | $Tuple2([StringEntry(keyNextPool, nextPoolStr)], true) |
---|
814 | 814 | | case _ => |
---|
815 | 815 | | throw("Match error") |
---|
816 | 816 | | } |
---|
817 | 817 | | case poolStr: String => |
---|
818 | 818 | | let nextPoolOrUnit = getString(keyListNext(poolsListName, poolStr)) |
---|
819 | 819 | | if ((nextPoolOrUnit == nextPoolOrUnit)) |
---|
820 | 820 | | then { |
---|
821 | 821 | | let r = { |
---|
822 | 822 | | let @ = invoke(this, "processPoolINTERNAL", [poolStr, force], nil) |
---|
823 | 823 | | if ($isInstanceOf(@, "Boolean")) |
---|
824 | 824 | | then @ |
---|
825 | 825 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
826 | 826 | | } |
---|
827 | 827 | | if ((r == r)) |
---|
828 | 828 | | then if (r) |
---|
829 | 829 | | then $Tuple2(nil, true) |
---|
830 | 830 | | else match nextPoolOrUnit { |
---|
831 | 831 | | case _: Unit => |
---|
832 | 832 | | let actions = if (force) |
---|
833 | 833 | | then [DeleteEntry(keyFinalizationStage), DeleteEntry(keyFinalizationShouldBeForced), DeleteEntry(keyNextPool)] |
---|
834 | 834 | | else [DeleteEntry(keyFinalizationStage), BooleanEntry(keyFinalized(previousEpoch), true), IntegerEntry(keyCurrentEpochUi, epoch), IntegerEntry(keyStartHeightUi, startHeight), DeleteEntry(keyNextPool)] |
---|
835 | 835 | | let gwxRewardDepositInv = gwxRewardDeposit() |
---|
836 | 836 | | if ((gwxRewardDepositInv == gwxRewardDepositInv)) |
---|
837 | 837 | | then $Tuple2(actions, true) |
---|
838 | 838 | | else throw("Strict value is not equal to itself.") |
---|
839 | 839 | | case nextPoolStr: String => |
---|
840 | 840 | | $Tuple2([StringEntry(keyNextPool, nextPoolStr)], true) |
---|
841 | 841 | | case _ => |
---|
842 | 842 | | throw("Match error") |
---|
843 | 843 | | } |
---|
844 | 844 | | else throw("Strict value is not equal to itself.") |
---|
845 | 845 | | } |
---|
846 | 846 | | else throw("Strict value is not equal to itself.") |
---|
847 | 847 | | case _ => |
---|
848 | 848 | | throw("Match error") |
---|
849 | 849 | | } |
---|
850 | 850 | | } |
---|
851 | 851 | | else throwErr("finalization is broken") |
---|
852 | 852 | | } |
---|
853 | 853 | | |
---|
854 | 854 | | |
---|
855 | 855 | | |
---|
856 | 856 | | @Callable(i) |
---|
857 | 857 | | func finalizeWrapper (counter) = { |
---|
858 | 858 | | let votingEmissionRateContract = addressFromStringValue(getStringValue(this, keyVotingEmissionRateContract)) |
---|
859 | 859 | | let result = { |
---|
860 | 860 | | let @ = invoke(this, "finalizeHelper", nil, nil) |
---|
861 | 861 | | if ($isInstanceOf(@, "Boolean")) |
---|
862 | 862 | | then @ |
---|
863 | 863 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
864 | 864 | | } |
---|
865 | 865 | | if ((result == result)) |
---|
866 | 866 | | then if (!(result)) |
---|
867 | 867 | | then if ((counter == 0)) |
---|
868 | 868 | | then throwErr("Current voting is not over yet") |
---|
869 | 869 | | else { |
---|
870 | 870 | | let inv = invoke(votingEmissionRateContract, "finalize", nil, nil) |
---|
871 | 871 | | if ((inv == inv)) |
---|
872 | 872 | | then $Tuple2(nil, unit) |
---|
873 | 873 | | else throw("Strict value is not equal to itself.") |
---|
874 | 874 | | } |
---|
875 | 875 | | else { |
---|
876 | 876 | | let maxDepth = valueOrElse(getInteger(this, keyMaxDepth), maxDepthDefault) |
---|
877 | 877 | | if ((maxDepth > counter)) |
---|
878 | 878 | | then { |
---|
879 | 879 | | let inv = invoke(this, "finalizeWrapper", [(counter + 1)], nil) |
---|
880 | 880 | | if ((inv == inv)) |
---|
881 | 881 | | then $Tuple2(nil, unit) |
---|
882 | 882 | | else throw("Strict value is not equal to itself.") |
---|
883 | 883 | | } |
---|
884 | 884 | | else $Tuple2(nil, unit) |
---|
885 | 885 | | } |
---|
886 | 886 | | else throw("Strict value is not equal to itself.") |
---|
887 | 887 | | } |
---|
888 | 888 | | |
---|
889 | 889 | | |
---|
890 | 890 | | |
---|
891 | 891 | | @Callable(i) |
---|
892 | 892 | | func finalize () = { |
---|
893 | 893 | | let inv = invoke(this, "finalizeWrapper", [0], nil) |
---|
894 | 894 | | if ((inv == inv)) |
---|
895 | 895 | | then $Tuple2(nil, unit) |
---|
896 | 896 | | else throw("Strict value is not equal to itself.") |
---|
897 | 897 | | } |
---|
898 | 898 | | |
---|
899 | 899 | | |
---|
900 | 900 | | |
---|
901 | 901 | | @Callable(i) |
---|
902 | 902 | | func containsNodeREADONLY (listName,id) = $Tuple2(nil, containsNode(listName, id)) |
---|
903 | 903 | | |
---|
904 | 904 | | |
---|
905 | 905 | | |
---|
906 | 906 | | @Callable(i) |
---|
907 | 907 | | func insertNode (listName,id) = { |
---|
908 | 908 | | let checkCaller = mustManager(i) |
---|
909 | 909 | | if ((checkCaller == checkCaller)) |
---|
910 | 910 | | then $Tuple2(insertNodeActions(listName, id), unit) |
---|
911 | 911 | | else throw("Strict value is not equal to itself.") |
---|
912 | 912 | | } |
---|
913 | 913 | | |
---|
914 | 914 | | |
---|
915 | 915 | | |
---|
916 | 916 | | @Callable(i) |
---|
917 | 917 | | func deleteNode (listName,id) = { |
---|
918 | 918 | | let checkCaller = mustManager(i) |
---|
919 | 919 | | if ((checkCaller == checkCaller)) |
---|
920 | 920 | | then $Tuple2(deleteNodeActions(listName, id), unit) |
---|
921 | 921 | | else throw("Strict value is not equal to itself.") |
---|
922 | 922 | | } |
---|
923 | 923 | | |
---|
924 | 924 | | |
---|
925 | 925 | | |
---|
926 | 926 | | @Callable(i) |
---|
927 | 927 | | func isFinalizationInProgress () = { |
---|
928 | 928 | | let finalizationStageOrUnit = getInteger(this, keyFinalizationStage) |
---|
929 | 929 | | let finalizationInProgress = (finalizationStageOrUnit != unit) |
---|
930 | 930 | | $Tuple2(nil, finalizationInProgress) |
---|
931 | 931 | | } |
---|
932 | 932 | | |
---|
933 | 933 | | |
---|
934 | 934 | | |
---|
935 | 935 | | @Callable(i) |
---|
936 | 936 | | func deletePool (amountAssetId,priceAssetId) = { |
---|
937 | 937 | | let checkCaller = if (if ((i.caller == factoryContract)) |
---|
938 | 938 | | then true |
---|
939 | 939 | | else mustManager(i)) |
---|
940 | 940 | | then true |
---|
941 | 941 | | else throwErr("Permission denied") |
---|
942 | 942 | | if ((checkCaller == checkCaller)) |
---|
943 | 943 | | then { |
---|
944 | 944 | | let listName = "pools" |
---|
945 | 945 | | let pool = $Tuple2(amountAssetId, priceAssetId) |
---|
946 | 946 | | let id = makeString([amountAssetId, priceAssetId], separator) |
---|
947 | 947 | | let actions = if (containsNode(listName, id)) |
---|
948 | 948 | | then deleteNodeActions(listName, id) |
---|
949 | 949 | | else nil |
---|
950 | 950 | | ([DeleteEntry(keyInList(pool))] ++ actions) |
---|
951 | 951 | | } |
---|
952 | 952 | | else throw("Strict value is not equal to itself.") |
---|
953 | 953 | | } |
---|
954 | 954 | | |
---|
955 | 955 | | |
---|
956 | 956 | | |
---|
957 | 957 | | @Callable(i) |
---|
958 | 958 | | func resume (amountAssetId,priceAssetId) = { |
---|
959 | 959 | | let lpAssetId = getLpAssetByPoolAssets(amountAssetId, priceAssetId) |
---|
960 | 960 | | let balanceIsOk = { |
---|
961 | 961 | | let @ = invoke(factoryContract, "checkBalance", [lpAssetId], nil) |
---|
962 | 962 | | if ($isInstanceOf(@, "Boolean")) |
---|
963 | 963 | | then @ |
---|
964 | 964 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
965 | 965 | | } |
---|
966 | 966 | | let payment = i.payments[0] |
---|
967 | 967 | | let assetsStoreContract = addressFromStringValue(getStringValue(this, keyAssetsStoreContract)) |
---|
968 | 968 | | let kBoostingConfig = "%s__config" |
---|
969 | 969 | | let idxCfgAssetId = 1 |
---|
970 | 970 | | let boostingContract = addressFromStringValue(getStrOrFail(this, keyBoostingContract)) |
---|
971 | 971 | | let wxAssetId = fromBase58String(split(getStringValue(boostingContract, kBoostingConfig), separator)[idxCfgAssetId]) |
---|
972 | 972 | | let amountAssetVerified = { |
---|
973 | 973 | | let @ = invoke(assetsStoreContract, "isVerifiedREADONLY", [amountAssetId], nil) |
---|
974 | 974 | | if ($isInstanceOf(@, "Boolean")) |
---|
975 | 975 | | then @ |
---|
976 | 976 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
977 | 977 | | } |
---|
978 | 978 | | let priceAssetVerified = { |
---|
979 | 979 | | let @ = invoke(assetsStoreContract, "isVerifiedREADONLY", [priceAssetId], nil) |
---|
980 | 980 | | if ($isInstanceOf(@, "Boolean")) |
---|
981 | 981 | | then @ |
---|
982 | 982 | | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
983 | 983 | | } |
---|
984 | 984 | | let resumptionFee = valueOrElse(getInteger(this, keyResumptionFee), resumptionFeeDefault) |
---|
985 | 985 | | let checks = [if (balanceIsOk) |
---|
986 | 986 | | then true |
---|
987 | 987 | | else throwErr("insufficient balances"), if ((size(i.payments) == 1)) |
---|
988 | 988 | | then true |
---|
989 | 989 | | else throwErr("1 payment is required"), if ((payment.assetId == wxAssetId)) |
---|
990 | 990 | | then true |
---|
991 | 991 | | else throwErr("invalid payment asset id"), if ((payment.amount == resumptionFee)) |
---|
992 | 992 | | then true |
---|
993 | 993 | | else throwErr("invalid payment amount"), if (if (amountAssetVerified) |
---|
994 | 994 | | then priceAssetVerified |
---|
995 | 995 | | else false) |
---|
996 | 996 | | then true |
---|
997 | 997 | | else throwErr("both assets should be verified")] |
---|
998 | 998 | | if ((checks == checks)) |
---|
999 | 999 | | then { |
---|