tx · 8szyEyWapZV47QxYMnLM65rc4oWPxHh4U3A2GAPzeeRs

3MyLDDChFqiZMV8At2rmymVkQzrrPbSE7EK:  -0.01000000 Waves

2023.06.26 13:21 [2639483] smart account 3MyLDDChFqiZMV8At2rmymVkQzrrPbSE7EK > SELF 0.00000000 Waves

{ "type": 13, "id": "8szyEyWapZV47QxYMnLM65rc4oWPxHh4U3A2GAPzeeRs", "fee": 1000000, "feeAssetId": null, "timestamp": 1687774918962, "version": 2, "chainId": 84, "sender": "3MyLDDChFqiZMV8At2rmymVkQzrrPbSE7EK", "senderPublicKey": "GFmKZ2naZFRoCvNbwKAQVGmLb1uBeWGDgFabdGBuZiuy", "proofs": [ "3ho4si5Bs2MZ8kUazrVCuGHQyah4FsxC34Xw5ToHACfkcxZo4WjQZCWMDF8n6cMDVt36W2jaM7mVZ8Z5VqMdBeqV" ], "script": null, "height": 2639483, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: HmxLUPaMZ4Perz1JyEbV5epXuQ6Umuy5VPiJ8e6H4oP8 Next: none Full:
OldNewDifferences
1-{-# STDLIB_VERSION 6 #-}
2-{-# SCRIPT_TYPE ACCOUNT #-}
3-{-# CONTENT_TYPE DAPP #-}
4-let SF_POOL = "SF"
5-
6-let WX_POOL = "WX"
7-
8-let CAP_FEE_NO_LOAN = "capNoLoan"
9-
10-let CAP_FEE_LOAN = "capLoan"
11-
12-let STOPLOSS_FEE_NO_LOAN = "stopLossNoLoan"
13-
14-let STOPLOSS_LOAN = "stopLossLoan"
15-
16-let LOAN_FEE = "loan"
17-
18-let NO_LOAN_FEE = "noLoan"
19-
20-let NO_FEE = "noFee"
21-
22-let SCALE8 = 100000000
23-
24-let SCALE10 = 10000000000
25-
26-let SCALE16 = toBigInt(10000000000000000)
27-
28-let FEE_SCALE6 = 1000000
29-
30-let kSFPoolAAssetBalance = "A_asset_balance"
31-
32-let kSFPoolBAssetBalance = "B_asset_balance"
33-
34-let kSFPoolAAssetId = "A_asset_id"
35-
36-let kSFPoolBAssetId = "B_asset_id"
37-
38-let kSFPoolShareId = "share_asset_id"
39-
40-let kSFPoolShareSupply = "share_asset_supply"
41-
42-let kSFPoolFee = "commission"
43-
44-let kUserPosition = "_user_position"
45-
46-let kUserPositionPool = "_user_position_pool"
47-
48-let kUserBorrowAmount = "_user_position_borrow_amount"
49-
50-let kUserBorrowAssetId = "_user_position_borrow_asset_id"
51-
52-let kUserPositionNum = "_user_position_number"
53-
54-let kUserPositionInterest = "_user_position_interest"
55-
56-let kPoolTotal = "_pool_total"
57-
58-let kPoolTotalLoan = "_pool_total_loan"
59-
60-let kPoolInterestLoan = "_pool_interest_loan"
61-
62-let kPoolInterestNoLoan = "_pool_interest_no_loan"
63-
64-let kAxlyInFeeWithoutLoan = "_axly_fee_without_loan"
65-
66-let kAxlyInFeeWithLoan = "_axly_fee_with_loan"
67-
68-let kAxlyNoLoanCapFee = "_axly_fee_cap_with_loan"
69-
70-let kAxlyWithLoanCapFee = "_axly_fee_cap_no_loan"
71-
72-let kAxlyStopLossNoLoanFee = "_axly_fee_stoploss_with_loan"
73-
74-let kAxlyStopLossLoanFee = "_axly_fee_stoploss_no_loan"
75-
76-let kRequestId = "_request_id"
77-
78-let kRequestIter = "requests_iter"
79-
80-let kPool = "pool_"
81-
82-let kSharePool = "_pool_share_id"
83-
84-let kPoolCapChange = "_pool_cap_change"
85-
86-let kTokenLastPrice = "last_price"
87-
88-let kUserStopLoss = "_stop_loss"
89-
90-let kMoneyBox = "axly_money_box"
91-
92-let kSFFarmingAddr = "swopfi_farming_addr"
93-
94-let kLendService = "lend_service_addr"
95-
96-let kAdminCallPK = "admin_call_pub_key"
97-
98-let kPriceOracle = "price_oracle"
99-
100-let kExContract = "exchange_contract"
101-
102-let kWxSwapContract = "wx_swap_contract"
103-
104-let moneyBox = Address(fromBase58String(valueOrErrorMessage(getString(this, kMoneyBox), "No axly moneyBox address")))
105-
106-let exContract = Address(fromBase58String(valueOrErrorMessage(getString(this, kExContract), "No exchange contract address")))
107-
108-let priceOracleAddr = Address(fromBase58String(valueOrErrorMessage(getString(this, kPriceOracle), "No price oracle address")))
109-
110-let wxSwapContract = Address(fromBase58String(valueOrErrorMessage(getString(this, kWxSwapContract), "No wx swap address")))
111-
112-let SWOPID = base58'4W19ndijcc2CsQa9HGW2dfXKTVXhnneWWttxXrtjPmEp'
113-
114-let WXID = base58'EMAMLxDnv3xiz8RXg8Btj33jcEw3wLczL3JKYYmuubpc'
115-
116-func getLendSrvAddr () = Address(fromBase58String(valueOrErrorMessage(getString(this, kLendService), "Can't get lend service addr")))
117-
118-
119-func getAdminCallAddr () = addressFromPublicKey(fromBase58String(valueOrErrorMessage(getString(this, kAdminCallPK), "Can't get admin addr")))
120-
121-
122-func isAdminCall (i) = if ((i.caller == getAdminCallAddr()))
123- then unit
124- else throw("Only admin can call this function")
125-
126-
127-func isSelfCall (i) = if ((i.caller == this))
128- then unit
129- else throw("Only contract itself can call this function")
130-
131-
132-func isLandCall (i) = if ((i.caller == getLendSrvAddr()))
133- then unit
134- else throw("Only land contract can call this function")
135-
136-
137-func accountBalance (assetId) = match assetId {
138- case id: ByteVector =>
139- assetBalance(this, id)
140- case waves: Unit =>
141- wavesBalance(this).available
142- case _ =>
143- throw("Match error")
144-}
145-
146-
147-func getSFPoolData (poolAddr) = $Tuple5(valueOrErrorMessage(getString(poolAddr, kSFPoolAAssetId), "Can't get pool A asset id"), valueOrErrorMessage(getString(poolAddr, kSFPoolBAssetId), "Can't get pool B asset id"), valueOrErrorMessage(getInteger(poolAddr, kSFPoolAAssetBalance), "Can't get pool A asset balance"), valueOrErrorMessage(getInteger(poolAddr, kSFPoolBAssetBalance), "Can't get pool B asset balance"), valueOrErrorMessage(getString(poolAddr, kSFPoolShareId), "Can't get share asset id"))
148-
149-
150-func getWXPoolData (poolAddr) = {
151- let cfg = {
152- let @ = invoke(poolAddr, "getPoolConfigWrapperREADONLY", nil, nil)
153- if ($isInstanceOf(@, "List[Any]"))
154- then @
155- else throw(($getType(@) + " couldn't be cast to List[Any]"))
156- }
157- if ((cfg == cfg))
158- then {
159- let aId = valueOrErrorMessage({
160- let @ = cfg[4]
161- if ($isInstanceOf(@, "String"))
162- then @
163- else unit
164- }, "Can't get pool A asset id")
165- let bId = valueOrErrorMessage({
166- let @ = cfg[5]
167- if ($isInstanceOf(@, "String"))
168- then @
169- else unit
170- }, "Can't get pool B asset id")
171- let shareId = valueOrErrorMessage({
172- let @ = cfg[3]
173- if ($isInstanceOf(@, "String"))
174- then @
175- else unit
176- }, "Can't get pool LP asset id")
177- let balA = {
178- let @ = invoke(poolAddr, "getAccBalanceWrapperREADONLY", [aId], nil)
179- if ($isInstanceOf(@, "Int"))
180- then @
181- else throw(($getType(@) + " couldn't be cast to Int"))
182- }
183- if ((balA == balA))
184- then {
185- let balB = {
186- let @ = invoke(poolAddr, "getAccBalanceWrapperREADONLY", [bId], nil)
187- if ($isInstanceOf(@, "Int"))
188- then @
189- else throw(($getType(@) + " couldn't be cast to Int"))
190- }
191- if ((balB == balB))
192- then $Tuple5(aId, bId, balA, balB, shareId)
193- else throw("Strict value is not equal to itself.")
194- }
195- else throw("Strict value is not equal to itself.")
196- }
197- else throw("Strict value is not equal to itself.")
198- }
199-
200-
201-func getPoolData (poolAddr,type) = if ((type == SF_POOL))
202- then getSFPoolData(poolAddr)
203- else if ((type == WX_POOL))
204- then getWXPoolData(poolAddr)
205- else throw("Wrong pool type")
206-
207-
208-func getShareSupply (poolAddr,type,shareId) = if ((type == SF_POOL))
209- then valueOrErrorMessage(getInteger(poolAddr, kSFPoolShareSupply), "Can't get share asset supply")
210- else if ((type == WX_POOL))
211- then valueOrErrorMessage(assetInfo(fromBase58String(shareId)), "Wrong ShareId").quantity
212- else throw("Wrong pool type")
213-
214-
215-func getPoolTotalShare (pool) = valueOrElse(getInteger(this, (pool + kPoolTotal)), 0)
216-
217-
218-func getPoolTotalShareWithLoan (pool) = valueOrElse(getInteger(this, (pool + kPoolTotalLoan)), 0)
219-
220-
221-func getNewUserPositionNumber (user) = (valueOrElse(getInteger(this, (user + kUserPositionNum)), 0) + 1)
222-
223-
224-func getAxlyFee (pool,feeType) = if ((feeType == CAP_FEE_LOAN))
225- then getIntegerValue(this, (pool + kAxlyWithLoanCapFee))
226- else if ((feeType == CAP_FEE_NO_LOAN))
227- then getIntegerValue(this, (pool + kAxlyNoLoanCapFee))
228- else if ((feeType == LOAN_FEE))
229- then getIntegerValue(this, (pool + kAxlyInFeeWithLoan))
230- else if ((feeType == NO_LOAN_FEE))
231- then getIntegerValue(this, (pool + kAxlyInFeeWithoutLoan))
232- else if ((feeType == NO_FEE))
233- then 0
234- else throw("Wrong fee type")
235-
236-
237-func getSFFarmingAddr () = Address(fromBase58String(valueOrErrorMessage(getString(this, kSFFarmingAddr), "Can't get swopfi farming addr")))
238-
239-
240-func getWXFarmingAddr (poolAddr) = {
241- let fContract = Address(fromBase58String(valueOrErrorMessage(getString(poolAddr, "%s__factoryContract"), "Can't get WX factory contract addr")))
242- let factroyCfg = split(valueOrErrorMessage(getString(fContract, "%s__factoryConfig"), "Can't get WX factory cfg"), "__")
243- Address(fromBase58String(factroyCfg[1]))
244- }
245-
246-
247-func assetIdToStr (assetId) = match assetId {
248- case id: ByteVector =>
249- toBase58String(id)
250- case waves: Unit =>
251- "WAVES"
252- case _ =>
253- throw("Not Asset id")
254-}
255-
256-
257-func assetIdFromStr (assetId) = if ((assetId == "WAVES"))
258- then unit
259- else fromBase58String(assetId)
260-
261-
262-func getAssetDecimals (assetId) = if ((assetId == "WAVES"))
263- then 8
264- else match assetInfo(fromBase58String(assetId)) {
265- case asset: Asset =>
266- asset.decimals
267- case _ =>
268- throw("Can't find asset")
269- }
270-
271-
272-func getAssetPrecition (assetId) = pow(10, 0, getAssetDecimals(assetId), 0, 0, DOWN)
273-
274-
275-func getAssetsPrice (assetIds) = {
276- func getPrices (a,assetId) = {
277- let assetPrice = ( let @ = invoke(priceOracleAddr, "getTWAP60", [assetId, false], nil)
278- if ($isInstanceOf(@, "(Int, Int)"))
279- then @
280- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._2
281- (a :+ assetPrice)
282- }
283-
284- let $l = assetIds
285- let $s = size($l)
286- let $acc0 = nil
287- func $f0_1 ($a,$i) = if (($i >= $s))
288- then $a
289- else getPrices($a, $l[$i])
290-
291- func $f0_2 ($a,$i) = if (($i >= $s))
292- then $a
293- else throw("List size exceeds 50")
294-
295- $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($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), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20), 21), 22), 23), 24), 25), 26), 27), 28), 29), 30), 31), 32), 33), 34), 35), 36), 37), 38), 39), 40), 41), 42), 43), 44), 45), 46), 47), 48), 49), 50)
296- }
297-
298-
299-func getSharePrice (shareId) = {
300- let pool = valueOrErrorMessage(getString(this, (shareId + kSharePool)), "Can't find pool addr by share id")
301- let poolAddr = Address(fromBase58String(pool))
302- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Pool is not inited")
303- let $t088698934 = getPoolData(poolAddr, pType)
304- let aId = $t088698934._1
305- let bId = $t088698934._2
306- let aBalance = $t088698934._3
307- let bBalance = $t088698934._4
308- let dPriceA = ( let @ = invoke(priceOracleAddr, "getTWAP60", [aId, false], nil)
309- if ($isInstanceOf(@, "(Int, Int)"))
310- then @
311- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._2
312- let dPriceB = ( let @ = invoke(priceOracleAddr, "getTWAP60", [bId, false], nil)
313- if ($isInstanceOf(@, "(Int, Int)"))
314- then @
315- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._2
316- let shareSupply = getShareSupply(poolAddr, pType, shareId)
317- let APrecision = pow(10, 0, getAssetDecimals(aId), 0, 0, DOWN)
318- let BPrecision = pow(10, 0, getAssetDecimals(bId), 0, 0, DOWN)
319- let sharePrecision = pow(10, 0, getAssetDecimals(shareId), 0, 0, DOWN)
320- let sum = (fraction(aBalance, dPriceA, APrecision) + fraction(bBalance, dPriceB, BPrecision))
321- fraction(sum, sharePrecision, shareSupply)
322- }
323-
324-
325-func getSharePrices (shareIds) = {
326- func getPrices (a,shareId) = (a :+ getSharePrice(shareId))
327-
328- let $l = shareIds
329- let $s = size($l)
330- let $acc0 = nil
331- func $f0_1 ($a,$i) = if (($i >= $s))
332- then $a
333- else getPrices($a, $l[$i])
334-
335- func $f0_2 ($a,$i) = if (($i >= $s))
336- then $a
337- else throw("List size exceeds 20")
338-
339- $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($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), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20)
340- }
341-
342-
343-func getCursEntries (aId,bId,shareId) = {
344- let assetsPrices = getAssetsPrice([aId, bId])
345- let sharePrice = getSharePrice(shareId)
346- let prices = [toString(assetsPrices[0]), toString(assetsPrices[1]), toString(sharePrice)]
347-[StringEntry(kTokenLastPrice, makeString(prices, ","))]
348- }
349-
350-
351-func calcReplenishByTwoTokens (pType,poolAddr,pmtA,aId,pmtB,bId,balA,balB) = if ((pType == SF_POOL))
352- then {
353- let repl = {
354- let @ = invoke(poolAddr, "callFunction", ["calcLPReplenishTwoTokensREADONLY", [toString(pmtA), toString(pmtB)]], nil)
355- if ($isInstanceOf(@, "List[Any]"))
356- then @
357- else throw(($getType(@) + " couldn't be cast to List[Any]"))
358- }
359- if ((repl == repl))
360- then $Tuple5({
361- let @ = repl[3]
362- if ($isInstanceOf(@, "Int"))
363- then @
364- else throw(($getType(@) + " couldn't be cast to Int"))
365- }, {
366- let @ = repl[4]
367- if ($isInstanceOf(@, "Int"))
368- then @
369- else throw(($getType(@) + " couldn't be cast to Int"))
370- }, {
371- let @ = repl[1]
372- if ($isInstanceOf(@, "Int"))
373- then @
374- else throw(($getType(@) + " couldn't be cast to Int"))
375- }, assetIdToStr(repl[2]), {
376- let @ = repl[0]
377- if ($isInstanceOf(@, "Int"))
378- then @
379- else throw(($getType(@) + " couldn't be cast to Int"))
380- })
381- else throw("Strict value is not equal to itself.")
382- }
383- else {
384- let $t01046410714 = $Tuple2(split({
385- let @ = invoke(poolAddr, "evaluatePutByAmountAssetREADONLY", [pmtA], nil)
386- if ($isInstanceOf(@, "String"))
387- then @
388- else throw(($getType(@) + " couldn't be cast to String"))
389- }, "__"), split({
390- let @ = invoke(poolAddr, "evaluatePutByPriceAssetREADONLY", [pmtB], nil)
391- if ($isInstanceOf(@, "String"))
392- then @
393- else throw(($getType(@) + " couldn't be cast to String"))
394- }, "__"))
395- if (($t01046410714 == $t01046410714))
396- then {
397- let evalPutInB = $t01046410714._2
398- let evalPutInA = $t01046410714._1
399- let lpInA = parseIntValue(evalPutInA[1])
400- let lpInB = parseIntValue(evalPutInB[1])
401- if ((lpInB > lpInA))
402- then {
403- let pmt = parseIntValue(evalPutInA[8])
404- $Tuple5(pmtA, pmt, (pmtB - pmt), bId, lpInB)
405- }
406- else {
407- let pmt = parseIntValue(evalPutInA[8])
408- $Tuple5(pmt, pmtB, (pmtA - pmt), aId, lpInA)
409- }
410- }
411- else throw("Strict value is not equal to itself.")
412- }
413-
414-
415-func replenishTwoTokensByType (poolAddr,pType,pmtA,aId,pmtB,bId) = {
416- let payments = [AttachedPayment(assetIdFromStr(aId), pmtA), AttachedPayment(assetIdFromStr(bId), pmtB)]
417- if ((pType == SF_POOL))
418- then invoke(poolAddr, "callFunction", ["replenishWithTwoTokens", ["false", "0"]], payments)
419- else invoke(poolAddr, "put", [1000000, false], payments)
420- }
421-
422-
423-func replenishOneTokenByType (poolAddr,pType,pmt,pmtId) = {
424- let payments = [AttachedPayment(assetIdFromStr(pmtId), pmt)]
425- if ((pType == SF_POOL))
426- then invoke(poolAddr, "callFunction", ["replenishWithOneToken", ["0", "false", "0"]], payments)
427- else invoke(poolAddr, "putOneTkn", [0, false], payments)
428- }
429-
430-
431-func stakeLP (pool,pType,shareId,amount) = {
432- let payments = [AttachedPayment(fromBase58String(shareId), amount)]
433- if ((pType == SF_POOL))
434- then invoke(getSFFarmingAddr(), "lockShareTokens", [pool, 0], payments)
435- else invoke(getWXFarmingAddr(addressFromStringValue(pool)), "stake", nil, payments)
436- }
437-
438-
439-func unstakeLP (pool,pType,shareId,amount) = {
440- let $t01219912556 = if ((pType == SF_POOL))
441- then $Tuple3(getSFFarmingAddr(), "withdrawShareTokens", [pool, amount])
442- else if ((pType == WX_POOL))
443- then $Tuple3(getWXFarmingAddr(Address(fromBase58String(pool))), "unstake", [shareId, amount])
444- else throw("Wrong pool type")
445- let farmAddr = $t01219912556._1
446- let fName = $t01219912556._2
447- let params = $t01219912556._3
448- let inv = invoke(farmAddr, fName, params, nil)
449- if ((inv == inv))
450- then amount
451- else throw("Strict value is not equal to itself.")
452- }
453-
454-
455-func calcAmountToPaySF (pool,assetIdA,assetIdB,balA,balB,amountTokenToGet,assetTokenToGet) = {
456- let poolAddr = Address(fromBase58String(pool))
457- let feeScale6 = 1000000
458- let fee = getIntegerValue(poolAddr, kSFPoolFee)
459- let amntGetNoFee = fraction(amountTokenToGet, feeScale6, (feeScale6 - fee))
460- let $t01296913257 = if ((assetTokenToGet == assetIdA))
461- then {
462- let amountToPay = fraction(amntGetNoFee, balB, (balA - amntGetNoFee))
463- $Tuple2(amountToPay, assetIdB)
464- }
465- else {
466- let amountToPay = fraction(amntGetNoFee, balA, (balB - amntGetNoFee))
467- $Tuple2(amountToPay, assetIdA)
468- }
469- let amountToPay = $t01296913257._1
470- let assetToPay = $t01296913257._2
471- $Tuple2(assetToPay, amountToPay)
472- }
473-
474-
475-func calcAmountToPayWX (pool,assetIdA,assetIdB,balA,balB,amountTokenToGet,assetTokenToGet) = {
476- let prFee = getIntegerValue(wxSwapContract, "%s__protocolFee")
477- let pFee = getIntegerValue(wxSwapContract, "%s__poolFee")
478- let feeScale = toBigInt(100000000)
479- let $t01359613903 = if ((assetTokenToGet == assetIdA))
480- then {
481- let amountToPay = fraction(amountTokenToGet, balB, (balA - amountTokenToGet))
482- $Tuple2(amountToPay, assetIdB)
483- }
484- else {
485- let amountToPay = fraction(amountTokenToGet, balA, (balB - amountTokenToGet))
486- $Tuple2(amountToPay, assetIdA)
487- }
488- let amountToPay = $t01359613903._1
489- let assetToPay = $t01359613903._2
490- let amountToPayWithFee = toInt(fraction(toBigInt(amountToPay), feeScale, (feeScale - toBigInt((prFee + pFee)))))
491- $Tuple2(assetToPay, amountToPayWithFee)
492- }
493-
494-
495-func exchangeDirectly (type,pool,assetIdA,assetIdB,balA,balB,amountTokenToGet,assetTokenToGet) = if ((type == SF_POOL))
496- then {
497- let $t01424114363 = calcAmountToPaySF(pool, assetIdA, assetIdB, balA, balB, amountTokenToGet, assetTokenToGet)
498- let assetToPay = $t01424114363._1
499- let amountToPay = $t01424114363._2
500- invoke(addressFromStringValue(pool), "callFunction", ["exchange", ["1"]], [AttachedPayment(assetIdFromStr(assetToPay), amountToPay)])
501- }
502- else {
503- let $t01451414636 = calcAmountToPayWX(pool, assetIdA, assetIdB, balA, balB, amountTokenToGet, assetTokenToGet)
504- let assetToPay = $t01451414636._1
505- let amountToPay = $t01451414636._2
506- invoke(wxSwapContract, "swap", [1, assetTokenToGet, toString(this)], [AttachedPayment(assetIdFromStr(assetToPay), amountToPay)])
507- }
508-
509-
510-func calcReplenishLPVirt (pType,pool,pmtA,aId,pmtB,bId,balA,balB) = {
511- let poolAddr = addressFromStringValue(pool)
512- if ((pType == SF_POOL))
513- then {
514- let inv = {
515- let @ = invoke(poolAddr, "callFunction", ["calcLPReplenishDiffPropREADONLY", [toString(pmtA), toString(pmtB), "0"]], nil)
516- if ($isInstanceOf(@, "List[Any]"))
517- then @
518- else throw(($getType(@) + " couldn't be cast to List[Any]"))
519- }
520- if ((inv == inv))
521- then $Tuple2({
522- let @ = inv[0]
523- if ($isInstanceOf(@, "Int"))
524- then @
525- else throw(($getType(@) + " couldn't be cast to Int"))
526- }, {
527- let @ = inv[1]
528- if ($isInstanceOf(@, "Int"))
529- then @
530- else throw(($getType(@) + " couldn't be cast to Int"))
531- })
532- else throw("Strict value is not equal to itself.")
533- }
534- else if ((pType == WX_POOL))
535- then {
536- let $t01522415565 = if (if ((pmtA > 0))
537- then (pmtB > 0)
538- else false)
539- then {
540- let $t01530515431 = calcReplenishByTwoTokens(pType, poolAddr, pmtA, aId, pmtB, bId, balA, balB)
541- let pmtInA = $t01530515431._1
542- let pmtInB = $t01530515431._2
543- let change = $t01530515431._3
544- let changeId = $t01530515431._4
545- let lpAmount = $t01530515431._5
546- $Tuple3(change, changeId, lpAmount)
547- }
548- else if ((pmtA > 0))
549- then $Tuple3(pmtA, aId, 0)
550- else $Tuple3(pmtB, bId, 0)
551- let change = $t01522415565._1
552- let changeId = $t01522415565._2
553- let lpTwo = $t01522415565._3
554- let $t01557215788 = if ((change > 0))
555- then {
556- let inv = {
557- let @ = invoke(poolAddr, "putOneTknREADONLY", [changeId, change], nil)
558- if ($isInstanceOf(@, "(Int, Int, Int)"))
559- then @
560- else throw(($getType(@) + " couldn't be cast to (Int, Int, Int)"))
561- }
562- if ((inv == inv))
563- then $Tuple2(inv._1, inv._3)
564- else throw("Strict value is not equal to itself.")
565- }
566- else $Tuple2(0, 0)
567- let lpOne = $t01557215788._1
568- let loss = $t01557215788._2
569- $Tuple2((lpTwo + lpOne), loss)
570- }
571- else throw("Wrong pool type")
572- }
573-
574-
575-func calcWithdrawLPVirt (pType,pool,lpAmount,shareId,aId,bId,balA,balB,borrowAmount,borrowAssetId) = {
576- let poolAddr = addressFromStringValue(pool)
577- let $t01608216582 = if ((pType == SF_POOL))
578- then {
579- let inv = {
580- let @ = invoke(poolAddr, "callFunction", ["withdrawREADONLY", [toString(lpAmount), toString(balA), toString(balB)]], nil)
581- if ($isInstanceOf(@, "List[Any]"))
582- then @
583- else throw(($getType(@) + " couldn't be cast to List[Any]"))
584- }
585- if ((inv == inv))
586- then $Tuple2({
587- let @ = inv[0]
588- if ($isInstanceOf(@, "Int"))
589- then @
590- else throw(($getType(@) + " couldn't be cast to Int"))
591- }, {
592- let @ = inv[1]
593- if ($isInstanceOf(@, "Int"))
594- then @
595- else throw(($getType(@) + " couldn't be cast to Int"))
596- })
597- else throw("Strict value is not equal to itself.")
598- }
599- else if ((pType == WX_POOL))
600- then {
601- let get = split({
602- let @ = invoke(poolAddr, "evaluateGetREADONLY", [shareId, lpAmount], nil)
603- if ($isInstanceOf(@, "String"))
604- then @
605- else throw(($getType(@) + " couldn't be cast to String"))
606- }, "__")
607- if ((get == get))
608- then $Tuple2(parseIntValue(get[1]), parseIntValue(get[2]))
609- else throw("Strict value is not equal to itself.")
610- }
611- else throw("Wrong pool type")
612- let getAmountA = $t01608216582._1
613- let getAmountB = $t01608216582._2
614- if ((borrowAmount > 0))
615- then {
616- let amountToGetEx = if (if ((borrowAssetId == aId))
617- then (borrowAmount > getAmountA)
618- else false)
619- then (borrowAmount - getAmountA)
620- else if (if ((borrowAssetId == bId))
621- then (borrowAmount > getAmountB)
622- else false)
623- then (borrowAmount - getAmountB)
624- else 0
625- let $t01686017163 = if ((amountToGetEx > 0))
626- then if ((pType == SF_POOL))
627- then calcAmountToPaySF(pool, aId, bId, balA, balB, amountToGetEx, borrowAssetId)
628- else calcAmountToPayWX(pool, aId, bId, balA, balB, amountToGetEx, borrowAssetId)
629- else $Tuple2("", 0)
630- let assetToPay = $t01686017163._1
631- let amountToPay = $t01686017163._2
632- if ((borrowAssetId == aId))
633- then $Tuple2(((getAmountA + amountToGetEx) - borrowAmount), (getAmountB - amountToPay))
634- else $Tuple2((getAmountA - amountToPay), ((getAmountB + amountToGetEx) - borrowAmount))
635- }
636- else $Tuple2(getAmountA, getAmountB)
637- }
638-
639-
640-func claimFarmed (type,pool) = if ((type == SF_POOL))
641- then {
642- let balBefore = accountBalance(SWOPID)
643- if ((balBefore == balBefore))
644- then {
645- let inv = invoke(getSFFarmingAddr(), "claim", [pool], nil)
646- if ((inv == inv))
647- then {
648- let balAfter = accountBalance(SWOPID)
649- $Tuple2((balAfter - balBefore), SWOPID)
650- }
651- else throw("Strict value is not equal to itself.")
652- }
653- else throw("Strict value is not equal to itself.")
654- }
655- else if ((type == WX_POOL))
656- then {
657- let balBefore = accountBalance(WXID)
658- if ((balBefore == balBefore))
659- then {
660- let inv = invoke(getWXFarmingAddr(Address(fromBase58String(pool))), "claimWX", [pool], nil)
661- if ((inv == inv))
662- then {
663- let balAfter = accountBalance(WXID)
664- $Tuple2((balAfter - balBefore), WXID)
665- }
666- else throw("Strict value is not equal to itself.")
667- }
668- else throw("Strict value is not equal to itself.")
669- }
670- else throw("Wrong pool type")
671-
672-
673-func replenishByType (pType,pool,feeType,pmtA,aId,pmtB,bId,balA,balB,LPId) = {
674- let lpBalanceBefore = accountBalance(fromBase58String(LPId))
675- if ((lpBalanceBefore == lpBalanceBefore))
676- then {
677- let poolAddr = addressFromStringValue(pool)
678- let $t01836018776 = if (if ((pmtA > 0))
679- then (pmtB > 0)
680- else false)
681- then {
682- let $t01842618542 = calcReplenishByTwoTokens(pType, poolAddr, pmtA, aId, pmtB, bId, balA, balB)
683- let pmtInA = $t01842618542._1
684- let pmtInB = $t01842618542._2
685- let change = $t01842618542._3
686- let changeId = $t01842618542._4
687- let inv = replenishTwoTokensByType(poolAddr, pType, pmtInA, aId, pmtInB, bId)
688- if ((inv == inv))
689- then $Tuple2(change, changeId)
690- else throw("Strict value is not equal to itself.")
691- }
692- else if ((pmtA > 0))
693- then $Tuple2(pmtA, aId)
694- else if ((pmtB > 0))
695- then $Tuple2(pmtB, bId)
696- else throw("pmts must be > 0")
697- let change = $t01836018776._1
698- let changeId = $t01836018776._2
699- let inv = if ((change > 0))
700- then replenishOneTokenByType(poolAddr, pType, change, changeId)
701- else nil
702- if ((inv == inv))
703- then {
704- let lpBalanceAfter = accountBalance(fromBase58String(LPId))
705- let totalStaked = (lpBalanceAfter - lpBalanceBefore)
706- let axlyFeeAmount = fraction(totalStaked, getAxlyFee(pool, feeType), FEE_SCALE6)
707- let userShareForStake = (totalStaked - axlyFeeAmount)
708- if ((0 >= userShareForStake))
709- then throw("amount of staked sharetokens must be > 0")
710- else {
711- let invLP = stakeLP(pool, pType, LPId, userShareForStake)
712- if ((invLP == invLP))
713- then $Tuple2(userShareForStake, axlyFeeAmount)
714- else throw("Strict value is not equal to itself.")
715- }
716- }
717- else throw("Strict value is not equal to itself.")
718- }
719- else throw("Strict value is not equal to itself.")
720- }
721-
722-
723-func replenishEntries (pool,user,stakedAmount,axlyFeeAmount,posNum,shareId,type,withLoan) = {
724- let totalAmount = getPoolTotalShare(pool)
725- let totalAmountLoan = getPoolTotalShareWithLoan(pool)
726- let $t01963419872 = if (withLoan)
727- then $Tuple2(getIntegerValue(this, (pool + kPoolInterestLoan)), (totalAmountLoan + stakedAmount))
728- else $Tuple2(getIntegerValue(this, (pool + kPoolInterestNoLoan)), totalAmountLoan)
729- let curPoolInterest = $t01963419872._1
730- let totalStakedWithLoan = $t01963419872._2
731-[IntegerEntry((pool + kPoolTotal), (totalAmount + stakedAmount)), IntegerEntry((pool + kPoolTotalLoan), totalStakedWithLoan), IntegerEntry((((((pool + "_") + user) + "_") + toString(posNum)) + kUserPosition), stakedAmount), IntegerEntry((((((pool + "_") + user) + "_") + toString(posNum)) + kUserPositionInterest), curPoolInterest), StringEntry((((user + "_") + toString(posNum)) + kUserPositionPool), pool), IntegerEntry((user + kUserPositionNum), posNum), ScriptTransfer(moneyBox, axlyFeeAmount, fromBase58String(shareId))]
732- }
733-
734-
735-func exchangeKeeper (toToken,pmtAmount,pmtAsset,amountsIn,addresses,assetsToReceive,estReceived,slippageTolerance,minReceived,options) = {
736- let tokenBalanceBefore = accountBalance(assetIdFromStr(toToken))
737- if ((tokenBalanceBefore == tokenBalanceBefore))
738- then {
739- let inv = invoke(exContract, "swap", [amountsIn, addresses, assetsToReceive, estReceived, slippageTolerance, minReceived, options], [AttachedPayment(pmtAsset, pmtAmount)])
740- if ((inv == inv))
741- then (accountBalance(assetIdFromStr(toToken)) - tokenBalanceBefore)
742- else throw("Strict value is not equal to itself.")
743- }
744- else throw("Strict value is not equal to itself.")
745- }
746-
747-
748-func exchangePuzzle (toToken,pmtAmount,pmtAsset,routesStr,minToReceive,options) = {
749- let tokenBalanceBefore = accountBalance(assetIdFromStr(toToken))
750- if ((tokenBalanceBefore == tokenBalanceBefore))
751- then {
752- let inv = invoke(exContract, "puzzleSwap", [routesStr, minToReceive, options], [AttachedPayment(pmtAsset, pmtAmount)])
753- if ((inv == inv))
754- then (accountBalance(assetIdFromStr(toToken)) - tokenBalanceBefore)
755- else throw("Strict value is not equal to itself.")
756- }
757- else throw("Strict value is not equal to itself.")
758- }
759-
760-
761-func exchangeSwopFi (toToken,pmtAmount,pmtAsset,exchangers,exchangersType,args1,args2,routingAssetsKeys,minAmountToReceive,options) = {
762- let tokenBalanceBefore = accountBalance(assetIdFromStr(toToken))
763- if ((tokenBalanceBefore == tokenBalanceBefore))
764- then {
765- let inv = invoke(exContract, "swopfiSwap", [exchangers, exchangersType, args1, args2, routingAssetsKeys, minAmountToReceive, options], [AttachedPayment(pmtAsset, pmtAmount)])
766- if ((inv == inv))
767- then (accountBalance(assetIdFromStr(toToken)) - tokenBalanceBefore)
768- else throw("Strict value is not equal to itself.")
769- }
770- else throw("Strict value is not equal to itself.")
771- }
772-
773-
774-func capitalize (pool,pType,tokenId,tokenAmount) = {
775- let poolAddr = Address(fromBase58String(pool))
776- let $t02222322289 = getPoolData(poolAddr, pType)
777- let AId = $t02222322289._1
778- let BId = $t02222322289._2
779- let balA = $t02222322289._3
780- let balB = $t02222322289._4
781- let shareId = $t02222322289._5
782- if (if ((tokenId != AId))
783- then (tokenId != BId)
784- else false)
785- then throw("Wrong asset")
786- else {
787- let $t02237422454 = if ((tokenId == AId))
788- then $Tuple2(tokenAmount, 0)
789- else $Tuple2(0, tokenAmount)
790- let pmtA = $t02237422454._1
791- let pmtB = $t02237422454._2
792- let $t02245722561 = replenishByType(pType, pool, NO_FEE, pmtA, AId, pmtB, BId, balA, balB, shareId)
793- let stakedAmount = $t02245722561._1
794- let nf = $t02245722561._2
795- let curPoolInterestLoan = valueOrElse(getInteger(this, (pool + kPoolInterestLoan)), 0)
796- let curPoolInterestNoLoan = valueOrElse(getInteger(this, (pool + kPoolInterestNoLoan)), 0)
797- let totalShareAmount = getPoolTotalShare(pool)
798- let totalShareAmountWithLoan = getPoolTotalShareWithLoan(pool)
799- let loanPercent = fraction(totalShareAmountWithLoan, SCALE8, totalShareAmount)
800- let stakedLoan = fraction(stakedAmount, loanPercent, SCALE8)
801- let stakedNoLoan = (stakedAmount - stakedLoan)
802- let newInterestLoan = if ((totalShareAmountWithLoan > 0))
803- then (curPoolInterestLoan + fraction(stakedLoan, SCALE10, totalShareAmountWithLoan))
804- else 0
805- let newInterestNoLoan = if (((totalShareAmount - totalShareAmountWithLoan) > 0))
806- then (curPoolInterestNoLoan + fraction(stakedNoLoan, SCALE10, (totalShareAmount - totalShareAmountWithLoan)))
807- else 0
808- let axlyFeeLoan = fraction(stakedLoan, getAxlyFee(pool, CAP_FEE_LOAN), FEE_SCALE6)
809- let axlyFeeNoLoan = fraction(stakedNoLoan, getAxlyFee(pool, CAP_FEE_NO_LOAN), FEE_SCALE6)
810- let axlyFee = unstakeLP(pool, pType, shareId, (axlyFeeLoan + axlyFeeNoLoan))
811- if ((axlyFee == axlyFee))
812- then ([IntegerEntry((pool + kPoolInterestLoan), newInterestLoan), IntegerEntry((pool + kPoolInterestNoLoan), newInterestNoLoan), IntegerEntry((pool + kPoolTotal), ((totalShareAmount + stakedAmount) - axlyFee)), IntegerEntry((pool + kPoolTotalLoan), ((totalShareAmountWithLoan + stakedLoan) - axlyFeeLoan)), ScriptTransfer(moneyBox, (axlyFeeLoan + axlyFeeNoLoan), fromBase58String(shareId))] ++ getCursEntries(AId, BId, shareId))
813- else throw("Strict value is not equal to itself.")
814- }
815- }
816-
817-
818-func withdrawAmountCalc (pool,userCanWithdraw,debt,borrowAsset) = {
819- let poolAddr = Address(fromBase58String(pool))
820- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Unknown pool")
821- let $t02432924405 = getPoolData(poolAddr, pType)
822- let assetIdA = $t02432924405._1
823- let assetIdB = $t02432924405._2
824- let balA = $t02432924405._3
825- let balB = $t02432924405._4
826- let shareId = $t02432924405._5
827- let cBalABefore = accountBalance(assetIdFromStr(assetIdA))
828- if ((cBalABefore == cBalABefore))
829- then {
830- let cBalBBefore = accountBalance(assetIdFromStr(assetIdB))
831- if ((cBalBBefore == cBalBBefore))
832- then {
833- let inv = if ((pType == SF_POOL))
834- then invoke(poolAddr, "callFunction", ["withdraw", [toString(userCanWithdraw)]], nil)
835- else {
836- let inv = unstakeLP(pool, pType, shareId, userCanWithdraw)
837- if ((inv == inv))
838- then invoke(poolAddr, "get", nil, [AttachedPayment(assetIdFromStr(shareId), userCanWithdraw)])
839- else throw("Strict value is not equal to itself.")
840- }
841- if ((inv == inv))
842- then {
843- let cBalAAfter = accountBalance(assetIdFromStr(assetIdA))
844- let cBalBAfter = accountBalance(assetIdFromStr(assetIdB))
845- let $t02496825057 = $Tuple2((cBalAAfter - cBalABefore), (cBalBAfter - cBalBBefore))
846- let tokensAmountA = $t02496825057._1
847- let tokensAmountB = $t02496825057._2
848- let $t02506025892 = if ((debt > 0))
849- then {
850- let amountToGetEx = if (if ((borrowAsset == assetIdA))
851- then (debt > tokensAmountA)
852- else false)
853- then (debt - tokensAmountA)
854- else if (if ((borrowAsset == assetIdB))
855- then (debt > tokensAmountB)
856- else false)
857- then (debt - tokensAmountB)
858- else 0
859- let exInv = if ((amountToGetEx > 0))
860- then exchangeDirectly(pType, pool, assetIdA, assetIdB, balA, balB, amountToGetEx, borrowAsset)
861- else 0
862- if ((exInv == exInv))
863- then {
864- let cBalAAfterRepay = accountBalance(assetIdFromStr(assetIdA))
865- let cBalBAfterRepay = accountBalance(assetIdFromStr(assetIdB))
866- if ((borrowAsset == assetIdA))
867- then $Tuple2(((cBalAAfterRepay - cBalABefore) - debt), (cBalBAfterRepay - cBalBBefore))
868- else $Tuple2((cBalAAfterRepay - cBalABefore), ((cBalBAfterRepay - cBalBBefore) - debt))
869- }
870- else throw("Strict value is not equal to itself.")
871- }
872- else $Tuple2(tokensAmountA, tokensAmountB)
873- let toUserAmountA = $t02506025892._1
874- let toUserAmountB = $t02506025892._2
875- $Tuple7(toUserAmountA, assetIdA, toUserAmountB, assetIdB, cBalAAfter, cBalBAfter, shareId)
876- }
877- else throw("Strict value is not equal to itself.")
878- }
879- else throw("Strict value is not equal to itself.")
880- }
881- else throw("Strict value is not equal to itself.")
882- }
883-
884-
885-func userCanWithdrawShareCalc (user,pool,posId,borrowed) = {
886- let pAmount = valueOrErrorMessage(getInteger(this, (((((pool + "_") + user) + "_") + posId) + kUserPosition)), "Unknown position")
887- let userInterest = getIntegerValue(this, (((((pool + "_") + user) + "_") + posId) + kUserPositionInterest))
888- let poolInterest = if (borrowed)
889- then getIntegerValue(this, (pool + kPoolInterestLoan))
890- else getIntegerValue(this, (pool + kPoolInterestNoLoan))
891- (pAmount + fraction(pAmount, (poolInterest - userInterest), SCALE10))
892- }
893-
894-
895-func withdrawToUser (user,pool,posId,stopLoss) = {
896- let pAmount = valueOrErrorMessage(getInteger(this, (((((pool + "_") + user) + "_") + posId) + kUserPosition)), "Unknown position")
897- let borrowAmount = valueOrElse(getInteger(this, (((((pool + "_") + user) + "_") + posId) + kUserBorrowAmount)), 0)
898- let userCanWithdraw = userCanWithdrawShareCalc(user, pool, posId, (borrowAmount > 0))
899- let poolTotalShare = getPoolTotalShare(pool)
900- let userAddr = Address(fromBase58String(user))
901- let borrowAsset = valueOrElse(getString(this, (((((pool + "_") + user) + "_") + posId) + kUserBorrowAssetId)), "")
902- let debt = if ((borrowAmount > 0))
903- then {
904- let @ = invoke(getLendSrvAddr(), "getAssetDebt", [false, ((user + "_") + posId), borrowAsset], nil)
905- if ($isInstanceOf(@, "Int"))
906- then @
907- else throw(($getType(@) + " couldn't be cast to Int"))
908- }
909- else 0
910- let $t02729227445 = withdrawAmountCalc(pool, userCanWithdraw, debt, borrowAsset)
911- if (($t02729227445 == $t02729227445))
912- then {
913- let shareId = $t02729227445._7
914- let cBalBAfter = $t02729227445._6
915- let cBalAAfter = $t02729227445._5
916- let assetIdB = $t02729227445._4
917- let toUserAmountB = $t02729227445._3
918- let assetIdA = $t02729227445._2
919- let toUserAmountA = $t02729227445._1
920- let closeDbtInv = if ((debt > 0))
921- then invoke(getLendSrvAddr(), "repayFor", [((user + "_") + posId)], [AttachedPayment(assetIdFromStr(borrowAsset), debt)])
922- else 0
923- if ((closeDbtInv == closeDbtInv))
924- then ([DeleteEntry((((((pool + "_") + user) + "_") + posId) + kUserPosition)), DeleteEntry((((((pool + "_") + user) + "_") + posId) + kUserPositionInterest)), IntegerEntry((pool + kPoolTotal), (poolTotalShare - userCanWithdraw)), ScriptTransfer(userAddr, toUserAmountA, assetIdFromStr(assetIdA)), ScriptTransfer(userAddr, toUserAmountB, assetIdFromStr(assetIdB))] ++ getCursEntries(assetIdA, assetIdB, shareId))
925- else throw("Strict value is not equal to itself.")
926- }
927- else throw("Strict value is not equal to itself.")
928- }
929-
930-
931-func parseRequest (requestId) = {
932- let request = split(valueOrErrorMessage(getString(this, (requestId + kRequestId)), ("No request with id " + requestId)), ",")
933- let user = request[0]
934- let pool = request[1]
935- let pmtA = parseIntValue(request[2])
936- let AId = request[3]
937- let pmtB = parseIntValue(request[4])
938- let BId = request[5]
939- let balA = parseIntValue(request[6])
940- let balB = parseIntValue(request[7])
941- let shareId = request[8]
942- let bwAsset = request[9]
943- let bwAmount = parseIntValue(request[10])
944- $Tuple11(user, pool, pmtA, AId, pmtB, BId, balA, balB, shareId, bwAsset, bwAmount)
945- }
946-
947-
948-func calcBorrowAmount (pmtA,pmtB,aId,bId,leverage,borrowId) = {
949- let dPriceA = ( let @ = invoke(priceOracleAddr, "getTWAP60", [aId, false], nil)
950- if ($isInstanceOf(@, "(Int, Int)"))
951- then @
952- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._2
953- let dPriceB = ( let @ = invoke(priceOracleAddr, "getTWAP60", [bId, false], nil)
954- if ($isInstanceOf(@, "(Int, Int)"))
955- then @
956- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._2
957- let decPrA = pow(10, 0, getAssetDecimals(aId), 0, 0, DOWN)
958- let decPrB = pow(10, 0, getAssetDecimals(bId), 0, 0, DOWN)
959- let paydInDollar = (fraction(dPriceA, pmtA, decPrA) + fraction(dPriceB, pmtB, decPrB))
960- let $t02913129228 = if ((borrowId == aId))
961- then $Tuple2(dPriceA, decPrA)
962- else $Tuple2(dPriceB, decPrB)
963- let borrowPrice = $t02913129228._1
964- let borrowDecPr = $t02913129228._2
965- fraction(fraction(paydInDollar, (leverage - 100), 100), borrowDecPr, borrowPrice)
966- }
967-
968-
969-@Callable(i)
970-func calcPriceImpactEVALONLY (pool,leverage,borrowId,pmtA,pmtB) = if (if ((100 > leverage))
971- then true
972- else (leverage > 300))
973- then throw("Leverage can't be <100 and >300")
974- else {
975- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Pool is not inited")
976- let $t02964629736 = getPoolData(Address(fromBase58String(pool)), pType)
977- let AId = $t02964629736._1
978- let BId = $t02964629736._2
979- let balA = $t02964629736._3
980- let balB = $t02964629736._4
981- let shareId = $t02964629736._5
982- let borrowAmount = if ((leverage > 100))
983- then calcBorrowAmount(pmtA, pmtB, AId, BId, leverage, borrowId)
984- else 0
985- let $t02987129978 = if ((borrowId == AId))
986- then $Tuple2((pmtA + borrowAmount), pmtB)
987- else $Tuple2(pmtA, (pmtB + borrowAmount))
988- let payInA = $t02987129978._1
989- let payInB = $t02987129978._2
990- let $t02998130074 = calcReplenishLPVirt(pType, pool, payInA, AId, payInB, BId, balA, balB)
991- let lpAmount = $t02998130074._1
992- let loss = $t02998130074._2
993- let impactMod = if ((0 > loss))
994- then (loss * -1)
995- else loss
996- $Tuple2(nil, [impactMod])
997- }
998-
999-
1000-
1001-@Callable(i)
1002-func getShareAssetPriceREADONLY (shareId) = {
1003- let sharePrices = getSharePrice(shareId)
1004- $Tuple2(nil, sharePrices)
1005- }
1006-
1007-
1008-
1009-@Callable(i)
1010-func getUserPositionShareAmountREADONLY (user,posNum) = {
1011- let pool = valueOrErrorMessage(getString(this, (((user + "_") + posNum) + kUserPositionPool)), "Unknown position")
1012- let borrowAmount = getIntegerValue(this, (((((pool + "_") + user) + "_") + posNum) + kUserBorrowAmount))
1013- let userCanWithdraw = userCanWithdrawShareCalc(user, pool, posNum, (borrowAmount > 0))
1014- $Tuple2(nil, userCanWithdraw)
1015- }
1016-
1017-
1018-
1019-@Callable(i)
1020-func getUserPositionInDollarsREADONLY (user,pools,posNum) = {
1021- func userPos (a,pool) = {
1022- let $t03088730921 = a
1023- let totalPos = $t03088730921._1
1024- let posDebt = $t03088730921._2
1025- let index = $t03088730921._3
1026- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Pool is not inited")
1027- let $t03101331103 = getPoolData(Address(fromBase58String(pool)), pType)
1028- let AId = $t03101331103._1
1029- let BId = $t03101331103._2
1030- let balA = $t03101331103._3
1031- let balB = $t03101331103._4
1032- let shareId = $t03101331103._5
1033- let borrowAmount = valueOrElse(getInteger(this, (((((pool + "_") + user) + "_") + posNum[index]) + kUserBorrowAmount)), 0)
1034- let userCanWithdraw = userCanWithdrawShareCalc(user, pool, posNum[index], (borrowAmount > 0))
1035- let sharePrices = getSharePrice(shareId)
1036- let decPrShare = pow(10, 0, getAssetDecimals(shareId), 0, 0, DOWN)
1037- let shareD = fraction(userCanWithdraw, sharePrices, decPrShare)
1038- if ((borrowAmount == 0))
1039- then $Tuple3((totalPos :+ shareD), (posDebt :+ 0), (index + 1))
1040- else {
1041- let borrowAsset = getStringValue(this, (((((pool + "_") + user) + "_") + posNum[index]) + kUserBorrowAssetId))
1042- let debt = {
1043- let @ = invoke(getLendSrvAddr(), "getAssetDebt", [false, ((user + "_") + posNum[index]), borrowAsset], nil)
1044- if ($isInstanceOf(@, "Int"))
1045- then @
1046- else throw(($getType(@) + " couldn't be cast to Int"))
1047- }
1048- let borrowAssetPrice = ( let @ = invoke(priceOracleAddr, "getTWAP60", [borrowAsset, false], nil)
1049- if ($isInstanceOf(@, "(Int, Int)"))
1050- then @
1051- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._1
1052- let decPrBorrowId = pow(10, 0, getAssetDecimals(borrowAsset), 0, 0, DOWN)
1053- let debtD = fraction(debt, borrowAssetPrice, decPrBorrowId)
1054- $Tuple3((totalPos :+ shareD), (posDebt :+ debtD), (index + 1))
1055- }
1056- }
1057-
1058- let $t03214632201 = {
1059- let $l = pools
1060- let $s = size($l)
1061- let $acc0 = $Tuple3(nil, nil, 0)
1062- func $f0_1 ($a,$i) = if (($i >= $s))
1063- then $a
1064- else userPos($a, $l[$i])
1065-
1066- func $f0_2 ($a,$i) = if (($i >= $s))
1067- then $a
1068- else throw("List size exceeds 20")
1069-
1070- $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($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), 11), 12), 13), 14), 15), 16), 17), 18), 19), 20)
1071- }
1072- let pos = $t03214632201._1
1073- let debt = $t03214632201._2
1074- $Tuple2(nil, $Tuple2(pos, debt))
1075- }
1076-
1077-
1078-
1079-@Callable(i)
1080-func replenish (pool,leverage,borrowId) = if (if ((100 > leverage))
1081- then true
1082- else (leverage > 300))
1083- then throw("Leverage can't be <100 and >300")
1084- else {
1085- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Pool is not inited")
1086- let $t03252432614 = getPoolData(Address(fromBase58String(pool)), pType)
1087- let AId = $t03252432614._1
1088- let BId = $t03252432614._2
1089- let balA = $t03252432614._3
1090- let balB = $t03252432614._4
1091- let shareId = $t03252432614._5
1092- let $t03261733254 = if ((size(i.payments) == 2))
1093- then if ((assetIdToStr(i.payments[0].assetId) != AId))
1094- then throw("Wrong payment asset A")
1095- else if ((assetIdToStr(i.payments[1].assetId) != BId))
1096- then throw("Wrong payment asset B")
1097- else $Tuple4(i.payments[0].amount, AId, i.payments[1].amount, BId)
1098- else if ((size(i.payments) == 1))
1099- then if ((assetIdToStr(i.payments[0].assetId) == AId))
1100- then $Tuple4(i.payments[0].amount, AId, 0, BId)
1101- else if ((assetIdToStr(i.payments[0].assetId) == BId))
1102- then $Tuple4(0, AId, i.payments[0].amount, BId)
1103- else throw("Wrong payment")
1104- else throw("One or two payments expected")
1105- let pmtA = $t03261733254._1
1106- let pmtAssetA = $t03261733254._2
1107- let pmtB = $t03261733254._3
1108- let pmtAssetB = $t03261733254._4
1109- let newPosNum = getNewUserPositionNumber(toString(i.caller))
1110- if ((leverage > 100))
1111- then {
1112- let borrowAmount = calcBorrowAmount(pmtA, pmtB, pmtAssetA, pmtAssetB, leverage, borrowId)
1113- let request = makeString([toString(i.caller), pool, toString(pmtA), pmtAssetA, toString(pmtB), pmtAssetB, toString(balA), toString(balB), shareId, borrowId, toString(borrowAmount)], ",")
1114- let newRequestId = {
1115- let @ = invoke(this, "createNewRequest", [request], nil)
1116- if ($isInstanceOf(@, "Int"))
1117- then @
1118- else throw(($getType(@) + " couldn't be cast to Int"))
1119- }
1120- if ((newRequestId == newRequestId))
1121- then {
1122- let args = [((toString(i.caller) + "_") + toString(newPosNum)), shareId, borrowId, borrowAmount, toString(this), "replenishFromLand", toString(valueOrErrorMessage(newRequestId, "Can't create new request"))]
1123- let inv = reentrantInvoke(getLendSrvAddr(), "flashPosition", args, nil)
1124- if ((inv == inv))
1125- then nil
1126- else throw("Strict value is not equal to itself.")
1127- }
1128- else throw("Strict value is not equal to itself.")
1129- }
1130- else {
1131- let $t03418234294 = replenishByType(pType, pool, NO_LOAN_FEE, pmtA, AId, pmtB, BId, balA, balB, shareId)
1132- let userStaked = $t03418234294._1
1133- let axlyFee = $t03418234294._2
1134- (replenishEntries(pool, toString(i.caller), userStaked, axlyFee, newPosNum, shareId, pType, false) ++ getCursEntries(AId, BId, shareId))
1135- }
1136- }
1137-
1138-
1139-
1140-@Callable(i)
1141-func withdraw (pool,posId) = withdrawToUser(toString(i.caller), pool, toString(posId), false)
1142-
1143-
1144-
1145-@Callable(i)
1146-func createUpdateStopLoss (posId,poolId,assetId,price) = {
1147- let tokenOraclePrice = ( let @ = invoke(priceOracleAddr, "getTWAP60", [assetId, false], nil)
1148- if ($isInstanceOf(@, "(Int, Int)"))
1149- then @
1150- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._1
1151- if (!(isDefined(getInteger(this, (((((poolId + "_") + toString(i.caller)) + "_") + toString(posId)) + kUserPosition)))))
1152- then throw("There are no user position")
1153- else if ((0 >= price))
1154- then throw("Price must be greater than 0")
1155- else if ((price > tokenOraclePrice))
1156- then throw("Price must be less than current token price")
1157- else [IntegerEntry((((((((toString(i.caller) + "_") + toString(posId)) + "_") + poolId) + "_") + assetId) + kUserStopLoss), price)]
1158- }
1159-
1160-
1161-
1162-@Callable(i)
1163-func deleteStopLoss (posId,poolId,assetId) = if (!(isDefined(getInteger(this, (((((((toString(i.caller) + "_") + toString(posId)) + "_") + poolId) + "_") + assetId) + kUserStopLoss)))))
1164- then throw("No entry")
1165- else [DeleteEntry((((((((toString(i.caller) + "_") + toString(posId)) + "_") + poolId) + "_") + assetId) + kUserStopLoss))]
1166-
1167-
1168-
1169-@Callable(i)
1170-func createNewRequest (params) = valueOrElse(isSelfCall(i), {
1171- let newRequestId = (valueOrElse(getInteger(this, kRequestIter), 0) + 1)
1172- $Tuple2([StringEntry((toString(newRequestId) + kRequestId), params), IntegerEntry(kRequestIter, newRequestId)], newRequestId)
1173- })
1174-
1175-
1176-
1177-@Callable(i)
1178-func replenishFromLand (requestId) = valueOrElse(isLandCall(i), {
1179- let $t03604736151 = parseRequest(requestId)
1180- let user = $t03604736151._1
1181- let pool = $t03604736151._2
1182- let pmtA = $t03604736151._3
1183- let AId = $t03604736151._4
1184- let pmtB = $t03604736151._5
1185- let BId = $t03604736151._6
1186- let balA = $t03604736151._7
1187- let balB = $t03604736151._8
1188- let shareId = $t03604736151._9
1189- let bwAsset = $t03604736151._10
1190- let bwAmount = $t03604736151._11
1191- if ((size(i.payments) != 1))
1192- then throw("Wrong payment size")
1193- else if (if ((assetIdToStr(i.payments[0].assetId) != bwAsset))
1194- then true
1195- else (i.payments[0].amount != bwAmount))
1196- then throw("Wrong payment")
1197- else {
1198- let $t03634136465 = if ((AId == bwAsset))
1199- then $Tuple2((pmtA + i.payments[0].amount), pmtB)
1200- else $Tuple2(pmtA, (pmtB + i.payments[0].amount))
1201- let pmtAllA = $t03634136465._1
1202- let pmtAllB = $t03634136465._2
1203- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Unknown pool")
1204- let $t03654736662 = replenishByType(pType, pool, LOAN_FEE, pmtAllA, AId, pmtAllB, BId, balA, balB, shareId)
1205- let userStaked = $t03654736662._1
1206- let axlyFee = $t03654736662._2
1207- let posNum = getNewUserPositionNumber(user)
1208- let borrowEntries = [IntegerEntry((((((pool + "_") + user) + "_") + toString(posNum)) + kUserBorrowAmount), bwAmount), StringEntry((((((pool + "_") + user) + "_") + toString(posNum)) + kUserBorrowAssetId), bwAsset)]
1209- let entries = replenishEntries(pool, user, userStaked, axlyFee, posNum, shareId, pType, true)
1210- $Tuple2((((entries ++ getCursEntries(AId, BId, shareId)) ++ borrowEntries) :+ DeleteEntry((requestId + kRequestId))), userStaked)
1211- }
1212- })
1213-
1214-
1215-
1216-@Callable(i)
1217-func liquidate (user,posId,liquidateAmount) = valueOrElse(isLandCall(i), {
1218- let pool = valueOrErrorMessage(getString(this, (((user + "_") + posId) + kUserPositionPool)), "no position")
1219- let pType = valueOrErrorMessage(getString(this, (kPool + pool)), "Pool is not inited")
1220- let $t03744137531 = getPoolData(Address(fromBase58String(pool)), pType)
1221- let AId = $t03744137531._1
1222- let BId = $t03744137531._2
1223- let balA = $t03744137531._3
1224- let balB = $t03744137531._4
1225- let shareId = $t03744137531._5
1226- let amount = unstakeLP(pool, pType, shareId, liquidateAmount)
1227- let borrowAmount = getIntegerValue(this, (((((pool + "_") + user) + "_") + posId) + kUserBorrowAmount))
1228- let borrowAsset = getStringValue(this, (((((pool + "_") + user) + "_") + posId) + kUserBorrowAssetId))
1229- let userCanWithdraw = userCanWithdrawShareCalc(user, pool, posId, (borrowAmount > 0))
1230- if ((borrowAmount == 0))
1231- then throw("You can't liquidate position without borrow")
1232- else [IntegerEntry((((((pool + "_") + user) + "_") + posId) + kUserPositionInterest), getIntegerValue(this, (pool + kPoolInterestLoan))), IntegerEntry((pool + kPoolTotalLoan), (getPoolTotalShareWithLoan(pool) - liquidateAmount)), IntegerEntry((pool + kPoolTotal), (getPoolTotalShare(pool) - liquidateAmount)), IntegerEntry((((((pool + "_") + user) + "_") + posId) + kUserPosition), (userCanWithdraw - liquidateAmount)), ScriptTransfer(i.caller, amount, fromBase58String(shareId))]
1233- })
1234-
1235-
1236-
1237-@Callable(i)
1238-func stopLoss (user,posId,pool,assetId) = valueOrElse(isAdminCall(i), {
1239- let tokenOraclePrice = ( let @ = invoke(priceOracleAddr, "getTWAP60", [assetId, false], nil)
1240- if ($isInstanceOf(@, "(Int, Int)"))
1241- then @
1242- else throw(($getType(@) + " couldn't be cast to (Int, Int)")))._1
1243- if (!(isDefined(getInteger(this, (((((((user + "_") + toString(posId)) + "_") + pool) + "_") + assetId) + kUserStopLoss)))))
1244- then throw("No entry")
1245- else (withdrawToUser(toString(i.caller), pool, toString(posId), true) :+ DeleteEntry((((((((user + "_") + toString(posId)) + "_") + pool) + "_") + assetId) + kUserStopLoss)))
1246- })
1247-
1248-
1249-
1250-@Callable(i)
1251-func capitalizeExKeeper (pool,type,tokenToId,amountToExchange,claim,amountsIn,addresses,assetsToReceive,estReceived,slippageTolerance,minReceived,options) = valueOrElse(isAdminCall(i), {
1252- let $t03931739511 = if (claim)
1253- then claimFarmed(type, pool)
1254- else {
1255- let claimedAsset = if ((type == SF_POOL))
1256- then SWOPID
1257- else WXID
1258- $Tuple2(amountToExchange, claimedAsset)
1259- }
1260- let claimedAmount = $t03931739511._1
1261- let claimedAsset = $t03931739511._2
1262- let exchangedAmount = exchangeKeeper(tokenToId, amountToExchange, claimedAsset, amountsIn, addresses, assetsToReceive, estReceived, slippageTolerance, minReceived, options)
1263- let change = (claimedAmount - amountToExchange)
1264- let changeEntry = if ((change > 0))
1265- then [IntegerEntry((pool + kPoolCapChange), (change + valueOrElse(getInteger(this, (pool + kPoolCapChange)), 0)))]
1266- else nil
1267- (capitalize(pool, type, tokenToId, exchangedAmount) ++ changeEntry)
1268- })
1269-
1270-
1271-
1272-@Callable(i)
1273-func capitalizeExPuzzle (pool,type,tokenToId,amountToExchange,claim,routesStr,minToReceive,options) = valueOrElse(isAdminCall(i), {
1274- let $t04019640390 = if (claim)
1275- then claimFarmed(type, pool)
1276- else {
1277- let claimedAsset = if ((type == SF_POOL))
1278- then SWOPID
1279- else WXID
1280- $Tuple2(amountToExchange, claimedAsset)
1281- }
1282- let claimedAmount = $t04019640390._1
1283- let claimedAsset = $t04019640390._2
1284- let exchangedAmount = exchangePuzzle(tokenToId, amountToExchange, claimedAsset, routesStr, minToReceive, options)
1285- let change = (claimedAmount - amountToExchange)
1286- let changeEntry = if ((change > 0))
1287- then [IntegerEntry((pool + kPoolCapChange), (change + valueOrElse(getInteger(this, (pool + kPoolCapChange)), 0)))]
1288- else nil
1289- (capitalize(pool, type, tokenToId, exchangedAmount) ++ changeEntry)
1290- })
1291-
1292-
1293-
1294-@Callable(i)
1295-func capitalizeExSwopFi (pool,type,tokenToId,amountToExchange,claim,exchangers,exchangersType,args1,args2,routingAssetsKeys,minAmountToReceive,options) = valueOrElse(isAdminCall(i), {
1296- let $t04114141335 = if (claim)
1297- then claimFarmed(type, pool)
1298- else {
1299- let claimedAsset = if ((type == SF_POOL))
1300- then SWOPID
1301- else WXID
1302- $Tuple2(amountToExchange, claimedAsset)
1303- }
1304- let claimedAmount = $t04114141335._1
1305- let claimedAsset = $t04114141335._2
1306- let exchangedAmount = exchangeSwopFi(tokenToId, amountToExchange, claimedAsset, exchangers, exchangersType, args1, args2, routingAssetsKeys, minAmountToReceive, options)
1307- let change = (claimedAmount - amountToExchange)
1308- let changeEntry = if ((change > 0))
1309- then [IntegerEntry((pool + kPoolCapChange), (change + valueOrElse(getInteger(this, (pool + kPoolCapChange)), 0)))]
1310- else nil
1311- (capitalize(pool, type, tokenToId, exchangedAmount) ++ changeEntry)
1312- })
1313-
1314-
1315-
1316-@Callable(i)
1317-func initNewPool (type,poolAddr,inFeeNoLoan,inFeeLoan,capFeeNoLoan,capFeeWithLoan,stoplossFeeNoLoan,stoplossFeeWithLoan) = valueOrElse(isAdminCall(i), if (if ((type != SF_POOL))
1318- then (type != WX_POOL)
1319- else false)
1320- then throw("Wrong type")
1321- else {
1322- let $t04205642150 = getPoolData(Address(fromBase58String(poolAddr)), type)
1323- let aId = $t04205642150._1
1324- let bId = $t04205642150._2
1325- let aBal = $t04205642150._3
1326- let bBal = $t04205642150._4
1327- let shareId = $t04205642150._5
1328-[IntegerEntry((poolAddr + kAxlyInFeeWithoutLoan), inFeeNoLoan), IntegerEntry((poolAddr + kAxlyInFeeWithLoan), inFeeLoan), IntegerEntry((poolAddr + kAxlyNoLoanCapFee), capFeeNoLoan), IntegerEntry((poolAddr + kAxlyWithLoanCapFee), capFeeWithLoan), IntegerEntry((poolAddr + kAxlyStopLossNoLoanFee), stoplossFeeNoLoan), IntegerEntry((poolAddr + kAxlyStopLossLoanFee), stoplossFeeWithLoan), IntegerEntry((poolAddr + kPoolInterestLoan), 0), IntegerEntry((poolAddr + kPoolInterestNoLoan), 0), StringEntry((kPool + poolAddr), type), StringEntry((shareId + kSharePool), poolAddr)]
1329- })
1330-
1331-
1332-@Verifier(tx)
1333-func verify () = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
1334-
1+# no script

github/deemru/w8io/3ef1775 
84.36 ms