| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $DAY = 86400; |
| 9 | $PURCHASE_PERIOD = 1 * $DAY; |
| 10 | $PAYOUT_PERIOD = 1 * $DAY; |
| 11 | $PRICE = 1000; |
| 12 | $CONTROL_ADDRESS = 'T7BTEFK6V6GBD3XLFTCZVBZTS4HS7R7Q'; |
| 13 | |
| 14 | $round = var['round'] otherwise 0; |
| 15 | $asset = var[$round || '-asset']; |
| 16 | $enddate = var[$round || '-enddate'] otherwise 0; |
| 17 | $tickets = var[$round || '-tickets'] otherwise 0; |
| 18 | |
| 19 | $open = timestamp < $enddate; |
| 20 | $hours_till_next_round = max(0, round(($enddate + $PAYOUT_PERIOD - timestamp) / 3600, 2)); |
| 21 | |
| 22 | $player = $round || '-' || trigger.address; |
| 23 | $winner = var[$round || '-winner']; |
| 24 | $prize = var[$round || '-prize']; |
| 25 | $lo = var[$player || '-lo'] otherwise 0; |
| 26 | $hi = var[$player || '-hi'] otherwise 0; |
| 27 | }", |
| 28 | "messages": { |
| 29 | "cases": [ |
| 30 | { |
| 31 | "if": "{ trigger.data.update AND trigger.address == $CONTROL_ADDRESS}", |
| 32 | "messages": [ |
| 33 | { |
| 34 | "app": "payment", |
| 35 | "payload": { |
| 36 | "asset": "base", |
| 37 | "outputs": [ |
| 38 | { |
| 39 | "address": "{trigger.address}", |
| 40 | "amount": "{ trigger.data.sweep ? "": trigger.output[[asset='base']] - 1000 }" |
| 41 | } |
| 42 | ] |
| 43 | } |
| 44 | }, |
| 45 | { |
| 46 | "app": "state", |
| 47 | "state": "{ |
| 48 | if (trigger.data.enddate) { |
| 49 | var[$round || '-enddate'] = trigger.data.enddate; |
| 50 | response[$round || '-enddate'] = trigger.data.enddate; |
| 51 | } |
| 52 | }" |
| 53 | } |
| 54 | ] |
| 55 | }, |
| 56 | { |
| 57 | "if": "{ trigger.data.query }", |
| 58 | "messages": [ |
| 59 | { |
| 60 | "app": "payment", |
| 61 | "payload": { |
| 62 | "asset": "base", |
| 63 | "outputs": [ |
| 64 | { |
| 65 | "address": "{trigger.address}", |
| 66 | "amount": "{ trigger.output[[asset='base']] - 1000 }" |
| 67 | } |
| 68 | ] |
| 69 | } |
| 70 | }, |
| 71 | { |
| 72 | "app": "state", |
| 73 | "state": "{ |
| 74 | response['round'] = $round; |
| 75 | response['open'] = $open; |
| 76 | response['asset'] = $asset; |
| 77 | response['enddate'] = $enddate; |
| 78 | response['payout_enddate'] = $enddate + $PAYOUT_PERIOD; |
| 79 | response['message'] = 'Round #' || $round || ' is currently ' |
| 80 | || ($open |
| 81 | ? 'open with accumulated balance of ' || balance[base] |
| 82 | : 'closed. The final prize is ' || $prize ); |
| 83 | }" |
| 84 | } |
| 85 | ] |
| 86 | }, |
| 87 | { |
| 88 | "if": "{ trigger.data.open }", |
| 89 | "init": "{ |
| 90 | if (timestamp < $enddate + $PAYOUT_PERIOD) { |
| 91 | bounce("Lottery ticket for round #" || $round || ' has already been created. Next round is possible in ' || $hours_till_next_round || ' hours'); |
| 92 | } |
| 93 | }", |
| 94 | "messages": [ |
| 95 | { |
| 96 | "app": "asset", |
| 97 | "payload": { |
| 98 | "is_private": false, |
| 99 | "is_transferrable": true, |
| 100 | "auto_destroy": true, |
| 101 | "fixed_denominations": false, |
| 102 | "issued_by_definer_only": true, |
| 103 | "cosigned_by_definer": false, |
| 104 | "spender_attested": false |
| 105 | } |
| 106 | }, |
| 107 | { |
| 108 | "app": "state", |
| 109 | "state": "{ |
| 110 | $new_round = $round + 1; |
| 111 | $new_enddate = timestamp + $PURCHASE_PERIOD; |
| 112 | |
| 113 | var['round'] = $new_round; |
| 114 | var[$new_round || '-enddate'] = $new_enddate; |
| 115 | var[$new_round || '-asset'] = response_unit; |
| 116 | |
| 117 | response['round'] = $new_round; |
| 118 | response['enddate'] = $new_enddate; |
| 119 | response['asset'] = response_unit; |
| 120 | response['message'] = 'New lottery round ' || $new_round || ' created.'; |
| 121 | }" |
| 122 | } |
| 123 | ] |
| 124 | }, |
| 125 | { |
| 126 | "if": "{ trigger.data.close }", |
| 127 | "init": "{ |
| 128 | if (!$round) bounce('Lottery has not started yet.'); |
| 129 | if ($open) bounce("Lottery is still open. Wait until the purchase period expires!"); |
| 130 | if ($winner) bounce("The lottery is already closed and the winner has been selected."); |
| 131 | }", |
| 132 | "messages": [ |
| 133 | { |
| 134 | "app": "state", |
| 135 | "state": "{ |
| 136 | $seed = mc_unit; |
| 137 | var[$round || '-seed'] = $seed; |
| 138 | var[$round || '-winner'] = number_from_seed($seed, $points - 1); |
| 139 | var[$round || '-prize'] = balance['base']; |
| 140 | }" |
| 141 | } |
| 142 | ] |
| 143 | }, |
| 144 | { |
| 145 | "if": "{ $prize AND trigger.output[[asset=$asset]] > 0 }", |
| 146 | "init": "{ |
| 147 | $reimbursement = trigger.output[[asset='base']] - 1000; |
| 148 | |
| 149 | if ($lo <= $winner AND $winner < $hi) { |
| 150 | $amount = $reimbursement + $prize; |
| 151 | } else { |
| 152 | $amount = $reimbursement; |
| 153 | } |
| 154 | }", |
| 155 | "messages": [ |
| 156 | { |
| 157 | "app": "payment", |
| 158 | "payload": { |
| 159 | "asset": "base", |
| 160 | "outputs": [ |
| 161 | { |
| 162 | "address": "{trigger.address}", |
| 163 | "amount": "{ $amount }" |
| 164 | } |
| 165 | ] |
| 166 | } |
| 167 | } |
| 168 | ] |
| 169 | }, |
| 170 | { |
| 171 | "init": "{ |
| 172 | if (!$asset) bounce("Lottery tickets are not available yet"); |
| 173 | if (!$open) bounce("Lottery round has closed."); |
| 174 | if ($lo OR $hi) bounce("You already bought tickets in this round. Come back in the next round!"); |
| 175 | $amount = round(trigger.output[[asset=base]] / $PRICE); |
| 176 | }", |
| 177 | "messages": [ |
| 178 | { |
| 179 | "app": "payment", |
| 180 | "payload": { |
| 181 | "asset": "{$asset}", |
| 182 | "outputs": [ |
| 183 | { |
| 184 | "address": "{trigger.address}", |
| 185 | "amount": "{$amount}" |
| 186 | } |
| 187 | ] |
| 188 | } |
| 189 | }, |
| 190 | { |
| 191 | "app": "state", |
| 192 | "state": "{ |
| 193 | var[$player || '-lo'] = $tickets; |
| 194 | var[$player || '-hi'] = $tickets + $amount; |
| 195 | var[$round || 'tickets'] += $amount; |
| 196 | }" |
| 197 | } |
| 198 | ] |
| 199 | } |
| 200 | ] |
| 201 | } |
| 202 | } |
| 203 | ] |