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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use anyhow::anyhow;
use cosmwasm_std::{
    CustomMsg, CustomQuery, DepsMut, Empty, Env, Ibc3ChannelOpenResponse, IbcBasicResponse,
    IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcPacketAckMsg,
    IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, Never,
};
use cw_multi_test::{Contract, ContractWrapper};
use serde::de::DeserializeOwned;
use std::fmt::{Debug, Display};

use crate::error::AppResult;

use self::closures::{
    IbcChannelCloseClosure, IbcChannelCloseFn, IbcChannelConnectClosure, IbcChannelConnectFn,
    IbcChannelOpenClosure, IbcChannelOpenFn, IbcPacketAckClosure, IbcPacketAckFn,
    IbcPacketReceiveClosure, IbcPacketReceiveFn, IbcPacketTimeoutClosure, IbcPacketTimeoutFn,
};

use cw_multi_test::error::AnyError;

#[rustfmt::skip]
mod closures {
    use cosmwasm_std::{Ibc3ChannelOpenResponse, IbcReceiveResponse};
    use super::*;

    pub type IbcChannelOpenFn<E, Q>       = fn(DepsMut<Q>, Env, IbcChannelOpenMsg)    -> Result<Option<Ibc3ChannelOpenResponse>, E>;
    pub type IbcChannelCloseFn<E, Q, C>   = fn(DepsMut<Q>, Env, IbcChannelCloseMsg)   -> Result<IbcBasicResponse<C>, E>;
    pub type IbcChannelConnectFn<E, Q, C> = fn(DepsMut<Q>, Env, IbcChannelConnectMsg) -> Result<IbcBasicResponse<C>, E>;
    pub type IbcPacketReceiveFn<Q, C>     = fn(DepsMut<Q>, Env, IbcPacketReceiveMsg)  -> Result<IbcReceiveResponse<C>, Never>;
    pub type IbcPacketAckFn<E, Q, C>      = fn(DepsMut<Q>, Env, IbcPacketAckMsg)      -> Result<IbcBasicResponse<C>, E>;
    pub type IbcPacketTimeoutFn<E, Q, C>  = fn(DepsMut<Q>, Env, IbcPacketTimeoutMsg)  -> Result<IbcBasicResponse<C>, E>;
    
    pub type IbcChannelOpenClosure<E, Q>       = Box<dyn Fn(DepsMut<Q>, Env, IbcChannelOpenMsg)    -> Result<Option<Ibc3ChannelOpenResponse>, E>>;
    pub type IbcChannelCloseClosure<E, Q, C>   = Box<dyn Fn(DepsMut<Q>, Env, IbcChannelCloseMsg)   -> Result<IbcBasicResponse<C>, E>>;
    pub type IbcChannelConnectClosure<E, Q, C> = Box<dyn Fn(DepsMut<Q>, Env, IbcChannelConnectMsg) -> Result<IbcBasicResponse<C>, E>>;
    pub type IbcPacketReceiveClosure<Q, C>     = Box<dyn Fn(DepsMut<Q>, Env, IbcPacketReceiveMsg)  -> Result<IbcReceiveResponse<C>, Never>>;
    pub type IbcPacketAckClosure<E, Q, C>      = Box<dyn Fn(DepsMut<Q>, Env, IbcPacketAckMsg)      -> Result<IbcBasicResponse<C>, E>>;
    pub type IbcPacketTimeoutClosure<E, Q, C>  = Box<dyn Fn(DepsMut<Q>, Env, IbcPacketTimeoutMsg)  -> Result<IbcBasicResponse<C>, E>>;
}

