[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$type = trigger.data.type;
$id = trigger.data.project_id;
if(!exists($type)){
bounce("'type' required");
}
if(!exists($id)){
bounce("'project_id' required");
}
}",
"messages": {
"cases": [
{
"if": "{$type == 'create'}",
"init": "{
$owner = trigger.address;
$goal = trigger.data.goal;
$days = trigger.data.days;
if(!exists($goal) OR !exists($days)){
bounce("days and goal required");
}
if (var[$id || '_owner']) {
bounce("project exists");
}
}",
"messages": [
{
"app": "state",
"state": "{
var[$id || '_owner'] = $owner;
var[$id || '_goal'] = $goal;
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]];
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(exists(var['amount_' || $id || '_' || trigger.address])){
var['amount_' || $id || '_' || trigger.address] += $amount;
}else{
var['amount_' || $id || '_' || trigger.address] = $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");
}
if(var[$id || '_balance'] < var[$id || '_goal']){
bounce("goal not reached");
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{var[$id || '_balance']}"
}
]
}
},
{
"app": "state",
"state": "{
var[$id || '_goal'] = 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": "{
if(var[$id || '_balance'] < var[$id || '_goal']){
var[$id || '_status'] = 'stopped_f'; // fail
}else {
var[$id || '_status'] = 'stopped_s'; // successfully
}
}"
}
]
},
{
"if": "{$type == "get_my_money"}",
"init": "{
if(!exists(var[$id || '_owner'])){
bounce("Project not found");
}
if(var[$id || '_status'] != 'stopped_f'){
bounce("you cannot take your money");
}
$triggerAmount = var['amount_' || $id || '_' || trigger.address];
if(!exists($triggerAmount) AND $triggerAmount > 0){
bounce("I do not have your money");
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$triggerAmount}"
}
]
}
},
{
"app": "state",
"state": "{
var['amount_' || $id || '_' || trigger.address] = false;
response['result'] = 'done';
}"
}
]
}
]
}
}
]