Definition: [
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$department = var['department'] OTHERWISE '';
$default_asset = var['default_asset'] OTHERWISE '';
$sent_asset = trigger.output[[asset!=base]].asset OTHERWISE 'none';
$address = trigger.data.address;
$amount = trigger.data.amount;
$period_daily = timestamp_to_string(timestamp, 'date'); // daily
$period_monthly = substring(timestamp_to_string(timestamp, 'date'), 0, 7); // monthly
$period_yearly = substring(timestamp_to_string(timestamp, 'date'), 0, 4); // yearly
$period = $period_daily; // choose period_daily, period_monthly or period_yearly
if ($sent_asset == 'ambiguous') {
bounce('multiple assets sent, send only one');
}
if ($address AND !is_valid_address(trigger.data.address)) {
bounce('invalid address, fix the address');
}
}",
"messages": {
"cases": [
{
"if": "{$address AND $amount}",
"init": "{
// order to spend some asset to some address
$asset = ($sent_asset != 'none') ? $sent_asset : $default_asset;
$default_allowance = var['default_allowance_' || $asset] OTHERWISE 0;
$allowance_now = var[$period || '_' || trigger.address || '_' || $asset] OTHERWISE $default_allowance;
$sent_amount = trigger.output[[asset=$sent_asset]].amount;
$department_pays = $default_allowance ? $amount - $sent_amount : 0;
if ($department_pays > 0) {
$allowance_left = $allowance_now - $department_pays;
if ($allowance_left < 0) {
bounce('not allowed spend that much, contact your department');
}
if ($department_pays > balance[$asset]) {
bounce('unsufficent funds, contact your department');
}
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$address}",
"amount": "{$amount}"
}
]
}
},
{
"if": "{$department_pays < 0}",
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{abs($department_pays)}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{trigger.output[[asset=base]] - 1000}"
}
]
}
},
{
"if": "{$department_pays != 0}",
"app": "state",
"state": "{
if ($department_pays > 0) {
// change how much allowance is left in current period
var[$period || '_' || trigger.address || '_' || $asset] = $allowance_left;
}
else {
// add surplus of sent asset to current period allowance
var[$period || '_' || trigger.address || '_' || $asset] += abs($department_pays);
}
}"
}
]
},
{
"messages": [
{
"init": "{
// assets are accepted, bytes are sent back
$send_back = ($department AND $department == trigger.address) ? '' : trigger.output[[asset=base]] - 1000;
}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$send_back}"
}
]
}
},
{
"app": "state",
"state": "{
// this is for depositing different assets, first deposited asset will be default
if (!$default_asset AND $sent_asset != 'none') {
var['department'] = trigger.address;
var['default_asset'] = $sent_asset;
var['default_allowance' || $sent_asset] = trigger.data.allowance >=0 ? trigger.data.allowance : 50;
}
// changing allowance
$allowance = trigger.data.allowance;
if ($department == trigger.address) {
if (trigger.data.allowance >= 0 AND $default_asset == $sent_asset) {
// changing asset allowance
var['default_allowance_' || $sent_asset] = trigger.data.allowance;
}
if ($address AND $allowance) {
// changing allowance for specific address for current period
var[$period || '_' || $address || '_' || $default_asset] = $allowance;
}
}
}"
}
]
}
]
}
}
]