[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$type = trigger.data.type;
$id = trigger.data.project_id;
}",
"messages": {
"cases": [
{
"if": "{$type == 'create'}",
"init": "{
$owner = trigger.address;
$amount = trigger.data.amount;
$days = trigger.data.days;
if(!exists($id) OR !exists($amount) OR !exists($days)){
bounce("id, days and amount required");
}
if (var[$id || '_owner']) {
bounce("project exists");
}
}",
"messages": [
{
"app": "state",
"state": "{
var[$id || '_owner'] = $owner;
var[$id || '_amount'] = $amount;
var[$id || '_days'] = $days;
var[$id || '_unit'] = trigger.unit;
var[$id || '_start_time'] = timestamp;
var[$id || '_balance'] = 0;
var[$id || '_status'] = 'stared';
response['result'] = 'added';
}"
}
]
},
{
"if": "{$type == 'addReward'}",
"init": "{
$reward_id = trigger.data.reward_id;
$amount = trigger.data.amount;
if(!exists(var[$id || '_owner'])){
bounce("Project not found");
}
if(var[$id || '_owner'] != trigger.address){
bounce("you are not admin");
}
$current_data = unit[var[$id || '_unit']].messages[[.app='data']].payload;
if(exists($current_data[$amount]) OR exists($current_data[$reward_id])){
bounce('reward already exists');
}
if(exists(var[$id || '_' || $amount]) OR exists(var[$id || '_' || $reward_id])){
bounce('reward already exists');
}
}",
"messages": [
{
"app": "state",
"state": "{
var[$id || '_' || $amount] = $reward_id;
var[$id || '_' || $reward_id] = $amount;
response['result'] = 'added';
}"
}
]
},
{
"if": "{$type == 'buy'}",
"init": "{
$amount = trigger.output[[asset=base]];
$my_address = trigger.address;
if(!exists(var[$id || '_owner'])){
bounce("Project not found");
}
$current_data = unit[var[$id || '_unit']].messages[[.app='data']].payload;
if(var[$id || '_status'] != 'started'){
bounce("project is not active");
}
}",
"messages": [
{
"app": "state",
"state": "{
if(!exists($current_data[$amount]) AND !exists(var[$id || '_' || $amount])){
response['result'] = 'no reward purchase';
} else {
$reward_id = exists($current_data[$amount]) ?
$current_data[$amount] :
var[$id || '_' || $amount];
response['result'] = 'reward_id:' || $reward_id;
}
var[$id || '_balance'] += $amount;
}"
}
]
},
{
"if": "{$type == 'withdraw'}",
"init": "{
if(!exists(var[$id || '_owner'])){
bounce("Project not found");
}
if(var[$id || '_owner'] != trigger.address){
bounce("you are not admin");
}
$finish_day = (86400 * var[$id || '_days']) + var[$id || '_start_time'];
if(timestamp < $finish_day){
bounce("project did't finish");
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{var[$id || '_balance']}"
}
]
}
},
{
"app": "state",
"state": "{
var[$id || '_amount'] = false;
var[$id || '_days'] = false;
var[$id || '_unit'] = false;
var[$id || '_start_time'] = false;
var[$id || '_balance'] = false;
var[$id || '_status'] = 'finished';
}"
}
]
},
{
"if": "{$type == 'stop'}",
"init": "{
if(!exists(var[$id || '_owner'])){
bounce("Project not found");
}
$finish_day = (86400 * var[$id || '_days']) + var[$id || '_start_time'];
if(timestamp < $finish_day){
bounce("project did't finish");
}
}",
"messages": [
{
"app": "state",
"state": "{
var[$id || '_status'] = 'stopped';
}"
}
]
}
]
}
}
]