/// Structure containing the various `ibc closures`
pub struct IbcClosures<E1, E2, E3, E4, E5, C, Q = Empty>
where
    Q: CustomQuery,
    C: CustomMsg,
{
    /// `#[entry_point]` `ibc_channel_open` closure
    pub fn_ibc_channel_open: IbcChannelOpenClosure<E1, Q>,
    /// `#[entry_point]` `ibc_channel_close` closure
    pub fn_ibc_channel_close: IbcChannelCloseClosure<E2, Q, C>,
    /// `#[entry_point]` `ibc_channel_connect` closure
    pub fn_ibc_channel_connect: IbcChannelConnectClosure<E3, Q, C>,
    /// `#[entry_point]` `ibc_packet_receive` closure
    pub fn_ibc_packet_receive: IbcPacketReceiveClosure<Q, C>,
    /// `#[entry_point]` `ibc_packet_ack` closure
    pub fn_ibc_packet_ack: IbcPacketAckClosure<E4, Q, C>,
    /// `#[entry_point]` `ibc_packet_timeout` closure
    pub fn_ibc_packet_timeout: IbcPacketTimeoutClosure<E5, Q, C>,
}

impl<E1, E2, E3, E4, E5, C, Q> IbcClosures<E1, E2, E3, E4, E5, C, Q>
where
    Q: CustomQuery + 'static,
    C: CustomMsg + 'static,
    E1: Display + Debug + Send + Sync + 'static,
    E2: Display + Debug + Send + Sync + 'static,
    E3: Display + Debug + Send + Sync + 'static,
    E4: Display + Debug + Send + Sync + 'static,
    E5: Display + Debug + Send + Sync + 'static,
{
    /// Constructor function
    pub fn new(
        fn_ibc_channel_open: IbcChannelOpenFn<E1, Q>,
        fn_ibc_channel_close: IbcChannelCloseFn<E2, Q, C>,
        fn_ibc_channel_connect: IbcChannelConnectFn<E3, Q, C>,
        fn_ibc_packet_receive: IbcPacketReceiveFn<Q, C>,
        fn_ibc_packet_ack: IbcPacketAckFn<E4, Q, C>,
        fn_ibc_packet_timeout: IbcPacketTimeoutFn<E5, Q, C>,
    ) -> IbcClosures<E1, E2, E3, E4, E5, C, Q> {
        IbcClosures {
            fn_ibc_channel_open: Box::new(fn_ibc_channel_open),
            fn_ibc_channel_close: Box::new(fn_ibc_channel_close),
            fn_ibc_channel_connect: Box::new(fn_ibc_channel_connect),
            fn_ibc_packet_receive: Box::new(fn_ibc_packet_receive),
            fn_ibc_packet_ack: Box::new(fn_ibc_packet_ack),
            fn_ibc_packet_timeout: Box::new(fn_ibc_packet_timeout),
        }
    }

    /// Create a new [`IbcClosures`] as [`IbcContract`]
    pub fn new_as_ibc_contract(
        fn_ibc_channel_open: IbcChannelOpenFn<E1, Q>,
        fn_ibc_channel_close: IbcChannelCloseFn<E2, Q, C>,
        fn_ibc_channel_connect: IbcChannelConnectFn<E3, Q, C>,
        fn_ibc_packet_receive: IbcPacketReceiveFn<Q, C>,
        fn_ibc_packet_ack: IbcPacketAckFn<E4, Q, C>,
        fn_ibc_packet_timeout: IbcPacketTimeoutFn<E5, Q, C>,
    ) -> Box<dyn IbcContract<C, Q>> {
        Box::new(IbcClosures {
            fn_ibc_channel_open: Box::new(fn_ibc_channel_open),
            fn_ibc_channel_close: Box::new(fn_ibc_channel_close),
            fn_ibc_channel_connect: Box::new(fn_ibc_channel_connect),
            fn_ibc_packet_receive: Box::new(fn_ibc_packet_receive),
            fn_ibc_packet_ack: Box::new(fn_ibc_packet_ack),
            fn_ibc_packet_timeout: Box::new(fn_ibc_packet_timeout),
        }) as Box<dyn IbcContract<C, Q>>
    }
}

