1 | | - | {-# STDLIB_VERSION 3 #-} |
---|
2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | let COUNTER = "cnt" |
---|
5 | | - | |
---|
6 | | - | let S = "_" |
---|
7 | | - | |
---|
8 | | - | let FREEZED = "freezed" |
---|
9 | | - | |
---|
10 | | - | let SHARES = "shares" |
---|
11 | | - | |
---|
12 | | - | let MARKETSTATUS = "mktstatus" |
---|
13 | | - | |
---|
14 | | - | let ORDERSTATUS = "ordstatus" |
---|
15 | | - | |
---|
16 | | - | let ORDEROWNER = "ordowner" |
---|
17 | | - | |
---|
18 | | - | let LASTBLOCK = "lastblock" |
---|
19 | | - | |
---|
20 | | - | let REPORTER = "reporter" |
---|
21 | | - | |
---|
22 | | - | let ACTIONBUY = "b" |
---|
23 | | - | |
---|
24 | | - | let ACTIONSELL = "s" |
---|
25 | | - | |
---|
26 | | - | let NEW = "new" |
---|
27 | | - | |
---|
28 | | - | let ACTIVE = "active" |
---|
29 | | - | |
---|
30 | | - | let CLOSED = "closed" |
---|
31 | | - | |
---|
32 | | - | let ORDER = "ord" |
---|
33 | | - | |
---|
34 | | - | let DEFAULTBLOCKSDELAY = 5 |
---|
35 | | - | |
---|
36 | | - | let DEFAULTPRICE = 1000000 |
---|
37 | | - | |
---|
38 | | - | let CLOSEDYES = "closed_yes" |
---|
39 | | - | |
---|
40 | | - | let CLOSEDNO = "closed_no" |
---|
41 | | - | |
---|
42 | | - | @Callable(i) |
---|
43 | | - | func deposit () = { |
---|
44 | | - | let pmt = extract(i.payment) |
---|
45 | | - | if (isDefined(pmt.assetId)) |
---|
46 | | - | then throw("can hodl waves only at the moment") |
---|
47 | | - | else { |
---|
48 | | - | let currentKey = toBase58String(i.caller.bytes) |
---|
49 | | - | let currentAmount = match getInteger(this, currentKey) { |
---|
50 | | - | case a: Int => |
---|
51 | | - | a |
---|
52 | | - | case _ => |
---|
53 | | - | 0 |
---|
54 | | - | } |
---|
55 | | - | let newAmount = (currentAmount + pmt.amount) |
---|
56 | | - | WriteSet([DataEntry(currentKey, newAmount)]) |
---|
57 | | - | } |
---|
58 | | - | } |
---|
59 | | - | |
---|
60 | | - | |
---|
61 | | - | |
---|
62 | | - | @Callable(i) |
---|
63 | | - | func withdraw (amount) = { |
---|
64 | | - | let currentKey = toBase58String(i.caller.bytes) |
---|
65 | | - | let currentAmount = match getInteger(this, currentKey) { |
---|
66 | | - | case a: Int => |
---|
67 | | - | a |
---|
68 | | - | case _ => |
---|
69 | | - | 0 |
---|
70 | | - | } |
---|
71 | | - | let newAmount = (currentAmount - amount) |
---|
72 | | - | if ((0 > amount)) |
---|
73 | | - | then throw("Can't withdraw negative amount") |
---|
74 | | - | else if ((0 > newAmount)) |
---|
75 | | - | then throw("Not enough balance") |
---|
76 | | - | else ScriptResult(WriteSet([DataEntry(currentKey, newAmount)]), TransferSet([ScriptTransfer(i.caller, amount, unit)])) |
---|
77 | | - | } |
---|
78 | | - | |
---|
79 | | - | |
---|
80 | | - | |
---|
81 | | - | @Callable(i) |
---|
82 | | - | func createMarket (title,description,oracle,interval) = { |
---|
83 | | - | let currentKey = toBase58String(i.caller.bytes) |
---|
84 | | - | let counter = match getInteger(this, COUNTER) { |
---|
85 | | - | case a: Int => |
---|
86 | | - | a |
---|
87 | | - | case _ => |
---|
88 | | - | 0 |
---|
89 | | - | } |
---|
90 | | - | let marketIdKey = ((((toString(counter) + S) + toString(height)) + S) + currentKey) |
---|
91 | | - | let lastBlockKey = ((marketIdKey + S) + LASTBLOCK) |
---|
92 | | - | WriteSet([DataEntry(COUNTER, (counter + 1)), DataEntry(("market_n_" + toString(counter)), marketIdKey), DataEntry(((marketIdKey + S) + REPORTER), oracle), DataEntry(((marketIdKey + S) + MARKETSTATUS), ACTIVE), DataEntry(lastBlockKey, (height + interval)), DataEntry(((marketIdKey + S) + "title"), title), DataEntry(((marketIdKey + S) + "description"), description)]) |
---|
93 | | - | } |
---|
94 | | - | |
---|
95 | | - | |
---|
96 | | - | |
---|
97 | | - | @Callable(i) |
---|
98 | | - | func setOrderLimitSell (marketId,price,volume) = { |
---|
99 | | - | let currentKey = toBase58String(i.caller.bytes) |
---|
100 | | - | let currentFreezedBalanceKey = ((((marketId + S) + currentKey) + S) + FREEZED) |
---|
101 | | - | let orderSybKey = ((((((ORDER + S) + marketId) + S) + toString(price)) + S) + toString(volume)) |
---|
102 | | - | let orderKey = ((orderSybKey + S) + ACTIONSELL) |
---|
103 | | - | let orderStatusKey = ((orderKey + S) + ORDERSTATUS) |
---|
104 | | - | let orderOwnerKey = ((orderKey + S) + ORDEROWNER) |
---|
105 | | - | let adjustedAmount = (volume * DEFAULTPRICE) |
---|
106 | | - | let currentAmount = match getInteger(this, currentKey) { |
---|
107 | | - | case a: Int => |
---|
108 | | - | a |
---|
109 | | - | case _ => |
---|
110 | | - | 0 |
---|
111 | | - | } |
---|
112 | | - | let freezedBalance = match getInteger(this, currentFreezedBalanceKey) { |
---|
113 | | - | case a: Int => |
---|
114 | | - | a |
---|
115 | | - | case _ => |
---|
116 | | - | 0 |
---|
117 | | - | } |
---|
118 | | - | let status = match getString(this, orderStatusKey) { |
---|
119 | | - | case a: String => |
---|
120 | | - | a |
---|
121 | | - | case _ => |
---|
122 | | - | NEW |
---|
123 | | - | } |
---|
124 | | - | let lastBlockKey = ((marketId + S) + LASTBLOCK) |
---|
125 | | - | let lastblock = match getInteger(this, lastBlockKey) { |
---|
126 | | - | case a: Int => |
---|
127 | | - | a |
---|
128 | | - | case _ => |
---|
129 | | - | 0 |
---|
130 | | - | } |
---|
131 | | - | if ((height > lastblock)) |
---|
132 | | - | then throw("Can't create an order after market finished") |
---|
133 | | - | else if ((status == ACTIVE)) |
---|
134 | | - | then throw("An order already exist") |
---|
135 | | - | else if ((adjustedAmount > currentAmount)) |
---|
136 | | - | then throw("Not enough funds") |
---|
137 | | - | else WriteSet([DataEntry(currentKey, (currentAmount - adjustedAmount)), DataEntry(currentFreezedBalanceKey, (freezedBalance + adjustedAmount)), DataEntry(orderStatusKey, ACTIVE), DataEntry(orderOwnerKey, currentKey)]) |
---|
138 | | - | } |
---|
139 | | - | |
---|
140 | | - | |
---|
141 | | - | |
---|
142 | | - | @Callable(i) |
---|
143 | | - | func setOrderMarketBuy (marketId,price,volume) = { |
---|
144 | | - | let currentKey = toBase58String(i.caller.bytes) |
---|
145 | | - | let currentSharesBalanceKey = ((((marketId + S) + currentKey) + S) + SHARES) |
---|
146 | | - | let orderSybKey = ((((((ORDER + S) + marketId) + S) + toString(price)) + S) + toString(volume)) |
---|
147 | | - | let orderKey = ((orderSybKey + S) + ACTIONBUY) |
---|
148 | | - | let contrOrderKey = ((orderSybKey + S) + ACTIONSELL) |
---|
149 | | - | let contrOrderStatusKey = ((contrOrderKey + S) + ORDERSTATUS) |
---|
150 | | - | let contrOrderOwnerKey = ((contrOrderKey + S) + ORDEROWNER) |
---|
151 | | - | let currentAmount = match getInteger(this, currentKey) { |
---|
152 | | - | case a: Int => |
---|
153 | | - | a |
---|
154 | | - | case _ => |
---|
155 | | - | 0 |
---|
156 | | - | } |
---|
157 | | - | let sharesBalance = match getInteger(this, currentSharesBalanceKey) { |
---|
158 | | - | case a: Int => |
---|
159 | | - | a |
---|
160 | | - | case _ => |
---|
161 | | - | 0 |
---|
162 | | - | } |
---|
163 | | - | let contrStatus = match getString(this, contrOrderStatusKey) { |
---|
164 | | - | case a: String => |
---|
165 | | - | a |
---|
166 | | - | case _ => |
---|
167 | | - | NEW |
---|
168 | | - | } |
---|
169 | | - | let contrOwner = match getString(this, contrOrderOwnerKey) { |
---|
170 | | - | case a: String => |
---|
171 | | - | a |
---|
172 | | - | case _ => |
---|
173 | | - | NEW |
---|
174 | | - | } |
---|
175 | | - | let contrCurrentAmount = match getInteger(this, contrOwner) { |
---|
176 | | - | case a: Int => |
---|
177 | | - | a |
---|
178 | | - | case _ => |
---|
179 | | - | 0 |
---|
180 | | - | } |
---|
181 | | - | let contrCurrentSharesBalanceKey = ((((marketId + S) + contrOwner) + S) + SHARES) |
---|
182 | | - | let contrSharesBalance = match getInteger(this, contrCurrentSharesBalanceKey) { |
---|
183 | | - | case a: Int => |
---|
184 | | - | a |
---|
185 | | - | case _ => |
---|
186 | | - | 0 |
---|
187 | | - | } |
---|
188 | | - | let lastblock = match getInteger(this, ((marketId + S) + LASTBLOCK)) { |
---|
189 | | - | case a: Int => |
---|
190 | | - | a |
---|
191 | | - | case _ => |
---|
192 | | - | 0 |
---|
193 | | - | } |
---|
194 | | - | let adjustedAmount = ((volume * price) * (DEFAULTPRICE / 100)) |
---|
195 | | - | if ((height > lastblock)) |
---|
196 | | - | then throw("Can't execute an order after market finished") |
---|
197 | | - | else if ((contrStatus != ACTIVE)) |
---|
198 | | - | then throw("A counter-order doesn't exist yet") |
---|
199 | | - | else if (((volume * price) > currentAmount)) |
---|
200 | | - | then throw("Not enough funds") |
---|
201 | | - | else WriteSet([DataEntry(currentKey, (currentAmount - adjustedAmount)), DataEntry(currentSharesBalanceKey, (sharesBalance + volume)), DataEntry(contrOrderStatusKey, CLOSED), DataEntry(contrOwner, (contrCurrentAmount + adjustedAmount)), DataEntry(contrCurrentSharesBalanceKey, (contrSharesBalance - volume))]) |
---|
202 | | - | } |
---|
203 | | - | |
---|
204 | | - | |
---|
205 | | - | |
---|
206 | | - | @Callable(i) |
---|
207 | | - | func reportMarket (marketId,result) = { |
---|
208 | | - | let reporter = toBase58String(i.caller.bytes) |
---|
209 | | - | let reporterKey = ((marketId + S) + REPORTER) |
---|
210 | | - | let lastBlockKey = ((marketId + S) + LASTBLOCK) |
---|
211 | | - | let marketStatusKey = ((marketId + S) + MARKETSTATUS) |
---|
212 | | - | let marketReporter = match getString(this, reporterKey) { |
---|
213 | | - | case a: String => |
---|
214 | | - | a |
---|
215 | | - | case _ => |
---|
216 | | - | NEW |
---|
217 | | - | } |
---|
218 | | - | let marketStatus = match getString(this, marketStatusKey) { |
---|
219 | | - | case a: String => |
---|
220 | | - | a |
---|
221 | | - | case _ => |
---|
222 | | - | NEW |
---|
223 | | - | } |
---|
224 | | - | let lastblock = match getInteger(this, lastBlockKey) { |
---|
225 | | - | case a: Int => |
---|
226 | | - | a |
---|
227 | | - | case _ => |
---|
228 | | - | 0 |
---|
229 | | - | } |
---|
230 | | - | if ((marketReporter != reporter)) |
---|
231 | | - | then throw("You are not reporter for this market") |
---|
232 | | - | else if ((marketStatus != ACTIVE)) |
---|
233 | | - | then throw("Market isn't active") |
---|
234 | | - | else if ((lastblock > height)) |
---|
235 | | - | then throw("Too early to report market") |
---|
236 | | - | else if ((height > (lastblock + DEFAULTBLOCKSDELAY))) |
---|
237 | | - | then throw("Too late to report market") |
---|
238 | | - | else if (if ((result != CLOSEDYES)) |
---|
239 | | - | then (result != CLOSEDNO) |
---|
240 | | - | else false) |
---|
241 | | - | then throw("Bad report result format") |
---|
242 | | - | else WriteSet([DataEntry(marketStatusKey, result)]) |
---|
243 | | - | } |
---|
244 | | - | |
---|
245 | | - | |
---|
246 | | - | |
---|
247 | | - | @Callable(i) |
---|
248 | | - | func claimFundsResultYes (marketId,account) = { |
---|
249 | | - | let callerAdress = account |
---|
250 | | - | let currentSharesBalanceKey = ((((marketId + S) + callerAdress) + S) + SHARES) |
---|
251 | | - | let currentFreezedBalanceKey = ((((marketId + S) + callerAdress) + S) + FREEZED) |
---|
252 | | - | let marketStatusKey = ((marketId + S) + MARKETSTATUS) |
---|
253 | | - | let marketStatus = match getString(this, marketStatusKey) { |
---|
254 | | - | case a: String => |
---|
255 | | - | a |
---|
256 | | - | case _ => |
---|
257 | | - | NEW |
---|
258 | | - | } |
---|
259 | | - | let freezedBalance = match getInteger(this, currentFreezedBalanceKey) { |
---|
260 | | - | case a: Int => |
---|
261 | | - | a |
---|
262 | | - | case _ => |
---|
263 | | - | 0 |
---|
264 | | - | } |
---|
265 | | - | let sharesBalance = match getInteger(this, currentSharesBalanceKey) { |
---|
266 | | - | case a: Int => |
---|
267 | | - | a |
---|
268 | | - | case _ => |
---|
269 | | - | 0 |
---|
270 | | - | } |
---|
271 | | - | let currentAmount = match getInteger(this, callerAdress) { |
---|
272 | | - | case a: Int => |
---|
273 | | - | a |
---|
274 | | - | case _ => |
---|
275 | | - | 0 |
---|
276 | | - | } |
---|
277 | | - | if ((marketStatus != CLOSEDYES)) |
---|
278 | | - | then throw("claimFundsResultYes - wrong market result") |
---|
279 | | - | else if (if ((freezedBalance == 0)) |
---|
280 | | - | then (sharesBalance == 0) |
---|
281 | | - | else false) |
---|
282 | | - | then throw("You can't claim funds for market and account") |
---|
283 | | - | else WriteSet([DataEntry(callerAdress, ((currentAmount + (sharesBalance * DEFAULTPRICE)) + freezedBalance)), DataEntry(currentSharesBalanceKey, 0), DataEntry(currentFreezedBalanceKey, 0)]) |
---|
284 | | - | } |
---|
285 | | - | |
---|
286 | | - | |
---|
287 | | - | |
---|
288 | | - | @Callable(i) |
---|
289 | | - | func claimFundsResultNo (marketId,account) = { |
---|
290 | | - | let callerAdress = account |
---|
291 | | - | let currentSharesBalanceKey = ((((marketId + S) + callerAdress) + S) + SHARES) |
---|
292 | | - | let currentFreezedBalanceKey = ((((marketId + S) + callerAdress) + S) + FREEZED) |
---|
293 | | - | let marketStatusKey = ((marketId + S) + MARKETSTATUS) |
---|
294 | | - | let lastBlockKey = ((marketId + S) + LASTBLOCK) |
---|
295 | | - | let marketStatus = match getString(this, marketStatusKey) { |
---|
296 | | - | case a: String => |
---|
297 | | - | a |
---|
298 | | - | case _ => |
---|
299 | | - | NEW |
---|
300 | | - | } |
---|
301 | | - | let freezedBalance = match getInteger(this, currentFreezedBalanceKey) { |
---|
302 | | - | case a: Int => |
---|
303 | | - | a |
---|
304 | | - | case _ => |
---|
305 | | - | 0 |
---|
306 | | - | } |
---|
307 | | - | let sharesBalance = match getInteger(this, currentSharesBalanceKey) { |
---|
308 | | - | case a: Int => |
---|
309 | | - | a |
---|
310 | | - | case _ => |
---|
311 | | - | 0 |
---|
312 | | - | } |
---|
313 | | - | let currentAmount = match getInteger(this, callerAdress) { |
---|
314 | | - | case a: Int => |
---|
315 | | - | a |
---|
316 | | - | case _ => |
---|
317 | | - | 0 |
---|
318 | | - | } |
---|
319 | | - | let lastblock = match getInteger(this, lastBlockKey) { |
---|
320 | | - | case a: Int => |
---|
321 | | - | a |
---|
322 | | - | case _ => |
---|
323 | | - | 0 |
---|
324 | | - | } |
---|
325 | | - | if (if ((marketStatus == CLOSEDYES)) |
---|
326 | | - | then true |
---|
327 | | - | else ((lastblock + DEFAULTBLOCKSDELAY) >= height)) |
---|
328 | | - | then throw("claimFundsResultNo - wrong market result or time") |
---|
329 | | - | else if (if ((freezedBalance == 0)) |
---|
330 | | - | then (sharesBalance == 0) |
---|
331 | | - | else false) |
---|
332 | | - | then throw("You can't claim funds for market and account") |
---|
333 | | - | else WriteSet([DataEntry(callerAdress, (currentAmount + freezedBalance)), DataEntry(currentSharesBalanceKey, 0), DataEntry(currentFreezedBalanceKey, 0)]) |
---|
334 | | - | } |
---|
335 | | - | |
---|
336 | | - | |
---|
| 1 | + | # no script |
---|