| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $BYTE_ASSET = var['7T3MIJFUUZD23DZJ6BDDGBZPNS6RYFAM']['asset']; |
| 6 | $BASE_INP = 9300; |
| 7 | $asset_wanted = typeof(trigger.data.want) == 'string' AND trigger.data.want=='bytes'?$BYTE_ASSET:trigger.data.want; |
| 8 | $asset_inp = trigger.output[[asset!=base]]; |
| 9 | if(trigger.output[[asset!=base]].asset != 'none') |
| 10 | $asset = trigger.output[[asset!=base]].asset; |
| 11 | if(!trigger.data.cancel){ |
| 12 | if(!$asset) bounce('You must send an asset to offer for exchange. To fill already created order, send fill = 1'); |
| 13 | if(!$asset_wanted OR !asset[$asset_wanted].exists) bounce('want param missing. You must send asset you ask for which will be atomically exchanged for yours or send "bytes" if you want a asset redeemable for bytes, issued by AA 7T3MIJFUUZD23DZJ6BDDGBZPNS6RYFAM'); |
| 14 | if($asset == 'ambiguous') bounce('Only 1 asset per tx'); |
| 15 | } |
| 16 | $asset_offered = $asset; |
| 17 | $fill_pair = sha256($asset_wanted||'_'||$asset_offered); |
| 18 | $price = trigger.data.price; |
| 19 | $orders_to_fill = $fill_pair||'_'||$price; |
| 20 | }", |
| 21 | "messages": { |
| 22 | "cases": [ |
| 23 | { |
| 24 | "if": "{trigger.data.cancel}", |
| 25 | "init": "{ |
| 26 | $asset_ofered = trigger.data.asset; |
| 27 | if(!$asset_ofered OR !asset[$asset_ofered].exists) bounce('asset param missing, send asset you offered in your order'); |
| 28 | if(!$asset_wanted OR !asset[$asset_wanted].exists) bounce('want param missing, send asset you asked for to cancel'); |
| 29 | if(!$price OR !is_integer($price)) bounce('price param missing, send price of your order'); |
| 30 | $order_id = sha256($asset_ofered||'_'||$asset_wanted)||'_'||$price||'_'||trigger.address; |
| 31 | if(!var[$order_id]) bounce('Order not found, check params'); |
| 32 | }", |
| 33 | "messages": [ |
| 34 | { |
| 35 | "app": "payment", |
| 36 | "payload": { |
| 37 | "asset": "{$asset_ofered}", |
| 38 | "outputs": [ |
| 39 | { |
| 40 | "address": "{trigger.address}", |
| 41 | "amount": "{var[$order_id]}" |
| 42 | } |
| 43 | ] |
| 44 | } |
| 45 | }, |
| 46 | { |
| 47 | "app": "payment", |
| 48 | "payload": { |
| 49 | "asset": "base", |
| 50 | "outputs": [ |
| 51 | { |
| 52 | "address": "{trigger.address}", |
| 53 | "amount": "{$BASE_INP}" |
| 54 | } |
| 55 | ] |
| 56 | } |
| 57 | }, |
| 58 | { |
| 59 | "app": "state", |
| 60 | "state": "{ |
| 61 | var[$cancel_key] -= 1; |
| 62 | if(var[$cancel_key] == 0){ |
| 63 | var[$cancel_key]=false; |
| 64 | var[$pair_key] = false; |
| 65 | } |
| 66 | $am = var[$order_id]; |
| 67 | var[$order_id] = false; |
| 68 | response['message'] = 'Canceled order to sell '||$am||' '||$asset_ofered||' at '||$price||' '||$asset_wanted||' per unit'; |
| 69 | }" |
| 70 | } |
| 71 | ] |
| 72 | }, |
| 73 | { |
| 74 | "if": "{trigger.data.fill}", |
| 75 | "init": "{ |
| 76 | $to_fill = trigger.data.address; |
| 77 | $fill_key = $orders_to_fill||'_'||$to_fill; |
| 78 | if(!var[$fill_pair]) bounce('No orders to fill for this pair'); |
| 79 | if(!var[$orders_to_fill]) bounce('No orders to fill, check price param. There are '||var[$fill_pair]||' orders who match, to know address to fill check '||this_address||' state vars around '||$orders_to_fill); |
| 80 | if(!$price OR !is_integer($price)) bounce('price param missing or invalid'); |
| 81 | if(!$to_fill OR !is_valid_address($to_fill)) bounce('address param missing or invalid.'); |
| 82 | if(!var[$fill_key]) bounce('Address has no created order on this pair'); |
| 83 | $a_amount = var[$fill_key]; |
| 84 | $in_exch = ($a_amount*$price_fill); |
| 85 | if($asset_inp < $in_exch) bounce('Not enough funds sent to fill the order, need '||$in_exch); |
| 86 | $b_amount = $a_amount*$price_fill; |
| 87 | $return_amount = ($asset_inp - $in_exch) > 0?($asset_inp - $in_exch):0; |
| 88 | }", |
| 89 | "messages": [ |
| 90 | { |
| 91 | "app": "payment", |
| 92 | "payload": { |
| 93 | "asset": "{$asset_offered}", |
| 94 | "outputs": [ |
| 95 | { |
| 96 | "address": "{$to_fill}", |
| 97 | "amount": "{$in_exch+$return_amount}" |
| 98 | } |
| 99 | ] |
| 100 | } |
| 101 | }, |
| 102 | { |
| 103 | "app": "payment", |
| 104 | "payload": { |
| 105 | "asset": "{$asset_wanted}", |
| 106 | "outputs": [ |
| 107 | { |
| 108 | "address": "{trigger.address}", |
| 109 | "amount": "{$a_amount}" |
| 110 | } |
| 111 | ] |
| 112 | } |
| 113 | }, |
| 114 | { |
| 115 | "app": "payment", |
| 116 | "payload": { |
| 117 | "asset": "base", |
| 118 | "outputs": [ |
| 119 | { |
| 120 | "address": "{trigger.address}", |
| 121 | "amount": "{$BASE_INP}" |
| 122 | }, |
| 123 | { |
| 124 | "address": "{$to_fill}", |
| 125 | "amount": "{$BASE_INP}" |
| 126 | } |
| 127 | ] |
| 128 | } |
| 129 | }, |
| 130 | { |
| 131 | "app": "state", |
| 132 | "state": "{ |
| 133 | var[$orders_to_fill] -= 1; |
| 134 | if(var[$orders_to_fill] == 0){ |
| 135 | var[$orders_to_fill]=false; |
| 136 | var[$fill_pair] = false; |
| 137 | } |
| 138 | response['message'] = 'Exchanged '||$a_amount||' '||$asset_wanted||' for '||$b_amount||' '||$asset_offered; |
| 139 | var[$fill_key] = false; |
| 140 | }" |
| 141 | } |
| 142 | ] |
| 143 | }, |
| 144 | { |
| 145 | "init": "{ |
| 146 | $ord_pair = sha256($asset_offered||'_'||$asset_wanted); |
| 147 | $ord_key = $ord_pair ||'_'||$price; |
| 148 | $ord_addr_key = $ord_pair||'_'||$price||'_'||trigger.address; |
| 149 | if(var[$orders_to_fill]) bounce('There are '||var[$orders_to_fill]||' orders who match, to know address to fill check '||this_address||' state vars around '||$orders_to_fill||'. Send fill = 1 or change params to continue'); |
| 150 | if(!$price OR !is_integer($price)) bounce('price param missing or invalid. Send a price (in assets wanted per asset offered) to create order'); |
| 151 | if(var[$ord_addr_key]) bounce('You already sent to exchange for this pair, please cancel order first sending cancel = 1'); |
| 152 | }", |
| 153 | "messages": [ |
| 154 | { |
| 155 | "app": "state", |
| 156 | "state": "{ |
| 157 | var[$ord_key] += 1; |
| 158 | var[$ord_addr_key] = $asset_inp; |
| 159 | response['message'] = 'Order created to sell '||$asset_inp||' '||$asset_offered||' at '||$price||' '||$asset_wanted||' per unit'; |
| 160 | response['cancel'] = 'To cancel order, send cancel = 1 and follow instructions'; |
| 161 | }" |
| 162 | } |
| 163 | ] |
| 164 | } |
| 165 | ] |
| 166 | } |
| 167 | } |
| 168 | ] |