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