[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$OWNER = "
O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ";
$DANAA = "
LLAVLVFN3CPBHTAB62NOIZLAMPABR2YF"; // for asset shot names;
$i = trigger.data;
}",
"messages": {
"cases": [
{
"if": "{ !!$i.help}",
"init": "{
$aa = trigger.address;
$non_bytes = trigger.output[[asset!=base]]; $bytes = trigger.output[[asset=base]];
$reward = $non_bytes.amount otherwise $bytes.amount otherwise 0;
$reward_asset = $non_bytes.asset != "none" ? $non_bytes.asset : "base";
$asset_nice_name = var[$DANAA][$reward_asset||"_shortName"] otherwise $reward_asset;
$time = $i.time otherwise ($i.delay) ? timestamp : false;
// message creation
$m_reward = "Get "||$reward||" "||$asset_nice_name||" if you are first to reply to me";
$m_time = (!!$time) ? " at "||timestamp_to_string($time, 'datetime') : "";
$m_delay = (!!$i.delay) ? " (delay max = "||$i.delay ||")" : "";
// identify helper
if (!$i.helper)
{
$good_guys_count = var["good_guys_count"] otherwise 0;
if ($good_guys_count == 0 ) bounce ("No 'helper' registered !");
$helper = var["good_guys_"||number_from_seed(timestamp,1,$good_guys_count)];
$key = $aa;
}
else
{
$helper = $i.helper;
$key = $i.private ? sha256($i.helper||"_"||$aa) : $aa;
}
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{ $m_reward||$m_time||$m_delay||$asset_nice_name||" with data: 'aa' = < helped_aa >" }",
"me": "{this_address}",
"helped_aa": "{$aa}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $helper }",
"amount": "{ 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var[$key||"_reward"] = $reward; var[$key||"_asset"] = $reward_asset;
var[$key||"_time"] = $time; var[$key||"_delay"] = $i.delay;
response['message'] = $helper||" has been asked for help ^^";
}"
}
]
},
{
"if": "{ !!$i.aa }",
"init": "{
$helper = trigger.address; $aa = $i.aa;
// been overtaken?
$key = !!var[$aa||"_reward"] ? $aa : sha256($helper||"_"||$aa);
if (!var[$key||"_reward"])
bounce ("Reward not available, you have been overtaken or you specified the wrong 'aa'!");
// too early ?
if (!!var[$key||"_time"]) if (var[$key||"_time"] > timestamp)
bounce ("Too early, try in "||(var[$key||"_time"] - timestamp));
// too late ?
if (!!var[$key||"_delay"]) if (var[$key||"_time"] + var[$key||"_delay"] > timestamp)
bounce ("Too late, sorry !");
// prepare rewards !
$bytes = trigger.output[[asset=base]].amount - 2000; // to post this TX and to give 1000 to AA
$reward_in_bytes = var[$key||"_asset"] == "base" ? var[$key||"_reward"] + $bytes: $bytes;
$reward_in_other = var[$key||"_asset"] != "base" ? var[$key||"_reward"] : false;
}",
"messages": [
{
"app": "data",
"payload": {
"help_from": "{ $helper }"
}
},
{
"if": "{ !!$reward_in_other }",
"app": "payment",
"payload": {
"asset": "{ var[$key||"_asset"] }",
"outputs": [
{
"address": "{ $helper }",
"amount": "{ $reward_in_other }"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $helper }",
"amount": "{ $reward_in_bytes }"
},
{
"address": "{ $aa }",
"amount": "{ 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var[$key||"_reward"] = false; var[$key||"_asset"] = false;
var[$key||"_time"] = false; var[$key||"_delay"] = false;
response['message'] = trigger.address||" has been rewarded ^^";
}"
}
]
},
{
"if": "{ $i.subscribe}",
"init": "{
$helper = trigger.address;
if (var[$helper]) bounce ("You are motivated, but once is enough!");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $helper }",
"amount": "{ $bytes.amount - 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var["good_guys_count"] += 1;
var[$helper] = var["good_guys_count"]; var["good_guys_"||var["good_guys_count"]] = $helper;
response['message'] = "Subscribed";
}"
}
]
},
{
"if": "{ $i.unsubscribe }",
"init": "{
$helper = trigger.address;
if (!(var[$helper])) bounce ("You were not subscribed at the first place !");
}",
"messages": [
{
"app": "state",
"state": "{
var["good_guys_count"] -=1; $good_guy_number = var[$helper];
var["good_guys_"||$good_guy_number] = false; var[$helper] = false;
response['message'] = "Unsubscribed";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
bounce ("Input not expected, please read the doc at https://github.com/pascalSAINVITU/THAANKS");
}"
}
]
}
]
}
}
]