apollo_cw_multi_test/
module.rs

1use std::marker::PhantomData;
2
3use anyhow::{bail, Result as AnyResult};
4use cosmwasm_std::{Addr, Api, Binary, BlockInfo, CustomQuery, Querier, Storage};
5
6use crate::app::CosmosRouter;
7use crate::AppResponse;
8use schemars::JsonSchema;
9use serde::de::DeserializeOwned;
10
11pub trait Module {
12    type ExecT;
13    type QueryT;
14    type SudoT;
15
16    /// execute runs any ExecT message, which can be called by any external actor
17    /// or smart contract
18    fn execute<ExecC, QueryC>(
19        &self,
20        api: &dyn Api,
21        storage: &mut dyn Storage,
22        router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
23        block: &BlockInfo,
24        sender: Addr,
25        msg: Self::ExecT,
26    ) -> AnyResult<AppResponse>
27    where
28        ExecC: std::fmt::Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
29        QueryC: CustomQuery + DeserializeOwned + 'static;
30
31    /// sudo runs privileged actions, like minting tokens, or governance proposals.
32    /// This allows modules to have full access to these privileged actions,
33    /// that cannot be triggered by smart contracts.
34    ///
35    /// There is no sender, as this must be previously authorized before the call
36    fn sudo<ExecC, QueryC>(
37        &self,
38        api: &dyn Api,
39        storage: &mut dyn Storage,
40        router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
41        block: &BlockInfo,
42        msg: Self::SudoT,
43    ) -> AnyResult<AppResponse>
44    where
45        ExecC: std::fmt::Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
46        QueryC: CustomQuery + DeserializeOwned + 'static;
47
48    fn query(
49        &self,
50        api: &dyn Api,
51        storage: &dyn Storage,
52        querier: &dyn Querier,
53        block: &BlockInfo,
54        request: Self::QueryT,
55    ) -> AnyResult<Binary>;
56}
57
58pub struct FailingModule<ExecT, QueryT, SudoT>(PhantomData<(ExecT, QueryT, SudoT)>);
59
60impl<Exec, Query, Sudo> FailingModule<Exec, Query, Sudo> {
61    pub fn new() -> Self {
62        FailingModule(PhantomData)
63    }
64}
65
66impl<Exec, Query, Sudo> Default for FailingModule<Exec, Query, Sudo> {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72impl<Exec, Query, Sudo> Module for FailingModule<Exec, Query, Sudo>
73where
74    Exec: std::fmt::Debug,
75    Query: std::fmt::Debug,
76    Sudo: std::fmt::Debug,
77{
78    type ExecT = Exec;
79    type QueryT = Query;
80    type SudoT = Sudo;
81
82    fn execute<ExecC, QueryC>(
83        &self,
84        _api: &dyn Api,
85        _storage: &mut dyn Storage,
86        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
87        _block: &BlockInfo,
88        sender: Addr,
89        msg: Self::ExecT,
90    ) -> AnyResult<AppResponse> {
91        bail!("Unexpected exec msg {:?} from {:?}", msg, sender)
92    }
93
94    fn sudo<ExecC, QueryC>(
95        &self,
96        _api: &dyn Api,
97        _storage: &mut dyn Storage,
98        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
99        _block: &BlockInfo,
100        msg: Self::SudoT,
101    ) -> AnyResult<AppResponse> {
102        bail!("Unexpected sudo msg {:?}", msg)
103    }
104
105    fn query(
106        &self,
107        _api: &dyn Api,
108        _storage: &dyn Storage,
109        _querier: &dyn Querier,
110        _block: &BlockInfo,
111        request: Self::QueryT,
112    ) -> AnyResult<Binary> {
113        bail!("Unexpected custom query {:?}", request)
114    }
115}