/
library.cairo
113 lines (91 loc) · 2.58 KB
/
library.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.6.0 (upgrades/library.cairo)
%lang starknet
from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.math import assert_not_zero
//
// Events
//
@event
func Upgraded(implementation: felt) {
}
@event
func AdminChanged(previousAdmin: felt, newAdmin: felt) {
}
//
// Storage variables
//
@storage_var
func Proxy_implementation_hash() -> (implementation: felt) {
}
@storage_var
func Proxy_admin() -> (admin: felt) {
}
@storage_var
func Proxy_initialized() -> (initialized: felt) {
}
namespace Proxy {
//
// Initializer
//
func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
proxy_admin: felt
) {
let (initialized) = Proxy_initialized.read();
with_attr error_message("Proxy: contract already initialized") {
assert initialized = FALSE;
}
Proxy_initialized.write(TRUE);
_set_admin(proxy_admin);
return ();
}
//
// Guards
//
func assert_only_admin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
let (caller) = get_caller_address();
let (admin) = Proxy_admin.read();
with_attr error_message("Proxy: caller is not admin") {
assert admin = caller;
}
return ();
}
//
// Getters
//
func get_implementation_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
) -> (implementation: felt) {
return Proxy_implementation_hash.read();
}
func get_admin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
admin: felt
) {
return Proxy_admin.read();
}
//
// Unprotected
//
func _set_admin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
new_admin: felt
) {
let (previous_admin) = get_admin();
Proxy_admin.write(new_admin);
AdminChanged.emit(previous_admin, new_admin);
return ();
}
//
// Upgrade
//
func _set_implementation_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
new_implementation: felt
) {
with_attr error_message("Proxy: implementation hash cannot be zero") {
assert_not_zero(new_implementation);
}
Proxy_implementation_hash.write(new_implementation);
Upgraded.emit(new_implementation);
return ();
}
}