4 | | - | let adminKey = base58'3P8qVX189qpoTJZQQQdKS9endHK5sxWsvrd' |
---|
5 | | - | |
---|
6 | | - | let T = 3 |
---|
7 | | - | |
---|
8 | | - | let assetIds = [base58'HEB8Qaw9xrWpWs8tHsiATYGBWDBtP2S7kcPALrMu43AS', base58'34N9YcEETLWn93qYQ64EsP1x89tSruJU44RrEMSXXEPJ', base58'DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p'] |
---|
9 | | - | |
---|
10 | | - | let AssetsWeights = [80, 10, 10] |
---|
11 | | - | |
---|
12 | | - | let AssetsWeightsDecimals = 2 |
---|
13 | | - | |
---|
14 | | - | let PoolTokenDecimals = 8 |
---|
15 | | - | |
---|
16 | | - | let Decimals = [8, 6, 6] |
---|
17 | | - | |
---|
18 | | - | let Scales = [100000000, 1000000, 1000000] |
---|
19 | | - | |
---|
20 | | - | let Fee = 200 |
---|
21 | | - | |
---|
22 | | - | let Scale = 10000 |
---|
23 | | - | |
---|
24 | | - | let Scale8 = 100000000 |
---|
25 | | - | |
---|
26 | | - | let FeeScale = 10000 |
---|
27 | | - | |
---|
28 | | - | let feeAggregator = Address(base58'3PES7MMthaKJx9WMXnNCY3cwTGG9nD9YT8f') |
---|
29 | | - | |
---|
30 | | - | func getAssetString (assetId) = match assetId { |
---|
31 | | - | case b: ByteVector => |
---|
32 | | - | toBase58String(b) |
---|
33 | | - | case _ => |
---|
34 | | - | "WAVES" |
---|
35 | | - | } |
---|
36 | | - | |
---|
37 | | - | |
---|
38 | | - | func getAssetBytes (assetIdStr) = if ((assetIdStr == "WAVES")) |
---|
39 | | - | then unit |
---|
40 | | - | else fromBase58String(assetIdStr) |
---|
41 | | - | |
---|
42 | | - | |
---|
43 | | - | let earnedAssets = assetIds |
---|
44 | | - | |
---|
45 | | - | func tryGetInteger (key) = match getInteger(this, key) { |
---|
46 | | - | case b: Int => |
---|
47 | | - | b |
---|
48 | | - | case _ => |
---|
49 | | - | 0 |
---|
50 | | - | } |
---|
51 | | - | |
---|
52 | | - | |
---|
53 | | - | func tryGetBinary (key) = match getBinary(this, key) { |
---|
54 | | - | case b: ByteVector => |
---|
55 | | - | b |
---|
56 | | - | case _ => |
---|
57 | | - | base58'' |
---|
58 | | - | } |
---|
59 | | - | |
---|
60 | | - | |
---|
61 | | - | func getCurrentTokenBalance (tokenType) = { |
---|
62 | | - | let tokenId = getAssetString(assetIds[tokenType]) |
---|
63 | | - | tryGetInteger((("global_" + tokenId) + "_balance")) |
---|
64 | | - | } |
---|
65 | | - | |
---|
66 | | - | |
---|
67 | | - | func calculatePIssued (amount,tokenId) = { |
---|
68 | | - | let Psupply = tryGetInteger("global_poolToken_amount") |
---|
69 | | - | let Balance = tryGetInteger((("global_" + toBase58String(tokenId)) + "_balance")) |
---|
70 | | - | fraction(amount, Psupply, Balance, DOWN) |
---|
71 | | - | } |
---|
72 | | - | |
---|
73 | | - | |
---|
74 | | - | func getMinPIssued (payments) = { |
---|
75 | | - | func handler (accum,current) = { |
---|
76 | | - | let PIssued = calculatePIssued(current.amount, value(current.assetId)) |
---|
77 | | - | if (if ((accum == 0)) |
---|
78 | | - | then true |
---|
79 | | - | else (accum > PIssued)) |
---|
80 | | - | then PIssued |
---|
81 | | - | else accum |
---|
82 | | - | } |
---|
83 | | - | |
---|
84 | | - | let minPIssed = { |
---|
85 | | - | let $l = payments |
---|
86 | | - | let $s = size($l) |
---|
87 | | - | let $acc0 = 0 |
---|
88 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
89 | | - | then $a |
---|
90 | | - | else handler($a, $l[$i]) |
---|
91 | | - | |
---|
92 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
93 | | - | then $a |
---|
94 | | - | else throw("List size exceeds 10") |
---|
95 | | - | |
---|
96 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
97 | | - | } |
---|
98 | | - | minPIssed |
---|
99 | | - | } |
---|
100 | | - | |
---|
101 | | - | |
---|
102 | | - | func calculateUsdnValue (assetId,amount,aBalance) = { |
---|
103 | | - | let usdnId = base58'DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p' |
---|
104 | | - | let usdnWeight = AssetsWeights[value(indexOf(assetIds, usdnId))] |
---|
105 | | - | let assetWeight = AssetsWeights[value(indexOf(assetIds, assetId))] |
---|
106 | | - | let usdnBalance = tryGetInteger((("global_" + getAssetString(usdnId)) + "_balance")) |
---|
107 | | - | fraction(fraction(amount, usdnBalance, usdnWeight), assetWeight, aBalance) |
---|
108 | | - | } |
---|
109 | | - | |
---|
110 | | - | |
---|
111 | | - | func checkTokensValidity (payments) = { |
---|
112 | | - | func handler1 (accum,payment) = (accum ++ [value(payment.assetId)]) |
---|
113 | | - | |
---|
114 | | - | let ids = { |
---|
115 | | - | let $l = payments |
---|
116 | | - | let $s = size($l) |
---|
117 | | - | let $acc0 = nil |
---|
118 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
119 | | - | then $a |
---|
120 | | - | else handler1($a, $l[$i]) |
---|
121 | | - | |
---|
122 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
123 | | - | then $a |
---|
124 | | - | else throw("List size exceeds 10") |
---|
125 | | - | |
---|
126 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
127 | | - | } |
---|
128 | | - | if ((ids == ids)) |
---|
129 | | - | then { |
---|
130 | | - | func handler2 (accum,assetId) = if ((indexOf(ids, assetId) != unit)) |
---|
131 | | - | then (accum + 1) |
---|
132 | | - | else throw(("asset not attached: " + getAssetString(assetId))) |
---|
133 | | - | |
---|
134 | | - | let checks = { |
---|
135 | | - | let $l = assetIds |
---|
136 | | - | let $s = size($l) |
---|
137 | | - | let $acc0 = 0 |
---|
138 | | - | func $f1_1 ($a,$i) = if (($i >= $s)) |
---|
139 | | - | then $a |
---|
140 | | - | else handler2($a, $l[$i]) |
---|
141 | | - | |
---|
142 | | - | func $f1_2 ($a,$i) = if (($i >= $s)) |
---|
143 | | - | then $a |
---|
144 | | - | else throw("List size exceeds 10") |
---|
145 | | - | |
---|
146 | | - | $f1_2($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
147 | | - | } |
---|
148 | | - | if ((checks == checks)) |
---|
149 | | - | then true |
---|
150 | | - | else throw("Strict value is not equal to itself.") |
---|
151 | | - | } |
---|
152 | | - | else throw("Strict value is not equal to itself.") |
---|
153 | | - | } |
---|
154 | | - | |
---|
155 | | - | |
---|
156 | | - | func handlePoolTokensAdd (PIssued,payments,userAddress,needChange) = { |
---|
157 | | - | func getTokenPaymentAmount (tokenId) = { |
---|
158 | | - | func handler (accum,payment) = if ((payment.assetId == tokenId)) |
---|
159 | | - | then payment.amount |
---|
160 | | - | else accum |
---|
161 | | - | |
---|
162 | | - | let $l = payments |
---|
163 | | - | let $s = size($l) |
---|
164 | | - | let $acc0 = 0 |
---|
165 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
166 | | - | then $a |
---|
167 | | - | else handler($a, $l[$i]) |
---|
168 | | - | |
---|
169 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
170 | | - | then $a |
---|
171 | | - | else throw("List size exceeds 10") |
---|
172 | | - | |
---|
173 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
174 | | - | } |
---|
175 | | - | |
---|
176 | | - | func handleTokenChange (accum,tokenId) = { |
---|
177 | | - | let Bk = tryGetInteger((("global_" + getAssetString(tokenId)) + "_balance")) |
---|
178 | | - | let PSupply = tryGetInteger("global_poolToken_amount") |
---|
179 | | - | let tokenDecimals = tryGetInteger((("static_" + getAssetString(tokenId)) + "_scale")) |
---|
180 | | - | let Dk = fraction((fraction((PSupply + PIssued), tokenDecimals, PSupply, DOWN) - tokenDecimals), Bk, tokenDecimals, DOWN) |
---|
181 | | - | let paymentAmount = getTokenPaymentAmount(tokenId) |
---|
182 | | - | let toReturn = ((if ((paymentAmount != 0)) |
---|
183 | | - | then paymentAmount |
---|
184 | | - | else 0) - Dk) |
---|
185 | | - | let t = if (needChange) |
---|
186 | | - | then [ScriptTransfer(userAddress, toReturn, tokenId)] |
---|
187 | | - | else nil |
---|
188 | | - | ((accum ++ t) ++ [IntegerEntry((("global_" + getAssetString(tokenId)) + "_balance"), (Bk + Dk))]) |
---|
189 | | - | } |
---|
190 | | - | |
---|
191 | | - | let $l = assetIds |
---|
192 | | - | let $s = size($l) |
---|
193 | | - | let $acc0 = nil |
---|
194 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
195 | | - | then $a |
---|
196 | | - | else handleTokenChange($a, $l[$i]) |
---|
197 | | - | |
---|
198 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
199 | | - | then $a |
---|
200 | | - | else throw("List size exceeds 10") |
---|
201 | | - | |
---|
202 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
203 | | - | } |
---|
204 | | - | |
---|
205 | | - | |
---|
206 | | - | func handlePoolTokensRedeem (PRedeemed,userAddress) = { |
---|
207 | | - | func handleTokenRedeem (accum,tokenId) = { |
---|
208 | | - | let Bk = tryGetInteger((("global_" + getAssetString(tokenId)) + "_balance")) |
---|
209 | | - | let PSupply = tryGetInteger("global_poolToken_amount") |
---|
210 | | - | let tokenDecimals = tryGetInteger((("static_" + getAssetString(tokenId)) + "_scale")) |
---|
211 | | - | let amount = fraction((tokenDecimals - fraction((PSupply - PRedeemed), tokenDecimals, PSupply, DOWN)), Bk, tokenDecimals, DOWN) |
---|
212 | | - | (accum ++ [IntegerEntry((("global_" + getAssetString(tokenId)) + "_balance"), (Bk - amount))]) |
---|
213 | | - | } |
---|
214 | | - | |
---|
215 | | - | func handleTokenRedeem2 (accum,tokenId) = { |
---|
216 | | - | let Bk = tryGetInteger((("global_" + getAssetString(tokenId)) + "_balance")) |
---|
217 | | - | let PSupply = tryGetInteger("global_poolToken_amount") |
---|
218 | | - | let tokenDecimals = tryGetInteger((("static_" + getAssetString(tokenId)) + "_scale")) |
---|
219 | | - | let amount = fraction((tokenDecimals - fraction((PSupply - PRedeemed), tokenDecimals, PSupply, DOWN)), Bk, tokenDecimals, DOWN) |
---|
220 | | - | (accum ++ [ScriptTransfer(userAddress, amount, tokenId)]) |
---|
221 | | - | } |
---|
222 | | - | |
---|
223 | | - | ({ |
---|
224 | | - | let $l = assetIds |
---|
225 | | - | let $s = size($l) |
---|
226 | | - | let $acc0 = nil |
---|
227 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
228 | | - | then $a |
---|
229 | | - | else handleTokenRedeem($a, $l[$i]) |
---|
230 | | - | |
---|
231 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
232 | | - | then $a |
---|
233 | | - | else throw("List size exceeds 10") |
---|
234 | | - | |
---|
235 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
236 | | - | } ++ { |
---|
237 | | - | let $l = assetIds |
---|
238 | | - | let $s = size($l) |
---|
239 | | - | let $acc0 = nil |
---|
240 | | - | func $f1_1 ($a,$i) = if (($i >= $s)) |
---|
241 | | - | then $a |
---|
242 | | - | else handleTokenRedeem2($a, $l[$i]) |
---|
243 | | - | |
---|
244 | | - | func $f1_2 ($a,$i) = if (($i >= $s)) |
---|
245 | | - | then $a |
---|
246 | | - | else throw("List size exceeds 10") |
---|
247 | | - | |
---|
248 | | - | $f1_2($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($f1_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
249 | | - | }) |
---|
250 | | - | } |
---|
251 | | - | |
---|
252 | | - | |
---|
253 | | - | func calculateOutAmount (AmountIn,assetIn,assetOut,BalanceIn,BalanceOut) = { |
---|
254 | | - | let IndexIn = value(indexOf(assetIds, assetIn)) |
---|
255 | | - | let IndexOut = value(indexOf(assetIds, assetOut)) |
---|
256 | | - | if ((IndexIn == IndexOut)) |
---|
257 | | - | then throw("wrong tokens pair") |
---|
258 | | - | else fraction(BalanceOut, ((Scale8 * Scale8) - toInt(pow(fraction(toBigInt(BalanceIn), toBigInt((Scale8 * Scale8)), toBigInt((BalanceIn + AmountIn)), HALFUP), 16, toBigInt(fraction(AssetsWeights[IndexIn], 10000, AssetsWeights[IndexOut])), 4, 16, CEILING))), (Scale8 * Scale8), DOWN) |
---|
259 | | - | } |
---|
260 | | - | |
---|
261 | | - | |
---|
262 | | - | func getTokenBalance (assetId) = match assetId { |
---|
263 | | - | case t: ByteVector => |
---|
264 | | - | assetBalance(this, t) |
---|
265 | | - | case _ => |
---|
266 | | - | wavesBalance(this).available |
---|
267 | | - | } |
---|
268 | | - | |
---|
269 | | - | |
---|
270 | | - | func calculateCurrentAssetInterest (assetId,assetIdStr,aBalance,tokenEarningsLastCheck) = { |
---|
271 | | - | let totalStaked = tryGetInteger("global_indexStaked") |
---|
272 | | - | let tokenBalanceLastCheck = tokenEarningsLastCheck |
---|
273 | | - | let currentBalanceDelta = (getTokenBalance(assetId) - aBalance) |
---|
274 | | - | let currentTokenEarnings = if ((currentBalanceDelta > tokenBalanceLastCheck)) |
---|
275 | | - | then currentBalanceDelta |
---|
276 | | - | else tokenBalanceLastCheck |
---|
277 | | - | let newEarnings = (currentTokenEarnings - tokenBalanceLastCheck) |
---|
278 | | - | let newInterest = if ((totalStaked == 0)) |
---|
279 | | - | then 0 |
---|
280 | | - | else fraction(newEarnings, Scale8, totalStaked) |
---|
281 | | - | let lastCheckInterest = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_interest")) |
---|
282 | | - | (lastCheckInterest + newInterest) |
---|
283 | | - | } |
---|
284 | | - | |
---|
285 | | - | |
---|
286 | | - | func claimResult (address) = { |
---|
287 | | - | let addressStr = toString(address) |
---|
288 | | - | let puzzleAmount = tryGetInteger((addressStr + "_indexStaked")) |
---|
289 | | - | func handler (accum,assetId) = { |
---|
290 | | - | let assetIdStr = getAssetString(assetId) |
---|
291 | | - | let aBalance = tryGetInteger((("global_" + getAssetString(assetId)) + "_balance")) |
---|
292 | | - | let tokenEarningsLastCheck = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_earnings")) |
---|
293 | | - | let currentTokenInterest = calculateCurrentAssetInterest(assetId, assetIdStr, aBalance, tokenEarningsLastCheck) |
---|
294 | | - | let currentTokenEarnings = max([tokenEarningsLastCheck, (getTokenBalance(assetId) - aBalance)]) |
---|
295 | | - | let rewardAmount = fraction(puzzleAmount, (currentTokenInterest - tryGetInteger((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"))), Scale8) |
---|
296 | | - | let transfer = if ((rewardAmount == 0)) |
---|
297 | | - | then nil |
---|
298 | | - | else [ScriptTransfer(address, rewardAmount, assetId)] |
---|
299 | | - | $Tuple2(((accum._1 ++ transfer) ++ [IntegerEntry((("global_lastCheck_" + assetIdStr) + "_earnings"), (currentTokenEarnings - rewardAmount)), IntegerEntry((("global_lastCheck_" + assetIdStr) + "_interest"), currentTokenInterest), IntegerEntry((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"), currentTokenInterest)]), (accum._2 + calculateUsdnValue(assetId, rewardAmount, aBalance))) |
---|
300 | | - | } |
---|
301 | | - | |
---|
302 | | - | let accum = { |
---|
303 | | - | let $l = earnedAssets |
---|
304 | | - | let $s = size($l) |
---|
305 | | - | let $acc0 = $Tuple2(nil, 0) |
---|
306 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
307 | | - | then $a |
---|
308 | | - | else handler($a, $l[$i]) |
---|
309 | | - | |
---|
310 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
311 | | - | then $a |
---|
312 | | - | else throw("List size exceeds 10") |
---|
313 | | - | |
---|
314 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
315 | | - | } |
---|
316 | | - | (accum._1 ++ [IntegerEntry((addressStr + "_claimedRewardValue"), (tryGetInteger((addressStr + "_claimedRewardValue")) + accum._2)), IntegerEntry((addressStr + "_lastClaim"), lastBlock.timestamp)]) |
---|
317 | | - | } |
---|
318 | | - | |
---|
319 | | - | |
---|
320 | | - | func indexStakeResult (addressStr,amount) = { |
---|
321 | | - | let li = claimResult(addressFromStringValue(addressStr)) |
---|
322 | | - | (li ++ [IntegerEntry((addressStr + "_indexStaked"), (tryGetInteger((addressStr + "_indexStaked")) + amount)), IntegerEntry("global_indexStaked", (tryGetInteger("global_indexStaked") + amount))]) |
---|
323 | | - | } |
---|
332 | | - | |
---|
333 | | - | @Callable(i) |
---|
334 | | - | func preInit () = { |
---|
335 | | - | func handler (accum,assetNum) = if ((assetNum >= T)) |
---|
336 | | - | then accum |
---|
337 | | - | else (accum ++ [IntegerEntry((("static_" + getAssetString(assetIds[assetNum])) + "_scale"), Scales[assetNum]), IntegerEntry((("static_" + getAssetString(assetIds[assetNum])) + "_weight"), AssetsWeights[assetNum])]) |
---|
338 | | - | |
---|
339 | | - | let $l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
---|
340 | | - | let $s = size($l) |
---|
341 | | - | let $acc0 = nil |
---|
342 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
343 | | - | then $a |
---|
344 | | - | else handler($a, $l[$i]) |
---|
345 | | - | |
---|
346 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
347 | | - | then $a |
---|
348 | | - | else throw("List size exceeds 10") |
---|
349 | | - | |
---|
350 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
351 | | - | } |
---|
352 | | - | |
---|
353 | | - | |
---|
354 | | - | |
---|
355 | | - | @Callable(i) |
---|
356 | | - | func deInit () = if ((i.caller != this)) |
---|
357 | | - | then throw("admin only") |
---|
358 | | - | else [IntegerEntry("global_wasInited", 0)] |
---|
359 | | - | |
---|
360 | | - | |
---|
361 | | - | |
---|
362 | | - | @Callable(i) |
---|
363 | | - | func init () = { |
---|
364 | | - | func prepareList () = { |
---|
365 | | - | func handler (accum,n) = (accum ++ [IntegerEntry((("global_" + toBase58String(value(n.assetId))) + "_balance"), n.amount)]) |
---|
366 | | - | |
---|
367 | | - | let $l = i.payments |
---|
368 | | - | let $s = size($l) |
---|
369 | | - | let $acc0 = nil |
---|
370 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
371 | | - | then $a |
---|
372 | | - | else handler($a, $l[$i]) |
---|
373 | | - | |
---|
374 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
375 | | - | then $a |
---|
376 | | - | else throw("List size exceeds 10") |
---|
377 | | - | |
---|
378 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
379 | | - | } |
---|
380 | | - | |
---|
381 | | - | func calculatePoolTokensAmount (payments) = { |
---|
382 | | - | func handler (accum,pmt) = { |
---|
383 | | - | let assetId = value(pmt.assetId) |
---|
384 | | - | func handler2 (accum,n) = if ((n == assetId)) |
---|
385 | | - | then value(indexOf(assetIds, n)) |
---|
386 | | - | else accum |
---|
387 | | - | |
---|
388 | | - | let Token = { |
---|
389 | | - | let $l = assetIds |
---|
390 | | - | let $s = size($l) |
---|
391 | | - | let $acc0 = 1 |
---|
392 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
393 | | - | then $a |
---|
394 | | - | else handler2($a, $l[$i]) |
---|
395 | | - | |
---|
396 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
397 | | - | then $a |
---|
398 | | - | else throw("List size exceeds 10") |
---|
399 | | - | |
---|
400 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
401 | | - | } |
---|
402 | | - | (accum * pow(pmt.amount, Decimals[Token], AssetsWeights[Token], AssetsWeightsDecimals, 1, FLOOR)) |
---|
403 | | - | } |
---|
404 | | - | |
---|
405 | | - | let $l = payments |
---|
406 | | - | let $s = size($l) |
---|
407 | | - | let $acc0 = 1 |
---|
408 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
409 | | - | then $a |
---|
410 | | - | else handler($a, $l[$i]) |
---|
411 | | - | |
---|
412 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
413 | | - | then $a |
---|
414 | | - | else throw("List size exceeds 10") |
---|
415 | | - | |
---|
416 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
417 | | - | } |
---|
418 | | - | |
---|
419 | | - | if ((tryGetInteger("global_wasInited") > 0)) |
---|
420 | | - | then throw("pool already inited") |
---|
421 | | - | else { |
---|
422 | | - | let initialPoolTokens = calculatePoolTokensAmount(i.payments) |
---|
423 | | - | let poolTokenIssue = Issue("PZ Puzzle Index", "Puzzle parent pool index token", initialPoolTokens, PoolTokenDecimals, true, unit, 0) |
---|
424 | | - | let poolTokenId = calculateAssetId(poolTokenIssue) |
---|
425 | | - | (prepareList() ++ [poolTokenIssue, IntegerEntry("global_poolToken_amount", initialPoolTokens), IntegerEntry("global_wasInited", 1), BinaryEntry("global_poolToken_id", poolTokenId), ScriptTransfer(i.caller, initialPoolTokens, poolTokenId)]) |
---|
426 | | - | } |
---|
427 | | - | } |
---|
428 | | - | |
---|
429 | | - | |
---|
430 | | - | |
---|
431 | | - | @Callable(i) |
---|
432 | | - | func generateIndex (needChange) = throw("LP is closed yet") |
---|
433 | | - | |
---|
434 | | - | |
---|
435 | | - | |
---|
436 | | - | @Callable(i) |
---|
437 | | - | func redeemIndex (sendToOrigin) = { |
---|
438 | | - | let pmt = i.payments[0] |
---|
439 | | - | if ((pmt.assetId != tryGetBinary("global_poolToken_id"))) |
---|
440 | | - | then throw("please attach pool share token") |
---|
441 | | - | else { |
---|
442 | | - | let PRedeemed = pmt.amount |
---|
443 | | - | let result = handlePoolTokensRedeem(PRedeemed, if (sendToOrigin) |
---|
444 | | - | then i.originCaller |
---|
445 | | - | else i.caller) |
---|
446 | | - | (result ++ [Burn(tryGetBinary("global_poolToken_id"), PRedeemed), IntegerEntry("global_poolToken_amount", (tryGetInteger("global_poolToken_amount") - PRedeemed))]) |
---|
447 | | - | } |
---|
448 | | - | } |
---|
449 | | - | |
---|
450 | | - | |
---|
451 | | - | |
---|
452 | | - | @Callable(i) |
---|
453 | | - | func getPuzzlePrice () = { |
---|
454 | | - | let AssetInBalance = tryGetInteger("global_HEB8Qaw9xrWpWs8tHsiATYGBWDBtP2S7kcPALrMu43AS_balance") |
---|
455 | | - | let AssetOutBalance = tryGetInteger("global_DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p_balance") |
---|
456 | | - | $Tuple2(nil, fraction(Scale8, (AssetOutBalance / 10), (AssetInBalance / 80))) |
---|
457 | | - | } |
---|
458 | | - | |
---|
459 | | - | |
---|
460 | | - | |
---|
461 | | - | @Callable(i) |
---|
462 | | - | func swap (assetOut,minimum) = if ((tryGetInteger("static_wasLocked") > 0)) |
---|
463 | | - | then throw("locked for swaps") |
---|
464 | | - | else { |
---|
465 | | - | let pmt = value(i.payments[0]) |
---|
466 | | - | let AmountIn = value(i.payments[0].amount) |
---|
467 | | - | let AssetIn = pmt.assetId |
---|
468 | | - | let AssetOut = getAssetBytes(assetOut) |
---|
469 | | - | let AssetInBalance = tryGetInteger((("global_" + getAssetString(AssetIn)) + "_balance")) |
---|
470 | | - | let AssetOutBalance = tryGetInteger((("global_" + assetOut) + "_balance")) |
---|
471 | | - | let AmountOut = calculateOutAmount(AmountIn, AssetIn, AssetOut, AssetInBalance, AssetOutBalance) |
---|
472 | | - | let feeAmount = fraction(AmountOut, Fee, FeeScale) |
---|
473 | | - | let protocolFeeAmount = feeAmount |
---|
474 | | - | let cleanAmountOut = (AmountOut - feeAmount) |
---|
475 | | - | let puzzlePriceChange = if (if ((AssetIn == base58'HEB8Qaw9xrWpWs8tHsiATYGBWDBtP2S7kcPALrMu43AS')) |
---|
476 | | - | then (AssetOut == base58'DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p') |
---|
477 | | - | else false) |
---|
478 | | - | then IntegerEntry("global_lastPuzzlePrice", fraction(Scale8, AmountOut, AmountIn)) |
---|
479 | | - | else StringEntry("hello", "world") |
---|
480 | | - | if ((minimum > cleanAmountOut)) |
---|
481 | | - | then throw(("amount to recieve is lower than given one: " + toString(cleanAmountOut))) |
---|
482 | | - | else if ((0 > (AssetOutBalance - AmountOut))) |
---|
483 | | - | then throw("contract is out of reserves") |
---|
484 | | - | else $Tuple2([puzzlePriceChange, IntegerEntry((("global_" + assetOut) + "_balance"), (AssetOutBalance - AmountOut)), ScriptTransfer(feeAggregator, protocolFeeAmount, AssetOut), IntegerEntry((("global_" + getAssetString(AssetIn)) + "_balance"), (AssetInBalance + AmountIn)), ScriptTransfer(i.caller, cleanAmountOut, AssetOut)], cleanAmountOut) |
---|
485 | | - | } |
---|
486 | | - | |
---|
487 | | - | |
---|
488 | | - | |
---|
489 | | - | @Callable(i) |
---|
490 | | - | func swapWithReferral (assetOut,minimum,refKey) = if ((tryGetInteger("static_wasLocked") > 0)) |
---|
491 | | - | then throw("locked for swaps") |
---|
492 | | - | else { |
---|
493 | | - | let pmt = value(i.payments[0]) |
---|
494 | | - | let AmountIn = value(i.payments[0].amount) |
---|
495 | | - | let AssetIn = pmt.assetId |
---|
496 | | - | let AssetOut = getAssetBytes(assetOut) |
---|
497 | | - | let referralAddress = Address(base58'3PQGxTse5eWufds6GA41UnbUNqLcCEnVJL3') |
---|
498 | | - | let AssetInBalance = tryGetInteger((("global_" + getAssetString(AssetIn)) + "_balance")) |
---|
499 | | - | let AssetOutBalance = tryGetInteger((("global_" + assetOut) + "_balance")) |
---|
500 | | - | let AmountOut = calculateOutAmount(AmountIn, AssetIn, AssetOut, AssetInBalance, AssetOutBalance) |
---|
501 | | - | let feeAmount = fraction(AmountOut, Fee, FeeScale) |
---|
502 | | - | let protocolFeeAmount = feeAmount |
---|
503 | | - | let cleanAmountOut = (AmountOut - feeAmount) |
---|
504 | | - | if ((minimum > cleanAmountOut)) |
---|
505 | | - | then throw(("amount to recieve is lower than given one: " + toString(cleanAmountOut))) |
---|
506 | | - | else if ((0 > (AssetOutBalance - AmountOut))) |
---|
507 | | - | then throw("contract is out of reserves") |
---|
508 | | - | else $Tuple2([IntegerEntry((("global_" + assetOut) + "_balance"), (AssetOutBalance - AmountOut)), ScriptTransfer(feeAggregator, fraction(protocolFeeAmount, 9, 10), AssetOut), ScriptTransfer(referralAddress, fraction(protocolFeeAmount, 1, 10), AssetOut), IntegerEntry((("global_" + getAssetString(AssetIn)) + "_balance"), (AssetInBalance + AmountIn)), ScriptTransfer(i.caller, cleanAmountOut, AssetOut)], cleanAmountOut) |
---|
509 | | - | } |
---|
510 | | - | |
---|
| 10 | + | @Verifier(tx) |
---|
| 11 | + | func verify () = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|