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