[
"autonomous agent",
{
"init": "{
$ASSET = 'hd936lRtPN11pkgNv8qVnqN2OHMyAp8P/NsLp6KsuqA=';
$OWNER = 'JBGMJI7XSKLKNHZF26BUBWHVUZSDV3P5';
$DEFAULT_TIME_CHANGE_FEE = 60*60*24*7;
$DEFAULT_FEE = 1000;
if(trigger.data.to AND !is_valid_address(trigger.data.to)) bounce('To param invalid');
if(trigger.data.from AND !is_valid_address(trigger.data.from)) bounce('From param invalid');
if(trigger.data.spender AND !is_valid_address(trigger.data.spender)) bounce('Spender param invalid');
if(!trigger.data.approve AND trigger.data.value AND trigger.data.value <= 0) bounce('Value param invalid');
if(!var['fee'] AND trigger.address != $OWNER) bounce('Not active yet');
$base_inp = trigger.output[[asset=base]]-var['fee'];
}",
"messages": {
"cases": [
{
"if": "{
trigger.data.approve
}",
"init": "{
if(!trigger.data.spender) bounce('Spender param missing');
if(!trigger.data.value) bounce('Value param missing');
if(!var['a_' || trigger.address]) bounce('You have no funds');
if(trigger.data.value < 0) bounce('Value param invalid');
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$base_inp}"
}
]
}
},
{
"app": "state",
"state": "{
var['perm_' || trigger.address || '_' || trigger.data.spender] = trigger.data.value;
if(var['perm_' || trigger.address || '_' || trigger.data.spender] == 0) var['perm_' || trigger.address || '_' || trigger.data.spender] = false;
response['message'] = 'Approved '||trigger.data.spender||' to spend '||trigger.data.value ||' from your account';
}"
}
]
},
{
"if": "{
trigger.data.deposit
}",
"init": "{
if(trigger.output[[asset!=base]].asset != 'none')
$asset = trigger.output[[asset!=base]].asset;
else
bounce('You must deposit '||$ASSET);
if($asset == 'ambiguous') bounce('Only 1 asset per tx');
if($asset != $ASSET) bounce('You must deposit '||$ASSET);
$asset_inp = trigger.output[[asset!=base]];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$base_inp}"
}
]
}
},
{
"app": "state",
"state": "{
var['a_' || trigger.address] += $asset_inp;
response['message'] = 'Added '||$asset_inp||' to your account';
}"
}
]
},
{
"if": "{
trigger.data.withdraw
}",
"init": "{
if(!var['a_' || trigger.address]) bounce('Nothing to withdraw');
$max_w = var['a_' || trigger.address]-(var['a_' || trigger.address] % 1);
if(!trigger.data.value) bounce('Value param missing, max to withdraw '||$max_w) ;
$w_amount = trigger.data.value - (trigger.data.value % 1);
if($w_amount <= 0) bounce('Max to withdraw '||$max_w);
if($w_amount > $max_w) bounce('Not enough funds, max to withdraw '||$max_w);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$base_inp}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$ASSET}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$w_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['a_' || trigger.address] -= $w_amount;
response['message'] = 'Withdrawn '||$w_amount||' from your account';
}"
}
]
},
{
"if": "{
trigger.data.transfer
}",
"init": "{
if(!var['a_'||trigger.address]) bounce('No funds found');
if(!trigger.data.to) bounce('To param missing');
if(!trigger.data.value) bounce('Value param missing');
if(trigger.data.value > var['a_'||trigger.address]) bounce('You have max '||var['a_'||trigger.address]);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$base_inp}"
}
]
}
},
{
"app": "state",
"state": "{
var['a_' || trigger.data.to] += trigger.data.value;
var['a_'||trigger.address] -= trigger.data.value;
if(var['a_'||trigger.address] == 0) var['a_'||trigger.address] = false;
response['message'] = 'Transfered '||trigger.data.value||' to '||trigger.data.to;
}"
}
]
},
{
"if": "{
trigger.data.transfer_from
}",
"init": "{
if(!trigger.data.from) bounce('From param missing');
if(!var['a_' || trigger.data.from]) bounce('Account have no funds');
if(!trigger.data.to) bounce('To param missing');
if(!trigger.data.value) bounce('Value param missing');
if(!var['perm_' || trigger.data.from || '_' || trigger.address]) bounce('Not allowed to spend');
if(var['perm_' || trigger.data.from || '_' || trigger.address] < trigger.data.value) bounce('You can spend max '||var['perm_' || trigger.data.from || '_' || trigger.address]);
if(trigger.data.value > var['a_' || trigger.data.from]) bounce('Account have '||var['a_' || trigger.data.from]);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$base_inp}"
}
]
}
},
{
"app": "state",
"state": "{
var['a_' || trigger.data.to] += trigger.data.value;
var['a_' || trigger.data.from] -= trigger.data.value;
if(var['a_' || trigger.data.from] == 0) var['a_' || trigger.data.from] = false;
var['perm_' || trigger.data.from || '_' || trigger.address] -= trigger.data.value;
if(var['perm_' || trigger.data.from || '_' || trigger.address] == 0) var['perm_' || trigger.data.from || '_' || trigger.address] = false;
response['message'] = 'Transfered '||trigger.data.value||' to '||trigger.data.to;
}"
}
]
},
{
"if": "{
trigger.address != $OWNER
}",
"messages": [
{
"app": "state",
"state": "{
response['usage'] = 'Send param = 1. Accepted params: deposit,withdraw,transfer,transfer_from,approve';
response['actual_fee'] = var['fee'];
response['allowed_fee_change_since'] = timestamp_to_string(var['last_fee_change'],'datetime');
response['message'] = 'Received bytes for funding AA operation';
}"
}
]
},
{
"if": "{
trigger.data.withdraw_bytes
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}"
}
]
}
},
{
"app": "state",
"state": "{
var['_'] = false;
var['fee'] = $DEFAULT_FEE;
var['last_fee_change'] = timestamp;
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
if(trigger.output[[asset!=base]].asset != 'none') bounce('Not allowed');
if(typeof(trigger.data.fee) == 'number' AND (!var['last_fee_change'] OR ((var['last_fee_change']+$DEFAULT_TIME_CHANGE_FEE) < timestamp))){
var['last_fee_change'] = timestamp;
var['fee'] = trigger.data.fee <= 1500 ? round(trigger.data.fee):$DEFAULT_FEE;
var['_'] = 'XXXX';
response['fee'] = 'Fee set ok';
}
response['message'] = 'Received bytes for funding AA operation';
}"
}
]
}
]
}
}
]