/// Wrapper struct to store both default [`Contract`] trait and optional [`IbcContract`] trait
pub struct IperContract<C, Q = Empty>
where
    C: CustomMsg,
    Q: CustomQuery,
{
    /// default [`Contract`] interface
    pub base: Box<dyn Contract<C, Q>>,
    /// Optional [`IbcContract`] interface. Used only on `contracts` that implements `ibc entry_points`
    pub ibc: Option<Box<dyn IbcContract<C, Q>>>,
}

impl<C, Q> IperContract<C, Q>
where
    C: CustomMsg,
    Q: CustomQuery,
{
    /// Constructor function
    pub fn new(
        base: Box<dyn Contract<C, Q>>,
        ibc: Option<Box<dyn IbcContract<C, Q>>>,
    ) -> IperContract<C, Q> {
        Self { base, ibc }
    }
}

/// Similar to [`Contract`], this trait serves as a primary interface for interacting `ibc entry_points` functions.
pub trait IbcContract<C, Q = Empty>
where
    C: CustomMsg,
    Q: CustomQuery,
{
    /// Evaluates contract's `ibc_channel_open` `entry_point`.
    fn ibc_channel_open(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcChannelOpenMsg,
    ) -> AppResult<Option<Ibc3ChannelOpenResponse>>;

    /// Evaluates contract's `ibc_channel_close` `entry_point`.
    fn ibc_channel_close(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcChannelCloseMsg,
    ) -> AppResult<IbcBasicResponse<C>>;

    /// Evaluates contract's `ibc_channel_connect` `entry_point`.
    fn ibc_channel_connect(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcChannelConnectMsg,
    ) -> AppResult<IbcBasicResponse<C>>;

    /// Evaluates contract's `ibc_packet_receive` `entry_point`.
    fn ibc_packet_receive(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcPacketReceiveMsg,
    ) -> AppResult<IbcReceiveResponse<C>>;

    /// Evaluates contract's `ibc_packet_ack` `entry_point`.
    fn ibc_packet_ack(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcPacketAckMsg,
    ) -> AppResult<IbcBasicResponse<C>>;

    /// Evaluates contract's `ibc_packet_timeout` `entry_point`.
    fn ibc_packet_timeout(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcPacketTimeoutMsg,
    ) -> AppResult<IbcBasicResponse<C>>;
}

impl<E1, E2, E3, E4, E5, C: CustomMsg, Q: CustomQuery> IbcContract<C, Q>
    for IbcClosures<E1, E2, E3, E4, E5, C, Q>
where
    E1: Display + Debug + Send + Sync + 'static, // Type of error returned from `execute` entry-point.
    E2: Display + Debug + Send + Sync + 'static, // Type of error returned from `instantiate` entry-point.
    E3: Display + Debug + Send + Sync + 'static, // Type of error returned from `query` entry-point.
    E4: Display + Debug + Send + Sync + 'static, // Type of error returned from `sudo` entry-point.
    E5: Display + Debug + Send + Sync + 'static, // Type of error returned from `reply` entry-point.
{
    fn ibc_channel_open(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcChannelOpenMsg,
    ) -> AppResult<Option<Ibc3ChannelOpenResponse>> {
        (self.fn_ibc_channel_open)(deps, env, msg).map_err(|err| anyhow!(err))
    }

    fn ibc_channel_close(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcChannelCloseMsg,
    ) -> AppResult<IbcBasicResponse<C>> {
        (self.fn_ibc_channel_close)(deps, env, msg).map_err(|err| anyhow!(err))
    }

    fn ibc_channel_connect(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcChannelConnectMsg,
    ) -> AppResult<IbcBasicResponse<C>> {
        (self.fn_ibc_channel_connect)(deps, env, msg).map_err(|err| anyhow!(err))
    }

    fn ibc_packet_receive(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcPacketReceiveMsg,
    ) -> AppResult<IbcReceiveResponse<C>> {
        (self.fn_ibc_packet_receive)(deps, env, msg).map_err(|err| anyhow!(err))
    }

    fn ibc_packet_ack(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcPacketAckMsg,
    ) -> AppResult<IbcBasicResponse<C>> {
        (self.fn_ibc_packet_ack)(deps, env, msg).map_err(|err| anyhow!(err))
    }

    fn ibc_packet_timeout(
        &self,
        deps: DepsMut<Q>,
        env: Env,
        msg: IbcPacketTimeoutMsg,
    ) -> AppResult<IbcBasicResponse<C>> {
        (self.fn_ibc_packet_timeout)(deps, env, msg).map_err(|err| anyhow!(err))
    }
}

