[
"autonomous agent",
{
"doc_url": "https://raw.githubusercontent.com/jldevelops/aa-lotto-oracle/master/description.json",
"init": "{
$ENTRY = params.entry;
$TRIGGER_FEE = params.trigger_fee;
$ORACLE = params.oracle;
$FEED_NAME = params.feed_name;
$now = timestamp_to_string(timestamp, 'date');
$day = var['today'] ? var['today'] : $now;
$total_bal = balance[base] - storage_size - trigger.output[[asset=base]] - var['user_bytes'];
$tickets_sold = var['trigger_fee'] / $TRIGGER_FEE;
if(trigger.output[[asset!=base]].asset != 'none')
bounce('no asset');
$op_fee = var['low_fee_mode'] ? 500 : 5000;
}",
"messages": {
"cases": [
{
"if": "{!var['low_fee_mode'] AND $total_bal > 10000000}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{trigger.output[[asset=base]]}"
}
]
}
},
{
"app": "state",
"state": "{
var['low_fee_mode'] = true;
response['message'] = 'Low fee mode active. Until AA depletion operations are subsidized';
}"
}
]
},
{
"if": "{$now != $day}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{var['trigger_fee'] + trigger.output[[asset=base]] - $op_fee}"
}
]
}
},
{
"app": "state",
"state": "{
$winner_ticket = number_from_seed(data_feed[[oracles=$ORACLE, feed_name=$FEED_NAME]],$tickets_sold);
var['winner_'||$day] = $winner_ticket;
var['amount_'||$day] = var['total_pot'];
var['user_bytes'] -= var['trigger_fee'];
var['today'] = $now;
var['total_pot'] = false;
var['trigger_fee'] = false;
if(var['low_fee_mode'] AND $total_bal < 50000)
var['low_fee_mode'] = false;
response['message'] = 'Winner ticket saved: '||$winner_ticket;
}"
}
]
},
{
"init": "{
$ticket = var[trigger.address];
if($ticket){
if(starts_with($ticket, $day))
bounce('Address already registered');
$date = substring($ticket, 0, 10);
if(var['amount_'||$date]){
$win = var['winner_'||$date];
$rest = substring($ticket, 11);
$ind = index_of($rest, '_');
$fr = substring($rest, 0, $ind);
$to = substring($rest, $ind+1);
if($fr <= $win AND $to > $win){
$pay = var['amount_'||$date];
}
}
}
if(!$pay AND trigger.output[[asset=base]] >= $ENTRY AND ((trigger.output[[asset=base]] % $ENTRY) != 0))
bounce('Amount has to be multiple of '||$ENTRY||' bytes. An address can buy tickets once a day.');
$tickets = trigger.output[[asset=base]] / $ENTRY;
}",
"messages": [
{
"if": "{$pay}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$pay + trigger.output[[asset=base]] - $op_fee}"
}
]
}
},
{
"app": "state",
"state": "{
if($pay){
var['user_bytes'] -= var['amount_'||$date];
var['amount_'||$date] = false;
var['winner_'||$date] = false;
response['message'] = 'You won '||$date||' draw. Congratulations!';
}
else{
var['user_bytes'] += trigger.output[[asset=base]];
var[trigger.address] = $day||'_'||$tickets_sold||'_'||($tickets + $tickets_sold);
$total_fee = $tickets * $TRIGGER_FEE;
var['total_pot'] += trigger.output[[asset=base]] - $total_fee;
var['trigger_fee'] += $total_fee;
if(!var['today']) var['today'] = $now;
response['message'] = 'You bought '||$tickets||'. Remember, if you trigger reward calculation tomorrow you will win '||(($TRIGGER_FEE/$ENTRY)*100)||'% bytes of total tickets';
}
}"
}
]
}
]
}
}
]