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