[
"autonomous agent",
{
"init": "{
$OWNER = "
O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; //office_testnet_wallet
// secondary AAs
$THAANKS = "
GVVLP4MUVDHYMQPHVIXUGX6RTHBDEQJM"; // getting user help with trigger
$DANAA = "
LLAVLVFN3CPBHTAB62NOIZLAMPABR2YF"; // Dynamic Asset Name AA;
$percentage_for_helper = 0.5; // 0.5 %
$attestor = '35IT3ZYKEPQSCXG2F7A7KWNRAD3SZXN4';
// lottery param
$ticket_price = 10000;
$round_buying_period = 15*60;
$asset = trigger.output[[asset!=base]].asset != "none" ? trigger.output[[asset!=base]].asset : "base";
$key = var[$DANAA][$asset||"_shortName"] otherwise $asset;
$dead_line = var[$key||"_deadline"] otherwise timestamp + $round_buying_period;
}",
"messages": {
"cases": [
{
"if": "{ (timestamp < ($dead_line)) and !(var[$key||"_stopped"]) }",
"init": "{
if (trigger.output[[asset=$asset]] != $ticket_price)
bounce ("Ticket price is "||$ticket_price||" "||$key);
}",
"messages": [
{
"app": "state",
"state": "{
$nop = $key||"_number_of_players";
var[$nop] = !!var[$nop] ? var[$nop] + 1 : 1;
var[$key||"_dead_line"] = $dead_line;
var[$key||"_player_"||var[$nop]] = trigger.address;
var[$key||"_balance"] = var[$key||"_balance"] ?
var[$key||"_balance"] + trigger.output[[asset=$asset]] :
trigger.output[[asset=$asset]];
// if attested address, it plays a role in the seed
if (attestation[[attestors=$attestor, address=trigger.address, ifnone=false]])
var[$key||"_seed"] = sha256(var[$key||"_seed"]||trigger.address);
response['message'] = "You have enter the lottery ^^";
}"
}
]
},
{
"if": "{ ( (timestamp > $dead_line)) and !(var[$key||"_stopped"]) }",
"init": "{
$buy_last_ticket = trigger.output[[asset=$asset]] >= $ticket_price;
if ($buy_last_ticket)
$reimbursement = trigger.output[[asset=$asset]] - $ticket_price -1000;
else
$reimbursement = trigger.output[[asset=$asset]] -1000;
// delay before we draw with seed based only on time variable data
$draw_time = number_from_seed(var[$key||"_seed"], 30 * 60 * 1000, 3 * 60 * 60 * 1000);
// pick a helper from the players, but someone else or a bot will be able to trigger for the reward
$old_nop = var[$key||"_number_of_players"] otherwise 0;
$nop = $old_nop+1;
if ($nop>1) // si un seul jouer pas tirage au sort
$helper = var[$key||"_player_"||number_from_seed($draw_time, 1, $nop )];
else
$helper = var[$key||"_player_1"];
$reward = round (var[$key||"_balance"] * $percentage_for_helper / 100);
}",
"messages": [
{
"app": "data",
"payload": {
"help": "{ true }",
"helper": "{ $helper }",
"time": "{ $draw_time }"
}
},
{
"app": "payment",
"payload": {
"asset": "{ $asset }",
"outputs": [
{
"address": "{ $THAANKS }",
"amount": "{ $reward }"
},
{
"address": "{ trigger.address }",
"amount": "{ $reimbursement }"
}
]
}
},
{
"app": "state",
"state": "{
if($buy_ticket)
{
var[$key||"_number_of_players"] = $nop;
var[$key||"_player_"||$nop] = trigger.address;
var[$key||"_balance"] += trigger.output[[asset=$asset]]-1000;
response['message'] = "You have enter the lottery, and trigger the draw ^^";
}
else
response['message'] = "You have trigger the draw ^^";
var[$key||"_balance"] -= $reward;
var[$key||"_seed"] = $helper; // for next helper draw if necessary
var[$key||"_stopped"] = timestamp;
var[$key||"_drawtime"] = $draw_time;
}"
}
]
},
{
"if": "{ !!var[$key||"_stopped"] and $draw_time > timestamp }",
"messages": [
{
"app": "state",
"state": "{
bounce ("Too early draw time is set to "|| var[$key||"_drawtime"]);
}"
}
]
},
{
"if": "{ !!var[$key||"_stopped"] and timestamp - $draw_time < 3 * 60 * 1000 }",
"init": "{
// all time variable fixed in the dag
$timestamp = data_feed[[oracles='OPNUXBRSSQQGHKQNEPD2GLWQYEUY5XLD', feed_name='timestamp']];
$total_cap = data_feed[[oracles='F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N', feed_name='TOTAL_CAP']];
$eth_btc_price = data_feed[[oracles='F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N', feed_name='ETH_BTC']];
//$bitcoin_hash = data_feed[[oracles='', feed_name='bitcoin_hash']]; // not available in testnet?
// and the winner is
$draw_seed = $timestamp||$total_cap||$bitcoin_hash||$eth_btc_price;
$winner_num = number_from_seed($draw_seed, 1, $key||"_number_of_players");
$winner = var["_player_"||$winner_num];
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{ "You won the lottery!" }"
}
},
{
"app": "payment",
"payload": {
"asset": "{ $asset }",
"outputs": [
{
"address": "{ $winner }",
"amount": "{ var[$key||"_balance"] }"
}
]
}
},
{
"app": "state",
"state": "{
var[$key||"_stopped"] = false;
var[$key||"_balance"] = false;
var[$key||"_dead_line"] = timestamp + $round_buying_period;
var[$key||"_number_of_players"] = false;
}"
}
]
},
{
"if": "{ !!var[$key||"_stopped"] and timestamp - $draw_time > 3 * 60 * 1000 }",
"init": "{
// delay before we draw with seed based only on time variable data
$draw_time = number_from_seed(var[$key||"_seed"], 15 * 60 * 1000, 60 * 60 * 1000);
// pick a helper from the players, but someone else or a bot will be able to trigger for the reward
$helper = var[$key||"_player_"||number_from_seed($draw_time, 1, var[$key||"_number_of_players"])];
$reward = round (var[$key||"_balance"] * $percentage_for_helper / 100);
}",
"messages": [
{
"app": "data",
"payload": {
"help": "{ true }",
"helper": "{ $helper }",
"time": "{ $draw_time }"
}
},
{
"app": "payment",
"payload": {
"asset": "{ $asset }",
"outputs": [
{
"address": "{ $THAANKS }",
"amount": "{ $reward }"
}
]
}
},
{
"app": "state",
"state": "{
if($buy_ticket)
{
$nop = $key||"_number_of_players";
var[$nop] = !!var[$nop] ? var[$nop] + 1 : 1;
var["_player_"||var[$nop]] = trigger.address;
var[$key||"_balance"] += trigger.output[[asset=$asset]]-1000;
response['message'] = "You have enter the lottery, and trigger the draw ^^";
}
else
response['message'] = "You have trigger the draw ^^";
var[$key||"_balance"] -= $reward;
var[$key||"_seed"] = $helper; // for next helper draw if necessary
var[$key||"_stopped"] = timestamp;
var[$key||"_drawtime"] = $draw_time;
}"
}
]
}
]
}
}
]