[
"autonomous agent",
{
"messages": {
"cases": [
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset ? 'base' : var['asset']}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$asset ? ($to_send + $base_inp) : $to_send}"
}
]
},
"if": "{$asset}"
},
{
"app": "state",
"state": "{response['message'] = 'Exchanged ('||($asset ? 'assets': 'bytes')||') at '||var['price']||' bytes per token';}"
}
],
"if": "{trigger.address != $OWNER}",
"init": "{if(!var['asset'] or !var['price'] or !var['want']) bounce('Not configured yet');if(var['address'] and trigger.address != var['address']) bounce('No permission for exchange');if($asset and $asset != var['asset']) bounce('Asset not allowed, please send ' || var['asset']);if(!$asset and var['want'] == 'asset') bounce('Owner wants assets');if($asset and var['want'] == 'byte') bounce('Owner wants bytes');$balance = balance[base] - $base_inp + $FEE;$available = $asset ? ($balance - ($balance % var['price'])) : balance[var['asset']];if($available <= 0) bounce('There are no '|| ($asset?'bytes':'assets')||' left');$to_send = $asset ?(trigger.output[[asset!=base]] * var['price']) : (($base_inp - ($base_inp % var['price'])) / var['price']);if($available < $to_send) bounce('Not enough funds, try lower amount');}"
},
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$OWNER}",
"amount": "{trigger.output[[asset!=base]]}"
}
]
},
"if": "{$asset}"
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$OWNER}",
"amount": "{$base_inp}"
}
]
}
},
{
"app": "state",
"state": "{if(trigger.data.price){if(round(trigger.data.price) <= 0) bounce('Price must be greater than 0');var['price'] = round(trigger.data.price);}if($asset or trigger.data.asset)var['asset'] = $asset otherwise trigger.data.asset;if(trigger.data.address){if(trigger.data.address == 'clear')var['address'] = false;elsevar['address'] = trigger.data.address;}if(trigger.data.want){if(trigger.data.want != 'asset' and trigger.data.want != 'byte')bounce('Not allowed. Valid values: asset,byte');var['want'] = trigger.data.want;}if(!(var['price'] and var['asset'] and var['want']))response['message'] = 'Not properly configured, you are missing required param/s';elseresponse['message'] = 'Configured and ready to exchange!';}"
}
],
"if": "{trigger.data.configure}"
},
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "{var['asset']}",
"outputs": [
{
"address": "{$OWNER}",
"amount": "{balance[var['asset']]}"
}
]
},
"if": "{var['asset'] and balance[var['asset']] > 0}"
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$OWNER}"
}
]
}
},
{
"app": "state",
"state": "{response['message'] = 'withdrawn '||var['asset']||' asset and/or bytes';}"
}
],
"if": "{trigger.data.withdraw}"
},
{
"messages": [
{
"app": "state",
"state": "{response['message'] = 'amount deposited correctly';}"
}
]
}
]
},
"init": "{$OWNER = 'JBGMJI7XSKLKNHZF26BUBWHVUZSDV3P5';$FEE = 1000;$base_inp = trigger.output[[asset=base]] - $FEE;if(trigger.output[[asset!=base]].asset != 'none')$asset = trigger.output[[asset!=base]].asset;if($asset and $asset == 'ambiguous') bounce('Only 1 asset per tx');}"
}
]