| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://raw.githubusercontent.com/jldevelops/aa-lotto-oracle/master/description.json", |
| 5 | "init": "{ |
| 6 | $ENTRY = params.entry; |
| 7 | $TRIGGER_FEE = params.trigger_fee; |
| 8 | $ORACLE = params.oracle; |
| 9 | $FEED_NAME = params.feed_name; |
| 10 | |
| 11 | $now = timestamp_to_string(timestamp, 'date'); |
| 12 | $day = var['today'] ? var['today'] : $now; |
| 13 | $total_bal = balance[base] - storage_size - trigger.output[[asset=base]] - var['user_bytes']; |
| 14 | $tickets_sold = var['trigger_fee'] / $TRIGGER_FEE; |
| 15 | if(trigger.output[[asset!=base]].asset != 'none') |
| 16 | bounce('no asset'); |
| 17 | $op_fee = var['low_fee_mode'] ? 500 : 5000; |
| 18 | }", |
| 19 | "messages": { |
| 20 | "cases": [ |
| 21 | { |
| 22 | "if": "{!var['low_fee_mode'] AND $total_bal > 10000000}", |
| 23 | "messages": [ |
| 24 | { |
| 25 | "app": "payment", |
| 26 | "payload": { |
| 27 | "asset": "base", |
| 28 | "outputs": [ |
| 29 | { |
| 30 | "address": "{trigger.address}", |
| 31 | "amount": "{trigger.output[[asset=base]]}" |
| 32 | } |
| 33 | ] |
| 34 | } |
| 35 | }, |
| 36 | { |
| 37 | "app": "state", |
| 38 | "state": "{ |
| 39 | var['low_fee_mode'] = true; |
| 40 | response['message'] = 'Low fee mode active. Until AA depletion operations are subsidized'; |
| 41 | }" |
| 42 | } |
| 43 | ] |
| 44 | }, |
| 45 | { |
| 46 | "if": "{$now != $day}", |
| 47 | "messages": [ |
| 48 | { |
| 49 | "app": "payment", |
| 50 | "payload": { |
| 51 | "asset": "base", |
| 52 | "outputs": [ |
| 53 | { |
| 54 | "address": "{trigger.address}", |
| 55 | "amount": "{var['trigger_fee'] + trigger.output[[asset=base]] - $op_fee}" |
| 56 | } |
| 57 | ] |
| 58 | } |
| 59 | }, |
| 60 | { |
| 61 | "app": "state", |
| 62 | "state": "{ |
| 63 | $winner_ticket = number_from_seed(data_feed[[oracles=$ORACLE, feed_name=$FEED_NAME]],$tickets_sold); |
| 64 | var['winner_'||$day] = $winner_ticket; |
| 65 | var['amount_'||$day] = var['total_pot']; |
| 66 | var['user_bytes'] -= var['trigger_fee']; |
| 67 | var['today'] = $now; |
| 68 | var['total_pot'] = false; |
| 69 | var['trigger_fee'] = false; |
| 70 | if(var['low_fee_mode'] AND $total_bal < 50000) |
| 71 | var['low_fee_mode'] = false; |
| 72 | response['message'] = 'Winner ticket saved: '||$winner_ticket; |
| 73 | }" |
| 74 | } |
| 75 | ] |
| 76 | }, |
| 77 | { |
| 78 | "init": "{ |
| 79 | $ticket = var[trigger.address]; |
| 80 | if($ticket){ |
| 81 | if(starts_with($ticket, $day)) |
| 82 | bounce('Address already registered'); |
| 83 | $date = substring($ticket, 0, 10); |
| 84 | if(var['amount_'||$date]){ |
| 85 | $win = var['winner_'||$date]; |
| 86 | $rest = substring($ticket, 11); |
| 87 | $ind = index_of($rest, '_'); |
| 88 | $fr = substring($rest, 0, $ind); |
| 89 | $to = substring($rest, $ind+1); |
| 90 | if($fr <= $win AND $to > $win){ |
| 91 | $pay = var['amount_'||$date]; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | if(!$pay AND trigger.output[[asset=base]] >= $ENTRY AND ((trigger.output[[asset=base]] % $ENTRY) != 0)) |
| 96 | bounce('Amount has to be multiple of '||$ENTRY||' bytes. An address can buy tickets once a day.'); |
| 97 | $tickets = trigger.output[[asset=base]] / $ENTRY; |
| 98 | }", |
| 99 | "messages": [ |
| 100 | { |
| 101 | "if": "{$pay}", |
| 102 | "app": "payment", |
| 103 | "payload": { |
| 104 | "asset": "base", |
| 105 | "outputs": [ |
| 106 | { |
| 107 | "address": "{trigger.address}", |
| 108 | "amount": "{$pay + trigger.output[[asset=base]] - $op_fee}" |
| 109 | } |
| 110 | ] |
| 111 | } |
| 112 | }, |
| 113 | { |
| 114 | "app": "state", |
| 115 | "state": "{ |
| 116 | if($pay){ |
| 117 | var['user_bytes'] -= var['amount_'||$date]; |
| 118 | var['amount_'||$date] = false; |
| 119 | var['winner_'||$date] = false; |
| 120 | response['message'] = 'You won '||$date||' draw. Congratulations!'; |
| 121 | } |
| 122 | else{ |
| 123 | var['user_bytes'] += trigger.output[[asset=base]]; |
| 124 | var[trigger.address] = $day||'_'||$tickets_sold||'_'||($tickets + $tickets_sold); |
| 125 | $total_fee = $tickets * $TRIGGER_FEE; |
| 126 | var['total_pot'] += trigger.output[[asset=base]] - $total_fee; |
| 127 | var['trigger_fee'] += $total_fee; |
| 128 | if(!var['today']) var['today'] = $now; |
| 129 | response['message'] = 'You bought '||$tickets||'. Remember, if you trigger reward calculation tomorrow you will win '||(($TRIGGER_FEE/$ENTRY)*100)||'% bytes of total tickets'; |
| 130 | } |
| 131 | }" |
| 132 | } |
| 133 | ] |
| 134 | } |
| 135 | ] |
| 136 | } |
| 137 | } |
| 138 | ] |