sg_multi_test/
multi.rs

1use anyhow::{bail, Result as AnyResult};
2use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
3use cosmwasm_std::{
4    Addr, Api, Binary, BlockInfo, CustomQuery, Empty, Querier, QuerierResult, Storage,
5};
6use cosmwasm_std::{BankMsg, OwnedDeps};
7use cw_multi_test::{
8    App, AppResponse, BankKeeper, BasicAppBuilder, CosmosRouter, Module, WasmKeeper,
9};
10use schemars::JsonSchema;
11use serde::de::DeserializeOwned;
12use sg_std::{StargazeMsgWrapper, StargazeQuery};
13use std::fmt::Debug;
14use std::marker::PhantomData;
15use std::ops::{Deref, DerefMut};
16
17pub struct StargazeModule {}
18
19pub type StargazeDeps = OwnedDeps<MockStorage, MockApi, MockQuerier, StargazeQuery>;
20
21pub fn mock_deps() -> StargazeDeps {
22    OwnedDeps {
23        storage: MockStorage::default(),
24        api: MockApi::default(),
25        querier: MockQuerier::default(),
26        custom_query_type: PhantomData,
27    }
28}
29
30impl StargazeModule {}
31
32impl Module for StargazeModule {
33    type ExecT = StargazeMsgWrapper;
34    type QueryT = Empty;
35    type SudoT = Empty;
36
37    fn execute<ExecC, QueryC>(
38        &self,
39        api: &dyn Api,
40        storage: &mut dyn Storage,
41        router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
42        block: &BlockInfo,
43        sender: Addr,
44        msg: StargazeMsgWrapper,
45    ) -> AnyResult<AppResponse>
46    where
47        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
48        QueryC: CustomQuery + DeserializeOwned + 'static,
49    {
50        let StargazeMsgWrapper {
51            route: _,
52            msg_data,
53            version: _,
54        } = msg;
55
56        match msg_data {
57            sg_std::StargazeMsg::FundCommunityPool { amount } => {
58                let msg = BankMsg::Send {
59                    to_address: "community_pool".to_owned(),
60                    amount,
61                }
62                .into();
63                router.execute(api, storage, block, sender, msg)?;
64                Ok(AppResponse::default())
65            }
66            sg_std::StargazeMsg::FundFairburnPool { amount } => {
67                let msg = BankMsg::Send {
68                    to_address: "fairburn_pool".to_owned(),
69                    amount,
70                }
71                .into();
72                router.execute(api, storage, block, sender, msg)?;
73                Ok(AppResponse::default())
74            }
75            sg_std::StargazeMsg::ClaimFor {
76                action: _,
77                address: _,
78            } => Ok(AppResponse::default()),
79        }
80    }
81    fn sudo<ExecC, QueryC>(
82        &self,
83        _api: &dyn Api,
84        _storage: &mut dyn Storage,
85        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
86        _block: &BlockInfo,
87        _msg: Self::SudoT,
88    ) -> AnyResult<AppResponse>
89    where
90        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
91        QueryC: CustomQuery + DeserializeOwned + 'static,
92    {
93        bail!("sudo not implemented")
94    }
95
96    fn query(
97        &self,
98        _api: &dyn Api,
99        _storage: &dyn Storage,
100        _querier: &dyn Querier,
101        _block: &BlockInfo,
102        request: Empty,
103    ) -> anyhow::Result<Binary> {
104        bail!("custom query not implemented {:?}", request)
105    }
106}
107
108pub type StargazeBasicApp =
109    App<BankKeeper, MockApi, MockStorage, StargazeModule, WasmKeeper<StargazeMsgWrapper, Empty>>;
110
111pub struct StargazeApp(StargazeBasicApp);
112
113impl Deref for StargazeApp {
114    type Target = StargazeBasicApp;
115
116    fn deref(&self) -> &Self::Target {
117        &self.0
118    }
119}
120
121impl DerefMut for StargazeApp {
122    fn deref_mut(&mut self) -> &mut Self::Target {
123        &mut self.0
124    }
125}
126
127impl Querier for StargazeApp {
128    fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
129        self.0.raw_query(bin_request)
130    }
131}
132
133impl StargazeApp {
134    pub fn new() -> Self {
135        Self(
136            BasicAppBuilder::<StargazeMsgWrapper, Empty>::new_custom()
137                .with_custom(StargazeModule {})
138                .build(|_, _, _| {}),
139        )
140    }
141}
142
143impl Default for StargazeApp {
144    fn default() -> Self {
145        Self::new()
146    }
147}