/ tests / tests / compiler / examples / lottery.adl
lottery.adl
 1  
 2  // The 'lottery' program.
 3  program lottery.alpha {
 4  
 5      mapping num_winners: u8 => u8;
 6  
 7      record Ticket {
 8          owner: address,
 9      }
10  
11      async transition play() -> (Ticket, Future) {
12          let ticket: Ticket = Ticket {
13              owner: self.signer,
14          };
15          return (ticket, finalize_play());
16      }
17  
18      async function finalize_play() {
19          // Check that the lottery has not expired.
20          assert(block.height <= 1000u32);
21  
22          // Randomly select whether or not the ticket is a winner.
23          assert(ChaCha::rand_bool());
24  
25          // Check that the maximum number of winners have not been reached.
26          let winners: u8 = num_winners.get_or_use(0u8, 0u8);
27          assert(winners < 5u8);
28          num_winners.set(0u8, winners + 1u8);
29  
30      }
31  }
32