| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $BYTE_ASSET = var['7T3MIJFUUZD23DZJ6BDDGBZPNS6RYFAM']['asset']; |
| 6 | $BASE_INP = 9000; |
| 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'); |
| 13 | if(!$asset_wanted OR !asset[$asset_wanted].exists) bounce('want param missing. You must send wanted asset which will be atomically exchanged for yours'); |
| 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 | $order_id = trigger.data.cancel; |
| 27 | if(typeof($order_id) != 'string') bounce('Order id must be a string'); |
| 28 | if(!ends_with($order_id,trigger.address)) bounce('Order id must end with your address'); |
| 29 | if(!var[$order_id]) bounce('Order id not found'); |
| 30 | $adr_index = index_of(trigger.address,$order_id); |
| 31 | $pair_key = substring($order_id,0,index_of('_',$order_id)+1); |
| 32 | $l_price = index_of(trigger.address,$order_id)-1 - index_of('_',$order_id); |
| 33 | $pri = substring($order_id,length($pair_key),$l_price); |
| 34 | $asset_cancel = substring(var[$pair_key],0,index_of('_',var[$pair_key])+1); |
| 35 | $asset_wante = substring(var[$pair_key],index_of('_',var[$pair_key])); |
| 36 | $cancel_key = var[$pair_key]||'_'||$pri; |
| 37 | }", |
| 38 | "messages": [ |
| 39 | { |
| 40 | "app": "payment", |
| 41 | "payload": { |
| 42 | "asset": "{$asset_cancel}", |
| 43 | "outputs": [ |
| 44 | { |
| 45 | "address": "{trigger.address}", |
| 46 | "amount": "{var[trigger.data.cancel]}" |
| 47 | } |
| 48 | ] |
| 49 | } |
| 50 | }, |
| 51 | { |
| 52 | "app": "state", |
| 53 | "state": "{ |
| 54 | var[$cancel_key] -= 1; |
| 55 | if(var[$cancel_key] == 0){ |
| 56 | var[$cancel_key]=false; |
| 57 | var[$pair_key] = false; |
| 58 | } |
| 59 | $am = var[$order_id]; |
| 60 | var[$order_id] = false; |
| 61 | response['message'] = 'Canceled order to sell '||$am||' '||$asset_cancel||' at '||$pri||' '||$asset_wante||' per unit'; |
| 62 | }" |
| 63 | } |
| 64 | ] |
| 65 | }, |
| 66 | { |
| 67 | "if": "{trigger.data.fill}", |
| 68 | "init": "{ |
| 69 | $to_fill = trigger.data.address; |
| 70 | $fill_key = $orders_to_fill||'_'||$to_fill; |
| 71 | if(!var[$fill_pair]) bounce('No orders to fill for this pair'); |
| 72 | 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); |
| 73 | if(!$price) bounce('price param missing'); |
| 74 | if(!$to_fill OR !is_valid_address($to_fill)) bounce('address param missing or invalid.'); |
| 75 | if(!var[$fill_key]) bounce('Address has no created order on this pair'); |
| 76 | $a_amount = var[$fill_key]; |
| 77 | $in_exch = ($a_amount*$price_fill); |
| 78 | if($asset_inp < $in_exch) bounce('Not enough funds sent to fill the order, need '||$in_exch); |
| 79 | $b_amount = $a_amount*$price_fill; |
| 80 | $return_amount = $asset_inp - $in_exch; |
| 81 | }", |
| 82 | "messages": [ |
| 83 | { |
| 84 | "if": "{$return_amount > 0}", |
| 85 | "app": "payment", |
| 86 | "payload": { |
| 87 | "asset": "{$asset_offered}", |
| 88 | "outputs": [ |
| 89 | { |
| 90 | "address": "{trigger.address}", |
| 91 | "amount": "{$return_amount}" |
| 92 | } |
| 93 | ] |
| 94 | } |
| 95 | }, |
| 96 | { |
| 97 | "app": "payment", |
| 98 | "payload": { |
| 99 | "asset": "{$asset_offered}", |
| 100 | "outputs": [ |
| 101 | { |
| 102 | "address": "{$to_fill}", |
| 103 | "amount": "{$in_exch}" |
| 104 | } |
| 105 | ] |
| 106 | } |
| 107 | }, |
| 108 | { |
| 109 | "app": "payment", |
| 110 | "payload": { |
| 111 | "asset": "{$asset_wanted}", |
| 112 | "outputs": [ |
| 113 | { |
| 114 | "address": "{trigger.address}", |
| 115 | "amount": "{$a_amount}" |
| 116 | } |
| 117 | ] |
| 118 | } |
| 119 | }, |
| 120 | { |
| 121 | "app": "payment", |
| 122 | "payload": { |
| 123 | "asset": "base", |
| 124 | "outputs": [ |
| 125 | { |
| 126 | "address": "{trigger.address}", |
| 127 | "amount": "{$BASE_INP}" |
| 128 | }, |
| 129 | { |
| 130 | "address": "{$to_fill}", |
| 131 | "amount": "{$BASE_INP}" |
| 132 | } |
| 133 | ] |
| 134 | } |
| 135 | }, |
| 136 | { |
| 137 | "app": "state", |
| 138 | "state": "{ |
| 139 | var[$orders_to_fill] -= 1; |
| 140 | if(var[$orders_to_fill] == 0){ |
| 141 | var[$orders_to_fill]=false; |
| 142 | var[$fill_pair] = false; |
| 143 | } |
| 144 | response['message'] = 'Exchanged '||$a_amount||' '||$asset_wanted||' for '||$b_amount||' '||$asset_offered; |
| 145 | var[$fill_key] = false; |
| 146 | }" |
| 147 | } |
| 148 | ] |
| 149 | }, |
| 150 | { |
| 151 | "init": "{ |
| 152 | $ord_pair = sha256($asset_offered||'_'||$asset_wanted); |
| 153 | $ord_key = $ord_pair ||'_'||$price; |
| 154 | $ord_addr_key = $ord_pair||'_'||$price||'_'||trigger.address; |
| 155 | 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); |
| 156 | if(!$price) bounce('price param missing. Send a price (in assets wanted per asset offered) to create order'); |
| 157 | if(var[$ord_addr_key]) bounce('You already sent to exchange for this pair, please cancel order first sending cancel = cancel_id'); |
| 158 | }", |
| 159 | "messages": [ |
| 160 | { |
| 161 | "app": "state", |
| 162 | "state": "{ |
| 163 | var[$ord_key] += 1; |
| 164 | var[$ord_pair] = $asset_offered; |
| 165 | var[$ord_addr_key] = $asset_inp; |
| 166 | response['message'] = 'Order created to sell '||$asset_inp||' '||$asset_offered||' at '||$price||' '||$asset_wanted||' per unit'; |
| 167 | response['cancel'] = 'To cancel order, send cancel param with cancel_id as value:'; |
| 168 | response['cancel_id'] = $ord_addr_key; |
| 169 | |
| 170 | }" |
| 171 | } |
| 172 | ] |
| 173 | } |
| 174 | ] |
| 175 | } |
| 176 | } |
| 177 | ] |