tx · 87Ad4gaY8CJfcw9hyN9Eirruc1SjrDK78eA5SpzigvsQ

3N8ZpuFLVKwxbpVFe1Pcs14hLCrujizpQsa:  -0.01000000 Waves

2023.06.15 17:48 [2623897] smart account 3N8ZpuFLVKwxbpVFe1Pcs14hLCrujizpQsa > SELF 0.00000000 Waves

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

github/deemru/w8io/3ef1775 
87.18 ms