-
Notifications
You must be signed in to change notification settings - Fork 349
/
ownable.cairo
325 lines (286 loc) · 12.1 KB
/
ownable.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.20.0 (access/ownable/ownable.cairo)
/// # Ownable Component
///
/// The Ownable component provides a basic access control mechanism, where
/// there is an account (an owner) that can be granted exclusive access to
/// specific functions.
///
/// The initial owner can be set by using the `initializer` function in
/// construction time. This can later be changed with `transfer_ownership`.
///
/// The component also offers functionality for a two-step ownership
/// transfer where the new owner first has to accept their ownership to
/// finalize the transfer.
#[starknet::component]
pub mod OwnableComponent {
use core::num::traits::Zero;
use crate::ownable::interface;
use crate::ownable::interface::IOwnableTwoStep;
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
#[storage]
pub struct Storage {
pub Ownable_owner: ContractAddress,
pub Ownable_pending_owner: ContractAddress,
}
#[event]
#[derive(Drop, PartialEq, starknet::Event)]
pub enum Event {
OwnershipTransferred: OwnershipTransferred,
OwnershipTransferStarted: OwnershipTransferStarted,
}
/// Emitted when `new_owner` is set as owner of the contract.
/// `new_owner` can be set to zero only if the ownership is renounced.
#[derive(Drop, PartialEq, starknet::Event)]
pub struct OwnershipTransferred {
#[key]
pub previous_owner: ContractAddress,
#[key]
pub new_owner: ContractAddress,
}
/// Emitted when `transfer_ownership` is called on a contract that implements `IOwnableTwoStep`.
/// `previous_owner` is the address of the current owner.
/// `new_owner` is the address of the pending owner.
#[derive(Drop, PartialEq, starknet::Event)]
pub struct OwnershipTransferStarted {
#[key]
pub previous_owner: ContractAddress,
#[key]
pub new_owner: ContractAddress,
}
pub mod Errors {
pub const NOT_OWNER: felt252 = 'Caller is not the owner';
pub const NOT_PENDING_OWNER: felt252 = 'Caller is not the pending owner';
pub const ZERO_ADDRESS_OWNER: felt252 = 'New owner is the zero address';
}
#[embeddable_as(OwnableImpl)]
impl Ownable<
TContractState, +HasComponent<TContractState>,
> of interface::IOwnable<ComponentState<TContractState>> {
/// Returns the address of the current owner.
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
self.Ownable_owner.read()
}
/// Transfers ownership of the contract to a new address.
///
/// Requirements:
///
/// - `new_owner` is not the zero address.
/// - The caller is the contract owner.
///
/// Emits an `OwnershipTransferred` event.
fn transfer_ownership(
ref self: ComponentState<TContractState>, new_owner: ContractAddress,
) {
assert(!new_owner.is_zero(), Errors::ZERO_ADDRESS_OWNER);
self.assert_only_owner();
self._transfer_ownership(new_owner);
}
/// Leaves the contract without an owner. It will not be possible to call
/// `assert_only_owner` functions anymore. Can only be called by the current owner.
///
/// Requirements:
///
/// - The caller is the contract owner.
///
/// Emits an `OwnershipTransferred` event.
fn renounce_ownership(ref self: ComponentState<TContractState>) {
self.assert_only_owner();
self._transfer_ownership(Zero::zero());
}
}
/// Adds support for two step ownership transfer.
#[embeddable_as(OwnableTwoStepImpl)]
impl OwnableTwoStep<
TContractState, +HasComponent<TContractState>,
> of interface::IOwnableTwoStep<ComponentState<TContractState>> {
/// Returns the address of the current owner.
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
self.Ownable_owner.read()
}
/// Returns the address of the pending owner.
fn pending_owner(self: @ComponentState<TContractState>) -> ContractAddress {
self.Ownable_pending_owner.read()
}
/// Finishes the two-step ownership transfer process by accepting the ownership.
/// Can only be called by the pending owner.
///
/// Requirements:
///
/// - The caller is the pending owner.
///
/// Emits an `OwnershipTransferred` event.
fn accept_ownership(ref self: ComponentState<TContractState>) {
let caller = get_caller_address();
let pending_owner = self.Ownable_pending_owner.read();
assert(caller == pending_owner, Errors::NOT_PENDING_OWNER);
self._transfer_ownership(pending_owner);
}
/// Starts the two-step ownership transfer process by setting the pending owner.
///
/// Setting `new_owner` to the zero address is allowed; this can be used to cancel an
/// initiated ownership transfer.
/// Requirements:
///
/// - The caller is the contract owner.
///
/// Emits an `OwnershipTransferStarted` event.
fn transfer_ownership(
ref self: ComponentState<TContractState>, new_owner: ContractAddress,
) {
self.assert_only_owner();
self._propose_owner(new_owner);
}
/// Leaves the contract without owner. It will not be possible to call `assert_only_owner`
/// functions anymore. Can only be called by the current owner.
///
/// Requirements:
///
/// - The caller is the contract owner.
///
/// Emits an `OwnershipTransferred` event.
fn renounce_ownership(ref self: ComponentState<TContractState>) {
self.assert_only_owner();
self._transfer_ownership(Zero::zero());
}
}
/// Adds camelCase support for `IOwnable`.
#[embeddable_as(OwnableCamelOnlyImpl)]
impl OwnableCamelOnly<
TContractState, +HasComponent<TContractState>,
> of interface::IOwnableCamelOnly<ComponentState<TContractState>> {
fn transferOwnership(ref self: ComponentState<TContractState>, newOwner: ContractAddress) {
Ownable::transfer_ownership(ref self, newOwner);
}
fn renounceOwnership(ref self: ComponentState<TContractState>) {
Ownable::renounce_ownership(ref self);
}
}
/// Adds camelCase support for `IOwnableTwoStep`.
#[embeddable_as(OwnableTwoStepCamelOnlyImpl)]
impl OwnableTwoStepCamelOnly<
TContractState, +HasComponent<TContractState>,
> of interface::IOwnableTwoStepCamelOnly<ComponentState<TContractState>> {
fn pendingOwner(self: @ComponentState<TContractState>) -> ContractAddress {
OwnableTwoStep::pending_owner(self)
}
fn acceptOwnership(ref self: ComponentState<TContractState>) {
self.accept_ownership();
}
fn transferOwnership(ref self: ComponentState<TContractState>, newOwner: ContractAddress) {
OwnableTwoStep::transfer_ownership(ref self, newOwner);
}
fn renounceOwnership(ref self: ComponentState<TContractState>) {
OwnableTwoStep::renounce_ownership(ref self);
}
}
#[embeddable_as(OwnableMixinImpl)]
impl OwnableMixin<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of interface::OwnableABI<ComponentState<TContractState>> {
// IOwnable
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
Ownable::owner(self)
}
fn transfer_ownership(
ref self: ComponentState<TContractState>, new_owner: ContractAddress,
) {
Ownable::transfer_ownership(ref self, new_owner);
}
fn renounce_ownership(ref self: ComponentState<TContractState>) {
Ownable::renounce_ownership(ref self);
}
// IOwnableCamelOnly
fn transferOwnership(ref self: ComponentState<TContractState>, newOwner: ContractAddress) {
OwnableCamelOnly::transferOwnership(ref self, newOwner);
}
fn renounceOwnership(ref self: ComponentState<TContractState>) {
OwnableCamelOnly::renounceOwnership(ref self);
}
}
#[embeddable_as(OwnableTwoStepMixinImpl)]
impl OwnableTwoStepMixin<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of interface::OwnableTwoStepABI<ComponentState<TContractState>> {
// IOwnableTwoStep
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
OwnableTwoStep::owner(self)
}
fn pending_owner(self: @ComponentState<TContractState>) -> ContractAddress {
OwnableTwoStep::pending_owner(self)
}
fn accept_ownership(ref self: ComponentState<TContractState>) {
OwnableTwoStep::accept_ownership(ref self);
}
fn transfer_ownership(
ref self: ComponentState<TContractState>, new_owner: ContractAddress,
) {
OwnableTwoStep::transfer_ownership(ref self, new_owner);
}
fn renounce_ownership(ref self: ComponentState<TContractState>) {
OwnableTwoStep::renounce_ownership(ref self);
}
// IOwnableTwoStepCamelOnly
fn pendingOwner(self: @ComponentState<TContractState>) -> ContractAddress {
OwnableTwoStepCamelOnly::pendingOwner(self)
}
fn acceptOwnership(ref self: ComponentState<TContractState>) {
OwnableTwoStepCamelOnly::acceptOwnership(ref self);
}
fn transferOwnership(ref self: ComponentState<TContractState>, newOwner: ContractAddress) {
OwnableTwoStepCamelOnly::transferOwnership(ref self, newOwner);
}
fn renounceOwnership(ref self: ComponentState<TContractState>) {
OwnableTwoStepCamelOnly::renounceOwnership(ref self);
}
}
#[generate_trait]
pub impl InternalImpl<
TContractState, +HasComponent<TContractState>,
> of InternalTrait<TContractState> {
/// Sets the contract's initial owner.
///
/// Requirements:
///
/// - `owner` is not the zero address.
///
/// This function should be called at construction time.
fn initializer(ref self: ComponentState<TContractState>, owner: ContractAddress) {
assert(!owner.is_zero(), Errors::ZERO_ADDRESS_OWNER);
self._transfer_ownership(owner);
}
/// Panics if called by any account other than the owner. Use this
/// to restrict access to certain functions to the owner.
fn assert_only_owner(self: @ComponentState<TContractState>) {
let owner = self.Ownable_owner.read();
let caller = get_caller_address();
assert(caller == owner, Errors::NOT_OWNER);
}
/// Transfers ownership of the contract to a new address and resets
/// the pending owner to the zero address.
///
/// Internal function without access restriction.
///
/// Emits an `OwnershipTransferred` event.
fn _transfer_ownership(
ref self: ComponentState<TContractState>, new_owner: ContractAddress,
) {
self.Ownable_pending_owner.write(Zero::zero());
let previous_owner: ContractAddress = self.Ownable_owner.read();
self.Ownable_owner.write(new_owner);
self.emit(OwnershipTransferred { previous_owner, new_owner });
}
/// Sets a new pending owner.
///
/// Internal function without access restriction.
///
/// Emits an `OwnershipTransferStarted` event.
fn _propose_owner(ref self: ComponentState<TContractState>, new_owner: ContractAddress) {
let previous_owner = self.Ownable_owner.read();
self.Ownable_pending_owner.write(new_owner);
self.emit(OwnershipTransferStarted { previous_owner, new_owner });
}
}
}