Definition: [
"autonomous agent",
{
"init": "{
$ODEX = 'FVRZTCFXIDQ3EYRGQSLE5AMWUQF4PRYJ';
$ODEX_KEY = 'balance_'||this_address||'_base';
if(trigger.output[[asset!=base]].asset != 'none')
bounce('no assets here');
$total_bal = balance[base]-storage_size-trigger.output[[asset=base]];
}",
"messages": {
"cases": [
{
"if": "{!var['low_fee_mode'] AND $total_bal+var[$ODEX][$ODEX_KEY] > 10000000}",
"messages": [
{
"app": "data",
"payload": {
"withdraw": 1,
"asset": "base",
"amount": "all"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$ODEX}",
"amount": "{10000}"
}
]
}
},
{
"app": "state",
"state": "{
var['low_fee_mode'] = true;
response['message'] = 'Low fee mode active. Until AA depletion all operations are subsidized';
}"
}
]
},
{
"if": "{
trigger.data.match
}",
"init": "{
$my_order = trigger.data.order.signed_message;
$order_key = trigger.data.addr_to_match||(trigger.data.id?trigger.data.id:'');
$order_pkg = var[$order_key];
if(!$order_pkg)
bounce('no order to match');
if(typeof($order_pkg) != 'string')
bounce('order already matched');
if ($my_order.matcher != this_address)
bounce('wrong matcher in order');
if(!exists($my_order.matcher_fee_asset) or $my_order.matcher_fee_asset != 'base')
bounce('matcher_fee_asset must be base');
// matcher fees
if(!is_valid_address($my_order.matcher) OR $my_order.matcher != this_address)
bounce('I am the matcher, fuck offchain matchers');
if($my_order.matcher_fee != 0)
bounce('matcher fees not needed');
// affiliates
if ($my_order.affiliate)
bounce('affiliates not needed');
$order_to_match = json_parse($order_pkg);
}",
"messages": [
{
"app": "data",
"payload": {
"order1": "{$my_order}",
"order2": "{$order_to_match}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$ODEX}",
"amount": "{var['low_fee_mode']?1:9000}"
}
]
}
},
{
"if": "{var['low_fee_mode']}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": 9900
}
]
}
},
{
"app": "state",
"state": "{
var[$order_key] = false;
if(var['low_fee_mode'] AND $total_bal < 30000)
var['low_fee_mode'] = false;
}"
}
]
},
{
"if": "{
trigger.data.store
}",
"init": "{
if(var[trigger.address||(trigger.data.id?trigger.data.id:'')])
bounce('already stored, must be matched first');
$order = trigger.data.order.signed_message;
if (!$order.sell_asset or !$order.buy_asset)
bounce('buy_asset or sell_asset missing');
if(($order.sell_asset != 'base' and !asset[$order.sell_asset].exists) or ($order.buy_asset != 'base' and !asset[$order.buy_asset].exists))
bounce('invalid asset');
if ($order.sell_asset == $order.buy_asset)
bounce('same asset');
if ($order.matcher != this_address)
bounce('wrong matcher in order, I am the matcher, fuck offchain matchers');
if ($order.aa != $ODEX )
bounce('wrong aa in order');
if ($order.expiry_ts AND $order.expiry_ts <= timestamp)
bounce("order expired");
if(!exists($order.matcher_fee_asset) or $order.matcher_fee_asset != 'base')
bounce('matcher_fee_asset must be base');
$sell_key1 = 'balance_' || $order.address || '_' || $order.sell_asset;
$id1 = sha256($order.address || $order.sell_asset || $order.buy_asset || $order.sell_amount || $order.price || ($order.nonce otherwise '') || (trigger.data.order.last_ball_unit otherwise '-'));
if (var[$ODEX]['executed_' || $id1])
bounce('order already executed');
if (var[$ODEX]['cancelled_' || $id1])
bounce('order already cancelled');
$amount_left1 = var[$ODEX]['amount_left_' || $id1] otherwise $order.sell_amount;
// check balances
if ($amount_left1 > var[$ODEX][$sell_key1] + 0)
bounce('not sufficient balance in sell asset to complete order');
if($order.matcher_fee != 0)
bounce('matcher fees not needed');
// affiliates
if ($order.affiliate)
bounce('affiliates not needed');
$signer1 = trigger.data.order.authors[0].address;
if ($signer1 != $order.address AND !var[$ODEX]['grant_' || $order.address || '_to_' || $signer1])
bounce("order signer was not authorized to sign");
if (!is_valid_signed_package(trigger.data.order, $signer1))
bounce('bad signature of order');
// AA fees
$base_key1 = 'balance_' || $order.address || '_base';
if (var[$ODEX][$base_key1] <= 1000)
bounce('not enough balance for AA fees');
}",
"messages": [
{
"if": "{var['low_fee_mode']}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": 9900
}
]
}
},
{
"app": "state",
"state": "{
var[trigger.address||(trigger.data.id?trigger.data.id:'')] = json_stringify(trigger.data.order);
if(var['low_fee_mode'] AND $total_bal < 30000)
var['low_fee_mode'] = false;
response['message'] = 'Stored order with id '||trigger.data.id;
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
response['message'] = 'bytes received';
}"
}
]
}
]
}
}
]