[
"autonomous agent",
{
"init": "{
$BYTE_ASSET = var['7T3MIJFUUZD23DZJ6BDDGBZPNS6RYFAM']['asset'];
$BASE_INP = 9000;
$asset_wanted = typeof(trigger.data.want) == 'string' AND trigger.data.want=='bytes'?$BYTE_ASSET:trigger.data.want;
$asset_inp = trigger.output[[asset!=base]];
if(trigger.output[[asset!=base]].asset != 'none')
$asset = trigger.output[[asset!=base]].asset;
if(!trigger.data.cancel){
if(!$asset) bounce('You must send an asset to offer');
if(!$asset_wanted OR !asset[$asset_wanted].exists) bounce('want param missing. You must send wanted asset which will be atomically exchanged for yours');
if($asset == 'ambiguous') bounce('Only 1 asset per tx');
}
$asset_offered = $asset;
$fill_pair = sha256($asset_wanted||'_'||$asset_offered);
$price = trigger.data.price;
$orders_to_fill = $fill_pair||'_'||$price;
}",
"messages": {
"cases": [
{
"if": "{trigger.data.cancel}",
"init": "{
$order_id = trigger.data.cancel;
if(typeof($order_id) != 'string') bounce('Order id must be a string');
if(!ends_with($order_id,trigger.address)) bounce('Order id must end with your address');
if(!var[$order_id]) bounce('Order id not found');
$adr_index = index_of(trigger.address,$order_id);
$pair_key = substring($order_id,0,index_of('_',$order_id)+1);
$l_price = index_of(trigger.address,$order_id)-1 - index_of('_',$order_id);
$pri = substring($order_id,length($pair_key),$l_price);
$asset_cancel = substring(var[$pair_key],0,index_of('_',var[$pair_key])+1);
$asset_wante = substring(var[$pair_key],index_of('_',var[$pair_key]));
$cancel_key = var[$pair_key]||'_'||$pri;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset_cancel}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{var[trigger.data.cancel]}"
}
]
}
},
{
"app": "state",
"state": "{
var[$cancel_key] -= 1;
if(var[$cancel_key] == 0){
var[$cancel_key]=false;
var[$pair_key] = false;
}
$am = var[$order_id];
var[$order_id] = false;
response['message'] = 'Canceled order to sell '||$am||' '||$asset_cancel||' at '||$pri||' '||$asset_wante||' per unit';
}"
}
]
},
{
"if": "{trigger.data.fill}",
"init": "{
$to_fill = trigger.data.address;
$fill_key = $orders_to_fill||'_'||$to_fill;
if(!var[$fill_pair]) bounce('No orders to fill for this pair');
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);
if(!$price) bounce('price param missing');
if(!$to_fill OR !is_valid_address($to_fill)) bounce('address param missing or invalid.');
if(!var[$fill_key]) bounce('Address has no created order on this pair');
$a_amount = var[$fill_key];
$in_exch = ($a_amount*$price_fill);
if($asset_inp < $in_exch) bounce('Not enough funds sent to fill the order, need '||$in_exch);
$b_amount = $a_amount*$price_fill;
$return_amount = $asset_inp - $in_exch;
}",
"messages": [
{
"if": "{$return_amount > 0}",
"app": "payment",
"payload": {
"asset": "{$asset_offered}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$return_amount}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset_offered}",
"outputs": [
{
"address": "{$to_fill}",
"amount": "{$in_exch}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset_wanted}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$a_amount}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$BASE_INP}"
},
{
"address": "{$to_fill}",
"amount": "{$BASE_INP}"
}
]
}
},
{
"app": "state",
"state": "{
var[$orders_to_fill] -= 1;
if(var[$orders_to_fill] == 0){
var[$orders_to_fill]=false;
var[$fill_pair] = false;
}
response['message'] = 'Exchanged '||$a_amount||' '||$asset_wanted||' for '||$b_amount||' '||$asset_offered;
var[$fill_key] = false;
}"
}
]
},
{
"init": "{
$ord_pair = sha256($asset_offered||'_'||$asset_wanted);
$ord_key = $ord_pair ||'_'||$price;
$ord_addr_key = $ord_pair||'_'||$price||'_'||trigger.address;
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);
if(!$price) bounce('price param missing. Send a price (in assets wanted per asset offered) to create order');
if(var[$ord_addr_key]) bounce('You already sent to exchange for this pair, please cancel order first sending cancel = cancel_id');
}",
"messages": [
{
"app": "state",
"state": "{
var[$ord_key] += 1;
var[$ord_pair] = $asset_offered;
var[$ord_addr_key] = $asset_inp;
response['message'] = 'Order created to sell '||$asset_inp||' '||$asset_offered||' at '||$price||' '||$asset_wanted||' per unit';
response['cancel'] = 'To cancel order, send cancel param with cancel_id as value:';
response['cancel_id'] = $ord_addr_key;
}"
}
]
}
]
}
}
]