cw_multi_test/
stargate.rs

1//! # Handler for `CosmosMsg::Stargate`, `CosmosMsg::Any`, `QueryRequest::Stargate` and `QueryRequest::Grpc` messages
2
3use crate::error::std_error_bail;
4use crate::{AppResponse, CosmosRouter};
5use cosmwasm_std::{
6    to_json_binary, Addr, AnyMsg, Api, Binary, BlockInfo, CustomMsg, CustomQuery, Empty, GrpcQuery,
7    Querier, StdError, StdResult, Storage,
8};
9use serde::de::DeserializeOwned;
10
11/// Interface of handlers for processing `Stargate`/`Any` message variants
12/// and `Stargate`/`Grpc` queries.
13pub trait Stargate {
14    /// Processes `CosmosMsg::Stargate` message variant.
15    fn execute_stargate<ExecC, QueryC>(
16        &self,
17        _api: &dyn Api,
18        _storage: &mut dyn Storage,
19        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
20        _block: &BlockInfo,
21        sender: Addr,
22        type_url: String,
23        value: Binary,
24    ) -> StdResult<AppResponse>
25    where
26        ExecC: CustomMsg + DeserializeOwned + 'static,
27        QueryC: CustomQuery + DeserializeOwned + 'static,
28    {
29        std_error_bail!(
30            "Unexpected stargate execute: type_url={}, value={} from {}",
31            type_url,
32            value,
33            sender
34        )
35    }
36
37    /// Processes `QueryRequest::Stargate` query.
38    fn query_stargate(
39        &self,
40        _api: &dyn Api,
41        _storage: &dyn Storage,
42        _querier: &dyn Querier,
43        _block: &BlockInfo,
44        path: String,
45        data: Binary,
46    ) -> StdResult<Binary> {
47        std_error_bail!("Unexpected stargate query: path={}, data={}", path, data)
48    }
49
50    /// Processes `CosmosMsg::Any` message variant.
51    fn execute_any<ExecC, QueryC>(
52        &self,
53        _api: &dyn Api,
54        _storage: &mut dyn Storage,
55        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
56        _block: &BlockInfo,
57        sender: Addr,
58        msg: AnyMsg,
59    ) -> StdResult<AppResponse>
60    where
61        ExecC: CustomMsg + DeserializeOwned + 'static,
62        QueryC: CustomQuery + DeserializeOwned + 'static,
63    {
64        std_error_bail!("Unexpected any execute: msg={:?} from {}", msg, sender)
65    }
66
67    /// Processes `QueryRequest::Grpc` query.
68    fn query_grpc(
69        &self,
70        _api: &dyn Api,
71        _storage: &dyn Storage,
72        _querier: &dyn Querier,
73        _block: &BlockInfo,
74        request: GrpcQuery,
75    ) -> StdResult<Binary> {
76        std_error_bail!("Unexpected grpc query: request={:?}", request)
77    }
78}
79
80/// Always failing handler for `Stargate`/`Any` message variants and `Stargate`/`Grpc` queries.
81pub struct StargateFailing;
82
83impl Stargate for StargateFailing {}
84
85/// Always accepting handler for `Stargate`/`Any` message variants and `Stargate`/`Grpc` queries.
86pub struct StargateAccepting;
87
88impl Stargate for StargateAccepting {
89    fn execute_stargate<ExecC, QueryC>(
90        &self,
91        _api: &dyn Api,
92        _storage: &mut dyn Storage,
93        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
94        _block: &BlockInfo,
95        _sender: Addr,
96        _type_url: String,
97        _value: Binary,
98    ) -> StdResult<AppResponse>
99    where
100        ExecC: CustomMsg + DeserializeOwned + 'static,
101        QueryC: CustomQuery + DeserializeOwned + 'static,
102    {
103        Ok(AppResponse::default())
104    }
105
106    fn query_stargate(
107        &self,
108        _api: &dyn Api,
109        _storage: &dyn Storage,
110        _querier: &dyn Querier,
111        _block: &BlockInfo,
112        _path: String,
113        _data: Binary,
114    ) -> StdResult<Binary> {
115        to_json_binary(&Empty {})
116    }
117
118    fn execute_any<ExecC, QueryC>(
119        &self,
120        _api: &dyn Api,
121        _storage: &mut dyn Storage,
122        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
123        _block: &BlockInfo,
124        _sender: Addr,
125        _msg: AnyMsg,
126    ) -> StdResult<AppResponse>
127    where
128        ExecC: CustomMsg + DeserializeOwned + 'static,
129        QueryC: CustomQuery + DeserializeOwned + 'static,
130    {
131        Ok(AppResponse::default())
132    }
133
134    fn query_grpc(
135        &self,
136        _api: &dyn Api,
137        _storage: &dyn Storage,
138        _querier: &dyn Querier,
139        _block: &BlockInfo,
140        _request: GrpcQuery,
141    ) -> StdResult<Binary> {
142        Ok(Binary::default())
143    }
144}