-
Notifications
You must be signed in to change notification settings - Fork 0
/
Owner.cairo
62 lines (53 loc) · 1.63 KB
/
Owner.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
use starknet::ContractAddress;
#[starknet::interface]
trait OwnableTrait<T> {
fn transfer_ownership(ref self: T, new_owner: ContractAddress);
fn get_owner(self: @T) -> ContractAddress;
}
#[starknet::contract]
mod Ownable {
use super::ContractAddress;
use starknet::get_caller_address;
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
OwnershipTransferred1: OwnershipTransferred1,
}
#[derive(Drop, starknet::Event)]
struct OwnershipTransferred1 {
#[key]
prev_owner: ContractAddress,
#[key]
new_owner: ContractAddress,
}
#[storage]
struct Storage {
owner: ContractAddress,
}
#[constructor]
fn constructor(ref self: ContractState, init_owner: ContractAddress) {
self.owner.write(init_owner);
}
#[external(v0)]
impl OwnableImpl of super::OwnableTrait<ContractState> {
fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) {
self.only_owner();
let prev_owner = self.owner.read();
self.owner.write(new_owner);
self.emit(Event::OwnershipTransferred1(OwnershipTransferred1 {
prev_owner: prev_owner,
new_owner: new_owner,
}));
}
fn get_owner(self: @ContractState) -> ContractAddress {
self.owner.read()
}
}
#[generate_trait]
impl PrivateMethods of PrivateMethodsTrait {
fn only_owner(self: @ContractState) {
let caller = get_caller_address();
assert(caller == self.owner.read(), 'Caller is not the owner');
}
}
}