Code Examples

Example 1 : Bare Minimum

1
%lang starknet
2
​
3
%builtins pedersen range_check
4
​
5
from starkware.cairo.common.uint256 import Uint256
6
from starkware.starknet.common.syscalls import get_caller_address, get_block_timestamp
7
from starkware.cairo.common.cairo_builtins import HashBuiltin
8
from starkware.cairo.common.cairo_secp.bigint import BigInt3
9
​
10
@contract_interface
11
namespace IStarkLinkRandomness {
12
func request_randomness(beacon_address: felt) -> (requestId: felt) {
13
}
14
}
15
​
16
@storage_var
17
func oracle_address() -> (addr: felt) {
18
}
19
​
20
@storage_var
21
func beacon_address() -> (address: felt) {
22
}
23
​
24
@constructor
25
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
26
oracle_addr: felt, _beacon_address: felt
27
) {
28
oracle_address.write(oracle_addr);
29
beacon_address.write(_beacon_address);
30
​
31
return ();
32
}
33
​
34
@external
35
func request_rng{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
36
request_id: felt
37
) {
38
let (oracle) = oracle_address.read();
39
let (_beacon_address) = beacon_address.read();
40
let (request_id) = IStarkLinkRandomness.request_rng(
41
contract_address=oracle, beacon_address=_beacon_address
42
);
43
return (request_id,);
44
}
45
​
46
@external
47
func will_recieve_rng{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
48
rng: BigInt3, request_id: felt
49
) {
50
let (oracle) = oracle_address.read();
51
let (caller_address) = get_caller_address();
52
​
53
assert oracle = caller_address;
54
​
55
// # Do something with RNG
56
​
57
return ();
58
}
59
​

Example 2 : Dice Roll

1
%lang starknet
2
​
3
%builtins pedersen range_check
4
​
5
from starkware.cairo.common.uint256 import Uint256
6
from starkware.starknet.common.syscalls import get_caller_address
7
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
8
from starkware.cairo.common.math import unsigned_div_rem, split_felt
9
from starkware.cairo.common.cairo_secp.bigint import BigInt3
10
from starkware.cairo.common.hash import hash2
11
​
12
@contract_interface
13
namespace IRNGOracle {
14
func request_rng(beacon_address: felt) -> (requestId: felt) {
15
}
16
}
17
​
18
@storage_var
19
func oracle_address() -> (addr: felt) {
20
}
21
​
22
@storage_var
23
func beacon_address() -> (address: felt) {
24
}
25
​
26
@storage_var
27
func roll_results(id: felt) -> (result: felt) {
28
}
29
​
30
@event
31
func rng_request_resolved(rng: BigInt3, request_id: felt, result: felt) {
32
}
33
​
34
@constructor
35
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
36
oracle_addr: felt, _beacon_address: felt
37
) {
38
oracle_address.write(oracle_addr);
39
beacon_address.write(_beacon_address);
40
​
41
return ();
42
}
43
​
44
@external
45
func request_rng{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
46
request_id: felt
47
) {
48
let (oracle) = oracle_address.read();
49
let (_beacon_address) = beacon_address.read();
50
let (request_id) = IRNGOracle.request_rng(
51
contract_address=oracle, beacon_address=_beacon_address
52
);
53
return (request_id,);
54
}
55
​
56
func roll_dice{syscall_ptr: felt*, range_check_ptr}(rng: felt) -> (roll: felt) {
57
// Take the lower 128 bits of the random string
58
let (_, low) = split_felt(rng);
59
let (_, roll) = unsigned_div_rem(low, 6);
60
return (roll + 1,);
61
}
62
​
63
@external
64
func will_recieve_rng{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
65
rng: BigInt3, request_id: felt
66
) {
67
let (oracle) = oracle_address.read();
68
let (caller_address) = get_caller_address();
69
​
70
assert oracle = caller_address;
71
​
72
let (roll) = roll_dice(rng.d0);
73
​
74
roll_results.write(request_id, roll);
75
​
76
rng_request_resolved.emit(rng, request_id, roll);
77
return ();
78
}
79
​
80
@view
81
func get_roll_result{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(id: felt) -> (
82
roll: felt
83
) {
84
let (roll) = roll_results.read(id);
85
return (roll,);
86
}
87
​