bvs_pauser/
msg.rs

1use cosmwasm_schema::{cw_serde, QueryResponses};
2
3#[cw_serde]
4pub struct InstantiateMsg {
5    /// Owner of this contract, who can pause and unpause
6    pub owner: String,
7    /// The initial paused state of this contract
8    pub initial_paused: bool,
9}
10
11#[cw_serde]
12pub enum ExecuteMsg {
13    /// ExecuteMsg Pause pauses a method on a contract.
14    /// Callable by the owner of the pauser contract
15    Pause {
16        /// address of the contract to be paused
17        contract: String,
18        /// method of a particular contract to be paused
19        method: String,
20    },
21
22    /// ExecuteMsg Unpause unpauses a method on a contract.
23    /// Callable by the owner of the pauser contract
24    Unpause {
25        /// address of the contract to be unpaused
26        contract: String,
27        /// method of a particular contract to be unpaused
28        method: String,
29    },
30
31    /// ExecuteMsg PauseGlobal pauses all execution on all contracts and methods.
32    /// Callable by the owner of the pauser contract
33    PauseGlobal {},
34
35    /// ExecuteMsg UnpauseGlobal unpauses all contracts and methods that were previously paused.
36    /// Callable by the owner of the pauser contract
37    UnpauseGlobal {},
38
39    TransferOwnership {
40        /// See [`bvs_library::ownership::transfer_ownership`] for more information on this field
41        new_owner: String,
42    },
43}
44
45#[cw_serde]
46#[derive(QueryResponses)]
47pub enum QueryMsg {
48    #[returns(IsPausedResponse)]
49    IsPaused {
50        /// The (contract: Addr) calling this
51        #[serde(rename = "c")]
52        contract: String,
53        /// The (method: ExecuteMsg) to check if it is paused
54        #[serde(rename = "m")]
55        method: String,
56    },
57
58    #[returns(CanExecuteResponse)]
59    CanExecute {
60        /// The (contract: Addr) calling this
61        #[serde(rename = "c")]
62        contract: String,
63        /// The (sender: Addr) of the message
64        #[serde(rename = "s")]
65        sender: String,
66        /// The (method: ExecuteMsg) to check if it is paused
67        #[serde(rename = "m")]
68        method: String,
69    },
70}
71
72#[cw_serde]
73pub struct IsPausedResponse(pub u32);
74
75impl IsPausedResponse {
76    pub fn new(paused: bool) -> Self {
77        Self(paused as u32)
78    }
79
80    pub fn is_paused(&self) -> bool {
81        self.0 == 1
82    }
83}
84
85#[cw_serde]
86pub struct CanExecuteResponse(pub u32);
87
88#[derive(Debug, PartialEq)]
89pub enum CanExecuteFlag {
90    CanExecute = 0,
91    Paused = 1,
92    Unauthorized = 2,
93}
94
95impl From<CanExecuteFlag> for CanExecuteResponse {
96    fn from(flag: CanExecuteFlag) -> Self {
97        Self(flag as u32)
98    }
99}
100
101impl From<CanExecuteResponse> for CanExecuteFlag {
102    fn from(value: CanExecuteResponse) -> Self {
103        match value.0 {
104            0 => Self::CanExecute,
105            1 => Self::Paused,
106            2 => Self::Unauthorized,
107            _ => panic!("Unknown flag in CanExecuteResponse"),
108        }
109    }
110}
111
112#[cw_serde]
113pub struct MigrateMsg {}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_paused() {
121        let msg = IsPausedResponse::new(true);
122        assert!(msg.is_paused());
123
124        let msg = IsPausedResponse::new(false);
125        assert!(!msg.is_paused());
126    }
127
128    /// Test the raw value of the IsPausedResponse
129    /// If != 1, it is not paused
130    #[test]
131    fn test_paused_raw() {
132        let msg = IsPausedResponse(0);
133        assert!(!msg.is_paused());
134
135        let msg = IsPausedResponse(1);
136        assert!(msg.is_paused());
137    }
138
139    #[test]
140    fn test_can_execute() {
141        let msg = CanExecuteResponse(0);
142        assert_eq!(msg.0, 0);
143        let flag: CanExecuteFlag = msg.into();
144        assert_eq!(flag, CanExecuteFlag::CanExecute);
145
146        let msg = CanExecuteResponse(1);
147        assert_eq!(msg.0, 1);
148        let flag: CanExecuteFlag = msg.into();
149        assert_eq!(flag, CanExecuteFlag::Paused);
150
151        let msg = CanExecuteResponse(2);
152        assert_eq!(msg.0, 2);
153        let flag: CanExecuteFlag = msg.into();
154        assert_eq!(flag, CanExecuteFlag::Unauthorized);
155    }
156}