[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$DAY = 86400;
$PURCHASE_PERIOD = 1 * $DAY;
$PAYOUT_PERIOD = 1 * $DAY;
$TYPICAL_FEE = 1000;
$PRICE = 1000; // bytes per ticket
$ECHO_ADDRESS = "
OJSL357YOGWOCWE6ZV4CBR22J2GEFBYY";
$round = var['round'] otherwise 0;
$asset = var[$round || '-asset'];
$enddate = var[$round || '-enddate'] otherwise 0;
$tickets = var[$round || '-tickets'] otherwise 0;
$open = timestamp < $enddate;
$hours_till_next_round = max(0, round(($enddate + $PAYOUT_PERIOD - timestamp) / 3600, 2));
$player = $round || '-' || trigger.address;
$winner = var[$round || '-winner'];
$prize = var[$round || '-prize'];
$paid = var[$round || '-payout-unit'];
$lo = var[$player || '-lo'] otherwise 0;
$hi = var[$player || '-hi'] otherwise 0;
}",
"messages": {
"cases": [
{
"if": "{ trigger.data.open }",
"init": "{
if (timestamp < $enddate + $PAYOUT_PERIOD) {
bounce("Lottery ticket for round #" || $round || ' has already been created. Next round is possible in ' || $hours_till_next_round || ' hours');
}
}",
"messages": [
{
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": true,
"auto_destroy": true,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
$new_round = $round + 1;
$new_enddate = timestamp + $PURCHASE_PERIOD;
var['round'] = $new_round;
var[$new_round || '-enddate'] = $new_enddate;
var[$new_round || '-asset'] = response_unit;
response['round'] = $new_round;
response['enddate'] = $new_enddate;
response['asset'] = response_unit;
response['message'] = 'New lottery round ' || $new_round || ' created.';
}"
}
]
},
{
"if": "{ trigger.data.close AND trigger.address != $ECHO_ADDRESS}",
"init": "{
if (!$round) bounce('Lottery has not started yet.');
if (!$tickets) bounce("No tickets were sold in this round.");
if ($open) bounce("Lottery is still open. Wait until the purchase period expires!");
if ($winner) bounce("The lottery is already closed and the winner has been selected.");
if (trigger.output[[asset="base"]] < 13000) bounce("Minimum amount to close the lottery round is 13000");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$ECHO_ADDRESS}",
"amount": 12000
}
]
}
},
{
"app": "data",
"payload": {
"close": true
}
}
]
},
{
"if": "{ trigger.data.close AND trigger.address == $ECHO_ADDRESS }",
"init": "{
$reimbursement = 10000; // for the user who triggered closing the round
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.initial_address}",
"amount": "{$reimbursement}"
}
]
}
},
{
"if": "{ !$winner }",
"app": "state",
"state": "{
$seed = mc_unit;
var[$round || '-seed'] = $seed;
var[$round || '-winner'] = $tickets == 1 ? 1 : number_from_seed($seed, $tickets - 1) + 1;
var[$round || '-prize'] = balance['base'] - $reimbursement - $TYPICAL_FEE;
}"
}
]
},
{
"if": "{ $asset AND trigger.output[[asset=$asset]] > 0 }",
"init": "{
if (!$winner) bounce("Winner has not been selected yet! Try again later or send close=1 data message to trigger winner selection");
$reimbursement = trigger.output[[asset='base']] - $TYPICAL_FEE;
if ($lo <= $winner AND $winner <= $hi) {
if ($paid) bounce("The prize has been paid already!");
$amount = $reimbursement + $prize;
$won = true;
} else {
$amount = $reimbursement;
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{ $amount }"
}
]
}
},
{
"app": "state",
"state": "{
if ($won) {
var[$round || '-payout-unit'] = response_unit;
response['message'] = 'You won ' || $prize || ' bytes!';
} else {
response['message'] = 'You won did not win this time.';
}
}"
}
]
},
{
"init": "{
if (!$asset) bounce("Lottery tickets are not available yet");
if (!$open) bounce("Lottery purchase period is over. Return your tickets to find out if you won!");
if ($lo OR $hi) bounce("You already bought tickets in this round. Come back in the next round!");
$amount = round(trigger.output[[asset=base]] / $PRICE);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$amount}"
}
]
}
},
{
"app": "state",
"state": "{
var[$player || '-lo'] = $tickets + 1;
var[$player || '-hi'] = $tickets + $amount;
var[$round || '-tickets'] += $amount;
response['message'] = 'You purchased ' || $amount || ' tickets of ' || $asset || ' in round #' || $round || '. The first ticket number is ' || ($tickets + 1) || ', the last is ' || ($tickets + $amount);
}"
}
]
}
]
}
}
]