Definition: [
"autonomous agent",
{
"init": "{
$OWNER = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; //office_testnet_wallet
$RAAndomore = "MR3GL4JFWF3UTTSN42LRPCPWZSABO7WR"; // AA that add a randomness based on time variable sources
// lottery param
$HOUR = 3600;
$DAY = 24 * $HOURS;
$ticket_price = 10000;
$round_buying_period = 3 * $DAY;
$round_secret_sharing_period = 1 * $HOUR;
// input
$hash = trigger.data.sha256_of_secret otherwise trigger.data.h otherwise ""; $got_hash = ($hash!= "");
$secret = trigger.data.secret otherwise trigger.data.s otherwise ""; $got_secret = ($secret!= "");
// checks
$ellapsed_time = (timestamp - var["start_timestamp"]);
$running = var["running"] and ($ellapsed_time < $round_buying_period);
$waiting_secrets = var["waiting_secrets"] and ($ellapsed_time < $round_secret_sharing_period);
}",
"messages": {
"cases": [
{
"if": "{ $running and $got_hash }",
"init": "{
if (trigger.output[[asset=base]]<$ticket_price) bounce ("Ticket price is '"||$ticket_price||"' bytes");
}",
"messages": [
{
"app": "state",
"state": "{
// store hashes to force different ones
$count = (var["hash_count"] otherwise 0) +1;
$new_count = ($count!=6)? $count : 1;
var["hash_count"] = $new_count;
var["hash_"||$new_count] = $hash;
//store player
$player_count = (var["player_count"] otherwise 0) +1;
var["player_"||$player_count||"_a"] = trigger.address;
var["player_"||$player_count||"_h"] = $hash;
var["player_count"] = $player_count;
response['message'] = "entry accepted";
}"
}
]
},
{
"if": "{ $got_secret }",
"init": "{
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{$message}",
"pay_id": "{$pay_id}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$address}",
"amount": "{$amount}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$address2}",
"amount": "{$amount2}"
}
]
}
},
{
"app": "state",
"state": "{
}"
}
]
},
{
"init": "{
if ($got_hash) // but the round is drawing =>
response['message'] = "Draw in process, please try later for the next round, funds returned";
if (!$running)
{
// draw 3 players to ask for secret
// player1
$best_random = var[$RAAndomore]["last_random_hash"]||timestamp;
$num1 = number_from_seed($best_random, 1, var["player_count"]);
$ad1 = var["player_"||$num1||"_a"];
$h1 = var["player_"||$num1||"_h"];
// player2
$num2a= number_from_seed($ad1, 1, var["player_count"]-1);
$num2b = ($num2 != $num1) ? $num2 : $num2 + 1;
$ad2 = var["player_"||$num2b||"_a"];
$h2 = var["player_"||$num2b||"_h"];
// player3
$num3 = number_from_seed($ad2, 3, var["player_count"]);
$num3b = ($num3 != $num2) ? $num3 : $num3 - 1;
$num3c = ($num3b != $num1) ? $num3b : $num3b - 1;
$ad3 = var["player_"||$num3c||"_a"];
$h3 = var["player_"||$num3c||"_h"];
}
}",
"messages": [
{
"app": "data",
"payload": {
"seed": "{sha256(trigger.address||$hash)}"
}
},
{
"app": "payment",
"payload": {
"outputs": [
{
"address": "{$RAAndomore}",
"amount": "{1}"
}
]
}
},
{
"if": "{$got_hash}",
"app": "payment",
"payload": {
"outputs": [
{
"address": "{trigger.address}",
"amount": "{1000}"
}
]
}
},
{
"if": "{!$running}",
"app": "payment",
"payload": {
"outputs": [
{
"address": "{trigger.address}",
"amount": "{1000}"
}
]
}
},
{
"if": "{!$running}",
"app": "payment",
"payload": {
"outputs": [
{
"address": "{trigger.address}",
"amount": "{1000}"
}
]
}
},
{
"if": "{!$running}",
"app": "payment",
"payload": {
"outputs": [
{
"address": "{trigger.address}",
"amount": "{trigger.output[[asset=base]]-1000}"
}
]
}
},
{
"app": "state",
"state": "{
var["running"] = $running;
}"
}
]
}
]
}
}
]