multiversx_sc_modules/
pause.rs

1multiversx_sc::imports!();
2
3/// Standard smart contract module that, when added to a smart contract, offers pausability.
4///
5/// It provides a flag that contracts can use to check if owner decided to pause the entire contract.
6/// Use the features module for more granular on/off switches.
7///
8/// It offers:
9/// * an endpoint where the owner can pause/unpause contract
10/// * a method to check if contract is paused or not
11///
12#[multiversx_sc::module]
13pub trait PauseModule {
14    #[inline]
15    fn is_paused(&self) -> bool {
16        self.paused_status().get()
17    }
18
19    #[inline]
20    fn not_paused(&self) -> bool {
21        !self.is_paused()
22    }
23
24    #[inline]
25    fn set_paused(&self, paused: bool) {
26        self.paused_status().set(paused);
27    }
28
29    #[only_owner]
30    #[endpoint(pause)]
31    fn pause_endpoint(&self) {
32        self.set_paused(true);
33        self.pause_event();
34    }
35
36    #[only_owner]
37    #[endpoint(unpause)]
38    fn unpause_endpoint(&self) {
39        self.set_paused(false);
40        self.unpause_event();
41    }
42
43    fn require_paused(&self) {
44        require!(self.is_paused(), "Contract is not paused");
45    }
46
47    fn require_not_paused(&self) {
48        require!(self.not_paused(), "Contract is paused");
49    }
50
51    #[event("pauseContract")]
52    fn pause_event(&self);
53
54    #[event("unpauseContract")]
55    fn unpause_event(&self);
56
57    #[view(isPaused)]
58    #[storage_mapper("pause_module:paused")]
59    fn paused_status(&self) -> SingleValueMapper<bool>;
60}