-
Notifications
You must be signed in to change notification settings - Fork 316
/
pausable.cairo
95 lines (83 loc) · 2.88 KB
/
pausable.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
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.11.0 (security/pausable.cairo)
/// # Pausable Component
///
/// The Pausable component allows the using contract to implement an
/// emergency stop mechanism. Only functions that call `assert_paused`
/// or `assert_not_paused` will be affected by this mechanism.
#[starknet::component]
mod PausableComponent {
use openzeppelin::security::interface::IPausable;
use starknet::ContractAddress;
use starknet::get_caller_address;
#[storage]
struct Storage {
Pausable_paused: bool
}
#[event]
#[derive(Drop, PartialEq, starknet::Event)]
enum Event {
Paused: Paused,
Unpaused: Unpaused,
}
/// Emitted when the pause is triggered by `account`.
#[derive(Drop, PartialEq, starknet::Event)]
struct Paused {
account: ContractAddress
}
/// Emitted when the pause is lifted by `account`.
#[derive(Drop, PartialEq, starknet::Event)]
struct Unpaused {
account: ContractAddress
}
mod Errors {
const PAUSED: felt252 = 'Pausable: paused';
const NOT_PAUSED: felt252 = 'Pausable: not paused';
}
#[embeddable_as(PausableImpl)]
impl Pausable<
TContractState, +HasComponent<TContractState>
> of IPausable<ComponentState<TContractState>> {
/// Returns true if the contract is paused, and false otherwise.
fn is_paused(self: @ComponentState<TContractState>) -> bool {
self.Pausable_paused.read()
}
}
#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
/// Makes a function only callable when the contract is not paused.
fn assert_not_paused(self: @ComponentState<TContractState>) {
assert(!self.Pausable_paused.read(), Errors::PAUSED);
}
/// Makes a function only callable when the contract is paused.
fn assert_paused(self: @ComponentState<TContractState>) {
assert(self.Pausable_paused.read(), Errors::NOT_PAUSED);
}
/// Triggers a stopped state.
///
/// Requirements:
///
/// - The contract is not paused.
///
/// Emits a `Paused` event.
fn _pause(ref self: ComponentState<TContractState>) {
self.assert_not_paused();
self.Pausable_paused.write(true);
self.emit(Paused { account: get_caller_address() });
}
/// Lifts the pause on the contract.
///
/// Requirements:
///
/// - The contract is paused.
///
/// Emits an `Unpaused` event.
fn _unpause(ref self: ComponentState<TContractState>) {
self.assert_paused();
self.Pausable_paused.write(false);
self.emit(Unpaused { account: get_caller_address() });
}
}
}