tx · 4b3HoiU2gePZJeFgYUMUdzvUnVEynZhXB2ZCPpLxQnuV

3N1FWHMYZ9QJDkUaMPxexydkBqiUxEk3J6o:  -0.01400000 Waves

2020.11.10 10:56 [1258966] smart account 3N1FWHMYZ9QJDkUaMPxexydkBqiUxEk3J6o > SELF 0.00000000 Waves

{ "type": 13, "id": "4b3HoiU2gePZJeFgYUMUdzvUnVEynZhXB2ZCPpLxQnuV", "fee": 1400000, "feeAssetId": null, "timestamp": 1604995000929, "version": 1, "sender": "3N1FWHMYZ9QJDkUaMPxexydkBqiUxEk3J6o", "senderPublicKey": "oH6E3DnZcMibsYC4VB7KvfemJfz3koDVwZ5Z3pXGpCN", "proofs": [ "5SyjKQV7XjvgomYxHARwoMBoGEMYHkGgJngZXPf7mKVnN8SeG3y6djykaHKAGkaZMawc5zCn9jjaXUq1BqH16qa3" ], "script": "base64:AAIEAAAAAAAAAAwIAhIAEgASBAoCAQEAAAATAAAAAARVU0RUAgAAACw1U2g5S2doZmtaeWhqd3VvZG92RGhCNlBnaERVR0JIaUFQWjRNa3JQZ0t0WAAAAAAEVVNETgIAAAAsMjVGRXFFalJrcUs2eUNraVQ3THo2U0FZejdnVUZDdHhmQ0NobnJWRkQ1QVQAAAAABmlkVVNETgkAAlkAAAABBQAAAARVU0ROAAAAAAZpZFVTRFQJAAJZAAAAAQUAAAAEVVNEVAAAAAANd2l0aGRyYXdEZWxheQAAAAAAAAAAAgAAAAAPZGVjaW1hbFBhcnRVU0ROAAAAAAAAD0JAAAAAAA9kZWNpbWFsUGFydFVTRFQAAAAAAAAPQkAAAAAAF21pbmltYWxBbW91bnRUb1N3YXBVU0RUCQAAaAAAAAIAAAAAAAAAJxAFAAAAD2RlY2ltYWxQYXJ0VVNETgAAAAAXbWluaW1hbEFtb3VudFRvU3dhcFVTRE4JAABoAAAAAgAAAAAAAAAnEAUAAAAPZGVjaW1hbFBhcnRVU0RUAQAAAApjb2xsZWN0S2V5AAAAAwAAAAdhZGRyZXNzAAAAE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pcwQAAAAKdG9XaXRoZHJhdwkBAAAAF2dldExpc3RFbGVtZW50T3JEZWZhdWx0AAAAAwkABLUAAAACBQAAAA13aXRoZHJhd25JbmZvAgAAAAFfAAAAAAAAAAACCQABpAAAAAEFAAAABmFtb3VudAMDCQAAZwAAAAIFAAAABmhlaWdodAUAAAALaGVpZ2h0SW5LZXkJAQAAAAhjb250YWlucwAAAAIFAAAADXdpdGhkcmF3bkluZm8CAAAADW5vdF93aXRoZHJhd24HCQAETAAAAAIJAQAAAAtEZWxldGVFbnRyeQAAAAEFAAAAA2tleQkABEwAAAACCQEAAAAOU2NyaXB0VHJhbnNmZXIAAAADCAUAAAAGaW52b2tlAAAABmNhbGxlcgkBAAAAE3ZhbHVlT3JFcnJvck1lc3NhZ2UAAAACCQAEtgAAAAEFAAAACnRvV2l0aGRyYXcCAAAAFFdyb25nIHdpdGhkcmF3IGRhdGEhBQAAAAZpZFVTRFQFAAAAA25pbAkAAAIAAAABCQABLAAAAAIJAAEsAAAAAgIAAAAdV2l0aGRyYXdhbCBpcyBwb3NzaWJsZSBhZnRlciAJAAGkAAAAAQUAAAALaGVpZ2h0SW5LZXkCAAAAKyBoZWlnaHQgb3IgeW91IGhhdmUgYWxyZWFkeSB3aXRoZHJhd24gVVNEVC4AAAAAAG/iKg==", "chainId": 84, "height": 1258966, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: 3ME835z4bBUvt9SCcmSsNdsy4TS7V5M5i6Fa6eDuyv3i Next: none Diff:
OldNewDifferences
1-{-# STDLIB_VERSION 3 #-}
1+{-# STDLIB_VERSION 4 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
44 let USDT = "5Sh9KghfkZyhjwuodovDhB6PghDUGBHiAPZ4MkrPgKtX"
1919
2020 let minimalAmountToSwapUSDN = (10000 * decimalPartUSDT)
2121
22-func collectKey (address,amountToSwapInKey,allowToWithdrawHeight) = ((((toString(address) + "_WITHDRAW_") + toString(amountToSwapInKey)) + "_HEIGHT_") + toString(allowToWithdrawHeight))
22+func collectKey (address,amountToSwapInKey,allowToWithdrawHeight) = makeString([address, "WITHDRAW", toString(amountToSwapInKey), "HEIGHT", toString(allowToWithdrawHeight)], "_")
2323
2424
25-@Callable(contextObj)
25+func collectBalanceKey (address) = makeString([address, "balance"], "_")
26+
27+
28+func collectBalanceKeyWithFee (address) = makeString([address, "balance_with_fee"], "_")
29+
30+
31+func usdtToUsdnAssetError () = throw("To swap USDT to USDN you need attach payment in USDT")
32+
33+
34+func usdnToUsdtAssetError () = throw("To swap USDN to USDT you need attach payment in USDN")
35+
36+
37+func usdtToUsdnLowLimitError () = throw((("you can swap at least " + toString((minimalAmountToSwapUSDT / decimalPartUSDT))) + " tokens"))
38+
39+
40+func abs (n) = if ((0 > n))
41+ then (n * -1)
42+ else n
43+
44+
45+func getWithoutFeeWithdraw (amount,balanceWithoutFee) = min([amount, balanceWithoutFee])
46+
47+
48+func getListElementOrDefault (list,index,default) = if ((size(list) > index))
49+ then list[index]
50+ else default
51+
52+
53+func currentWithdrawals (address,amount) = {
54+ let balanceKey = collectBalanceKey(address)
55+ let balanceWithFeeKey = collectBalanceKeyWithFee(address)
56+ let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0)
57+ let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0)
58+ let userBalance = (currentClientBalance + currentClientBalanceWithFee)
59+ if (if ((amount > userBalance))
60+ then true
61+ else (minimalAmountToSwapUSDN > amount))
62+ then throw("Swap amount fail, amount is to small or to big.")
63+ else {
64+ let toWithdrawWithoutFee = getWithoutFeeWithdraw(amount, currentClientBalance)
65+ let toWithdrawWithFee = (amount - toWithdrawWithoutFee)
66+ let fee = ((toWithdrawWithFee / 100) * 1)
67+[toWithdrawWithoutFee, toWithdrawWithFee, fee]
68+ }
69+ }
70+
71+
72+@Callable(invoke)
2673 func swapUSDTtoUSDN () = {
27- let payment = match contextObj.payment {
28- case p: AttachedPayment =>
29- p
30- case _ =>
31- throw("Payment not attached")
32- }
33- let assetIdReceived = payment.assetId
34- let tokenReceiveAmount = payment.amount
35- let userAddress = contextObj.caller
36- let currentClientBalanceUSDT = match getInteger(this, (toString(userAddress) + "_balance")) {
37- case currentBalance: Int =>
38- value(getInteger(this, (toString(userAddress) + "_balance")))
39- case _ =>
40- 0
41- }
42- if ((assetIdReceived != idUSDT))
43- then throw("to swap USDT to USDN you need attach payment in USDT")
44- else if ((minimalAmountToSwapUSDT > tokenReceiveAmount))
45- then throw((("you can swap at least " + toString((minimalAmountToSwapUSDT / decimalPartUSDT))) + " tokens"))
46- else ScriptResult(WriteSet([DataEntry((toString(contextObj.caller) + "_balance"), (currentClientBalanceUSDT + tokenReceiveAmount))]), TransferSet([ScriptTransfer(contextObj.caller, tokenReceiveAmount, idUSDN)]))
74+ let balanceKey = collectBalanceKeyWithFee(toString(invoke.caller))
75+ let payment = value(invoke.payments[0])
76+ if ((payment.assetId != idUSDT))
77+ then usdtToUsdnAssetError()
78+ else {
79+ let amount = payment.amount
80+ let fee = ((amount / 100) * 1)
81+ let currentClientBalanceUSDT = valueOrElse(getInteger(this, balanceKey), 0)
82+ if ((minimalAmountToSwapUSDT > amount))
83+ then usdtToUsdnLowLimitError()
84+ else [IntegerEntry(balanceKey, (currentClientBalanceUSDT + amount)), ScriptTransfer(invoke.caller, (amount - fee), idUSDN)]
85+ }
4786 }
4887
4988
5089
51-@Callable(contextObj)
90+@Callable(invoke)
5291 func initializationUSDNtoUSDTswap () = {
53- let payment = match contextObj.payment {
54- case p: AttachedPayment =>
55- p
56- case _ =>
57- throw("Payment not attached")
58- }
59- let assetIdReceived = payment.assetId
60- let paymentAmount = payment.amount
61- let userAddress = contextObj.caller
62- let currentClientBalanceUSDT = match getInteger(this, (toString(userAddress) + "_balance")) {
63- case currentBalance: Int =>
64- getIntegerValue(this, (toString(userAddress) + "_balance"))
65- case _ =>
66- 0
67- }
68- if ((assetIdReceived != idUSDN))
69- then throw("to swap USDN to USDT you need attach payment in USDN")
70- else if (if ((paymentAmount > currentClientBalanceUSDT))
71- then true
72- else (minimalAmountToSwapUSDN > paymentAmount))
73- then throw("swap amount fail")
74- else {
75- let allowToWithDrawHeight = (height + withdrawDelay)
76- if (isDefined(getString(this, collectKey(contextObj.caller, paymentAmount, allowToWithDrawHeight))))
77- then throw("you have already started withdrawal process, wait next block")
78- else ScriptResult(WriteSet([DataEntry((toString(contextObj.caller) + "_balance"), (currentClientBalanceUSDT - paymentAmount)), DataEntry(collectKey(contextObj.caller, paymentAmount, allowToWithDrawHeight), "not_withdrawn")]), TransferSet(nil))
79- }
92+ let payment = value(invoke.payments[0])
93+ let address = toString(invoke.caller)
94+ if ((payment.assetId != idUSDN))
95+ then usdnToUsdtAssetError()
96+ else {
97+ let amount = payment.amount
98+ let balanceKey = collectBalanceKey(address)
99+ let balanceWithFeeKey = collectBalanceKeyWithFee(address)
100+ let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0)
101+ let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0)
102+ let userBalance = (currentClientBalance + currentClientBalanceWithFee)
103+ let data = currentWithdrawals(toString(invoke.caller), amount)
104+ let toWithdrawWithoutFee = valueOrErrorMessage(data[0], "Wrong 'currentWithdrawals' method!")
105+ let toWithdrawWithFee = valueOrErrorMessage(data[1], "Wrong 'currentWithdrawals' method!")
106+ let fee = valueOrErrorMessage(data[2], "Wrong 'currentWithdrawals' method!")
107+ let total = ((toWithdrawWithFee + toWithdrawWithoutFee) - fee)
108+ let allowToWithDrawHeight = (height + withdrawDelay)
109+ if (isDefined(getString(this, collectKey(toString(invoke.caller), amount, allowToWithDrawHeight))))
110+ then throw("You have already started withdrawal process, wait next block.")
111+ else [IntegerEntry(balanceKey, (currentClientBalance - toWithdrawWithoutFee)), IntegerEntry(balanceWithFeeKey, (currentClientBalanceWithFee - toWithdrawWithFee)), StringEntry(collectKey(toString(invoke.caller), amount, allowToWithDrawHeight), makeString(["not_withdrawn", toString((amount - fee))], "_"))]
112+ }
80113 }
81114
82115
83116
84-@Callable(contextObj)
85-func swapUSDNtoUSDT (amountToSwapInKey,heightInKey) = {
86- let key = collectKey(contextObj.caller, amountToSwapInKey, heightInKey)
87- let withdrawnInfo = match getString(this, key) {
88- case withdrawnInfo: String =>
89- getStringValue(this, key)
90- case _ =>
91- throw(((("this key doesn't exists " + key) + " at address ") + toString(this)))
92- }
117+@Callable(invoke)
118+func swapUSDNtoUSDT (amount,heightInKey) = {
119+ let key = collectKey(toString(invoke.caller), amount, heightInKey)
120+ let withdrawnInfo = valueOrErrorMessage(getString(this, key), ((("This key doesn't exists " + key) + " at address ") + toString(this)))
121+ let toWithdraw = getListElementOrDefault(split(withdrawnInfo, "_"), 2, toString(amount))
93122 if (if ((height >= heightInKey))
94- then (withdrawnInfo == "not_withdrawn")
123+ then contains(withdrawnInfo, "not_withdrawn")
95124 else false)
96- then ScriptResult(WriteSet([DataEntry(key, "withdrawn")]), TransferSet([ScriptTransfer(contextObj.caller, amountToSwapInKey, idUSDT)]))
97- else throw((("withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn USDT"))
125+ then [DeleteEntry(key), ScriptTransfer(invoke.caller, valueOrErrorMessage(parseInt(toWithdraw), "Wrong withdraw data!"), idUSDT)]
126+ else throw((("Withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn USDT."))
98127 }
99128
100129
Full:
OldNewDifferences
1-{-# STDLIB_VERSION 3 #-}
1+{-# STDLIB_VERSION 4 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
44 let USDT = "5Sh9KghfkZyhjwuodovDhB6PghDUGBHiAPZ4MkrPgKtX"
55
66 let USDN = "25FEqEjRkqK6yCkiT7Lz6SAYz7gUFCtxfCChnrVFD5AT"
77
88 let idUSDN = fromBase58String(USDN)
99
1010 let idUSDT = fromBase58String(USDT)
1111
1212 let withdrawDelay = 2
1313
1414 let decimalPartUSDN = 1000000
1515
1616 let decimalPartUSDT = 1000000
1717
1818 let minimalAmountToSwapUSDT = (10000 * decimalPartUSDN)
1919
2020 let minimalAmountToSwapUSDN = (10000 * decimalPartUSDT)
2121
22-func collectKey (address,amountToSwapInKey,allowToWithdrawHeight) = ((((toString(address) + "_WITHDRAW_") + toString(amountToSwapInKey)) + "_HEIGHT_") + toString(allowToWithdrawHeight))
22+func collectKey (address,amountToSwapInKey,allowToWithdrawHeight) = makeString([address, "WITHDRAW", toString(amountToSwapInKey), "HEIGHT", toString(allowToWithdrawHeight)], "_")
2323
2424
25-@Callable(contextObj)
25+func collectBalanceKey (address) = makeString([address, "balance"], "_")
26+
27+
28+func collectBalanceKeyWithFee (address) = makeString([address, "balance_with_fee"], "_")
29+
30+
31+func usdtToUsdnAssetError () = throw("To swap USDT to USDN you need attach payment in USDT")
32+
33+
34+func usdnToUsdtAssetError () = throw("To swap USDN to USDT you need attach payment in USDN")
35+
36+
37+func usdtToUsdnLowLimitError () = throw((("you can swap at least " + toString((minimalAmountToSwapUSDT / decimalPartUSDT))) + " tokens"))
38+
39+
40+func abs (n) = if ((0 > n))
41+ then (n * -1)
42+ else n
43+
44+
45+func getWithoutFeeWithdraw (amount,balanceWithoutFee) = min([amount, balanceWithoutFee])
46+
47+
48+func getListElementOrDefault (list,index,default) = if ((size(list) > index))
49+ then list[index]
50+ else default
51+
52+
53+func currentWithdrawals (address,amount) = {
54+ let balanceKey = collectBalanceKey(address)
55+ let balanceWithFeeKey = collectBalanceKeyWithFee(address)
56+ let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0)
57+ let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0)
58+ let userBalance = (currentClientBalance + currentClientBalanceWithFee)
59+ if (if ((amount > userBalance))
60+ then true
61+ else (minimalAmountToSwapUSDN > amount))
62+ then throw("Swap amount fail, amount is to small or to big.")
63+ else {
64+ let toWithdrawWithoutFee = getWithoutFeeWithdraw(amount, currentClientBalance)
65+ let toWithdrawWithFee = (amount - toWithdrawWithoutFee)
66+ let fee = ((toWithdrawWithFee / 100) * 1)
67+[toWithdrawWithoutFee, toWithdrawWithFee, fee]
68+ }
69+ }
70+
71+
72+@Callable(invoke)
2673 func swapUSDTtoUSDN () = {
27- let payment = match contextObj.payment {
28- case p: AttachedPayment =>
29- p
30- case _ =>
31- throw("Payment not attached")
32- }
33- let assetIdReceived = payment.assetId
34- let tokenReceiveAmount = payment.amount
35- let userAddress = contextObj.caller
36- let currentClientBalanceUSDT = match getInteger(this, (toString(userAddress) + "_balance")) {
37- case currentBalance: Int =>
38- value(getInteger(this, (toString(userAddress) + "_balance")))
39- case _ =>
40- 0
41- }
42- if ((assetIdReceived != idUSDT))
43- then throw("to swap USDT to USDN you need attach payment in USDT")
44- else if ((minimalAmountToSwapUSDT > tokenReceiveAmount))
45- then throw((("you can swap at least " + toString((minimalAmountToSwapUSDT / decimalPartUSDT))) + " tokens"))
46- else ScriptResult(WriteSet([DataEntry((toString(contextObj.caller) + "_balance"), (currentClientBalanceUSDT + tokenReceiveAmount))]), TransferSet([ScriptTransfer(contextObj.caller, tokenReceiveAmount, idUSDN)]))
74+ let balanceKey = collectBalanceKeyWithFee(toString(invoke.caller))
75+ let payment = value(invoke.payments[0])
76+ if ((payment.assetId != idUSDT))
77+ then usdtToUsdnAssetError()
78+ else {
79+ let amount = payment.amount
80+ let fee = ((amount / 100) * 1)
81+ let currentClientBalanceUSDT = valueOrElse(getInteger(this, balanceKey), 0)
82+ if ((minimalAmountToSwapUSDT > amount))
83+ then usdtToUsdnLowLimitError()
84+ else [IntegerEntry(balanceKey, (currentClientBalanceUSDT + amount)), ScriptTransfer(invoke.caller, (amount - fee), idUSDN)]
85+ }
4786 }
4887
4988
5089
51-@Callable(contextObj)
90+@Callable(invoke)
5291 func initializationUSDNtoUSDTswap () = {
53- let payment = match contextObj.payment {
54- case p: AttachedPayment =>
55- p
56- case _ =>
57- throw("Payment not attached")
58- }
59- let assetIdReceived = payment.assetId
60- let paymentAmount = payment.amount
61- let userAddress = contextObj.caller
62- let currentClientBalanceUSDT = match getInteger(this, (toString(userAddress) + "_balance")) {
63- case currentBalance: Int =>
64- getIntegerValue(this, (toString(userAddress) + "_balance"))
65- case _ =>
66- 0
67- }
68- if ((assetIdReceived != idUSDN))
69- then throw("to swap USDN to USDT you need attach payment in USDN")
70- else if (if ((paymentAmount > currentClientBalanceUSDT))
71- then true
72- else (minimalAmountToSwapUSDN > paymentAmount))
73- then throw("swap amount fail")
74- else {
75- let allowToWithDrawHeight = (height + withdrawDelay)
76- if (isDefined(getString(this, collectKey(contextObj.caller, paymentAmount, allowToWithDrawHeight))))
77- then throw("you have already started withdrawal process, wait next block")
78- else ScriptResult(WriteSet([DataEntry((toString(contextObj.caller) + "_balance"), (currentClientBalanceUSDT - paymentAmount)), DataEntry(collectKey(contextObj.caller, paymentAmount, allowToWithDrawHeight), "not_withdrawn")]), TransferSet(nil))
79- }
92+ let payment = value(invoke.payments[0])
93+ let address = toString(invoke.caller)
94+ if ((payment.assetId != idUSDN))
95+ then usdnToUsdtAssetError()
96+ else {
97+ let amount = payment.amount
98+ let balanceKey = collectBalanceKey(address)
99+ let balanceWithFeeKey = collectBalanceKeyWithFee(address)
100+ let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0)
101+ let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0)
102+ let userBalance = (currentClientBalance + currentClientBalanceWithFee)
103+ let data = currentWithdrawals(toString(invoke.caller), amount)
104+ let toWithdrawWithoutFee = valueOrErrorMessage(data[0], "Wrong 'currentWithdrawals' method!")
105+ let toWithdrawWithFee = valueOrErrorMessage(data[1], "Wrong 'currentWithdrawals' method!")
106+ let fee = valueOrErrorMessage(data[2], "Wrong 'currentWithdrawals' method!")
107+ let total = ((toWithdrawWithFee + toWithdrawWithoutFee) - fee)
108+ let allowToWithDrawHeight = (height + withdrawDelay)
109+ if (isDefined(getString(this, collectKey(toString(invoke.caller), amount, allowToWithDrawHeight))))
110+ then throw("You have already started withdrawal process, wait next block.")
111+ else [IntegerEntry(balanceKey, (currentClientBalance - toWithdrawWithoutFee)), IntegerEntry(balanceWithFeeKey, (currentClientBalanceWithFee - toWithdrawWithFee)), StringEntry(collectKey(toString(invoke.caller), amount, allowToWithDrawHeight), makeString(["not_withdrawn", toString((amount - fee))], "_"))]
112+ }
80113 }
81114
82115
83116
84-@Callable(contextObj)
85-func swapUSDNtoUSDT (amountToSwapInKey,heightInKey) = {
86- let key = collectKey(contextObj.caller, amountToSwapInKey, heightInKey)
87- let withdrawnInfo = match getString(this, key) {
88- case withdrawnInfo: String =>
89- getStringValue(this, key)
90- case _ =>
91- throw(((("this key doesn't exists " + key) + " at address ") + toString(this)))
92- }
117+@Callable(invoke)
118+func swapUSDNtoUSDT (amount,heightInKey) = {
119+ let key = collectKey(toString(invoke.caller), amount, heightInKey)
120+ let withdrawnInfo = valueOrErrorMessage(getString(this, key), ((("This key doesn't exists " + key) + " at address ") + toString(this)))
121+ let toWithdraw = getListElementOrDefault(split(withdrawnInfo, "_"), 2, toString(amount))
93122 if (if ((height >= heightInKey))
94- then (withdrawnInfo == "not_withdrawn")
123+ then contains(withdrawnInfo, "not_withdrawn")
95124 else false)
96- then ScriptResult(WriteSet([DataEntry(key, "withdrawn")]), TransferSet([ScriptTransfer(contextObj.caller, amountToSwapInKey, idUSDT)]))
97- else throw((("withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn USDT"))
125+ then [DeleteEntry(key), ScriptTransfer(invoke.caller, valueOrErrorMessage(parseInt(toWithdraw), "Wrong withdraw data!"), idUSDT)]
126+ else throw((("Withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn USDT."))
98127 }
99128
100129

github/deemru/w8io/026f985 
35.33 ms