-
Notifications
You must be signed in to change notification settings - Fork 0
/
ownable.cairo
71 lines (61 loc) · 2.43 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
use starknet::ContractAddress;
#[starknet::interface]
pub trait IOwnable<TContractState> {
fn get_owner(self: @TContractState) -> ContractAddress;
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress);
}
#[starknet::component]
pub mod OwnableComponent {
use core::num::traits::Zero;
use starknet::{ContractAddress, get_caller_address};
use super::IOwnable;
#[storage]
struct Storage {
owner: ContractAddress,
}
#[event]
#[derive(Drop, PartialEq, starknet::Event)]
pub enum Event {
OwnershipTransferred: OwnershipTransferred,
}
#[derive(Drop, PartialEq, starknet::Event)]
struct OwnershipTransferred {
#[key]
previous_owner: ContractAddress,
#[key]
new_owner: ContractAddress,
}
#[embeddable_as(OwnableImpl)]
pub impl Ownable<TContractState, +HasComponent<TContractState>> of IOwnable<ComponentState<TContractState>> {
fn get_owner(self: @ComponentState<TContractState>) -> ContractAddress {
self.owner.read()
}
fn transfer_ownership(ref self: ComponentState<TContractState>, new_owner: ContractAddress) {
assert(!new_owner.is_zero(), 'New owner is the zero address');
self.assert_only_owner();
self.set_owner(new_owner);
}
}
#[generate_trait]
pub impl OwnableInternalImpl<TContractState, +HasComponent<TContractState>> of OwnableInternal<TContractState> {
fn initialize(ref self: ComponentState<TContractState>, owner: ContractAddress) {
assert(!owner.is_zero(), 'New owner is the zero address');
assert(self.get_owner().is_zero(), 'Owner already initialized');
self.set_owner(owner);
}
fn assert_only_owner(self: @ComponentState<TContractState>) {
let owner = self.get_owner();
let caller = get_caller_address();
assert(!caller.is_zero(), 'Caller is the zero address');
assert(caller == owner, 'Caller is not the owner');
}
}
#[generate_trait]
impl PrivateImpl<TContractState, +HasComponent<TContractState>> of PrivateTrait<TContractState> {
fn set_owner(ref self: ComponentState<TContractState>, new_owner: ContractAddress) {
let previous_owner = self.get_owner();
self.owner.write(new_owner);
self.emit(OwnershipTransferred { previous_owner, new_owner });
}
}
}