Skip to content

Commit

Permalink
Add lottery example
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Jun 28, 2023
1 parent b14dbf0 commit 8ff9b87
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/lottery/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
outputs/
build/
8 changes: 8 additions & 0 deletions examples/lottery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# lottery.aleo

## Build Guide

To compile this Aleo program, run:
```bash
aleo build
```
4 changes: 4 additions & 0 deletions examples/lottery/inputs/lottery.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// The program input for lottery/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;
10 changes: 10 additions & 0 deletions examples/lottery/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"program": "lottery.aleo",
"version": "0.0.0",
"description": "",
"development": {
"private_key": "APrivateKey1zkpJrHD9orEuTpL8dDnSBZo6VyKWdbrk3VJfcwEv9SsQBN2",
"address": "aleo1dvx603addv7c2uqj6ksm9hccaypzn0ngus4n2vygvdlhqvkcyyfqvh4zdz"
},
"license": "MIT"
}
30 changes: 30 additions & 0 deletions examples/lottery/src/main.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// The 'lottery' program.
program lottery.aleo {

mapping num_winners: u8 => u8;

record Ticket {
owner: address,
}

transition play() -> u32 {
let ticket: Ticket = Ticket {
owner: self.caller,
};
return ticket then finalize();
}

finalize play() {
// Check that the lottery has not expired.
assert(block.height <= 1000);

// Randomly select whether or not the ticket is a winner.
assert(ChaCha::rand_bool());

// Check that the maximum number of winners have not been reached.
let winners: u8 = num_winners.get_or_use(0u8);
assert(winners < 5u8);
num_winners.set(0u8, winners + 1u8);

}
}
12 changes: 12 additions & 0 deletions tests/expectations/compiler/finalize/rand.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
namespace: Compile
expectation: Pass
outputs:
- - initial_ast: 2d7b208912c97c514488786ee171503bfe6d230fb6f0f35725fdcc0d217dad20
unrolled_ast: 2d7b208912c97c514488786ee171503bfe6d230fb6f0f35725fdcc0d217dad20
ssa_ast: d31261c1b1ffc59dae4f917070deae35adcb480707a00e4ce900458f962855cc
flattened_ast: 57633e91a7d54fa5f5ec007863b135a0b1bca23f3016c2bc01ab8293ad2f59d4
inlined_ast: 57633e91a7d54fa5f5ec007863b135a0b1bca23f3016c2bc01ab8293ad2f59d4
dce_ast: 09659efca22ece8c63c317e0e22e1400871541e2d40a6604e073e64a7fc54153
bytecode: 268f9afb6b8472b88b0c91f927b0cd4a69c10ad4457714dbb6faddeee5405cc6
warnings: ""
1 change: 1 addition & 0 deletions tests/tests/compiler/finalize/rand.leo
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ program test.aleo {
let m: u32 = ChaCha::rand_u32();
let n: u64 = ChaCha::rand_u64();
let o: u128 = ChaCha::rand_u128();
assert(b);
}
}

0 comments on commit 8ff9b87

Please sign in to comment.