| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://odex.ooo/odex.json", |
| 5 | "init": "{ |
| 6 | $CHAINDEX = var['chainDEX']; |
| 7 | }", |
| 8 | "messages": { |
| 9 | "cases": [ |
| 10 | { |
| 11 | "if": "{!$CHAINDEX}", |
| 12 | "messages": [ |
| 13 | { |
| 14 | "app": "definition", |
| 15 | "payload": { |
| 16 | "definition": [ |
| 17 | "autonomous agent", |
| 18 | { |
| 19 | "{'init'}": "{ |
| 20 | "{$ODEX = '"||this_address||"';" |
| 21 | || "$ODEX_KEY = 'balance_'||this_address||'_base';" |
| 22 | || "if(trigger.output[[asset!=base]].asset != 'none')" |
| 23 | || "bounce('no assets here');" |
| 24 | || "$total_bal = balance[base]-storage_size-trigger.output[[asset=base]];}" |
| 25 | }", |
| 26 | "messages": { |
| 27 | "{'cases'}": [ |
| 28 | { |
| 29 | "{'if'}": "{"{!var['low_fee_mode'] AND $total_bal+var[$ODEX][$ODEX_KEY] > 10000000}"}", |
| 30 | "messages": [ |
| 31 | { |
| 32 | "app": "data", |
| 33 | "payload": { |
| 34 | "withdraw": 1, |
| 35 | "asset": "base", |
| 36 | "amount": "all" |
| 37 | } |
| 38 | }, |
| 39 | { |
| 40 | "app": "payment", |
| 41 | "payload": { |
| 42 | "asset": "base", |
| 43 | "outputs": [ |
| 44 | { |
| 45 | "address": "{"{$ODEX}"}", |
| 46 | "amount": 10000 |
| 47 | } |
| 48 | ] |
| 49 | } |
| 50 | }, |
| 51 | { |
| 52 | "app": "state", |
| 53 | "{'state'}": "{ |
| 54 | "{var['low_fee_mode'] = true;" |
| 55 | || "response['message'] = 'Low fee mode active. Until AA depletion all operations are subsidized';}" |
| 56 | }" |
| 57 | } |
| 58 | ] |
| 59 | }, |
| 60 | { |
| 61 | "{'if'}": "{ |
| 62 | "{trigger.data.match}" |
| 63 | }", |
| 64 | "{'init'}": "{ |
| 65 | "{$my_order = trigger.data.order.signed_message;" |
| 66 | || "$order_key = trigger.data.addr_to_match;" |
| 67 | || "$order_pkg = var[$order_key];" |
| 68 | || "if(!$order_pkg)" |
| 69 | || "bounce('no order to match');" |
| 70 | || "if(typeof($order_pkg) != 'string')" |
| 71 | || "bounce('order already matched');" |
| 72 | || "if ($my_order.matcher != this_address)" |
| 73 | || "bounce('wrong matcher in order');" |
| 74 | || "if(!exists($my_order.matcher_fee_asset) or $my_order.matcher_fee_asset != 'base')" |
| 75 | || "bounce('matcher_fee_asset must be base');" |
| 76 | || "if(!is_valid_address($my_order.matcher) OR $my_order.matcher != this_address)" |
| 77 | || "bounce('I am the matcher, fuck offchain matchers');" |
| 78 | || "if($my_order.matcher_fee != 0)" |
| 79 | || "bounce('matcher fees not needed');" |
| 80 | || "if ($my_order.affiliate)" |
| 81 | || "bounce('affiliates not needed');" |
| 82 | || "$order_to_match = json_parse($order_pkg);}" |
| 83 | }", |
| 84 | "messages": [ |
| 85 | { |
| 86 | "app": "data", |
| 87 | "payload": { |
| 88 | "order1": "{"{trigger.data.order}"}", |
| 89 | "order2": "{"{$order_to_match}"}" |
| 90 | } |
| 91 | }, |
| 92 | { |
| 93 | "app": "payment", |
| 94 | "payload": { |
| 95 | "asset": "base", |
| 96 | "outputs": [ |
| 97 | { |
| 98 | "address": "{"{$ODEX}"}", |
| 99 | "amount": "{"{var['low_fee_mode']?1:9000}"}" |
| 100 | } |
| 101 | ] |
| 102 | } |
| 103 | }, |
| 104 | { |
| 105 | "{'if'}": "{"{var['low_fee_mode']}"}", |
| 106 | "app": "payment", |
| 107 | "payload": { |
| 108 | "asset": "base", |
| 109 | "outputs": [ |
| 110 | { |
| 111 | "address": "{"{trigger.address}"}", |
| 112 | "amount": 9500 |
| 113 | } |
| 114 | ] |
| 115 | } |
| 116 | }, |
| 117 | { |
| 118 | "app": "state", |
| 119 | "{'state'}": "{ |
| 120 | "$id1 = sha256($my_order.address || $my_order.sell_asset || $my_order.buy_asset || $my_order.sell_amount || $my_order.price || ($my_order.nonce otherwise '') || (trigger.data.order.last_ball_unit otherwise '-'));" |
| 121 | || "$order2 = $order_to_match.signed_message;" |
| 122 | || "$id2 = sha256($order2.address || $order2.sell_asset || $order2.buy_asset || $order2.sell_amount || $order2.price || ($order2.nonce otherwise '') || ($order_to_match.last_ball_unit otherwise '-'));" |
| 123 | || "$amount_left1 = var['amount_left_' || $id1] otherwise $order1.sell_amount;" |
| 124 | || "$amount_left2 = var['amount_left_' || $id2] otherwise $order2.sell_amount;" |
| 125 | || "$maker_price = $my_order.price;" |
| 126 | || "$buy_amount1 = round($amount_left1 * $my_order.price);" |
| 127 | || "if ($buy_amount1 > $amount_left2)" |
| 128 | || "var[$order_key] = false;" |
| 129 | || "if(var['low_fee_mode'] AND $total_bal < 30000)" |
| 130 | || "var['low_fee_mode'] = false;}" |
| 131 | }" |
| 132 | } |
| 133 | ] |
| 134 | }, |
| 135 | { |
| 136 | "{'if'}": "{ |
| 137 | "{trigger.data.match_stored}" |
| 138 | }", |
| 139 | "{'init'}": "{ |
| 140 | "{$to_match = trigger.data.addr_to_match;" |
| 141 | || "$str1 = var[trigger.address];" |
| 142 | || "$str2 = var[$to_match];" |
| 143 | || "if(!var[trigger.address])" |
| 144 | || "bounce('you have not stored order');" |
| 145 | || "if(!$to_match OR !is_valid_address($to_match) OR !$order2)" |
| 146 | || "bounce('addr_to_match must be an address with stored order');" |
| 147 | || "$order1 = json_parse($str1);" |
| 148 | || "$order2 = json_parse($str2);}" |
| 149 | |
| 150 | }", |
| 151 | "messages": [ |
| 152 | { |
| 153 | "app": "data", |
| 154 | "payload": { |
| 155 | "order1": "{"{$order1}"}", |
| 156 | "order2": "{"{$order2}"}" |
| 157 | } |
| 158 | }, |
| 159 | { |
| 160 | "app": "payment", |
| 161 | "payload": { |
| 162 | "asset": "base", |
| 163 | "outputs": [ |
| 164 | { |
| 165 | "address": "{"{$ODEX}"}", |
| 166 | "amount": "{"{var['low_fee_mode']?1:9000}"}" |
| 167 | } |
| 168 | ] |
| 169 | } |
| 170 | }, |
| 171 | { |
| 172 | "app": "payment", |
| 173 | "payload": { |
| 174 | "asset": "base", |
| 175 | "outputs": [ |
| 176 | { |
| 177 | "address": "{"{trigger.address}"}", |
| 178 | "amount": 10000 |
| 179 | } |
| 180 | ] |
| 181 | } |
| 182 | }, |
| 183 | { |
| 184 | "app": "state", |
| 185 | "{'state'}": "{ |
| 186 | "{$order1s = $order1.signed_message;" |
| 187 | || "$order2s = $order2.signed_message;" |
| 188 | || "$id1 = sha256($order1s.address || $order1s.sell_asset || $order1s.buy_asset || $order1s.sell_amount || $order1s.price || ($order1s.nonce otherwise '') || ($order1.last_ball_unit otherwise '-'));" |
| 189 | || "$id2 = sha256($order2s.address || $order2s.sell_asset || $order2s.buy_asset || $order2s.sell_amount || $order2s.price || ($order2s.nonce otherwise '') || ($order2.last_ball_unit otherwise '-'));" |
| 190 | || "$amount_left1 = var['amount_left_' || $id1] otherwise $order1s.sell_amount;" |
| 191 | || "$amount_left2 = var['amount_left_' || $id2] otherwise $order2s.sell_amount;" |
| 192 | || "$maker_price = $order1s.price;" |
| 193 | || "$buy_amount1 = round($amount_left1 * $order1s.price);" |
| 194 | || "if ($buy_amount1 > $amount_left2){" |
| 195 | || "$id_smaller = $id2;" |
| 196 | || "$id_larger = $id1;" |
| 197 | || "$amount_left_larger = $amount_left1;" |
| 198 | || "$buy_amount2 = round($amount_left2 / $maker_price);" |
| 199 | || "$buy_amount_smaller = $buy_amount2;" |
| 200 | || "}" |
| 201 | || "else{" |
| 202 | || "$id_smaller = $id1;" |
| 203 | || "$id_larger = $id2;" |
| 204 | || "$amount_left_larger = $amount_left2;" |
| 205 | || "$buy_amount_smaller = $buy_amount1;" |
| 206 | || "}" |
| 207 | || "$new_amount_left_larger = $amount_left_larger - $buy_amount_smaller;" |
| 208 | || "if($id1 == $id_smaller || (!$new_amount_left_larger AND $id1 == $id_larger))" |
| 209 | || "var[trigger.address] = false;" |
| 210 | || "if($id2 == $id_smaller || (!$new_amount_left_larger AND $id2 == $id_larger))" |
| 211 | || "var[$to_match] = false;}" |
| 212 | }" |
| 213 | } |
| 214 | ] |
| 215 | }, |
| 216 | { |
| 217 | "{'if'}": "{ |
| 218 | "{trigger.data.cancel}" |
| 219 | }", |
| 220 | "{'init'}": "{ |
| 221 | "{$str1 = var[trigger.address];" |
| 222 | || "if(!$str1)" |
| 223 | || "bounce('you have not stored order');" |
| 224 | || "$order1 = json_parse($str1);}" |
| 225 | }", |
| 226 | "messages": [ |
| 227 | { |
| 228 | "app": "data", |
| 229 | "payload": { |
| 230 | "cancel": 1, |
| 231 | "order": "{"{$order}"}" |
| 232 | } |
| 233 | }, |
| 234 | { |
| 235 | "app": "payment", |
| 236 | "payload": { |
| 237 | "asset": "base", |
| 238 | "outputs": [ |
| 239 | { |
| 240 | "address": "{"{$ODEX}"}", |
| 241 | "amount": "{"{var['low_fee_mode']?1000:9000}"}" |
| 242 | } |
| 243 | ] |
| 244 | } |
| 245 | }, |
| 246 | { |
| 247 | "{'if'}": "{"{var['low_fee_mode']}"}", |
| 248 | "app": "payment", |
| 249 | "payload": { |
| 250 | "asset": "base", |
| 251 | "outputs": [ |
| 252 | { |
| 253 | "address": "{"{trigger.address}"}", |
| 254 | "amount": 8500 |
| 255 | } |
| 256 | ] |
| 257 | } |
| 258 | }, |
| 259 | { |
| 260 | "app": "state", |
| 261 | "{'state'}": "{ |
| 262 | "{var[trigger.address] = false;}" |
| 263 | }" |
| 264 | } |
| 265 | ] |
| 266 | }, |
| 267 | { |
| 268 | "{'if'}": "{ |
| 269 | "{trigger.data.store}" |
| 270 | }", |
| 271 | "{'init'}": "{ |
| 272 | "{if(var[trigger.address])" |
| 273 | || "bounce('already stored, must be matched or cancelled first');" |
| 274 | || "$order = trigger.data.order.signed_message;" |
| 275 | || "if (!$order.sell_amount OR $order.sell_amount <= 0 or !$order.price or $order.price <= 0)" |
| 276 | || "bounce('wrong data in order');" |
| 277 | || "if (!$order.sell_asset or !$order.buy_asset)" |
| 278 | || "bounce('buy_asset or sell_asset missing');" |
| 279 | || "if(($order.sell_asset != 'base' and !asset[$order.sell_asset].exists) or ($order.buy_asset != 'base' and !asset[$order.buy_asset].exists))" |
| 280 | || "bounce('invalid asset');" |
| 281 | || "if ($order.sell_asset == $order.buy_asset)" |
| 282 | || "bounce('same asset');" |
| 283 | || "if ($order.matcher != this_address)" |
| 284 | || "bounce('wrong matcher in order, I am the matcher, fuck offchain matchers');" |
| 285 | || "if ($order.aa != $ODEX )" |
| 286 | || "bounce('wrong aa in order');" |
| 287 | || "if ($order.expiry_ts AND $order.expiry_ts <= timestamp)" |
| 288 | || "bounce('order expired');" |
| 289 | || "if(!exists($order.matcher_fee_asset) or $order.matcher_fee_asset != 'base')" |
| 290 | || "bounce('matcher_fee_asset must be base');" |
| 291 | || "$sell_key1 = 'balance_' || $order.address || '_' || $order.sell_asset;" |
| 292 | || "if($order.sell_amount <= 0)" |
| 293 | || "bounce('sell_amount must be greater than 0');" |
| 294 | || "$id1 = sha256($order.address || $order.sell_asset || $order.buy_asset || $order.sell_amount || $order.price || ($order.nonce otherwise '') || (trigger.data.order.last_ball_unit otherwise '-'));" |
| 295 | || "if (var[$ODEX]['executed_' || $id1])" |
| 296 | || "bounce('order already executed');" |
| 297 | || "if (var[$ODEX]['cancelled_' || $id1])" |
| 298 | || "bounce('order already cancelled');" |
| 299 | || "$amount_left1 = var[$ODEX]['amount_left_' || $id1] otherwise $order.sell_amount;" |
| 300 | || "if ($amount_left1 > var[$ODEX][$sell_key1] + 0)" |
| 301 | || "bounce('not sufficient balance in sell asset to complete order');" |
| 302 | || "if($order.matcher_fee != 0)" |
| 303 | || "bounce('matcher fees not needed');" |
| 304 | ||"if ($order.affiliate)" |
| 305 | || "bounce('affiliates not needed');" |
| 306 | || "$signer1 = trigger.data.order.authors[0].address;" |
| 307 | || "if($order.address != trigger.address OR $signer1 != $order.address)" |
| 308 | || "bounce('you have to store and sign your order');" |
| 309 | || "if (!is_valid_signed_package(trigger.data.order, $signer1))" |
| 310 | || "bounce('bad signature of order');" |
| 311 | || "$base_key1 = 'balance_' || $order.address || '_base';" |
| 312 | || "if (var[$ODEX][$base_key1] <= 1000)" |
| 313 | || "bounce('not enough balance for AA fees');}" |
| 314 | }", |
| 315 | "messages": [ |
| 316 | { |
| 317 | "{'if'}": "{"{var['low_fee_mode']}"}", |
| 318 | "app": "payment", |
| 319 | "payload": { |
| 320 | "asset": "base", |
| 321 | "outputs": [ |
| 322 | { |
| 323 | "address": "{"{trigger.address}"}", |
| 324 | "amount": 9500 |
| 325 | } |
| 326 | ] |
| 327 | } |
| 328 | }, |
| 329 | { |
| 330 | "app": "state", |
| 331 | "{'state'}": "{ |
| 332 | "{var[trigger.address] = json_stringify(trigger.data.order);" |
| 333 | || "if(var['low_fee_mode'] AND $total_bal < 30000)" |
| 334 | || "var['low_fee_mode'] = false;" |
| 335 | || "response['message'] = 'Stored order with id '||trigger.data.id;}" |
| 336 | }" |
| 337 | } |
| 338 | ] |
| 339 | }, |
| 340 | { |
| 341 | "messages": [ |
| 342 | { |
| 343 | "app": "state", |
| 344 | "{'state'}": "{ |
| 345 | "{response['message'] = 'bytes received';}" |
| 346 | }" |
| 347 | } |
| 348 | ] |
| 349 | } |
| 350 | ] |
| 351 | } |
| 352 | } |
| 353 | ] |
| 354 | } |
| 355 | }, |
| 356 | { |
| 357 | "app": "state", |
| 358 | "state": "{ |
| 359 | var['chainDEX'] = unit[response_unit].messages[[.app='definition']].payload.address; |
| 360 | }" |
| 361 | } |
| 362 | ] |
| 363 | }, |
| 364 | { |
| 365 | "if": "{ trigger.data.withdraw AND trigger.data.asset AND trigger.data.amount AND trigger.output[[asset=base]] >= 10000 }", |
| 366 | "init": "{ |
| 367 | if (trigger.data.to){ |
| 368 | if (!is_valid_address(trigger.data.to)) |
| 369 | bounce("invalid withdrawal address: " || trigger.data.to); |
| 370 | $address = trigger.data.to; |
| 371 | |
| 372 | $owner_address = var['grant_' || $address || '_to_' || trigger.address] ? $address : trigger.address; |
| 373 | } |
| 374 | else { |
| 375 | $address = trigger.address; |
| 376 | $owner_address = trigger.address; |
| 377 | } |
| 378 | $key = 'balance_' || $owner_address || '_' || trigger.data.asset; |
| 379 | $balance = var[$key] + 0; |
| 380 | |
| 381 | |
| 382 | $ord = json_parse(var[$CHAINDEX][trigger.address]); |
| 383 | if($ord){ |
| 384 | $ords = $ord.signed_message; |
| 385 | if($ords.sell_asset == trigger.data.asset){ |
| 386 | $id_ord = sha256($ords.address || $ords.sell_asset || $ords.buy_asset || $ords.sell_amount || $ords.price || ($ords.nonce otherwise '') || $ord.last_ball_unit); |
| 387 | $opened_order = var['amount_left_' || $id_ord] otherwise $ords.sell_amount; |
| 388 | if(trigger.data.amount == 'all' OR (trigger.data.amount+$opened_order) > $balance) |
| 389 | bounce('cannot withdraw, please cancel opened order on '||$CHAINDEX); |
| 390 | if ($ords.sell_asset == 'base' AND (trigger.data.amount == 'all' or var['balance_' || $owner_address || '_base' ]-trigger.data.amount <= 1000)) |
| 391 | bounce('not enough balance for AA fees, leave 1000 at least'); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
| 396 | if (trigger.data.amount == 'all') |
| 397 | $amount = $balance; |
| 398 | else if (trigger.data.amount > $balance) |
| 399 | bounce("withdrawal amount too large, balance: " || $balance); |
| 400 | else |
| 401 | $amount = trigger.data.amount; |
| 402 | }", |
| 403 | "messages": [ |
| 404 | { |
| 405 | "app": "payment", |
| 406 | "payload": { |
| 407 | "asset": "{trigger.data.asset}", |
| 408 | "outputs": [ |
| 409 | { |
| 410 | "address": "{$address}", |
| 411 | "amount": "{$amount}" |
| 412 | } |
| 413 | ] |
| 414 | } |
| 415 | }, |
| 416 | { |
| 417 | "app": "state", |
| 418 | "state": "{ |
| 419 | var[$key] -= $amount; |
| 420 | response[$owner_address || '_' || trigger.data.asset] = -$amount; |
| 421 | response['event'] = 'withdrawal'; |
| 422 | }" |
| 423 | } |
| 424 | ] |
| 425 | }, |
| 426 | { |
| 427 | "if": "{ |
| 428 | $order1 = trigger.data.order1.signed_message; |
| 429 | $order2 = trigger.data.order2.signed_message; |
| 430 | if (!$order1.sell_asset OR !$order2.sell_asset) |
| 431 | return false; |
| 432 | if ($order1.sell_asset != $order2.buy_asset OR $order1.buy_asset != $order2.sell_asset) |
| 433 | bounce('assets do not match'); |
| 434 | if ($order1.sell_asset == $order1.buy_asset) |
| 435 | bounce('same asset'); |
| 436 | |
| 437 | if ($order1.matcher != trigger.address) |
| 438 | bounce('wrong matcher in order1'); |
| 439 | if ($order2.matcher != trigger.address) |
| 440 | bounce('wrong matcher in order2'); |
| 441 | |
| 442 | if ($order1.aa != this_address) |
| 443 | bounce('wrong aa in order1'); |
| 444 | if ($order2.aa != this_address) |
| 445 | bounce('wrong aa in order2'); |
| 446 | |
| 447 | if ($order1.expiry_ts AND $order1.expiry_ts <= timestamp) |
| 448 | bounce("order1 expired"); |
| 449 | if ($order2.expiry_ts AND $order2.expiry_ts <= timestamp) |
| 450 | bounce("order2 expired"); |
| 451 | |
| 452 | $sell_key1 = 'balance_' || $order1.address || '_' || $order1.sell_asset; |
| 453 | $sell_key2 = 'balance_' || $order2.address || '_' || $order2.sell_asset; |
| 454 | |
| 455 | $id1 = sha256($order1.address || $order1.sell_asset || $order1.buy_asset || $order1.sell_amount || $order1.price || ($order1.nonce otherwise '') || (trigger.data.order1.last_ball_unit otherwise '-')); |
| 456 | $id2 = sha256($order2.address || $order2.sell_asset || $order2.buy_asset || $order2.sell_amount || $order2.price || ($order2.nonce otherwise '') || (trigger.data.order2.last_ball_unit otherwise '-')); |
| 457 | |
| 458 | if (var['executed_' || $id1]) |
| 459 | bounce('order1 already executed'); |
| 460 | if (var['executed_' || $id2]) |
| 461 | bounce('order2 already executed'); |
| 462 | |
| 463 | if (var['cancelled_' || $id1]) |
| 464 | bounce('order1 already cancelled'); |
| 465 | if (var['cancelled_' || $id2]) |
| 466 | bounce('order2 already cancelled'); |
| 467 | |
| 468 | $amount_left1 = var['amount_left_' || $id1] otherwise $order1.sell_amount; |
| 469 | $amount_left2 = var['amount_left_' || $id2] otherwise $order2.sell_amount; |
| 470 | |
| 471 | |
| 472 | if ($amount_left1 > var[$sell_key1] + 0) |
| 473 | bounce('not sufficient balance in sell asset to complete order1'); |
| 474 | if ($amount_left2 > var[$sell_key2] + 0) |
| 475 | bounce('not sufficient balance in sell asset to complete order2'); |
| 476 | |
| 477 | |
| 478 | $maker_price = $order1.price; |
| 479 | $buy_amount1 = round($amount_left1 * $order1.price); |
| 480 | if ($buy_amount1 > $amount_left2){ |
| 481 | $order_smaller = $order2; |
| 482 | $order_larger = $order1; |
| 483 | $id_smaller = $id2; |
| 484 | $id_larger = $id1; |
| 485 | $amount_left_smaller = $amount_left2; |
| 486 | $amount_left_larger = $amount_left1; |
| 487 | $buy_amount2 = round($amount_left2 / $maker_price); |
| 488 | $buy_amount_smaller = $buy_amount2; |
| 489 | $amount_sold2 = $amount_left2; |
| 490 | $amount_sold1 = $buy_amount2; |
| 491 | } |
| 492 | else{ |
| 493 | $order_smaller = $order1; |
| 494 | $order_larger = $order2; |
| 495 | $id_smaller = $id1; |
| 496 | $id_larger = $id2; |
| 497 | $amount_left_smaller = $amount_left1; |
| 498 | $amount_left_larger = $amount_left2; |
| 499 | $buy_amount_smaller = $buy_amount1; |
| 500 | $amount_sold1 = $amount_left1; |
| 501 | $amount_sold2 = $buy_amount1; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | if (round($amount_left_smaller * ($order1.price * $order2.price)) > $amount_left_smaller) |
| 506 | bounce("price mismatch"); |
| 507 | $expected_buy_amount_larger = round(($buy_amount_smaller-1) * $order_larger.price); |
| 508 | if ($expected_buy_amount_larger > $amount_left_smaller) |
| 509 | bounce("price mismatch: larger user " || $id_larger || " doesn't like the price, he gets less than expects: " || $amount_left_smaller || " < " || $expected_buy_amount_larger || ", amounts left: " || $amount_left1 || ", " || $amount_left2); |
| 510 | |
| 511 | |
| 512 | $max_matcher_fee1 = round($order1.matcher_fee * $amount_sold1/$order1.sell_amount); |
| 513 | $max_matcher_fee2 = round($order2.matcher_fee * $amount_sold2/$order2.sell_amount); |
| 514 | $matcher_fee1 = (!exists(trigger.data.matcher_fee1) OR trigger.data.matcher_fee1 == 'default') ? $max_matcher_fee1 : trigger.data.matcher_fee1; |
| 515 | $matcher_fee2 = (!exists(trigger.data.matcher_fee2) OR trigger.data.matcher_fee2 == 'default') ? $max_matcher_fee2 : trigger.data.matcher_fee2; |
| 516 | |
| 517 | if ($matcher_fee1 > $max_matcher_fee1) |
| 518 | bounce('matcher_fee1 is too large'); |
| 519 | if ($matcher_fee2 > $max_matcher_fee2) |
| 520 | bounce('matcher_fee2 is too large'); |
| 521 | |
| 522 | |
| 523 | if ($order1.affiliate){ |
| 524 | if (!$order1.affiliate_fee_asset) |
| 525 | bounce('no affiliate_fee_asset in order1'); |
| 526 | if ($order1.affiliate_fee < 0) |
| 527 | bounce('affiliate_fee < 0 in order1'); |
| 528 | $affiliate_fee1 = round($order1.affiliate_fee * $amount_sold1/$order1.sell_amount); |
| 529 | } |
| 530 | if ($order2.affiliate){ |
| 531 | if (!$order2.affiliate_fee_asset) |
| 532 | bounce('no affiliate_fee_asset in order2'); |
| 533 | if ($order2.affiliate_fee < 0) |
| 534 | bounce('affiliate_fee < 0 in order2'); |
| 535 | $affiliate_fee2 = round($order2.affiliate_fee * $amount_sold2/$order2.sell_amount); |
| 536 | } |
| 537 | |
| 538 | $signer1 = trigger.data.order1.authors[0].address; |
| 539 | $signer2 = trigger.data.order2.authors[0].address; |
| 540 | if ($signer1 != $order1.address AND !var['grant_' || $order1.address || '_to_' || $signer1]) |
| 541 | bounce("order1 signer was not authorized to sign"); |
| 542 | if ($signer2 != $order2.address AND !var['grant_' || $order2.address || '_to_' || $signer2]) |
| 543 | bounce("order2 signer was not authorized to sign"); |
| 544 | if (!is_valid_signed_package(trigger.data.order1, $signer1)) |
| 545 | bounce('bad signature of order1'); |
| 546 | if (!is_valid_signed_package(trigger.data.order2, $signer2)) |
| 547 | bounce('bad signature of order2'); |
| 548 | |
| 549 | true |
| 550 | }", |
| 551 | "messages": [ |
| 552 | { |
| 553 | "app": "state", |
| 554 | "state": "{ |
| 555 | $buy_key1 = 'balance_' || $order1.address || '_' || $order1.buy_asset; |
| 556 | $buy_key2 = 'balance_' || $order2.address || '_' || $order2.buy_asset; |
| 557 | var[$sell_key1] -= $amount_sold1; |
| 558 | var[$sell_key2] -= $amount_sold2; |
| 559 | var[$buy_key1] += $amount_sold2; |
| 560 | var[$buy_key2] += $amount_sold1; |
| 561 | |
| 562 | |
| 563 | $matcher_fee_user_key1 = 'balance_' || $order1.address || '_' || $order1.matcher_fee_asset; |
| 564 | $matcher_fee_user_key2 = 'balance_' || $order2.address || '_' || $order2.matcher_fee_asset; |
| 565 | $matcher_fee_matcher_key1 = 'balance_' || $order1.matcher || '_' || $order1.matcher_fee_asset; |
| 566 | $matcher_fee_matcher_key2 = 'balance_' || $order2.matcher || '_' || $order2.matcher_fee_asset; |
| 567 | var[$matcher_fee_matcher_key1] += $matcher_fee1; |
| 568 | var[$matcher_fee_matcher_key2] += $matcher_fee2; |
| 569 | var[$matcher_fee_user_key1] -= $matcher_fee1; |
| 570 | var[$matcher_fee_user_key2] -= $matcher_fee2; |
| 571 | if (var[$matcher_fee_user_key1] < 0) |
| 572 | bounce("not enough user1 balance for matcher fees"); |
| 573 | if (var[$matcher_fee_user_key2] < 0) |
| 574 | bounce("not enough user2 balance for matcher fees"); |
| 575 | |
| 576 | if (var[$matcher_fee_matcher_key1] < 0) |
| 577 | bounce("not enough matcher order1.matcher_fee_asset balance for matcher fees"); |
| 578 | if (var[$matcher_fee_matcher_key2] < 0) |
| 579 | bounce("not enough matcher order2.matcher_fee_asset balance for matcher fees"); |
| 580 | |
| 581 | |
| 582 | if ($order1.affiliate AND $affiliate_fee1){ |
| 583 | $affiliate_fee_user_key1 = 'balance_' || $order1.address || '_' || $order1.affiliate_fee_asset; |
| 584 | $affiliate_fee_affiliate_key1 = 'balance_' || $order1.affiliate || '_' || $order1.affiliate_fee_asset; |
| 585 | var[$affiliate_fee_user_key1] -= $affiliate_fee1; |
| 586 | var[$affiliate_fee_affiliate_key1] += $affiliate_fee1; |
| 587 | if (var[$affiliate_fee_user_key1] < 0) |
| 588 | bounce("not enough user1 balance for affiliate fees"); |
| 589 | } |
| 590 | if ($order2.affiliate AND $affiliate_fee2){ |
| 591 | $affiliate_fee_user_key2 = 'balance_' || $order2.address || '_' || $order2.affiliate_fee_asset; |
| 592 | $affiliate_fee_affiliate_key2 = 'balance_' || $order2.affiliate || '_' || $order2.affiliate_fee_asset; |
| 593 | var[$affiliate_fee_user_key2] -= $affiliate_fee2; |
| 594 | var[$affiliate_fee_affiliate_key2] += $affiliate_fee2; |
| 595 | if (var[$affiliate_fee_user_key2] < 0) |
| 596 | bounce("not enough user2 balance for affiliate fees"); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | $aa_fee = 1000; |
| 601 | $base_key1 = 'balance_' || $order1.address || '_base'; |
| 602 | $base_key2 = 'balance_' || $order2.address || '_base'; |
| 603 | var[$base_key1] -= $aa_fee; |
| 604 | var[$base_key2] -= $aa_fee; |
| 605 | if (var[$base_key1] < 0 OR var[$base_key2] < 0) |
| 606 | bounce('not enough balance for AA fees'); |
| 607 | |
| 608 | |
| 609 | var['balance_' || trigger.address || '_base'] += trigger.output[[asset=base]]; |
| 610 | |
| 611 | |
| 612 | var['executed_' || $id_smaller] = 1; |
| 613 | var['amount_left_' || $id_smaller] = false; |
| 614 | response['executed_' || $id_smaller] = 1; |
| 615 | $new_amount_left_larger = $amount_left_larger - $buy_amount_smaller; |
| 616 | if ($new_amount_left_larger < 0) |
| 617 | bounce("panic: new_amount_left_larger < 0"); |
| 618 | if ($new_amount_left_larger){ |
| 619 | var['amount_left_' || $id_larger] = $new_amount_left_larger; |
| 620 | response['amount_left_' || $id_larger] = $new_amount_left_larger; |
| 621 | } |
| 622 | else{ |
| 623 | var['executed_' || $id_larger] = 1; |
| 624 | var['amount_left_' || $id_larger] = false; |
| 625 | response['executed_' || $id_larger] = 1; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | if ($order1.address != $order2.address){ |
| 630 | response[$order1.address || '_' || $order1.sell_asset] = -$amount_sold1; |
| 631 | response[$order2.address || '_' || $order2.buy_asset] = $amount_sold1; |
| 632 | response[$order1.address || '_' || $order1.buy_asset] = $amount_sold2; |
| 633 | response[$order2.address || '_' || $order2.sell_asset] = -$amount_sold2; |
| 634 | } |
| 635 | else{ |
| 636 | response[$order1.address || '_' || $order1.sell_asset] = 0; |
| 637 | response[$order1.address || '_' || $order1.buy_asset] = 0; |
| 638 | } |
| 639 | response['amount_' || $order1.sell_asset] = $amount_sold1; |
| 640 | response['amount_' || $order1.buy_asset] = $amount_sold2; |
| 641 | response['event'] = 'trade'; |
| 642 | }" |
| 643 | } |
| 644 | ] |
| 645 | }, |
| 646 | { |
| 647 | "if": "{trigger.data.cancel AND trigger.data.order}", |
| 648 | "init": "{ |
| 649 | $order = trigger.data.order.signed_message; |
| 650 | if (!$order.sell_asset OR !$order.buy_asset OR !$order.sell_amount OR !$order.price OR !$order.address OR !$order.matcher) |
| 651 | bounce("missing data in order"); |
| 652 | |
| 653 | if(trigger.address != $CHAINDEX){ |
| 654 | if($order.matcher == $CHAINDEX) |
| 655 | bounce('please cancel order through '||$CHAINDEX); |
| 656 | if ($order.address != trigger.address AND !var['grant_' || $order.address || '_to_' || trigger.address]) |
| 657 | bounce('not your order'); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | if ($order.aa != this_address) |
| 662 | bounce('wrong aa in order'); |
| 663 | |
| 664 | if ($order.expiry_ts AND $order.expiry_ts <= timestamp) |
| 665 | bounce("order expired"); |
| 666 | |
| 667 | $id = sha256($order.address || $order.sell_asset || $order.buy_asset || $order.sell_amount || $order.price || ($order.nonce otherwise '') || trigger.data.order.last_ball_unit); |
| 668 | |
| 669 | if (var['executed_' || $id]) |
| 670 | bounce('order already executed'); |
| 671 | |
| 672 | $signer = trigger.data.order.authors[0].address; |
| 673 | if ($signer != $order.address AND !var['grant_' || $order.address || '_to_' || $signer]) |
| 674 | bounce("order signer was not authorized to sign"); |
| 675 | if (!is_valid_signed_package(trigger.data.order, $signer)) |
| 676 | bounce('bad signature of order'); |
| 677 | }", |
| 678 | "messages": [ |
| 679 | { |
| 680 | "app": "state", |
| 681 | "state": "{ |
| 682 | var['cancelled_' || $id] = 1; |
| 683 | response['message'] = 'cancelled order ' || $id; |
| 684 | response['id'] = $id; |
| 685 | response['event'] = 'cancel'; |
| 686 | |
| 687 | $cancel_fee = 1000; |
| 688 | $key = 'balance_' || ($order.matcher == $CHAINDEX?$CHAINDEX:$order.address) || '_base'; |
| 689 | var[$key] += trigger.output[[asset=base]] - $cancel_fee; |
| 690 | if (var[$key] < 0) |
| 691 | bounce("balance would drop below 0"); |
| 692 | }" |
| 693 | } |
| 694 | ] |
| 695 | }, |
| 696 | { |
| 697 | "if": "{ trigger.data.revoke AND trigger.data.address AND trigger.output[[asset=base]] >= 10000 }", |
| 698 | "init": "{ |
| 699 | if (!is_valid_address(trigger.data.address)) |
| 700 | bounce("invalid address"); |
| 701 | if (trigger.data.address == trigger.address) |
| 702 | bounce("same address"); |
| 703 | }", |
| 704 | "messages": [ |
| 705 | { |
| 706 | "app": "state", |
| 707 | "state": "{ |
| 708 | var['grant_' || trigger.address || '_to_' || trigger.data.address] = false; |
| 709 | response['message'] = 'revoked authorization from ' || trigger.data.address || ' to sign orders for you'; |
| 710 | response['address'] = trigger.data.address; |
| 711 | response['event'] = 'revocation'; |
| 712 | }" |
| 713 | } |
| 714 | ] |
| 715 | }, |
| 716 | { |
| 717 | "if": "{ (!trigger.data OR trigger.data.to OR trigger.data.grant AND trigger.data.address) AND trigger.output[[asset=base]] >= 10000 }", |
| 718 | "init": "{ |
| 719 | if (trigger.data.grant){ |
| 720 | if (!is_valid_address(trigger.data.address)) |
| 721 | bounce("invalid address"); |
| 722 | if (trigger.data.address == trigger.address) |
| 723 | bounce("same address"); |
| 724 | if (trigger.data.to) |
| 725 | bounce("grant and to at the same time"); |
| 726 | } |
| 727 | }", |
| 728 | "messages": [ |
| 729 | { |
| 730 | "app": "state", |
| 731 | "state": "{ |
| 732 | if (trigger.data.grant){ |
| 733 | var['grant_' || trigger.address || '_to_' || trigger.data.address] = 1; |
| 734 | $response_grant = 'authorized ' || trigger.data.address || ' to sign orders for you, also '; |
| 735 | response['authorized_address'] = trigger.data.address; |
| 736 | response['event'] = 'grant'; |
| 737 | } |
| 738 | $asset = trigger.output[[asset!=base]].asset; |
| 739 | if ($asset == 'ambiguous') |
| 740 | bounce('ambiguous asset'); |
| 741 | if (trigger.data.to){ |
| 742 | if (!is_valid_address(trigger.data.to)) |
| 743 | bounce("invalid deposit address: " || trigger.data.to); |
| 744 | $address = trigger.data.to; |
| 745 | } |
| 746 | else |
| 747 | $address = trigger.address; |
| 748 | $base_key = 'balance_'||$address||'_'||'base'; |
| 749 | var[$base_key] = var[$base_key] + trigger.output[[asset=base]]; |
| 750 | $response_base = trigger.output[[asset=base]] || ' bytes'; |
| 751 | response[$address || '_base'] = trigger.output[[asset=base]]; |
| 752 | if ($asset != 'none'){ |
| 753 | $asset_key = 'balance_'||$address||'_'||$asset; |
| 754 | var[$asset_key] = var[$asset_key] + trigger.output[[asset=$asset]]; |
| 755 | $response_asset = ' and ' || trigger.output[[asset=$asset]] || ' of ' || $asset; |
| 756 | response[$address || '_' || $asset] = trigger.output[[asset=$asset]]; |
| 757 | } |
| 758 | response['message'] = ($response_grant otherwise '') || 'accepted coins: ' || $response_base || ($response_asset otherwise ''); |
| 759 | response[trigger.data.grant ? 'secondary_event' : 'event'] = 'deposit'; |
| 760 | }" |
| 761 | } |
| 762 | ] |
| 763 | } |
| 764 | ] |
| 765 | } |
| 766 | } |
| 767 | ] |