tx · CHkoz4aMTqfr3rnW2MEkCtMVD4ETyGG1rrSKgApwxNkc

3NCSkSKFXn5HxJhKFRVEjaZexubfTjdsvLk:  -0.01000000 Waves

2023.09.26 10:42 [2772095] smart account 3NCSkSKFXn5HxJhKFRVEjaZexubfTjdsvLk > SELF 0.00000000 Waves

{ "type": 13, "id": "CHkoz4aMTqfr3rnW2MEkCtMVD4ETyGG1rrSKgApwxNkc", "fee": 1000000, "feeAssetId": null, "timestamp": 1695714228622, "version": 2, "chainId": 84, "sender": "3NCSkSKFXn5HxJhKFRVEjaZexubfTjdsvLk", "senderPublicKey": "EiQRGyXMSPywdZJcLQzgNDBrrvLUyhi8X8EeNn1KTjKu", "proofs": [ "4dSUdKHMuj3puNZ3kSxBnAozRsJH2TDqubHJ7wQ2BM9KKMeqzxJhrA8jexa5QyF3UyEKD9af9u8NXwikNk6AyxhL" ], "script": null, "height": 2772095, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: BJAKoPXKEdFMoca254FBVuz9b5X3cF3cPc7HWVqd1tvr Next: none Full:
OldNewDifferences
1-{-# STDLIB_VERSION 6 #-}
2-{-# SCRIPT_TYPE ACCOUNT #-}
3-{-# CONTENT_TYPE DAPP #-}
4-let usdnAssetId = base58'DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p'
5-
6-let puzzleAssetId = base58'HEB8Qaw9xrWpWs8tHsiATYGBWDBtP2S7kcPALrMu43AS'
7-
8-let usdtAssetId = base58'34N9YcEETLWn93qYQ64EsP1x89tSruJU44RrEMSXXEPJ'
9-
10-let usdtPptAssetId = base58'9wc3LXNA4TEBsXyKtoLE9mrbDD7WMHXvXrCjZvabLAsi'
11-
12-let usdnAssetIdStr = "DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p"
13-
14-let puzzleAssetIdStr = "HEB8Qaw9xrWpWs8tHsiATYGBWDBtP2S7kcPALrMu43AS"
15-
16-let usdtAssetIdStr = "34N9YcEETLWn93qYQ64EsP1x89tSruJU44RrEMSXXEPJ"
17-
18-let usdtPptAssetIdStr = "9wc3LXNA4TEBsXyKtoLE9mrbDD7WMHXvXrCjZvabLAsi"
19-
20-let parentPoolAddress = Address(base58'3PFDgzu1UtswAkCMxqqQjbTeHaX4cMab8Kh')
21-
22-let masterAddress = Address(base58'3PLjwHcz9NEuaTo63NZR9B9okQiKQxZSbmf')
23-
24-let masterPubKey = base58'4z8CKSYQBKkzx7PBb5uBP1YPa6YAHRNTApW1sQVHT5eU'
25-
26-func tryGetInteger (key) = match getInteger(this, key) {
27- case b: Int =>
28- b
29- case _ =>
30- 0
31-}
32-
33-
34-func tryGetBinary (key) = match getBinary(this, key) {
35- case b: ByteVector =>
36- b
37- case _ =>
38- base58''
39-}
40-
41-
42-func tryGetString (key) = match getString(this, key) {
43- case b: String =>
44- b
45- case _ =>
46- ""
47-}
48-
49-
50-func getAssetString (assetId) = match assetId {
51- case b: ByteVector =>
52- toBase58String(b)
53- case _ =>
54- "WAVES"
55-}
56-
57-
58-func getAssetBytes (assetIdStr) = if ((assetIdStr == "WAVES"))
59- then unit
60- else fromBase58String(assetIdStr)
61-
62-
63-func addAssetBytesToList (accum,item) = (accum ++ [getAssetBytes(item)])
64-
65-
66-func addAssetWeightToList (accum,item) = (accum ++ [tryGetInteger((("static_" + getAssetString(item)) + "_weight"))])
67-
68-
69-func addAssetDecimalsToList (accum,item) = (accum ++ [tryGetInteger((("static_" + getAssetString(item)) + "_decimals"))])
70-
71-
72-func addAssetScaleToList (accum,item) = (accum ++ [tryGetInteger((("static_" + getAssetString(item)) + "_scale"))])
73-
74-
75-func addIntToList (accum,item) = (accum ++ [parseIntValue(item)])
76-
77-
78-let T = tryGetInteger("static_tokensAmount")
79-
80-let assetIds = {
81- let $l = split(tryGetString("static_tokenIds"), ",")
82- let $s = size($l)
83- let $acc0 = nil
84- func $f0_1 ($a,$i) = if (($i >= $s))
85- then $a
86- else addAssetBytesToList($a, $l[$i])
87-
88- func $f0_2 ($a,$i) = if (($i >= $s))
89- then $a
90- else throw("List size exceeds 10")
91-
92- $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)
93- }
94-
95-let AssetsWeights = {
96- let $l = assetIds
97- let $s = size($l)
98- let $acc0 = nil
99- func $f1_1 ($a,$i) = if (($i >= $s))
100- then $a
101- else addAssetWeightToList($a, $l[$i])
102-
103- func $f1_2 ($a,$i) = if (($i >= $s))
104- then $a
105- else throw("List size exceeds 10")
106-
107- $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)
108- }
109-
110-let Decimals = {
111- let $l = assetIds
112- let $s = size($l)
113- let $acc0 = nil
114- func $f2_1 ($a,$i) = if (($i >= $s))
115- then $a
116- else addAssetDecimalsToList($a, $l[$i])
117-
118- func $f2_2 ($a,$i) = if (($i >= $s))
119- then $a
120- else throw("List size exceeds 10")
121-
122- $f2_2($f2_1($f2_1($f2_1($f2_1($f2_1($f2_1($f2_1($f2_1($f2_1($f2_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
123- }
124-
125-let Scales = {
126- let $l = assetIds
127- let $s = size($l)
128- let $acc0 = nil
129- func $f3_1 ($a,$i) = if (($i >= $s))
130- then $a
131- else addAssetScaleToList($a, $l[$i])
132-
133- func $f3_2 ($a,$i) = if (($i >= $s))
134- then $a
135- else throw("List size exceeds 10")
136-
137- $f3_2($f3_1($f3_1($f3_1($f3_1($f3_1($f3_1($f3_1($f3_1($f3_1($f3_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
138- }
139-
140-let Fee = tryGetInteger("static_fee")
141-
142-let AssetsWeightsDecimals = 4
143-
144-let Scale = 10000
145-
146-let Scale8 = 100000000
147-
148-let FeeScale = 10000
149-
150-let PoolTokenDecimals = 8
151-
152-let PoolTokenScale = pow(10, 0, PoolTokenDecimals, 0, 0, HALFUP)
153-
154-let earnedAssets = assetIds
155-
156-func isShutdown () = match getBoolean(Address(base58'3PEpv9hRFWEEBU22WRnLsw1bH4YGtcU728o'), "is_shutdown") {
157- case x: Boolean =>
158- x
159- case _ =>
160- false
161-}
162-
163-
164-func getCurrentTokenBalance (tokenType) = {
165- let tokenId = getAssetString(assetIds[tokenType])
166- tryGetInteger((("global_" + tokenId) + "_balance"))
167- }
168-
169-
170-func calculatePIssued (amount,tokenId) = {
171- let Psupply = tryGetInteger("global_poolToken_amount")
172- let Balance = tryGetInteger((("global_" + getAssetString(tokenId)) + "_balance"))
173- fraction(amount, Psupply, Balance, DOWN)
174- }
175-
176-
177-func getMinPIssued (payments) = {
178- func handler (accum,current) = {
179- let PIssued = calculatePIssued(current.amount, current.assetId)
180- if ((PIssued == 0))
181- then throw("one of the tokens amounts is too low")
182- else if (if ((accum == 0))
183- then true
184- else (accum > PIssued))
185- then PIssued
186- else accum
187- }
188-
189- let minPIssed = {
190- let $l = payments
191- let $s = size($l)
192- let $acc0 = 0
193- func $f4_1 ($a,$i) = if (($i >= $s))
194- then $a
195- else handler($a, $l[$i])
196-
197- func $f4_2 ($a,$i) = if (($i >= $s))
198- then $a
199- else throw("List size exceeds 10")
200-
201- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
202- }
203- minPIssed
204- }
205-
206-
207-func calculateUsdnValue (assetId,amount,aBalance,givenUsdnBalance) = {
208- let usdnInPool = indexOf(assetIds, usdnAssetId)
209- let puzzleInPool = indexOf(assetIds, puzzleAssetId)
210- let usdtInPool = indexOf(assetIds, usdtAssetId)
211- let usdtPptInPool = indexOf(assetIds, usdtPptAssetId)
212- let wavesInPool = indexOf(assetIds, unit)
213- let assetWeight = tryGetInteger((("static_" + getAssetString(assetId)) + "_weight"))
214- let feeAssetStr = tryGetString("static_feeToken")
215- if ((feeAssetStr == puzzleAssetIdStr))
216- then {
217- let puzzleWeight = AssetsWeights[value(indexOf(assetIds, puzzleAssetId))]
218- let puzzleBalance = tryGetInteger((("global_" + puzzleAssetIdStr) + "_balance"))
219- let amountInPuzzle = fraction(amount, (puzzleBalance / puzzleWeight), (aBalance / assetWeight))
220- let puzzlePrice = getIntegerValue(parentPoolAddress, "global_lastPuzzlePrice")
221- fraction((amountInPuzzle * puzzlePrice), 1, Scale8)
222- }
223- else if ((feeAssetStr == usdtAssetIdStr))
224- then {
225- let usdtWeight = AssetsWeights[value(usdtInPool)]
226- let usdtBalance = tryGetInteger((("global_" + usdtAssetIdStr) + "_balance"))
227- fraction(amount, (usdtBalance / usdtWeight), (aBalance / assetWeight))
228- }
229- else if ((feeAssetStr == usdtPptAssetIdStr))
230- then {
231- let usdtWeight = AssetsWeights[value(usdtPptInPool)]
232- let usdtBalance = tryGetInteger((("global_" + usdtPptAssetIdStr) + "_balance"))
233- fraction(amount, (usdtBalance / usdtWeight), (aBalance / assetWeight))
234- }
235- else if ((feeAssetStr == usdnAssetIdStr))
236- then {
237- let usdnWeight = AssetsWeights[value(indexOf(assetIds, usdnAssetId))]
238- let usdnBalance = match givenUsdnBalance {
239- case x: Int =>
240- givenUsdnBalance
241- case _ =>
242- tryGetInteger((("global_" + getAssetString(usdnAssetId)) + "_balance"))
243- }
244- fraction(amount, (value(usdnBalance) / usdnWeight), (aBalance / assetWeight))
245- }
246- else {
247- let wavesWeight = 3000
248- let wBalance = (tryGetInteger("global_WAVES_balance") / 50)
249- fraction(amount, (wBalance / wavesWeight), (aBalance / assetWeight))
250- }
251- }
252-
253-
254-func checkTokensValidity (payments) = {
255- func handler1 (accum,payment) = (accum ++ [payment.assetId])
256-
257- let ids = {
258- let $l = payments
259- let $s = size($l)
260- let $acc0 = nil
261- func $f4_1 ($a,$i) = if (($i >= $s))
262- then $a
263- else handler1($a, $l[$i])
264-
265- func $f4_2 ($a,$i) = if (($i >= $s))
266- then $a
267- else throw("List size exceeds 10")
268-
269- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
270- }
271- if ((ids == ids))
272- then {
273- func handler2 (accum,assetId) = if ((indexOf(ids, assetId) != unit))
274- then (accum + 1)
275- else throw(("asset not attached: " + getAssetString(assetId)))
276-
277- let checks = {
278- let $l = assetIds
279- let $s = size($l)
280- let $acc0 = 0
281- func $f5_1 ($a,$i) = if (($i >= $s))
282- then $a
283- else handler2($a, $l[$i])
284-
285- func $f5_2 ($a,$i) = if (($i >= $s))
286- then $a
287- else throw("List size exceeds 10")
288-
289- $f5_2($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
290- }
291- if ((checks == checks))
292- then true
293- else throw("Strict value is not equal to itself.")
294- }
295- else throw("Strict value is not equal to itself.")
296- }
297-
298-
299-func handlePoolTokensAdd (PIssued,payments,userAddress,needChange) = {
300- func getTokenPaymentAmount (tokenId) = {
301- func handler (accum,payment) = if ((payment.assetId == tokenId))
302- then payment.amount
303- else accum
304-
305- let $l = payments
306- let $s = size($l)
307- let $acc0 = 0
308- func $f4_1 ($a,$i) = if (($i >= $s))
309- then $a
310- else handler($a, $l[$i])
311-
312- func $f4_2 ($a,$i) = if (($i >= $s))
313- then $a
314- else throw("List size exceeds 10")
315-
316- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
317- }
318-
319- func handleTokenChange (accum,tokenId) = {
320- let Bk = tryGetInteger((("global_" + getAssetString(tokenId)) + "_balance"))
321- let PSupply = tryGetInteger("global_poolToken_amount")
322- let tokenDecimals = tryGetInteger((("static_" + getAssetString(tokenId)) + "_scale"))
323- let Dk = fraction((fraction((PSupply + PIssued), tokenDecimals, PSupply, CEILING) - tokenDecimals), Bk, tokenDecimals, CEILING)
324- let paymentAmount = getTokenPaymentAmount(tokenId)
325- let toReturn = ((if ((paymentAmount != 0))
326- then paymentAmount
327- else 0) - Dk)
328- let t = if (if (needChange)
329- then (toReturn > 0)
330- else false)
331- then [ScriptTransfer(userAddress, toReturn, tokenId)]
332- else nil
333- ((accum ++ t) ++ [IntegerEntry((("global_" + getAssetString(tokenId)) + "_balance"), (Bk + Dk))])
334- }
335-
336- let $l = assetIds
337- let $s = size($l)
338- let $acc0 = nil
339- func $f4_1 ($a,$i) = if (($i >= $s))
340- then $a
341- else handleTokenChange($a, $l[$i])
342-
343- func $f4_2 ($a,$i) = if (($i >= $s))
344- then $a
345- else throw("List size exceeds 10")
346-
347- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
348- }
349-
350-
351-func handlePoolTokensRedeem (PRedeemed,userAddress) = {
352- func handleTokenRedeem (accum,tokenId) = {
353- let Bk = tryGetInteger((("global_" + getAssetString(tokenId)) + "_balance"))
354- let PSupply = tryGetInteger("global_poolToken_amount")
355- let tokenDecimals = tryGetInteger((("static_" + getAssetString(tokenId)) + "_scale"))
356- let amount = toInt(fraction((toBigInt(Scale8) - fraction(toBigInt((PSupply - PRedeemed)), toBigInt(Scale8), toBigInt(PSupply), CEILING)), toBigInt(Bk), toBigInt(Scale8), DOWN))
357- (accum ++ [IntegerEntry((("global_" + getAssetString(tokenId)) + "_balance"), (Bk - amount)), ScriptTransfer(userAddress, amount, tokenId)])
358- }
359-
360- let $l = assetIds
361- let $s = size($l)
362- let $acc0 = nil
363- func $f4_1 ($a,$i) = if (($i >= $s))
364- then $a
365- else handleTokenRedeem($a, $l[$i])
366-
367- func $f4_2 ($a,$i) = if (($i >= $s))
368- then $a
369- else throw("List size exceeds 10")
370-
371- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
372- }
373-
374-
375-func calculateOutAmount (AmountIn,assetIn,assetOut,BalanceIn,BalanceOut) = {
376- let IndexIn = value(indexOf(assetIds, assetIn))
377- let IndexOut = value(indexOf(assetIds, assetOut))
378- if ((IndexIn == IndexOut))
379- then AmountIn
380- 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)
381- }
382-
383-
384-func getTokenBalance (assetId) = match assetId {
385- case t: ByteVector =>
386- assetBalance(this, t)
387- case _ =>
388- wavesBalance(this).available
389-}
390-
391-
392-func calculateCurrentAssetInterest (assetId,assetIdStr,aBalance,tokenEarningsLastCheck) = {
393- let totalStaked = tryGetInteger("global_indexStaked")
394- let tokenBalanceLastCheck = tokenEarningsLastCheck
395- let currentBalanceDelta = (getTokenBalance(assetId) - aBalance)
396- let currentTokenEarnings = if ((currentBalanceDelta > tokenBalanceLastCheck))
397- then currentBalanceDelta
398- else tokenBalanceLastCheck
399- let newEarnings = (currentTokenEarnings - tokenBalanceLastCheck)
400- let newInterest = if ((totalStaked == 0))
401- then 0
402- else fraction(newEarnings, Scale8, totalStaked)
403- let lastCheckInterest = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_interest"))
404- (lastCheckInterest + newInterest)
405- }
406-
407-
408-func claimResult (address) = {
409- let addressStr = toString(address)
410- let puzzleAmount = tryGetInteger((addressStr + "_indexStaked"))
411- func handler (accum,assetId) = {
412- let assetIdStr = getAssetString(assetId)
413- let aBalance = tryGetInteger((("global_" + getAssetString(assetId)) + "_balance"))
414- let tokenEarningsLastCheck = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_earnings"))
415- let currentTokenInterest = calculateCurrentAssetInterest(assetId, assetIdStr, aBalance, tokenEarningsLastCheck)
416- let currentTokenEarnings = max([tokenEarningsLastCheck, (getTokenBalance(assetId) - aBalance)])
417- let rewardAmount = fraction(puzzleAmount, (currentTokenInterest - tryGetInteger((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"))), Scale8)
418- let transfer = if ((rewardAmount == 0))
419- then nil
420- else [ScriptTransfer(address, rewardAmount, assetId)]
421- $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, unit)))
422- }
423-
424- let accum = {
425- let $l = earnedAssets
426- let $s = size($l)
427- let $acc0 = $Tuple2(nil, 0)
428- func $f4_1 ($a,$i) = if (($i >= $s))
429- then $a
430- else handler($a, $l[$i])
431-
432- func $f4_2 ($a,$i) = if (($i >= $s))
433- then $a
434- else throw("List size exceeds 10")
435-
436- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
437- }
438- $Tuple2((accum._1 ++ [IntegerEntry((addressStr + "_claimedRewardValue"), (tryGetInteger((addressStr + "_claimedRewardValue")) + accum._2)), IntegerEntry((addressStr + "_lastClaim"), lastBlock.timestamp)]), accum._2)
439- }
440-
441-
442-func indexStakeResult (addressStr,amount) = {
443- let li = claimResult(addressFromStringValue(addressStr))._1
444- (li ++ [IntegerEntry((addressStr + "_indexStaked"), (tryGetInteger((addressStr + "_indexStaked")) + amount)), IntegerEntry("global_indexStaked", (tryGetInteger("global_indexStaked") + amount))])
445- }
446-
447-
448-func sum (accum,n) = (accum + parseIntValue(n))
449-
450-
451-@Callable(i)
452-func preInit (assetIdsStr,assetWeightsStr,baseTokenIdStr,poolDomain,poolOwner,fee) = {
453- let poolOwnerAddress = Address(fromBase58String(poolOwner))
454- if (isShutdown())
455- then throw("contract is on stop")
456- else if ((this != i.caller))
457- then throw("admin only")
458- else if ((size(poolDomain) > 13))
459- then throw("too large pool domain")
460- else if (if ((fee > 500))
461- then true
462- else (50 > fee))
463- then throw("fee value must be between 50 and 500 (0.5-5%)")
464- else if (if (if (if (if ((indexOf(assetIdsStr, puzzleAssetIdStr) == unit))
465- then (indexOf(assetIdsStr, usdnAssetIdStr) == unit)
466- else false)
467- then (indexOf(assetIdsStr, usdtAssetIdStr) == unit)
468- else false)
469- then (indexOf(assetIdsStr, "WAVES") == unit)
470- else false)
471- then (indexOf(assetIdsStr, usdtPptAssetIdStr) == unit)
472- else false)
473- then throw("pool must have USDT-WXG, USDT-PPT, USDN, WAVES or PUZZLE in the composition")
474- else {
475- let assetIdsStrLi = split(assetIdsStr, ",")
476- let assetIdsLi = {
477- let $l = assetIdsStrLi
478- let $s = size($l)
479- let $acc0 = nil
480- func $f4_1 ($a,$i) = if (($i >= $s))
481- then $a
482- else addAssetBytesToList($a, $l[$i])
483-
484- func $f4_2 ($a,$i) = if (($i >= $s))
485- then $a
486- else throw("List size exceeds 10")
487-
488- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
489- }
490- let assetWeightsStrLi = split(assetWeightsStr, ",")
491- let assetWeightsSum = {
492- let $l = assetWeightsStrLi
493- let $s = size($l)
494- let $acc0 = 0
495- func $f5_1 ($a,$i) = if (($i >= $s))
496- then $a
497- else sum($a, $l[$i])
498-
499- func $f5_2 ($a,$i) = if (($i >= $s))
500- then $a
501- else throw("List size exceeds 10")
502-
503- $f5_2($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($f5_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
504- }
505- let feeAssetStr = if ((indexOf(assetIdsStr, usdnAssetIdStr) != unit))
506- then usdnAssetIdStr
507- else if ((indexOf(assetIdsStr, "WAVES") != unit))
508- then "WAVES"
509- else if ((indexOf(assetIdsStr, usdtAssetIdStr) != unit))
510- then usdtAssetIdStr
511- else if ((indexOf(assetIdsStr, puzzleAssetIdStr) != unit))
512- then puzzleAssetIdStr
513- else usdtPptAssetIdStr
514- func addTokenDataEntries (accum,assetNum) = if ((assetNum >= size(assetIdsLi)))
515- then accum
516- else {
517- let assetDecimals = match assetIdsLi[assetNum] {
518- case x: ByteVector =>
519- value(assetInfo(x)).decimals
520- case _ =>
521- 8
522- }
523- (accum ++ [IntegerEntry((("static_" + assetIdsStrLi[assetNum]) + "_scale"), pow(10, 0, assetDecimals, 0, 0, DOWN)), IntegerEntry((("static_" + assetIdsStrLi[assetNum]) + "_decimals"), assetDecimals), IntegerEntry((("static_" + assetIdsStrLi[assetNum]) + "_weight"), value(parseInt(assetWeightsStrLi[assetNum])))])
524- }
525-
526- if ((assetWeightsSum != 10000))
527- then throw("sum of token weights must be equal to 10000")
528- else ({
529- let $l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
530- let $s = size($l)
531- let $acc0 = nil
532- func $f6_1 ($a,$i) = if (($i >= $s))
533- then $a
534- else addTokenDataEntries($a, $l[$i])
535-
536- func $f6_2 ($a,$i) = if (($i >= $s))
537- then $a
538- else throw("List size exceeds 10")
539-
540- $f6_2($f6_1($f6_1($f6_1($f6_1($f6_1($f6_1($f6_1($f6_1($f6_1($f6_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
541- } ++ [StringEntry("static_tokenIds", assetIdsStr), StringEntry("static_feeToken", feeAssetStr), StringEntry("static_tokenWeights", assetWeightsStr), IntegerEntry("static_tokensAmount", size(assetIdsLi)), StringEntry("static_poolDomain", poolDomain), StringEntry("static_baseTokenId", baseTokenIdStr), StringEntry("static_poolOwner", poolOwner), IntegerEntry("static_fee", fee)])
542- }
543- }
544-
545-
546-
547-@Callable(i)
548-func deInit () = if (isShutdown())
549- then throw("contract is on stop")
550- else if ((i.caller != this))
551- then throw("admin only")
552- else [IntegerEntry("global_wasInited", 0)]
553-
554-
555-
556-@Callable(i)
557-func init () = {
558- func prepareList () = {
559- func handler (accum,n) = (accum ++ [IntegerEntry((("global_" + getAssetString(n.assetId)) + "_balance"), n.amount)])
560-
561- let $l = i.payments
562- let $s = size($l)
563- let $acc0 = nil
564- func $f4_1 ($a,$i) = if (($i >= $s))
565- then $a
566- else handler($a, $l[$i])
567-
568- func $f4_2 ($a,$i) = if (($i >= $s))
569- then $a
570- else throw("List size exceeds 10")
571-
572- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
573- }
574-
575- func calculatePoolTokensAmount (payments) = {
576- func handler (accum,pmt) = {
577- let assetId = pmt.assetId
578- func handler2 (accum,n) = if ((n == assetId))
579- then value(indexOf(assetIds, n))
580- else accum
581-
582- let Token = {
583- let $l = assetIds
584- let $s = size($l)
585- let $acc0 = 1
586- func $f4_1 ($a,$i) = if (($i >= $s))
587- then $a
588- else handler2($a, $l[$i])
589-
590- func $f4_2 ($a,$i) = if (($i >= $s))
591- then $a
592- else throw("List size exceeds 10")
593-
594- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
595- }
596- fraction(accum, pow(pmt.amount, Decimals[Token], AssetsWeights[Token], AssetsWeightsDecimals, 8, FLOOR), Scale8)
597- }
598-
599- let $l = payments
600- let $s = size($l)
601- let $acc0 = PoolTokenScale
602- func $f4_1 ($a,$i) = if (($i >= $s))
603- then $a
604- else handler($a, $l[$i])
605-
606- func $f4_2 ($a,$i) = if (($i >= $s))
607- then $a
608- else throw("List size exceeds 10")
609-
610- $f4_2($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($f4_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10)
611- }
612-
613- if (isShutdown())
614- then throw("contract is on stop")
615- else if ((tryGetInteger("global_wasInited") > 0))
616- then throw("pool already inited")
617- else {
618- let initialPoolTokens = calculatePoolTokensAmount(i.payments)
619- if ((initialPoolTokens == 0))
620- then throw("you need a bigger tokens amount to launch the pool")
621- else {
622- let poolTokenIssue = Issue(("PZ " + tryGetString("static_poolDomain")), "Puzzle Swap: pool index token", initialPoolTokens, PoolTokenDecimals, true, unit, 0)
623- let poolTokenId = calculateAssetId(poolTokenIssue)
624- (prepareList() ++ [poolTokenIssue, IntegerEntry("global_poolToken_amount", initialPoolTokens), IntegerEntry("global_wasInited", 1), BinaryEntry("global_poolToken_id", poolTokenId), StringEntry("static_poolToken_idStr", getAssetString(poolTokenId)), IntegerEntry((toString(i.caller) + "_indexStaked"), initialPoolTokens), IntegerEntry("global_indexStaked", initialPoolTokens)])
625- }
626- }
627- }
628-
629-
630-
631-@Callable(i)
632-func generateIndex (needChange) = if ((size(i.payments) != T))
633- then throw(("you need to attach all pool tokens. amount of pool tokens: " + toString(T)))
634- else if (!(checkTokensValidity(i.payments)))
635- then throw("wrong assets attached")
636- else {
637- let PIssued = getMinPIssued(i.payments)
638- let reissue = Reissue(getBinaryValue("global_poolToken_id"), PIssued, true)
639- let result = handlePoolTokensAdd(PIssued, i.payments, i.originCaller, needChange)
640- $Tuple2((result ++ [reissue, ScriptTransfer(i.caller, PIssued, tryGetBinary("global_poolToken_id")), IntegerEntry("global_poolToken_amount", (tryGetInteger("global_poolToken_amount") + PIssued))]), PIssued)
641- }
642-
643-
644-
645-@Callable(i)
646-func stakeIndex () = {
647- let addressStr = toString(i.originCaller)
648- let pmt = i.payments[0]
649- if ((value(pmt.assetId) != tryGetBinary("global_poolToken_id")))
650- then throw("wrong asset attached")
651- else indexStakeResult(addressStr, pmt.amount)
652- }
653-
654-
655-
656-@Callable(i)
657-func unstakeIndex (puzzleAmount) = {
658- let addressStr = toString(i.originCaller)
659- let puzzleAvailable = tryGetInteger((addressStr + "_indexStaked"))
660- if (isShutdown())
661- then throw("contract is on stop")
662- else if ((puzzleAmount > puzzleAvailable))
663- then throw("you don't have index tokens available")
664- else if (isShutdown())
665- then throw("contract is on stop")
666- else (claimResult(i.originCaller)._1 ++ [IntegerEntry((addressStr + "_indexStaked"), (puzzleAvailable - puzzleAmount)), IntegerEntry("global_indexStaked", (tryGetInteger("global_indexStaked") - puzzleAmount)), ScriptTransfer(i.caller, puzzleAmount, getBinaryValue("global_poolToken_id"))])
667- }
668-
669-
670-
671-@Callable(i)
672-func claimIndexRewards () = if (isShutdown())
673- then throw("contract is on stop")
674- else claimResult(i.caller)
675-
676-
677-
678-@Callable(i)
679-func evaluateClaim (user) = $Tuple2(nil, claimResult(addressFromStringValue(user))._2)
680-
681-
682-
683-@Callable(i)
684-func redeemIndex (sendToOrigin) = {
685- let pmt = i.payments[0]
686- if ((pmt.assetId != tryGetBinary("global_poolToken_id")))
687- then throw("please attach pool share token")
688- else if (isShutdown())
689- then throw("contract is on stop")
690- else {
691- let PRedeemed = pmt.amount
692- let result = handlePoolTokensRedeem(PRedeemed, if (sendToOrigin)
693- then i.originCaller
694- else i.caller)
695- (result ++ [Burn(tryGetBinary("global_poolToken_id"), PRedeemed), IntegerEntry("global_poolToken_amount", (tryGetInteger("global_poolToken_amount") - PRedeemed))])
696- }
697- }
698-
699-
700-
701-@Callable(i)
702-func swap (assetOut,minimum) = {
703- let pmt = if ((size(i.payments) == 1))
704- then value(i.payments[0])
705- else throw("please attach exactly one payment")
706- let AmountIn = value(i.payments[0].amount)
707- let AssetIn = pmt.assetId
708- let AssetOut = getAssetBytes(assetOut)
709- let feeAssetOutStr = tryGetString("static_feeToken")
710- let feeAssetOut = if ((feeAssetOutStr == ""))
711- then usdnAssetId
712- else getAssetBytes(feeAssetOutStr)
713- let AssetInBalance = tryGetInteger((("global_" + getAssetString(AssetIn)) + "_balance"))
714- let AssetOutBalance = tryGetInteger((("global_" + assetOut) + "_balance"))
715- let feeAmountIn = fraction(AmountIn, Fee, FeeScale)
716- let cleanAmountIn = (AmountIn - feeAmountIn)
717- let AmountOut = calculateOutAmount(cleanAmountIn, AssetIn, AssetOut, AssetInBalance, AssetOutBalance)
718- let AssetOutBalance2 = (AssetOutBalance - AmountOut)
719- let AssetInBalance2 = (AssetInBalance + cleanAmountIn)
720- let feeAssetOutBalance = if ((feeAssetOut == AssetIn))
721- then AssetInBalance2
722- else if ((feeAssetOut == AssetOut))
723- then AssetOutBalance2
724- else tryGetInteger((("global_" + getAssetString(feeAssetOut)) + "_balance"))
725- let feeAmountOut = calculateOutAmount(feeAmountIn, AssetIn, feeAssetOut, AssetInBalance, feeAssetOutBalance)
726- if ((minimum > AmountOut))
727- then throw("amount to recieve is lower than given one")
728- else if ((AssetOut == AssetIn))
729- then throw("this swap is not allowed")
730- else if ((0 > (AssetOutBalance - AmountOut)))
731- then throw("contract is out of reserves")
732- else if (isShutdown())
733- then throw("contract is on stop")
734- else {
735- let creatorFee = fraction(feeAmountOut, 1, 10)
736- let protocolFee = fraction(feeAmountOut, 4, 10)
737- let stakingTopUp = if (if ((feeAssetOut == usdnAssetId))
738- then true
739- else (feeAssetOut == puzzleAssetId))
740- then reentrantInvoke(Address(base58'3PFTbywqxtFfukX3HyT881g4iW5K4QL3FAS'), "topUpReward", nil, [AttachedPayment(feeAssetOut, protocolFee)])
741- else unit
742- if ((stakingTopUp == stakingTopUp))
743- then {
744- let newBalanceIn = AssetInBalance2
745- let newBalanceOut = (AssetOutBalance2 - (if ((AssetOut == feeAssetOut))
746- then feeAmountOut
747- else 0))
748- let newBalanceFeeAsset = if (if ((feeAssetOut != AssetIn))
749- then (feeAssetOut != AssetOut)
750- else false)
751- then (feeAssetOutBalance - feeAmountOut)
752- else unit
753- let assetInChange = IntegerEntry((("global_" + getAssetString(AssetIn)) + "_balance"), newBalanceIn)
754- let assetOutChange = IntegerEntry((("global_" + assetOut) + "_balance"), newBalanceOut)
755- let feeAssetOutChange = if ((newBalanceFeeAsset != unit))
756- then IntegerEntry((("global_" + getAssetString(feeAssetOut)) + "_balance"), value(newBalanceFeeAsset))
757- else StringEntry("hello", "world")
758- let volumeUpdate = calculateUsdnValue(AssetIn, AmountIn, AssetInBalance, feeAssetOutBalance)
759- $Tuple2(([assetOutChange, assetInChange, feeAssetOutChange, ScriptTransfer(i.caller, AmountOut, AssetOut), ScriptTransfer(addressFromStringValue(tryGetString("static_poolOwner")), creatorFee, feeAssetOut), IntegerEntry("global_earnedByOwner", (tryGetInteger("global_earnedByOwner") + creatorFee)), IntegerEntry("global_volume", (tryGetInteger("global_volume") + volumeUpdate))] ++ (if ((stakingTopUp == unit))
760- then [ScriptTransfer(Address(base58'3PFWAVKmXjfHXyzJb12jCbhP4Uhi9t4uWiD'), protocolFee, feeAssetOut)]
761- else nil)), AmountOut)
762- }
763- else throw("Strict value is not equal to itself.")
764- }
765- }
766-
767-
768-
769-@Callable(i)
770-func transferOwnership (newOwnerAddress) = if ((toString(i.caller) != tryGetString("static_poolOwner")))
771- then throw("this call available only for pool owner")
772- else [StringEntry("static_poolOwner", newOwnerAddress)]
773-
774-
775-@Verifier(tx)
776-func verify () = if (isShutdown())
777- then false
778- else if (sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey))
779- then true
780- else sigVerify(tx.bodyBytes, tx.proofs[0], masterPubKey)
781-
1+# no script

github/deemru/w8io/169f3d6 
66.88 ms