1 | 1 | | {-# STDLIB_VERSION 6 #-} |
---|
2 | 2 | | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | 3 | | {-# CONTENT_TYPE DAPP #-} |
---|
4 | 4 | | let SEP = "__" |
---|
5 | 5 | | |
---|
6 | 6 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
7 | 7 | | |
---|
8 | 8 | | |
---|
9 | 9 | | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" |
---|
10 | 10 | | |
---|
11 | 11 | | |
---|
12 | 12 | | func keyWithdrawDelay (assetA,assetB) = makeString(["%s%s%s", "withdrawDelay", assetA, assetB], SEP) |
---|
13 | 13 | | |
---|
14 | 14 | | |
---|
15 | 15 | | func keyDepositFeePermille (assetA,assetB) = makeString(["%s%s%s", "depositFeePermille", assetA, assetB], SEP) |
---|
16 | 16 | | |
---|
17 | 17 | | |
---|
18 | 18 | | func keyWithdrawFeePermille (assetA,assetB) = makeString(["%s%s%s", "withdrawFeePermille", assetA, assetB], SEP) |
---|
19 | 19 | | |
---|
20 | 20 | | |
---|
21 | 21 | | func keyMinAmountDeposit (assetA,assetB) = makeString(["%s%s%s", "minAmountDeposit", assetA, assetB], SEP) |
---|
22 | 22 | | |
---|
23 | 23 | | |
---|
24 | 24 | | func keyAssetsPairStatus (assetA,assetB) = makeString(["%s%s%s", "assetsPairStatus", assetA, assetB], SEP) |
---|
25 | 25 | | |
---|
26 | 26 | | |
---|
27 | 27 | | func keyMinAmountWithdraw (assetA,assetB) = makeString(["%s%s%s", "minAmountWithdraw", assetA, assetB], SEP) |
---|
28 | 28 | | |
---|
29 | 29 | | |
---|
30 | 30 | | func keyBalance (assetA,assetB,userAddress) = makeString(["%s%s%s%s", "balance", assetA, assetB, userAddress], SEP) |
---|
31 | 31 | | |
---|
32 | 32 | | |
---|
33 | 33 | | func keyTotalFeeCollectedDeposit (assetA,assetB) = makeString(["%s%s%s%s", "totalFeeCollected", "deposit", assetA, assetB], SEP) |
---|
34 | 34 | | |
---|
35 | 35 | | |
---|
36 | 36 | | func keyTotalFeeCollectedWithdraw (assetA,assetB) = makeString(["%s%s%s%s", "totalFeeCollected", "withdraw", assetA, assetB], SEP) |
---|
37 | 37 | | |
---|
38 | 38 | | |
---|
39 | 39 | | func keyProcessInProgress (userAddress,assetA,assetB,heightInKey) = makeString(["%s%s%s%s%s%d", "withdrawProcess", "inProgress", userAddress, assetA, assetB, toString(heightInKey)], SEP) |
---|
40 | 40 | | |
---|
41 | 41 | | |
---|
42 | 42 | | func keyProcessDone (userAddress,assetA,assetB,heightInKey) = makeString(["%s%s%s%s%s%d", "withdrawProcess", "done", userAddress, assetA, assetB, toString(heightInKey)], SEP) |
---|
43 | 43 | | |
---|
44 | 44 | | |
---|
45 | 45 | | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { |
---|
46 | 46 | | case s: String => |
---|
47 | 47 | | fromBase58String(s) |
---|
48 | 48 | | case _: Unit => |
---|
49 | 49 | | unit |
---|
50 | 50 | | case _ => |
---|
51 | 51 | | throw("Match error") |
---|
52 | 52 | | } |
---|
53 | 53 | | |
---|
54 | 54 | | |
---|
55 | 55 | | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { |
---|
56 | 56 | | case s: String => |
---|
57 | 57 | | fromBase58String(s) |
---|
58 | 58 | | case _: Unit => |
---|
59 | 59 | | unit |
---|
60 | 60 | | case _ => |
---|
61 | 61 | | throw("Match error") |
---|
62 | 62 | | } |
---|
63 | 63 | | |
---|
64 | 64 | | |
---|
65 | 65 | | func mustManager (i) = { |
---|
66 | 66 | | let pd = throw("Permission denied.") |
---|
67 | 67 | | match managerPublicKeyOrUnit() { |
---|
68 | 68 | | case pk: ByteVector => |
---|
69 | 69 | | if ((i.callerPublicKey == pk)) |
---|
70 | 70 | | then true |
---|
71 | 71 | | else pd |
---|
72 | 72 | | case _: Unit => |
---|
73 | 73 | | if ((i.caller == this)) |
---|
74 | 74 | | then true |
---|
75 | 75 | | else pd |
---|
76 | 76 | | case _ => |
---|
77 | 77 | | throw("Match error") |
---|
78 | 78 | | } |
---|
79 | 79 | | } |
---|
80 | 80 | | |
---|
81 | 81 | | |
---|
82 | 82 | | func fmtErr (msg) = makeString(["otc_multiasset.ride:", msg], " ") |
---|
83 | 83 | | |
---|
84 | 84 | | |
---|
85 | 85 | | func throwErr (msg) = throw(fmtErr(msg)) |
---|
86 | 86 | | |
---|
87 | 87 | | |
---|
88 | 88 | | @Callable(i) |
---|
89 | 89 | | func registerAsset (assetA,assetB,withdrawDelay,depositFee,withdrawFee,minAmountDeposit,minAmountWithdraw,pairStatus) = { |
---|
90 | 90 | | let checkCaller = mustManager(i) |
---|
91 | 91 | | if ((checkCaller == checkCaller)) |
---|
92 | 92 | | then { |
---|
93 | 93 | | let withdrawDelayKey = keyWithdrawDelay(assetA, assetB) |
---|
94 | 94 | | let depositFeePermilleKey = keyDepositFeePermille(assetA, assetB) |
---|
95 | 95 | | let withdrawFeePermilleKey = keyWithdrawFeePermille(assetA, assetB) |
---|
96 | 96 | | let minAmountDepositKey = keyMinAmountDeposit(assetA, assetB) |
---|
97 | 97 | | let minAmountWithdrawKey = keyMinAmountWithdraw(assetA, assetB) |
---|
98 | 98 | | let pairStatusKey = keyAssetsPairStatus(assetA, assetB) |
---|
99 | 99 | | [IntegerEntry(withdrawDelayKey, withdrawDelay), IntegerEntry(depositFeePermilleKey, depositFee), IntegerEntry(withdrawFeePermilleKey, withdrawFee), IntegerEntry(minAmountDepositKey, minAmountDeposit), IntegerEntry(minAmountWithdrawKey, minAmountWithdraw), IntegerEntry(pairStatusKey, pairStatus)] |
---|
100 | 100 | | } |
---|
101 | 101 | | else throw("Strict value is not equal to itself.") |
---|
102 | 102 | | } |
---|
103 | 103 | | |
---|
104 | 104 | | |
---|
105 | 105 | | |
---|
106 | 106 | | @Callable(i) |
---|
107 | 107 | | func swapAssetsAToB (assetB) = { |
---|
108 | 108 | | let payment = value(i.payments[0]) |
---|
109 | 109 | | let assetA = toBase58String(value(payment.assetId)) |
---|
110 | 110 | | let asset = fromBase58String(value(assetB)) |
---|
111 | 111 | | let userAddress = toString(i.caller) |
---|
112 | 112 | | let minAmountDeposit = valueOrErrorMessage(getInteger(keyMinAmountDeposit(assetA, assetB)), fmtErr("This asset pair does not exist.")) |
---|
113 | 113 | | let toDeposit = payment.amount |
---|
114 | 114 | | let depositFee = valueOrErrorMessage(getInteger(keyDepositFeePermille(assetA, assetB)), fmtErr("The deposit fee for this pair of assets is not set.")) |
---|
115 | 115 | | let fee = ((toDeposit / 1000) * depositFee) |
---|
116 | 116 | | let currentUserBalance = valueOrElse(getInteger(keyBalance(assetA, assetB, userAddress)), 0) |
---|
117 | 117 | | let totalCommissions = valueOrElse(getInteger(keyTotalFeeCollectedDeposit(assetA, assetB)), 0) |
---|
118 | 118 | | let pairStatus = valueOrErrorMessage(getInteger(keyAssetsPairStatus(assetA, assetB)), fmtErr("The asset pair status for this pair of assets is not set.")) |
---|
119 | 119 | | let checkPairStatus = if ((pairStatus == 0)) |
---|
120 | 120 | | then true |
---|
121 | 121 | | else throwErr("The couple's deposit is blocked.") |
---|
122 | 122 | | if ((checkPairStatus == checkPairStatus)) |
---|
123 | 123 | | then { |
---|
124 | 124 | | let checkPayment = if ((toDeposit >= minAmountDeposit)) |
---|
125 | 125 | | then true |
---|
126 | 126 | | else throwErr("The deposit amount is less than the minimum.") |
---|
127 | 127 | | if ((checkPayment == checkPayment)) |
---|
128 | 128 | | then { |
---|
129 | 129 | | let newBalance = ((currentUserBalance + toDeposit) - fee) |
---|
130 | 130 | | let checkBalance = if ((newBalance > 0)) |
---|
131 | 131 | | then true |
---|
132 | 132 | | else throwErr("The final balance is less than or equal to 0.") |
---|
133 | 133 | | if ((checkBalance == checkBalance)) |
---|
134 | 134 | | then [IntegerEntry(keyBalance(assetA, assetB, userAddress), newBalance), IntegerEntry(keyTotalFeeCollectedDeposit(assetA, assetB), (totalCommissions + fee)), ScriptTransfer(i.caller, (toDeposit - fee), asset)] |
---|
135 | 135 | | else throw("Strict value is not equal to itself.") |
---|
136 | 136 | | } |
---|
137 | 137 | | else throw("Strict value is not equal to itself.") |
---|
138 | 138 | | } |
---|
139 | 139 | | else throw("Strict value is not equal to itself.") |
---|
140 | 140 | | } |
---|
141 | 141 | | |
---|
142 | 142 | | |
---|
143 | 143 | | |
---|
144 | 144 | | @Callable(i) |
---|
145 | 145 | | func initializationSwapAssetsBToA (assetA) = { |
---|
146 | 146 | | let payment = value(i.payments[0]) |
---|
147 | 147 | | let toWithdraw = payment.amount |
---|
148 | 148 | | let assetB = toBase58String(value(payment.assetId)) |
---|
149 | 149 | | let userAddress = toString(i.caller) |
---|
150 | 150 | | let minAmountWithdraw = valueOrErrorMessage(getInteger(keyMinAmountWithdraw(assetA, assetB)), fmtErr("The minimum withdrawal amount for this pair of assets is not set.")) |
---|
151 | 151 | | let assetLockHeight = (height + valueOrErrorMessage(getInteger(keyWithdrawDelay(assetA, assetB)), fmtErr("Withdrawal delay is not set for the specified pair."))) |
---|
152 | 152 | | let currentUserBalance = valueOrErrorMessage(getInteger(keyBalance(assetA, assetB, userAddress)), fmtErr("This user balance does not exist.")) |
---|
153 | 153 | | let totalCommissions = valueOrElse(getInteger(keyTotalFeeCollectedWithdraw(assetA, assetB)), 0) |
---|
154 | 154 | | let withdrawFeePermilleKey = keyWithdrawFeePermille(assetA, assetB) |
---|
155 | 155 | | let withdrawFee = valueOrErrorMessage(getInteger(withdrawFeePermilleKey), fmtErr("The withdrawal fee for this pair of assets is not set.")) |
---|
156 | 156 | | let fee = ((toWithdraw / 1000) * withdrawFee) |
---|
157 | 157 | | let newBalance = (currentUserBalance - toWithdraw) |
---|
158 | 158 | | let checkBalance = if ((newBalance >= 0)) |
---|
159 | 159 | | then true |
---|
160 | 160 | | else throwErr("Swap amount fail, amount is to small.") |
---|
161 | 161 | | if ((checkBalance == checkBalance)) |
---|
162 | 162 | | then { |
---|
163 | 163 | | let checkPayment = if ((toWithdraw >= minAmountWithdraw)) |
---|
164 | 164 | | then true |
---|
165 | 165 | | else throwErr("The withdraw amount is less than the minimum.") |
---|
166 | 166 | | if ((checkPayment == checkPayment)) |
---|
167 | 167 | | then { |
---|
168 | 168 | | let checkProcessInProgress = if ((getInteger(keyProcessInProgress(userAddress, assetA, assetB, assetLockHeight)) == unit)) |
---|
169 | 169 | | then true |
---|
170 | 170 | | else throwErr("At this height, there is already an exchange of this pair.") |
---|
171 | 171 | | if ((checkProcessInProgress == checkProcessInProgress)) |
---|
172 | 172 | | then [IntegerEntry(keyBalance(assetA, assetB, userAddress), newBalance), IntegerEntry(keyProcessInProgress(userAddress, assetA, assetB, assetLockHeight), (toWithdraw - fee)), IntegerEntry(keyTotalFeeCollectedWithdraw(assetA, assetB), (totalCommissions + fee))] |
---|
173 | 173 | | else throw("Strict value is not equal to itself.") |
---|
174 | 174 | | } |
---|
175 | 175 | | else throw("Strict value is not equal to itself.") |
---|
176 | 176 | | } |
---|
177 | 177 | | else throw("Strict value is not equal to itself.") |
---|
178 | 178 | | } |
---|
179 | 179 | | |
---|
180 | 180 | | |
---|
181 | 181 | | |
---|
182 | 182 | | @Callable(i) |
---|
183 | 183 | | func withdrawAsset (assetA,assetB,heightInKey) = { |
---|
184 | 184 | | let userAddress = toString(i.caller) |
---|
185 | 185 | | let toWithdraw = valueOrErrorMessage(getInteger(keyProcessInProgress(userAddress, assetA, assetB, heightInKey)), fmtErr("At this height, withdraw was not initialized with this pair of assets.")) |
---|
186 | 186 | | let asset = fromBase58String(value(assetA)) |
---|
187 | 187 | | let checkHeight = if ((height >= heightInKey)) |
---|
188 | 188 | | then true |
---|
189 | 189 | | else throwErr((("Withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn.")) |
---|
190 | 190 | | if ((checkHeight == checkHeight)) |
---|
191 | 191 | | then [DeleteEntry(keyProcessInProgress(userAddress, assetA, assetB, heightInKey)), IntegerEntry(keyProcessDone(userAddress, assetA, assetB, heightInKey), toWithdraw), ScriptTransfer(i.caller, toWithdraw, asset)] |
---|
192 | 192 | | else throw("Strict value is not equal to itself.") |
---|
193 | 193 | | } |
---|
194 | 194 | | |
---|
195 | 195 | | |
---|
196 | 196 | | |
---|
197 | 197 | | @Callable(i) |
---|
198 | 198 | | func withdrawFee (assetA,assetB) = { |
---|
199 | 199 | | let checkCaller = mustManager(i) |
---|
200 | 200 | | if ((checkCaller == checkCaller)) |
---|
201 | 201 | | then { |
---|
202 | 202 | | let toWithdrawB = valueOrElse(getInteger(keyTotalFeeCollectedDeposit(assetA, assetB)), 0) |
---|
203 | 203 | | let withdrawAssetB = fromBase58String(value(assetB)) |
---|
204 | 204 | | let toWithdrawA = valueOrElse(getInteger(keyTotalFeeCollectedWithdraw(assetA, assetB)), 0) |
---|
205 | 205 | | let withdrawAssetA = fromBase58String(value(assetA)) |
---|
206 | 206 | | [IntegerEntry(keyTotalFeeCollectedDeposit(assetA, assetB), 0), ScriptTransfer(i.caller, toWithdrawA, withdrawAssetA), IntegerEntry(keyTotalFeeCollectedWithdraw(assetA, assetB), 0), ScriptTransfer(i.caller, toWithdrawB, withdrawAssetB)] |
---|
207 | 207 | | } |
---|
208 | 208 | | else throw("Strict value is not equal to itself.") |
---|
209 | 209 | | } |
---|
210 | 210 | | |
---|
211 | 211 | | |
---|
212 | 212 | | |
---|
213 | 213 | | @Callable(i) |
---|
214 | 214 | | func setManager (pendingManagerPublicKey) = { |
---|
215 | 215 | | let checkCaller = mustManager(i) |
---|
216 | 216 | | if ((checkCaller == checkCaller)) |
---|
217 | 217 | | then { |
---|
218 | 218 | | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
219 | 219 | | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
220 | 220 | | then [StringEntry(keyPendingManagerPublicKey(), pendingManagerPublicKey)] |
---|
221 | 221 | | else throw("Strict value is not equal to itself.") |
---|
222 | 222 | | } |
---|
223 | 223 | | else throw("Strict value is not equal to itself.") |
---|
224 | 224 | | } |
---|
225 | 225 | | |
---|
226 | 226 | | |
---|
227 | 227 | | |
---|
228 | 228 | | @Callable(i) |
---|
229 | 229 | | func confirmManager () = { |
---|
230 | 230 | | let pm = pendingManagerPublicKeyOrUnit() |
---|
231 | 231 | | let hasPM = if (isDefined(pm)) |
---|
232 | 232 | | then true |
---|
233 | 233 | | else throwErr("No pending manager.") |
---|
234 | 234 | | if ((hasPM == hasPM)) |
---|
235 | 235 | | then { |
---|
236 | 236 | | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
237 | 237 | | then true |
---|
238 | 238 | | else throwErr("You are not pending manager.") |
---|
239 | 239 | | if ((checkPM == checkPM)) |
---|
240 | 240 | | then [StringEntry(keyManagerPublicKey(), toBase58String(value(pm))), DeleteEntry(keyPendingManagerPublicKey())] |
---|
241 | 241 | | else throw("Strict value is not equal to itself.") |
---|
242 | 242 | | } |
---|
243 | 243 | | else throw("Strict value is not equal to itself.") |
---|
244 | 244 | | } |
---|
245 | 245 | | |
---|
246 | 246 | | |
---|
247 | 247 | | @Verifier(tx) |
---|
248 | 248 | | func verify () = { |
---|
249 | 249 | | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
250 | 250 | | case pk: ByteVector => |
---|
251 | 251 | | pk |
---|
252 | 252 | | case _: Unit => |
---|
253 | 253 | | tx.senderPublicKey |
---|
254 | 254 | | case _ => |
---|
255 | 255 | | throw("Match error") |
---|
256 | 256 | | } |
---|
257 | 257 | | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
258 | 258 | | } |
---|
259 | 259 | | |
---|