-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_auth.cairo
67 lines (58 loc) · 2.17 KB
/
user_auth.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Declare this file as a StarkNet contract.
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.math import assert_nn
// A map from user (represented by account contract address)
// to their balance.
@storage_var
func balance(user: felt) -> (res: felt) {
}
//signature of balance.read() and balance.write():
// func read{
// syscall_ptr: felt*,
// range_check_ptr,
// pedersen_ptr: HashBuiltin*,
// }(user: felt) -> (res: felt) {
// }
// func write{
// syscall_ptr: felt*,
// range_check_ptr,
// pedersen_ptr: HashBuiltin*,
// }(user: felt, value: felt) {
// }
// implicit argument:
// pedersen_ptr allows to compute the Pedersen hash function, range_check_ptr allows to compare integers.
// the reason is that storage variables require these implicit arguments in order to compute the actual memory address of this variable.
// this may not be needed in simple variables such as balance, but with maps (see Storage maps) computing the Pedersen hash is part of what read() and write() do.
// syscall_ptr is a new primitive, unique to StarkNet contracts (it doesn’t exist in Cairo).
// it allows the code to invoke system calls. It is also an implicit argument of read() and write() (required, in this case, because storage access is done using system calls).
// Increases the balance of the user by the given amount.
@external
func increase_balance{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
}(amount: felt) {
// Verify that the amount is positive.
with_attr error_message(
"Amount must be positive. Got: {amount}.") {
assert_nn(amount);
}
// Obtain the address of the account contract.
let (user) = get_caller_address();
// Read and update its balance.
let (res) = balance.read(user=user);
balance.write(user, res + amount);
return ();
}
// Returns the balance of the given user.
@view
func get_balance{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
}(user: felt) -> (res: felt) {
let (res) = balance.read(user=user);
return (res=res);
}