| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://odex.ooo/odex.json", |
| 5 | "getters": "{ |
| 6 | |
| 7 | |
| 8 | |
| 9 | $get_order_id = ($order) => { |
| 10 | $sm = $order.signed_message; |
| 11 | require(asset[$sm.sell_asset].exists, "no such sell_asset"); |
| 12 | require(asset[$sm.buy_asset].exists, "no such buy_asset"); |
| 13 | if (typeof($sm.nonce) == 'string' AND contains($sm.nonce, '_')) |
| 14 | bounce("invalid nonce: " || $sm.nonce); |
| 15 | $fields = [$sm.address, $sm.sell_asset, $sm.buy_asset, $sm.sell_amount, $sm.price, $sm.nonce otherwise '', $order.last_ball_unit otherwise '-']; |
| 16 | sha256(join($fields, '_')) |
| 17 | }; |
| 18 | }", |
| 19 | "messages": { |
| 20 | "cases": [ |
| 21 | { |
| 22 | "if": "{ trigger.data.withdraw AND trigger.data.asset AND trigger.data.amount AND trigger.output[[asset=base]] >= 10000 }", |
| 23 | "init": "{ |
| 24 | if (trigger.data.to){ |
| 25 | if (!is_valid_address(trigger.data.to)) |
| 26 | bounce("invalid withdrawal address: " || trigger.data.to); |
| 27 | $address = trigger.data.to; |
| 28 | |
| 29 | $owner_address = var['grant_' || $address || '_to_' || trigger.address] ? $address : trigger.address; |
| 30 | } |
| 31 | else { |
| 32 | $address = trigger.address; |
| 33 | $owner_address = trigger.address; |
| 34 | } |
| 35 | $key = 'balance_' || $owner_address || '_' || trigger.data.asset; |
| 36 | $balance = var[$key] + 0; |
| 37 | if (trigger.data.amount == 'all') |
| 38 | $amount = $balance; |
| 39 | else if (trigger.data.amount > $balance) |
| 40 | bounce("withdrawal amount too large, balance: " || $balance); |
| 41 | else |
| 42 | $amount = trigger.data.amount; |
| 43 | }", |
| 44 | "messages": [ |
| 45 | { |
| 46 | "app": "payment", |
| 47 | "payload": { |
| 48 | "asset": "{trigger.data.asset}", |
| 49 | "outputs": [ |
| 50 | { |
| 51 | "address": "{$address}", |
| 52 | "amount": "{$amount}" |
| 53 | } |
| 54 | ] |
| 55 | } |
| 56 | }, |
| 57 | { |
| 58 | "app": "state", |
| 59 | "state": "{ |
| 60 | var[$key] -= $amount; |
| 61 | response[$owner_address || '_' || trigger.data.asset] = -$amount; |
| 62 | response['event'] = 'withdrawal'; |
| 63 | }" |
| 64 | } |
| 65 | ] |
| 66 | }, |
| 67 | { |
| 68 | "if": "{ |
| 69 | $order1 = trigger.data.order1.signed_message; |
| 70 | $order2 = trigger.data.order2.signed_message; |
| 71 | if (!$order1.sell_asset OR !$order2.sell_asset) |
| 72 | return false; |
| 73 | require($order1.sell_amount > 0 AND $order2.sell_amount > 0, "sell_amount must be positive"); |
| 74 | require($order1.price > 0 AND $order2.price > 0, "price must be positive"); |
| 75 | require($order1.matcher_fee >= 0 AND $order2.matcher_fee >= 0, "order matcher_fee must be positive or 0"); |
| 76 | if ($order1.sell_asset != $order2.buy_asset OR $order1.buy_asset != $order2.sell_asset) |
| 77 | bounce('assets do not match'); |
| 78 | if ($order1.sell_asset == $order1.buy_asset) |
| 79 | bounce('same asset'); |
| 80 | |
| 81 | if ($order1.matcher != trigger.address) |
| 82 | bounce('wrong matcher in order1'); |
| 83 | if ($order2.matcher != trigger.address) |
| 84 | bounce('wrong matcher in order2'); |
| 85 | |
| 86 | if ($order1.aa != this_address) |
| 87 | bounce('wrong aa in order1'); |
| 88 | if ($order2.aa != this_address) |
| 89 | bounce('wrong aa in order2'); |
| 90 | |
| 91 | if ($order1.expiry_ts AND $order1.expiry_ts <= timestamp) |
| 92 | bounce("order1 expired"); |
| 93 | if ($order2.expiry_ts AND $order2.expiry_ts <= timestamp) |
| 94 | bounce("order2 expired"); |
| 95 | |
| 96 | $sell_key1 = 'balance_' || $order1.address || '_' || $order1.sell_asset; |
| 97 | $sell_key2 = 'balance_' || $order2.address || '_' || $order2.sell_asset; |
| 98 | |
| 99 | $id1 = $get_order_id(trigger.data.order1); |
| 100 | $id2 = $get_order_id(trigger.data.order2); |
| 101 | |
| 102 | if (var['executed_' || $id1]) |
| 103 | bounce('order1 already executed'); |
| 104 | if (var['executed_' || $id2]) |
| 105 | bounce('order2 already executed'); |
| 106 | |
| 107 | if (var['cancelled_' || $id1]) |
| 108 | bounce('order1 already cancelled'); |
| 109 | if (var['cancelled_' || $id2]) |
| 110 | bounce('order2 already cancelled'); |
| 111 | |
| 112 | $amount_left1 = var['amount_left_' || $id1] otherwise $order1.sell_amount; |
| 113 | $amount_left2 = var['amount_left_' || $id2] otherwise $order2.sell_amount; |
| 114 | |
| 115 | |
| 116 | if ($amount_left1 > var[$sell_key1] + 0) |
| 117 | bounce('not sufficient balance in sell asset to complete order1'); |
| 118 | if ($amount_left2 > var[$sell_key2] + 0) |
| 119 | bounce('not sufficient balance in sell asset to complete order2'); |
| 120 | |
| 121 | |
| 122 | $maker_price = $order1.price; |
| 123 | $buy_amount1 = round($amount_left1 * $order1.price); |
| 124 | if ($buy_amount1 > $amount_left2){ |
| 125 | $order_smaller = $order2; |
| 126 | $order_larger = $order1; |
| 127 | $id_smaller = $id2; |
| 128 | $id_larger = $id1; |
| 129 | $amount_left_smaller = $amount_left2; |
| 130 | $amount_left_larger = $amount_left1; |
| 131 | $buy_amount2 = round($amount_left2 / $maker_price); |
| 132 | $buy_amount_smaller = $buy_amount2; |
| 133 | $amount_sold2 = $amount_left2; |
| 134 | $amount_sold1 = $buy_amount2; |
| 135 | } |
| 136 | else{ |
| 137 | $order_smaller = $order1; |
| 138 | $order_larger = $order2; |
| 139 | $id_smaller = $id1; |
| 140 | $id_larger = $id2; |
| 141 | $amount_left_smaller = $amount_left1; |
| 142 | $amount_left_larger = $amount_left2; |
| 143 | $buy_amount_smaller = $buy_amount1; |
| 144 | $amount_sold1 = $amount_left1; |
| 145 | $amount_sold2 = $buy_amount1; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | if (round($amount_left_smaller * ($order1.price * $order2.price)) > $amount_left_smaller) |
| 150 | bounce("price mismatch"); |
| 151 | $expected_buy_amount_larger = round(($buy_amount_smaller-1) * $order_larger.price); |
| 152 | if ($expected_buy_amount_larger > $amount_left_smaller) |
| 153 | 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); |
| 154 | |
| 155 | |
| 156 | $max_matcher_fee1 = round($order1.matcher_fee * $amount_sold1/$order1.sell_amount); |
| 157 | $max_matcher_fee2 = round($order2.matcher_fee * $amount_sold2/$order2.sell_amount); |
| 158 | $matcher_fee1 = (!exists(trigger.data.matcher_fee1) OR trigger.data.matcher_fee1 == 'default') ? $max_matcher_fee1 : trigger.data.matcher_fee1; |
| 159 | $matcher_fee2 = (!exists(trigger.data.matcher_fee2) OR trigger.data.matcher_fee2 == 'default') ? $max_matcher_fee2 : trigger.data.matcher_fee2; |
| 160 | require($matcher_fee1 >= 0 AND $matcher_fee2 >= 0, "trade matcher_fee must be positive or 0"); |
| 161 | |
| 162 | if ($matcher_fee1 > $max_matcher_fee1) |
| 163 | bounce('matcher_fee1 is too large'); |
| 164 | if ($matcher_fee2 > $max_matcher_fee2) |
| 165 | bounce('matcher_fee2 is too large'); |
| 166 | |
| 167 | |
| 168 | if ($order1.affiliate){ |
| 169 | if (!$order1.affiliate_fee_asset) |
| 170 | bounce('no affiliate_fee_asset in order1'); |
| 171 | if ($order1.affiliate_fee < 0) |
| 172 | bounce('affiliate_fee < 0 in order1'); |
| 173 | $affiliate_fee1 = round($order1.affiliate_fee * $amount_sold1/$order1.sell_amount); |
| 174 | } |
| 175 | if ($order2.affiliate){ |
| 176 | if (!$order2.affiliate_fee_asset) |
| 177 | bounce('no affiliate_fee_asset in order2'); |
| 178 | if ($order2.affiliate_fee < 0) |
| 179 | bounce('affiliate_fee < 0 in order2'); |
| 180 | $affiliate_fee2 = round($order2.affiliate_fee * $amount_sold2/$order2.sell_amount); |
| 181 | } |
| 182 | |
| 183 | $signer1 = trigger.data.order1.authors[0].address; |
| 184 | $signer2 = trigger.data.order2.authors[0].address; |
| 185 | if ($signer1 != $order1.address AND !var['grant_' || $order1.address || '_to_' || $signer1]) |
| 186 | bounce("order1 signer was not authorized to sign"); |
| 187 | if ($signer2 != $order2.address AND !var['grant_' || $order2.address || '_to_' || $signer2]) |
| 188 | bounce("order2 signer was not authorized to sign"); |
| 189 | if (!is_valid_signed_package(trigger.data.order1, $signer1)) |
| 190 | bounce('bad signature of order1'); |
| 191 | if (!is_valid_signed_package(trigger.data.order2, $signer2)) |
| 192 | bounce('bad signature of order2'); |
| 193 | |
| 194 | true |
| 195 | }", |
| 196 | "messages": [ |
| 197 | { |
| 198 | "app": "state", |
| 199 | "state": "{ |
| 200 | $buy_key1 = 'balance_' || $order1.address || '_' || $order1.buy_asset; |
| 201 | $buy_key2 = 'balance_' || $order2.address || '_' || $order2.buy_asset; |
| 202 | var[$sell_key1] -= $amount_sold1; |
| 203 | var[$sell_key2] -= $amount_sold2; |
| 204 | var[$buy_key1] += $amount_sold2; |
| 205 | var[$buy_key2] += $amount_sold1; |
| 206 | require(var[$sell_key1] >= 0 AND var[$sell_key2] >= 0 AND var[$buy_key1] >= 0 AND var[$buy_key2] >= 0, "some balances would drop below 0"); |
| 207 | |
| 208 | |
| 209 | $matcher_fee_user_key1 = 'balance_' || $order1.address || '_' || $order1.matcher_fee_asset; |
| 210 | $matcher_fee_user_key2 = 'balance_' || $order2.address || '_' || $order2.matcher_fee_asset; |
| 211 | $matcher_fee_matcher_key1 = 'balance_' || $order1.matcher || '_' || $order1.matcher_fee_asset; |
| 212 | $matcher_fee_matcher_key2 = 'balance_' || $order2.matcher || '_' || $order2.matcher_fee_asset; |
| 213 | var[$matcher_fee_matcher_key1] += $matcher_fee1; |
| 214 | var[$matcher_fee_matcher_key2] += $matcher_fee2; |
| 215 | var[$matcher_fee_user_key1] -= $matcher_fee1; |
| 216 | var[$matcher_fee_user_key2] -= $matcher_fee2; |
| 217 | if (var[$matcher_fee_user_key1] < 0) |
| 218 | bounce("not enough user1 balance for matcher fees"); |
| 219 | if (var[$matcher_fee_user_key2] < 0) |
| 220 | bounce("not enough user2 balance for matcher fees"); |
| 221 | |
| 222 | if (var[$matcher_fee_matcher_key1] < 0) |
| 223 | bounce("not enough matcher order1.matcher_fee_asset balance for matcher fees"); |
| 224 | if (var[$matcher_fee_matcher_key2] < 0) |
| 225 | bounce("not enough matcher order2.matcher_fee_asset balance for matcher fees"); |
| 226 | |
| 227 | |
| 228 | if ($order1.affiliate AND $affiliate_fee1){ |
| 229 | $affiliate_fee_user_key1 = 'balance_' || $order1.address || '_' || $order1.affiliate_fee_asset; |
| 230 | $affiliate_fee_affiliate_key1 = 'balance_' || $order1.affiliate || '_' || $order1.affiliate_fee_asset; |
| 231 | var[$affiliate_fee_user_key1] -= $affiliate_fee1; |
| 232 | var[$affiliate_fee_affiliate_key1] += $affiliate_fee1; |
| 233 | if (var[$affiliate_fee_user_key1] < 0) |
| 234 | bounce("not enough user1 balance for affiliate fees"); |
| 235 | } |
| 236 | if ($order2.affiliate AND $affiliate_fee2){ |
| 237 | $affiliate_fee_user_key2 = 'balance_' || $order2.address || '_' || $order2.affiliate_fee_asset; |
| 238 | $affiliate_fee_affiliate_key2 = 'balance_' || $order2.affiliate || '_' || $order2.affiliate_fee_asset; |
| 239 | var[$affiliate_fee_user_key2] -= $affiliate_fee2; |
| 240 | var[$affiliate_fee_affiliate_key2] += $affiliate_fee2; |
| 241 | if (var[$affiliate_fee_user_key2] < 0) |
| 242 | bounce("not enough user2 balance for affiliate fees"); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | $aa_fee = 1000; |
| 247 | $base_key1 = 'balance_' || $order1.address || '_base'; |
| 248 | $base_key2 = 'balance_' || $order2.address || '_base'; |
| 249 | var[$base_key1] -= $aa_fee; |
| 250 | var[$base_key2] -= $aa_fee; |
| 251 | if (var[$base_key1] < 0 OR var[$base_key2] < 0) |
| 252 | bounce('not enough balance for AA fees'); |
| 253 | |
| 254 | |
| 255 | var['balance_' || trigger.address || '_base'] += trigger.output[[asset=base]]; |
| 256 | |
| 257 | |
| 258 | var['executed_' || $id_smaller] = 1; |
| 259 | var['amount_left_' || $id_smaller] = false; |
| 260 | response['executed_' || $id_smaller] = 1; |
| 261 | $new_amount_left_larger = $amount_left_larger - $buy_amount_smaller; |
| 262 | if ($new_amount_left_larger < 0) |
| 263 | bounce("panic: new_amount_left_larger < 0"); |
| 264 | if ($new_amount_left_larger){ |
| 265 | var['amount_left_' || $id_larger] = $new_amount_left_larger; |
| 266 | response['amount_left_' || $id_larger] = $new_amount_left_larger; |
| 267 | } |
| 268 | else{ |
| 269 | var['executed_' || $id_larger] = 1; |
| 270 | var['amount_left_' || $id_larger] = false; |
| 271 | response['executed_' || $id_larger] = 1; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | if ($order1.address != $order2.address){ |
| 276 | response[$order1.address || '_' || $order1.sell_asset] = -$amount_sold1; |
| 277 | response[$order2.address || '_' || $order2.buy_asset] = $amount_sold1; |
| 278 | response[$order1.address || '_' || $order1.buy_asset] = $amount_sold2; |
| 279 | response[$order2.address || '_' || $order2.sell_asset] = -$amount_sold2; |
| 280 | } |
| 281 | else{ |
| 282 | response[$order1.address || '_' || $order1.sell_asset] = 0; |
| 283 | response[$order1.address || '_' || $order1.buy_asset] = 0; |
| 284 | } |
| 285 | response['amount_' || $order1.sell_asset] = $amount_sold1; |
| 286 | response['amount_' || $order1.buy_asset] = $amount_sold2; |
| 287 | response['event'] = 'trade'; |
| 288 | }" |
| 289 | } |
| 290 | ] |
| 291 | }, |
| 292 | { |
| 293 | "if": "{trigger.data.cancel AND trigger.data.order}", |
| 294 | "init": "{ |
| 295 | $order = trigger.data.order.signed_message; |
| 296 | if (!$order.sell_asset OR !$order.buy_asset OR !$order.sell_amount OR !$order.price OR !$order.address OR !$order.matcher) |
| 297 | bounce("missing data in order"); |
| 298 | |
| 299 | if ($order.address != trigger.address AND !var['grant_' || $order.address || '_to_' || trigger.address]) |
| 300 | bounce('not your order'); |
| 301 | |
| 302 | if ($order.aa != this_address) |
| 303 | bounce('wrong aa in order'); |
| 304 | |
| 305 | if ($order.expiry_ts AND $order.expiry_ts <= timestamp) |
| 306 | bounce("order expired"); |
| 307 | |
| 308 | $id = $get_order_id(trigger.data.order); |
| 309 | |
| 310 | if (var['executed_' || $id]) |
| 311 | bounce('order already executed'); |
| 312 | |
| 313 | $signer = trigger.data.order.authors[0].address; |
| 314 | if ($signer != $order.address AND !var['grant_' || $order.address || '_to_' || $signer]) |
| 315 | bounce("order signer was not authorized to sign"); |
| 316 | if (!is_valid_signed_package(trigger.data.order, $signer)) |
| 317 | bounce('bad signature of order'); |
| 318 | }", |
| 319 | "messages": [ |
| 320 | { |
| 321 | "app": "state", |
| 322 | "state": "{ |
| 323 | var['cancelled_' || $id] = 1; |
| 324 | response['message'] = 'cancelled order ' || $id; |
| 325 | response['id'] = $id; |
| 326 | response['event'] = 'cancel'; |
| 327 | |
| 328 | $cancel_fee = 1000; |
| 329 | $key = 'balance_' || $order.address || '_base'; |
| 330 | var[$key] += trigger.output[[asset=base]] - $cancel_fee; |
| 331 | if (var[$key] < 0) |
| 332 | bounce("balance would drop below 0"); |
| 333 | }" |
| 334 | } |
| 335 | ] |
| 336 | }, |
| 337 | { |
| 338 | "if": "{ trigger.data.revoke AND trigger.data.address AND trigger.output[[asset=base]] >= 10000 }", |
| 339 | "init": "{ |
| 340 | if (!is_valid_address(trigger.data.address)) |
| 341 | bounce("invalid address"); |
| 342 | if (trigger.data.address == trigger.address) |
| 343 | bounce("same address"); |
| 344 | }", |
| 345 | "messages": [ |
| 346 | { |
| 347 | "app": "state", |
| 348 | "state": "{ |
| 349 | var['grant_' || trigger.address || '_to_' || trigger.data.address] = false; |
| 350 | response['message'] = 'revoked authorization from ' || trigger.data.address || ' to sign orders for you'; |
| 351 | response['address'] = trigger.data.address; |
| 352 | response['event'] = 'revocation'; |
| 353 | }" |
| 354 | } |
| 355 | ] |
| 356 | }, |
| 357 | { |
| 358 | "if": "{ (!trigger.data OR trigger.data.to OR trigger.data.grant AND trigger.data.address) AND trigger.output[[asset=base]] >= 10000 }", |
| 359 | "init": "{ |
| 360 | if (trigger.data.grant){ |
| 361 | if (!is_valid_address(trigger.data.address)) |
| 362 | bounce("invalid address"); |
| 363 | if (trigger.data.address == trigger.address) |
| 364 | bounce("same address"); |
| 365 | if (trigger.data.to) |
| 366 | bounce("grant and to at the same time"); |
| 367 | } |
| 368 | }", |
| 369 | "messages": [ |
| 370 | { |
| 371 | "app": "state", |
| 372 | "state": "{ |
| 373 | if (trigger.data.grant){ |
| 374 | var['grant_' || trigger.address || '_to_' || trigger.data.address] = 1; |
| 375 | $response_grant = 'authorized ' || trigger.data.address || ' to sign orders for you, also '; |
| 376 | response['authorized_address'] = trigger.data.address; |
| 377 | response['event'] = 'grant'; |
| 378 | } |
| 379 | $asset = trigger.output[[asset!=base]].asset; |
| 380 | if ($asset == 'ambiguous') |
| 381 | bounce('ambiguous asset'); |
| 382 | if (trigger.data.to){ |
| 383 | if (!is_valid_address(trigger.data.to)) |
| 384 | bounce("invalid deposit address: " || trigger.data.to); |
| 385 | $address = trigger.data.to; |
| 386 | } |
| 387 | else |
| 388 | $address = trigger.address; |
| 389 | $base_key = 'balance_'||$address||'_'||'base'; |
| 390 | var[$base_key] = var[$base_key] + trigger.output[[asset=base]]; |
| 391 | $response_base = trigger.output[[asset=base]] || ' bytes'; |
| 392 | response[$address || '_base'] = trigger.output[[asset=base]]; |
| 393 | if ($asset != 'none'){ |
| 394 | $asset_key = 'balance_'||$address||'_'||$asset; |
| 395 | var[$asset_key] = var[$asset_key] + trigger.output[[asset=$asset]]; |
| 396 | $response_asset = ' and ' || trigger.output[[asset=$asset]] || ' of ' || $asset; |
| 397 | response[$address || '_' || $asset] = trigger.output[[asset=$asset]]; |
| 398 | } |
| 399 | response['message'] = ($response_grant otherwise '') || 'accepted coins: ' || $response_base || ($response_asset otherwise ''); |
| 400 | response[trigger.data.grant ? 'secondary_event' : 'event'] = 'deposit'; |
| 401 | }" |
| 402 | } |
| 403 | ] |
| 404 | } |
| 405 | ] |
| 406 | } |
| 407 | } |
| 408 | ] |