Definition: [
"autonomous agent",
{
"init": "{
if(trigger.output[[asset=base]] < 5000) bounce('not enough funds for fees');
}",
"messages": {
"cases": [
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}"
}
]
}
},
{
"app": "definition",
"payload": {
"definition": [
"autonomous agent",
{
"{'init'}": "{
"{$addr_ct = '"||trigger.address||"';"
|| "$addr = trigger.address;"
|| "$fee = 1000;"
|| "if(var['asset']){"
|| "$ratio = var['price'];"//bytes per 1 asset
|| "$asset_ct = var['asset'];"
|| "}"
|| "else{"
|| "if($addr != $addr_ct) bounce('Not configured yet');"
|| "}"
//asset detection and checks
|| "if(trigger.output[[asset!=base]].asset != 'none')"
|| "$asset = trigger.output[[asset!=base]].asset;"
|| "if($asset){"
|| "if($asset == 'ambiguous') bounce('Only 1 asset per tx');"
|| "if($addr != $addr_ct and $asset != $asset_ct) bounce('Asset not allowed, please send ' || $asset_ct);"
|| "$asset_inp = trigger.output[[asset!=base]];"
|| "}"
|| "$base_inp = trigger.output[[asset=base]] - $fee;"
|| "$balance = balance[base] - $base_inp + $fee;}"
}",
"messages": {
"{'cases'}": [
{
"{'if'}": "{
"{$addr != $addr_ct and $asset_ct}"
}",
"{'init'}": "{
"{if(!$ratio) bounce('Exchange already done');"
|| "if(!$asset and var['wanted']) bounce('AA owner wants assets');"
|| "if($asset and !var['wanted']) bounce('AA owner wants bytes');"
|| "$available = $asset ? $balance : balance[$asset_ct];"
|| "if($available <= 0) bounce('Not enough funds');"
|| "$to_send = $asset ? ($asset_inp * $ratio) : (($base_inp - ($base_inp % $ratio)) / $ratio);"
|| "if($available < $to_send) bounce('Not enough funds');}"
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{$to_send + $base_inp}"}"
}
]
}
},
{
"{'if'}": "{"{!$asset}"}",
"app": "payment",
"payload": {
"asset": "{"{$asset_ct}"}",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{$to_send}"}"
}
]
}
},
{
"app": "state",
"{'state'}": "{
"{var['price'] = false;"
|| "response['message'] = 'exchanged ('||($asset ? 'assets': 'bytes')||') at '||$ratio||' bytes per token';}"
}"
}
]
},
{
"{'if'}": "{
"{$addr == $addr_ct and trigger.data.withdraw}"
}",
"messages": [
{
"{'if'}": "{"{balance[$asset_ct] > 0}"}",
"app": "payment",
"payload": {
"asset": "{"{$asset_ct}"}",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{balance[$asset_ct]}"}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{"{$addr}"}"
}
]
}
},
{
"app": "state",
"{'state'}": "{
"{response['message'] = 'withdrawn all assets and/or bytes';}"
}"
}
]
},
{
"{'if'}": "{
"{$addr == $addr_ct and trigger.data.configure}"
}",
"messages": [
{
"{'if'}": "{"{$asset}"}",
"app": "payment",
"payload": {
"asset": "{"{$asset}"}",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{$asset_inp}"}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{$base_inp}"}"
}
]
}
},
{
"app": "state",
"{'state'}": "{
"{var['price'] = trigger.data.price;"
|| "if($asset or trigger.data.asset)"
|| "var['asset'] = $asset otherwise trigger.data.asset;"
|| "var['wanted'] = trigger.data.wanted ? true : false;"//true = asset, false = base
|| "response['message'] = 'exchange box configured!';}"
}"
}
]
},
{
"{'if'}": "{
"{$addr == $addr_ct}"
}",
"messages": [
{
"app": "state",
"{'state'}": "{
"{response['message'] = 'amount deposited correctly';}"
}"
}
]
}
]
}
}
]
}
}
]
}
]
}
}
]