tx · 9Lf5GmCuun9BS4LwrKkw5dmLhePGexTRFDuwtAbp1jnn 3Mzj8MyE3E3G55SLVE6Dgd6ew2uERLxiE4U: -0.01300000 Waves 2022.10.07 11:51 [2261705] smart account 3Mzj8MyE3E3G55SLVE6Dgd6ew2uERLxiE4U > SELF 0.00000000 Waves
{ "type": 13, "id": "9Lf5GmCuun9BS4LwrKkw5dmLhePGexTRFDuwtAbp1jnn", "fee": 1300000, "feeAssetId": null, "timestamp": 1665132695675, "version": 1, "sender": "3Mzj8MyE3E3G55SLVE6Dgd6ew2uERLxiE4U", "senderPublicKey": "8E1ZoUTJyyA7ybXtP2NxUZKdAwvR1ruiQUxZD2bYJJMF", "proofs": [ "5MnY6MJEmjATauLHpG8csyVca9DmHc3C2D4N1UNXsQLpFz1dEMXPG5wRMYaGHA9wTJHdMLLUwiJpZ7xXCzeTHgoS" ], "script": "base64:AAIFAAAAAAAAAAkIAhIDCgEIEgAAAAAFAQAAABNrZXlNYW5hZ2VyUHVibGljS2V5AAAAAAIAAAAUJXNfX21hbmFnZXJQdWJsaWNLZXkBAAAAGmtleVBlbmRpbmdNYW5hZ2VyUHVibGljS2V5AAAAAAIAAAAbJXNfX3BlbmRpbmdNYW5hZ2VyUHVibGljS2V5AQAAABZtYW5hZ2VyUHVibGljS2V5T3JVbml0AAAAAAQAAAAHJG1hdGNoMAkABCIAAAABCQEAAAATa2V5TWFuYWdlclB1YmxpY0tleQAAAAADCQAAAQAAAAIFAAAAByRtYXRjaDACAAAABlN0cmluZwQAAAABcwUAAAAHJG1hdGNoMAkAAlkAAAABBQAAAAFzAwkAAAEAAAACBQAAAAckbWF0Y2gwAgAAAARVbml0BQAAAAR1bml0CQAAAgAAAAECAAAAC01hdGNoIGVycm9yAQAAAB1wZW5kaW5nTWFuYWdlclB1YmxpY0tleU9yVW5pdAAAAAAEAAAAByRtYXRjaDAJAAQiAAAAAQkBAAAAGmtleVBlbmRpbmdNYW5hZ2VyUHVibGljS2V5AAAAAAMJAAABA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MAUAAAACcGsDCQAAAQAAAAIFAAAAByRtYXRjaDACAAAABFVuaXQIBQAAAAJ0eAAAAA9zZW5kZXJQdWJsaWNLZXkJAAACAAAAAQIAAAALTWF0Y2ggZXJyb3IJAAH0AAAAAwgFAAAAAnR4AAAACWJvZHlCeXRlcwkAAZEAAAACCAUAAAACdHgAAAAGcHJvb2ZzAAAAAAAAAAAABQAAAA90YXJnZXRQdWJsaWNLZXmq8YBh", "chainId": 84, "height": 2261705, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: 9jHv5oDfMCDxpgAAs7dhEV46ZZx6TXN1qpXqFqX6p2av Next: D5UsCpu2cU7jm59pspp27MtvJpuzixgKH5q7cZsXtMaQ Diff:
Old | New | Differences | |
---|---|---|---|
1 | - | {-# STDLIB_VERSION | |
1 | + | {-# STDLIB_VERSION 5 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | - | let decimalPartUSDN = 1000000 | |
5 | - | ||
6 | - | let decimalPartUSDT = 1000000 | |
7 | - | ||
8 | - | func keyWithdrawDelay () = "%s__withdrawDelay" | |
9 | - | ||
10 | - | ||
11 | - | func withdrawDelay () = value(getInteger(keyWithdrawDelay())) | |
12 | - | ||
13 | - | ||
14 | - | func keyMinimalAmountToSwapUsdt () = "%s__minimalAmountToSwapUsdt" | |
15 | - | ||
16 | - | ||
17 | - | func keyMinimalAmountToSwapUsdn () = "%s__minimalAmountToSwapUsdn" | |
18 | - | ||
19 | - | ||
20 | - | func minUsdt () = value(getInteger(keyMinimalAmountToSwapUsdt())) | |
21 | - | ||
22 | - | ||
23 | - | func minUsdn () = value(getInteger(keyMinimalAmountToSwapUsdn())) | |
24 | - | ||
25 | - | ||
26 | 4 | func keyManagerPublicKey () = "%s__managerPublicKey" | |
27 | 5 | ||
28 | 6 | ||
29 | 7 | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" | |
30 | - | ||
31 | - | ||
32 | - | func keyUsdtAssetId () = "%s__usdtAssetId" | |
33 | - | ||
34 | - | ||
35 | - | func keyUsdnAssetId () = "%s__usdnAssetId" | |
36 | - | ||
37 | - | ||
38 | - | func keyDepositFee () = "%s__depositFee" | |
39 | - | ||
40 | - | ||
41 | - | func keyWithdrawFee () = "%s__withdrawFee" | |
42 | - | ||
43 | - | ||
44 | - | func usdt () = fromBase58String(value(getString(keyUsdtAssetId()))) | |
45 | - | ||
46 | - | ||
47 | - | func usdn () = fromBase58String(value(getString(keyUsdnAssetId()))) | |
48 | 8 | ||
49 | 9 | ||
50 | 10 | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { | |
67 | 27 | } | |
68 | 28 | ||
69 | 29 | ||
70 | - | func collectKey (address,amountToSwapInKey,allowToWithdrawHeight) = makeString([address, "WITHDRAW", toString(amountToSwapInKey), "HEIGHT", toString(allowToWithdrawHeight)], "_") | |
71 | - | ||
72 | - | ||
73 | - | func collectBalanceKey (address) = makeString([address, "balance"], "_") | |
74 | - | ||
75 | - | ||
76 | - | func collectBalanceKeyWithFee (address) = makeString([address, "balance_with_fee"], "_") | |
77 | - | ||
78 | - | ||
79 | - | func usdtToUsdnAssetError () = throw("To swap USDT to USDN you need attach payment in USDT") | |
80 | - | ||
81 | - | ||
82 | - | func usdnToUsdtAssetError () = throw("To swap USDN to USDT you need attach payment in USDN") | |
83 | - | ||
84 | - | ||
85 | - | func usdtToUsdnLowLimitError () = throw((("you can swap at least " + toString((minUsdt() / decimalPartUSDT))) + " tokens")) | |
86 | - | ||
87 | - | ||
88 | - | func abs (n) = if ((0 > n)) | |
89 | - | then (n * -1) | |
90 | - | else n | |
91 | - | ||
92 | - | ||
93 | - | func getWithoutFeeWithdraw (amount,balanceWithoutFee) = min([amount, balanceWithoutFee]) | |
94 | - | ||
95 | - | ||
96 | - | func getListElementOrDefault (list,index,default) = if ((size(list) > index)) | |
97 | - | then list[index] | |
98 | - | else default | |
99 | - | ||
100 | - | ||
101 | - | func currentWithdrawals (address,amount) = { | |
102 | - | let balanceKey = collectBalanceKey(address) | |
103 | - | let balanceWithFeeKey = collectBalanceKeyWithFee(address) | |
104 | - | let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0) | |
105 | - | let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0) | |
106 | - | let userBalance = (currentClientBalance + currentClientBalanceWithFee) | |
107 | - | if (if ((amount > userBalance)) | |
108 | - | then true | |
109 | - | else (minUsdn() > amount)) | |
110 | - | then throw("Swap amount fail, amount is to small or to big.") | |
111 | - | else { | |
112 | - | let toWithdrawWithoutFee = getWithoutFeeWithdraw(amount, currentClientBalance) | |
113 | - | let toWithdrawWithFee = (amount - toWithdrawWithoutFee) | |
114 | - | let fee = ((toWithdrawWithFee / 100) * value(getInteger(keyWithdrawFee()))) | |
115 | - | [toWithdrawWithoutFee, toWithdrawWithFee, fee] | |
116 | - | } | |
117 | - | } | |
118 | - | ||
119 | - | ||
120 | 30 | func mustManager (i) = { | |
121 | 31 | let pd = throw("Permission denied") | |
122 | 32 | match managerPublicKeyOrUnit() { | |
132 | 42 | throw("Match error") | |
133 | 43 | } | |
134 | 44 | } | |
135 | - | ||
136 | - | ||
137 | - | @Callable(i) | |
138 | - | func setWithdrawDelay (newWithdrawDelay) = { | |
139 | - | let checkCaller = mustManager(i) | |
140 | - | if ((checkCaller == checkCaller)) | |
141 | - | then [IntegerEntry(keyWithdrawDelay(), newWithdrawDelay)] | |
142 | - | else throw("Strict value is not equal to itself.") | |
143 | - | } | |
144 | - | ||
145 | - | ||
146 | - | ||
147 | - | @Callable(i) | |
148 | - | func setMinimalAmountToSwapUsdt (newMinimalAmount) = { | |
149 | - | let checkCaller = mustManager(i) | |
150 | - | if ((checkCaller == checkCaller)) | |
151 | - | then [IntegerEntry(keyMinimalAmountToSwapUsdt(), newMinimalAmount)] | |
152 | - | else throw("Strict value is not equal to itself.") | |
153 | - | } | |
154 | - | ||
155 | - | ||
156 | - | ||
157 | - | @Callable(i) | |
158 | - | func setMinimalAmountToSwapUsdn (newMinimalAmount) = { | |
159 | - | let checkCaller = mustManager(i) | |
160 | - | if ((checkCaller == checkCaller)) | |
161 | - | then [IntegerEntry(keyMinimalAmountToSwapUsdn(), newMinimalAmount)] | |
162 | - | else throw("Strict value is not equal to itself.") | |
163 | - | } | |
164 | - | ||
165 | - | ||
166 | - | ||
167 | - | @Callable(i) | |
168 | - | func setWithdrawFee (newFee) = { | |
169 | - | let checkCaller = mustManager(i) | |
170 | - | if ((checkCaller == checkCaller)) | |
171 | - | then [IntegerEntry(keyWithdrawFee(), newFee)] | |
172 | - | else throw("Strict value is not equal to itself.") | |
173 | - | } | |
174 | - | ||
175 | - | ||
176 | - | ||
177 | - | @Callable(i) | |
178 | - | func setDepositFee (newFee) = { | |
179 | - | let checkCaller = mustManager(i) | |
180 | - | if ((checkCaller == checkCaller)) | |
181 | - | then [IntegerEntry(keyDepositFee(), newFee)] | |
182 | - | else throw("Strict value is not equal to itself.") | |
183 | - | } | |
184 | - | ||
185 | - | ||
186 | - | ||
187 | - | @Callable(i) | |
188 | - | func constructor (usdtAssetId,usdnAssetId) = { | |
189 | - | let checkCaller = mustManager(i) | |
190 | - | if ((checkCaller == checkCaller)) | |
191 | - | then [StringEntry(keyUsdtAssetId(), usdtAssetId), StringEntry(keyUsdnAssetId(), usdnAssetId)] | |
192 | - | else throw("Strict value is not equal to itself.") | |
193 | - | } | |
194 | - | ||
195 | - | ||
196 | - | ||
197 | - | @Callable(invoke) | |
198 | - | func swapUSDTtoUSDN () = { | |
199 | - | let balanceKey = collectBalanceKeyWithFee(toString(invoke.caller)) | |
200 | - | let payment = value(invoke.payments[0]) | |
201 | - | if ((payment.assetId != usdt())) | |
202 | - | then usdtToUsdnAssetError() | |
203 | - | else { | |
204 | - | let amount = payment.amount | |
205 | - | let fee = ((amount / 100) * value(getInteger(keyDepositFee()))) | |
206 | - | let currentClientBalanceUSDT = valueOrElse(getInteger(this, balanceKey), 0) | |
207 | - | if ((minUsdt() > amount)) | |
208 | - | then usdtToUsdnLowLimitError() | |
209 | - | else [IntegerEntry(balanceKey, (currentClientBalanceUSDT + amount)), ScriptTransfer(invoke.caller, (amount - fee), usdn())] | |
210 | - | } | |
211 | - | } | |
212 | - | ||
213 | - | ||
214 | - | ||
215 | - | @Callable(invoke) | |
216 | - | func initializationUSDNtoUSDTswap () = { | |
217 | - | let payment = value(invoke.payments[0]) | |
218 | - | let address = toString(invoke.caller) | |
219 | - | if ((payment.assetId != usdn())) | |
220 | - | then usdnToUsdtAssetError() | |
221 | - | else { | |
222 | - | let amount = payment.amount | |
223 | - | let balanceKey = collectBalanceKey(address) | |
224 | - | let balanceWithFeeKey = collectBalanceKeyWithFee(address) | |
225 | - | let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0) | |
226 | - | let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0) | |
227 | - | let userBalance = (currentClientBalance + currentClientBalanceWithFee) | |
228 | - | let data = currentWithdrawals(toString(invoke.caller), amount) | |
229 | - | let toWithdrawWithoutFee = valueOrErrorMessage(data[0], "Wrong 'currentWithdrawals' method!") | |
230 | - | let toWithdrawWithFee = valueOrErrorMessage(data[1], "Wrong 'currentWithdrawals' method!") | |
231 | - | let fee = valueOrErrorMessage(data[2], "Wrong 'currentWithdrawals' method!") | |
232 | - | let total = ((toWithdrawWithFee + toWithdrawWithoutFee) - fee) | |
233 | - | let allowToWithDrawHeight = (height + withdrawDelay()) | |
234 | - | if (isDefined(getString(this, collectKey(toString(invoke.caller), amount, allowToWithDrawHeight)))) | |
235 | - | then throw("You have already started withdrawal process, wait next block.") | |
236 | - | else [IntegerEntry(balanceKey, (currentClientBalance - toWithdrawWithoutFee)), IntegerEntry(balanceWithFeeKey, (currentClientBalanceWithFee - toWithdrawWithFee)), StringEntry(collectKey(toString(invoke.caller), amount, allowToWithDrawHeight), makeString(["not_withdrawn", toString((amount - fee))], "_"))] | |
237 | - | } | |
238 | - | } | |
239 | - | ||
240 | - | ||
241 | - | ||
242 | - | @Callable(invoke) | |
243 | - | func swapUSDNtoUSDT (amount,heightInKey) = { | |
244 | - | let key = collectKey(toString(invoke.caller), amount, heightInKey) | |
245 | - | let withdrawnInfo = valueOrErrorMessage(getString(this, key), ((("This key doesn't exists " + key) + " at address ") + toString(this))) | |
246 | - | let toWithdraw = getListElementOrDefault(split(withdrawnInfo, "_"), 2, toString(amount)) | |
247 | - | if (if ((height >= heightInKey)) | |
248 | - | then contains(withdrawnInfo, "not_withdrawn") | |
249 | - | else false) | |
250 | - | then [DeleteEntry(key), ScriptTransfer(invoke.caller, valueOrErrorMessage(parseInt(toWithdraw), "Wrong withdraw data!"), usdt())] | |
251 | - | else throw((("Withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn USDT.")) | |
252 | - | } | |
253 | - | ||
254 | 45 | ||
255 | 46 | ||
256 | 47 | @Callable(i) |
Old | New | Differences | |
---|---|---|---|
1 | - | {-# STDLIB_VERSION | |
1 | + | {-# STDLIB_VERSION 5 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | - | let decimalPartUSDN = 1000000 | |
5 | - | ||
6 | - | let decimalPartUSDT = 1000000 | |
7 | - | ||
8 | - | func keyWithdrawDelay () = "%s__withdrawDelay" | |
9 | - | ||
10 | - | ||
11 | - | func withdrawDelay () = value(getInteger(keyWithdrawDelay())) | |
12 | - | ||
13 | - | ||
14 | - | func keyMinimalAmountToSwapUsdt () = "%s__minimalAmountToSwapUsdt" | |
15 | - | ||
16 | - | ||
17 | - | func keyMinimalAmountToSwapUsdn () = "%s__minimalAmountToSwapUsdn" | |
18 | - | ||
19 | - | ||
20 | - | func minUsdt () = value(getInteger(keyMinimalAmountToSwapUsdt())) | |
21 | - | ||
22 | - | ||
23 | - | func minUsdn () = value(getInteger(keyMinimalAmountToSwapUsdn())) | |
24 | - | ||
25 | - | ||
26 | 4 | func keyManagerPublicKey () = "%s__managerPublicKey" | |
27 | 5 | ||
28 | 6 | ||
29 | 7 | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" | |
30 | - | ||
31 | - | ||
32 | - | func keyUsdtAssetId () = "%s__usdtAssetId" | |
33 | - | ||
34 | - | ||
35 | - | func keyUsdnAssetId () = "%s__usdnAssetId" | |
36 | - | ||
37 | - | ||
38 | - | func keyDepositFee () = "%s__depositFee" | |
39 | - | ||
40 | - | ||
41 | - | func keyWithdrawFee () = "%s__withdrawFee" | |
42 | - | ||
43 | - | ||
44 | - | func usdt () = fromBase58String(value(getString(keyUsdtAssetId()))) | |
45 | - | ||
46 | - | ||
47 | - | func usdn () = fromBase58String(value(getString(keyUsdnAssetId()))) | |
48 | 8 | ||
49 | 9 | ||
50 | 10 | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { | |
51 | 11 | case s: String => | |
52 | 12 | fromBase58String(s) | |
53 | 13 | case _: Unit => | |
54 | 14 | unit | |
55 | 15 | case _ => | |
56 | 16 | throw("Match error") | |
57 | 17 | } | |
58 | 18 | ||
59 | 19 | ||
60 | 20 | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { | |
61 | 21 | case s: String => | |
62 | 22 | fromBase58String(s) | |
63 | 23 | case _: Unit => | |
64 | 24 | unit | |
65 | 25 | case _ => | |
66 | 26 | throw("Match error") | |
67 | 27 | } | |
68 | 28 | ||
69 | 29 | ||
70 | - | func collectKey (address,amountToSwapInKey,allowToWithdrawHeight) = makeString([address, "WITHDRAW", toString(amountToSwapInKey), "HEIGHT", toString(allowToWithdrawHeight)], "_") | |
71 | - | ||
72 | - | ||
73 | - | func collectBalanceKey (address) = makeString([address, "balance"], "_") | |
74 | - | ||
75 | - | ||
76 | - | func collectBalanceKeyWithFee (address) = makeString([address, "balance_with_fee"], "_") | |
77 | - | ||
78 | - | ||
79 | - | func usdtToUsdnAssetError () = throw("To swap USDT to USDN you need attach payment in USDT") | |
80 | - | ||
81 | - | ||
82 | - | func usdnToUsdtAssetError () = throw("To swap USDN to USDT you need attach payment in USDN") | |
83 | - | ||
84 | - | ||
85 | - | func usdtToUsdnLowLimitError () = throw((("you can swap at least " + toString((minUsdt() / decimalPartUSDT))) + " tokens")) | |
86 | - | ||
87 | - | ||
88 | - | func abs (n) = if ((0 > n)) | |
89 | - | then (n * -1) | |
90 | - | else n | |
91 | - | ||
92 | - | ||
93 | - | func getWithoutFeeWithdraw (amount,balanceWithoutFee) = min([amount, balanceWithoutFee]) | |
94 | - | ||
95 | - | ||
96 | - | func getListElementOrDefault (list,index,default) = if ((size(list) > index)) | |
97 | - | then list[index] | |
98 | - | else default | |
99 | - | ||
100 | - | ||
101 | - | func currentWithdrawals (address,amount) = { | |
102 | - | let balanceKey = collectBalanceKey(address) | |
103 | - | let balanceWithFeeKey = collectBalanceKeyWithFee(address) | |
104 | - | let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0) | |
105 | - | let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0) | |
106 | - | let userBalance = (currentClientBalance + currentClientBalanceWithFee) | |
107 | - | if (if ((amount > userBalance)) | |
108 | - | then true | |
109 | - | else (minUsdn() > amount)) | |
110 | - | then throw("Swap amount fail, amount is to small or to big.") | |
111 | - | else { | |
112 | - | let toWithdrawWithoutFee = getWithoutFeeWithdraw(amount, currentClientBalance) | |
113 | - | let toWithdrawWithFee = (amount - toWithdrawWithoutFee) | |
114 | - | let fee = ((toWithdrawWithFee / 100) * value(getInteger(keyWithdrawFee()))) | |
115 | - | [toWithdrawWithoutFee, toWithdrawWithFee, fee] | |
116 | - | } | |
117 | - | } | |
118 | - | ||
119 | - | ||
120 | 30 | func mustManager (i) = { | |
121 | 31 | let pd = throw("Permission denied") | |
122 | 32 | match managerPublicKeyOrUnit() { | |
123 | 33 | case pk: ByteVector => | |
124 | 34 | if ((i.callerPublicKey == pk)) | |
125 | 35 | then true | |
126 | 36 | else pd | |
127 | 37 | case _: Unit => | |
128 | 38 | if ((i.caller == this)) | |
129 | 39 | then true | |
130 | 40 | else pd | |
131 | 41 | case _ => | |
132 | 42 | throw("Match error") | |
133 | 43 | } | |
134 | 44 | } | |
135 | - | ||
136 | - | ||
137 | - | @Callable(i) | |
138 | - | func setWithdrawDelay (newWithdrawDelay) = { | |
139 | - | let checkCaller = mustManager(i) | |
140 | - | if ((checkCaller == checkCaller)) | |
141 | - | then [IntegerEntry(keyWithdrawDelay(), newWithdrawDelay)] | |
142 | - | else throw("Strict value is not equal to itself.") | |
143 | - | } | |
144 | - | ||
145 | - | ||
146 | - | ||
147 | - | @Callable(i) | |
148 | - | func setMinimalAmountToSwapUsdt (newMinimalAmount) = { | |
149 | - | let checkCaller = mustManager(i) | |
150 | - | if ((checkCaller == checkCaller)) | |
151 | - | then [IntegerEntry(keyMinimalAmountToSwapUsdt(), newMinimalAmount)] | |
152 | - | else throw("Strict value is not equal to itself.") | |
153 | - | } | |
154 | - | ||
155 | - | ||
156 | - | ||
157 | - | @Callable(i) | |
158 | - | func setMinimalAmountToSwapUsdn (newMinimalAmount) = { | |
159 | - | let checkCaller = mustManager(i) | |
160 | - | if ((checkCaller == checkCaller)) | |
161 | - | then [IntegerEntry(keyMinimalAmountToSwapUsdn(), newMinimalAmount)] | |
162 | - | else throw("Strict value is not equal to itself.") | |
163 | - | } | |
164 | - | ||
165 | - | ||
166 | - | ||
167 | - | @Callable(i) | |
168 | - | func setWithdrawFee (newFee) = { | |
169 | - | let checkCaller = mustManager(i) | |
170 | - | if ((checkCaller == checkCaller)) | |
171 | - | then [IntegerEntry(keyWithdrawFee(), newFee)] | |
172 | - | else throw("Strict value is not equal to itself.") | |
173 | - | } | |
174 | - | ||
175 | - | ||
176 | - | ||
177 | - | @Callable(i) | |
178 | - | func setDepositFee (newFee) = { | |
179 | - | let checkCaller = mustManager(i) | |
180 | - | if ((checkCaller == checkCaller)) | |
181 | - | then [IntegerEntry(keyDepositFee(), newFee)] | |
182 | - | else throw("Strict value is not equal to itself.") | |
183 | - | } | |
184 | - | ||
185 | - | ||
186 | - | ||
187 | - | @Callable(i) | |
188 | - | func constructor (usdtAssetId,usdnAssetId) = { | |
189 | - | let checkCaller = mustManager(i) | |
190 | - | if ((checkCaller == checkCaller)) | |
191 | - | then [StringEntry(keyUsdtAssetId(), usdtAssetId), StringEntry(keyUsdnAssetId(), usdnAssetId)] | |
192 | - | else throw("Strict value is not equal to itself.") | |
193 | - | } | |
194 | - | ||
195 | - | ||
196 | - | ||
197 | - | @Callable(invoke) | |
198 | - | func swapUSDTtoUSDN () = { | |
199 | - | let balanceKey = collectBalanceKeyWithFee(toString(invoke.caller)) | |
200 | - | let payment = value(invoke.payments[0]) | |
201 | - | if ((payment.assetId != usdt())) | |
202 | - | then usdtToUsdnAssetError() | |
203 | - | else { | |
204 | - | let amount = payment.amount | |
205 | - | let fee = ((amount / 100) * value(getInteger(keyDepositFee()))) | |
206 | - | let currentClientBalanceUSDT = valueOrElse(getInteger(this, balanceKey), 0) | |
207 | - | if ((minUsdt() > amount)) | |
208 | - | then usdtToUsdnLowLimitError() | |
209 | - | else [IntegerEntry(balanceKey, (currentClientBalanceUSDT + amount)), ScriptTransfer(invoke.caller, (amount - fee), usdn())] | |
210 | - | } | |
211 | - | } | |
212 | - | ||
213 | - | ||
214 | - | ||
215 | - | @Callable(invoke) | |
216 | - | func initializationUSDNtoUSDTswap () = { | |
217 | - | let payment = value(invoke.payments[0]) | |
218 | - | let address = toString(invoke.caller) | |
219 | - | if ((payment.assetId != usdn())) | |
220 | - | then usdnToUsdtAssetError() | |
221 | - | else { | |
222 | - | let amount = payment.amount | |
223 | - | let balanceKey = collectBalanceKey(address) | |
224 | - | let balanceWithFeeKey = collectBalanceKeyWithFee(address) | |
225 | - | let currentClientBalance = valueOrElse(getInteger(this, balanceKey), 0) | |
226 | - | let currentClientBalanceWithFee = valueOrElse(getInteger(this, balanceWithFeeKey), 0) | |
227 | - | let userBalance = (currentClientBalance + currentClientBalanceWithFee) | |
228 | - | let data = currentWithdrawals(toString(invoke.caller), amount) | |
229 | - | let toWithdrawWithoutFee = valueOrErrorMessage(data[0], "Wrong 'currentWithdrawals' method!") | |
230 | - | let toWithdrawWithFee = valueOrErrorMessage(data[1], "Wrong 'currentWithdrawals' method!") | |
231 | - | let fee = valueOrErrorMessage(data[2], "Wrong 'currentWithdrawals' method!") | |
232 | - | let total = ((toWithdrawWithFee + toWithdrawWithoutFee) - fee) | |
233 | - | let allowToWithDrawHeight = (height + withdrawDelay()) | |
234 | - | if (isDefined(getString(this, collectKey(toString(invoke.caller), amount, allowToWithDrawHeight)))) | |
235 | - | then throw("You have already started withdrawal process, wait next block.") | |
236 | - | else [IntegerEntry(balanceKey, (currentClientBalance - toWithdrawWithoutFee)), IntegerEntry(balanceWithFeeKey, (currentClientBalanceWithFee - toWithdrawWithFee)), StringEntry(collectKey(toString(invoke.caller), amount, allowToWithDrawHeight), makeString(["not_withdrawn", toString((amount - fee))], "_"))] | |
237 | - | } | |
238 | - | } | |
239 | - | ||
240 | - | ||
241 | - | ||
242 | - | @Callable(invoke) | |
243 | - | func swapUSDNtoUSDT (amount,heightInKey) = { | |
244 | - | let key = collectKey(toString(invoke.caller), amount, heightInKey) | |
245 | - | let withdrawnInfo = valueOrErrorMessage(getString(this, key), ((("This key doesn't exists " + key) + " at address ") + toString(this))) | |
246 | - | let toWithdraw = getListElementOrDefault(split(withdrawnInfo, "_"), 2, toString(amount)) | |
247 | - | if (if ((height >= heightInKey)) | |
248 | - | then contains(withdrawnInfo, "not_withdrawn") | |
249 | - | else false) | |
250 | - | then [DeleteEntry(key), ScriptTransfer(invoke.caller, valueOrErrorMessage(parseInt(toWithdraw), "Wrong withdraw data!"), usdt())] | |
251 | - | else throw((("Withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn USDT.")) | |
252 | - | } | |
253 | - | ||
254 | 45 | ||
255 | 46 | ||
256 | 47 | @Callable(i) | |
257 | 48 | func setManager (pendingManagerPublicKey) = { | |
258 | 49 | let checkCaller = mustManager(i) | |
259 | 50 | if ((checkCaller == checkCaller)) | |
260 | 51 | then { | |
261 | 52 | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) | |
262 | 53 | if ((checkManagerPublicKey == checkManagerPublicKey)) | |
263 | 54 | then [StringEntry(keyPendingManagerPublicKey(), pendingManagerPublicKey)] | |
264 | 55 | else throw("Strict value is not equal to itself.") | |
265 | 56 | } | |
266 | 57 | else throw("Strict value is not equal to itself.") | |
267 | 58 | } | |
268 | 59 | ||
269 | 60 | ||
270 | 61 | ||
271 | 62 | @Callable(i) | |
272 | 63 | func confirmManager () = { | |
273 | 64 | let pm = pendingManagerPublicKeyOrUnit() | |
274 | 65 | let hasPM = if (isDefined(pm)) | |
275 | 66 | then true | |
276 | 67 | else throw("No pending manager") | |
277 | 68 | if ((hasPM == hasPM)) | |
278 | 69 | then { | |
279 | 70 | let checkPM = if ((i.callerPublicKey == value(pm))) | |
280 | 71 | then true | |
281 | 72 | else throw("You are not pending manager") | |
282 | 73 | if ((checkPM == checkPM)) | |
283 | 74 | then [StringEntry(keyManagerPublicKey(), toBase58String(value(pm))), DeleteEntry(keyPendingManagerPublicKey())] | |
284 | 75 | else throw("Strict value is not equal to itself.") | |
285 | 76 | } | |
286 | 77 | else throw("Strict value is not equal to itself.") | |
287 | 78 | } | |
288 | 79 | ||
289 | 80 | ||
290 | 81 | @Verifier(tx) | |
291 | 82 | func verify () = { | |
292 | 83 | let targetPublicKey = match managerPublicKeyOrUnit() { | |
293 | 84 | case pk: ByteVector => | |
294 | 85 | pk | |
295 | 86 | case _: Unit => | |
296 | 87 | tx.senderPublicKey | |
297 | 88 | case _ => | |
298 | 89 | throw("Match error") | |
299 | 90 | } | |
300 | 91 | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) | |
301 | 92 | } | |
302 | 93 |
github/deemru/w8io/169f3d6 38.66 ms ◑