/// Extension of [`ContractWrapper`], allowing to transform it into [`Contract`] directly
pub trait ContractWrapperExt<
    T1,
    T2,
    T3,
    E1,
    E2,
    E3,
    C = Empty,
    Q = Empty,
    T4 = Empty,
    E4 = AnyError,
    E5 = AnyError,
    T6 = Empty,
    E6 = AnyError,
> where
    T1: DeserializeOwned, // Type of message passed to `execute` entry-point.
    T2: DeserializeOwned, // Type of message passed to `instantiate` entry-point.
    T3: DeserializeOwned, // Type of message passed to `query` entry-point.
    T4: DeserializeOwned, // Type of message passed to `sudo` entry-point.
    T6: DeserializeOwned, // Type of message passed to `migrate` entry-point.
    E1: Display + Debug + Send + Sync, // Type of error returned from `execute` entry-point.
    E2: Display + Debug + Send + Sync, // Type of error returned from `instantiate` entry-point.
    E3: Display + Debug + Send + Sync, // Type of error returned from `query` entry-point.
    E4: Display + Debug + Send + Sync, // Type of error returned from `sudo` entry-point.
    E5: Display + Debug + Send + Sync, // Type of error returned from `reply` entry-point.
    E6: Display + Debug + Send + Sync, // Type of error returned from `migrate` entry-point.
    C: CustomMsg,         // Type of custom message returned from all entry-points except `query`.
    Q: CustomQuery + DeserializeOwned,
{
    /// Transform [`ContractWrapper`] into [`Contract`]
    fn to_contract(self) -> Box<dyn Contract<C, Q>>;
}

impl<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6, E6>
    ContractWrapperExt<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6, E6>
    for ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6, E6>
where
    T1: DeserializeOwned + 'static, // Type of message passed to `execute` entry-point.
    T2: DeserializeOwned + 'static, // Type of message passed to `instantiate` entry-point.
    T3: DeserializeOwned + 'static, // Type of message passed to `query` entry-point.
    T4: DeserializeOwned + 'static, // Type of message passed to `sudo` entry-point.
    T6: DeserializeOwned + 'static, // Type of message passed to `migrate` entry-point.
    E1: Display + Debug + Send + Sync + 'static, // Type of error returned from `execute` entry-point.
    E2: Display + Debug + Send + Sync + 'static, // Type of error returned from `instantiate` entry-point.
    E3: Display + Debug + Send + Sync + 'static, // Type of error returned from `query` entry-point.
    E4: Display + Debug + Send + Sync + 'static, // Type of error returned from `sudo` entry-point.
    E5: Display + Debug + Send + Sync + 'static, // Type of error returned from `reply` entry-point.
    E6: Display + Debug + Send + Sync + 'static, // Type of error returned from `migrate` entry-point.
    C: CustomMsg + 'static, // Type of custom message returned from all entry-points except `query`.
    Q: CustomQuery + DeserializeOwned + 'static, // Type of custom query in querier passed as deps/deps_mut to all entry-points.
{
    fn to_contract(self) -> Box<dyn Contract<C, Q>> {
        Box::new(self) as Box<dyn Contract<C, Q>>
    }
}