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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use cosmwasm_std::{Deps, DepsMut, Empty, Env, MessageInfo};
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

pub struct BoundQuerier<'a, C: cosmwasm_std::CustomQuery, Contract> {
    contract: &'a cosmwasm_std::Addr,
    querier: &'a cosmwasm_std::QuerierWrapper<'a, C>,
    _phantom: std::marker::PhantomData<Contract>,
}

impl<'a, C: cosmwasm_std::CustomQuery, Contract> BoundQuerier<'a, C, Contract> {
    pub fn querier(&self) -> &'a cosmwasm_std::QuerierWrapper<'a, C> {
        self.querier
    }

    pub fn contract(&self) -> &'a cosmwasm_std::Addr {
        self.contract
    }

    pub fn borrowed(
        contract: &'a cosmwasm_std::Addr,
        querier: &'a cosmwasm_std::QuerierWrapper<'a, C>,
    ) -> Self {
        Self {
            contract,
            querier,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery, Contract> From<&'a BoundQuerier<'a, C, Contract>>
    for BoundQuerier<'a, C, Contract>
{
    fn from(input: &'a BoundQuerier<'a, C, Contract>) -> Self {
        BoundQuerier::borrowed(input.contract, input.querier)
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Remote<'a, Contract> {
    addr: std::borrow::Cow<'a, cosmwasm_std::Addr>,
    #[serde(skip)]
    _phantom: std::marker::PhantomData<Contract>,
}

impl<'a, Contract> Remote<'a, Contract> {
    pub fn new(addr: cosmwasm_std::Addr) -> Self {
        Self {
            addr: std::borrow::Cow::Owned(addr),
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn borrowed(addr: &'a cosmwasm_std::Addr) -> Self {
        Self {
            addr: std::borrow::Cow::Borrowed(addr),
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn querier<C: cosmwasm_std::CustomQuery>(
        &'a self,
        querier: &'a cosmwasm_std::QuerierWrapper<'a, C>,
    ) -> BoundQuerier<'a, C, Contract> {
        BoundQuerier {
            contract: &self.addr,
            querier,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<'a, Contract> AsRef<cosmwasm_std::Addr> for Remote<'a, Contract> {
    fn as_ref(&self) -> &cosmwasm_std::Addr {
        &self.addr
    }
}

/// Represantation of `reply` context received in entry point as
/// (DepsMut, Env) tuple.
pub struct ReplyCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
    pub deps: DepsMut<'a, C>,
    pub env: Env,
}

/// Represantation of `reply` context received in entry point as
/// (DepsMut, Env) tuple.
pub struct MigrateCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
    pub deps: DepsMut<'a, C>,
    pub env: Env,
}

/// Represantation of `reply` context received in entry point as
/// (DepsMut, Env, MessageInfo) tuple.
pub struct ExecCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
    pub deps: DepsMut<'a, C>,
    pub env: Env,
    pub info: MessageInfo,
}

/// Represantation of `instantiate` context received in entry point as
/// (DepsMut, Env, MessageInfo) tuple.
pub struct InstantiateCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
    pub deps: DepsMut<'a, C>,
    pub env: Env,
    pub info: MessageInfo,
}

/// Represantation of `query` context received in entry point as
/// (Deps, Env) tuple.
pub struct QueryCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
    pub deps: Deps<'a, C>,
    pub env: Env,
}

/// Represantation of `sudo` context received in entry point as
/// (DepsMut, Env) tuple.
pub struct SudoCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
    pub deps: DepsMut<'a, C>,
    pub env: Env,
}

impl<C: cosmwasm_std::CustomQuery> ExecCtx<'_, C> {
    pub fn branch(&'_ mut self) -> ExecCtx<'_, C> {
        ExecCtx {
            deps: self.deps.branch(),
            env: self.env.clone(),
            info: self.info.clone(),
        }
    }
}

impl<C: cosmwasm_std::CustomQuery> InstantiateCtx<'_, C> {
    pub fn branch(&'_ mut self) -> InstantiateCtx<'_, C> {
        InstantiateCtx {
            deps: self.deps.branch(),
            env: self.env.clone(),
            info: self.info.clone(),
        }
    }
}

impl<C: cosmwasm_std::CustomQuery> SudoCtx<'_, C> {
    pub fn branch(&'_ mut self) -> SudoCtx<'_, C> {
        SudoCtx {
            deps: self.deps.branch(),
            env: self.env.clone(),
        }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for MigrateCtx<'a, C> {
    fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self {
        Self { deps, env }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for ReplyCtx<'a, C> {
    fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self {
        Self { deps, env }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env, MessageInfo)> for ExecCtx<'a, C> {
    fn from((deps, env, info): (DepsMut<'a, C>, Env, MessageInfo)) -> Self {
        Self { deps, env, info }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env, MessageInfo)>
    for InstantiateCtx<'a, C>
{
    fn from((deps, env, info): (DepsMut<'a, C>, Env, MessageInfo)) -> Self {
        Self { deps, env, info }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(Deps<'a, C>, Env)> for QueryCtx<'a, C> {
    fn from((deps, env): (Deps<'a, C>, Env)) -> Self {
        Self { deps, env }
    }
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for SudoCtx<'a, C> {
    fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self {
        Self { deps, env }
    }
}

pub trait CustomMsg: cosmwasm_std::CustomMsg + DeserializeOwned {}

impl<T> CustomMsg for T where T: cosmwasm_std::CustomMsg + DeserializeOwned {}

pub trait CustomQuery: cosmwasm_std::CustomQuery + DeserializeOwned + JsonSchema {}

impl<T> CustomQuery for T where T: cosmwasm_std::CustomQuery + DeserializeOwned + JsonSchema {}

/// Api trait for easier access to generated types and messages.
pub trait InterfaceApi {
    type Exec;
    type Query;
    type Sudo;
    type Querier<'querier, Contract>;
}

/// Api trait for easier access to generated types and messages.
pub trait ContractApi {
    type Instantiate;
    type Query;
    type Exec;
    type ContractQuery;
    type ContractExec;
    type ContractSudo;
    type Migrate;
    type Sudo;
    type Querier<'querier>;
    type Remote<'remote>;
}