1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::error::AnyResult;
use crate::{AppResponse, CosmosRouter};
use anyhow::bail;
use cosmwasm_std::{Addr, Api, Binary, BlockInfo, CustomQuery, Querier, Storage};
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use std::fmt::Debug;

/// Stargate interface.
///
/// This trait provides the default behaviour for all functions
/// that is equal to [StargateFailing] implementation.
pub trait Stargate {
    /// Processes stargate messages.
    ///
    /// The `CosmosMsg::Stargate` message is unwrapped before processing.
    /// The `type_url` and `value` attributes of `CosmosMsg::Stargate`
    /// are passed directly to this handler.
    fn execute<ExecC, QueryC>(
        &self,
        api: &dyn Api,
        storage: &mut dyn Storage,
        router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
        block: &BlockInfo,
        sender: Addr,
        type_url: String,
        value: Binary,
    ) -> AnyResult<AppResponse>
    where
        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    {
        let _ = (api, storage, router, block);
        bail!(
            "Unexpected stargate message: (type_ur = {}, value = {:?}) from {:?}",
            type_url,
            value,
            sender
        )
    }

    /// Processes stargate queries.
    ///
    /// The `QueryRequest::Stargate` query request is unwrapped before processing.
    /// The `path` and `data` attributes of `QueryRequest::Stargate` are passed
    /// directly to this handler.
    fn query(
        &self,
        api: &dyn Api,
        storage: &dyn Storage,
        querier: &dyn Querier,
        block: &BlockInfo,
        path: String,
        data: Binary,
    ) -> AnyResult<Binary> {
        let _ = (api, storage, querier, block);
        bail!(
            "Unexpected stargate query: path = {:?}, data = {:?}",
            path,
            data
        )
    }
}

/// Always failing stargate mock implementation.
pub struct StargateFailing;

impl Stargate for StargateFailing {}

/// Always accepting stargate mock implementation.
pub struct StargateAccepting;

impl Stargate for StargateAccepting {
    /// Accepts all stargate messages. Returns default `AppResponse`.
    fn execute<ExecC, QueryC>(
        &self,
        api: &dyn Api,
        storage: &mut dyn Storage,
        router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
        block: &BlockInfo,
        sender: Addr,
        type_url: String,
        value: Binary,
    ) -> AnyResult<AppResponse>
    where
        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    {
        let _ = (api, storage, router, block, sender, type_url, value);
        Ok(AppResponse::default())
    }

    /// Accepts all stargate queries. Returns default `Binary`.
    fn query(
        &self,
        api: &dyn Api,
        storage: &dyn Storage,
        querier: &dyn Querier,
        block: &BlockInfo,
        path: String,
        data: Binary,
    ) -> AnyResult<Binary> {
        let _ = (api, storage, querier, block, path, data);
        Ok(Binary::default())
    }
}