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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use anyhow::{bail, Result as AnyResult};
use cosmwasm_std::{testing::MockApi, Addr, Api, Binary, BlockInfo, Coin, CustomQuery, Empty, MemoryStorage, Querier, Storage};
use cosmwasm_std::{to_binary, StdError};
use cw_multi_test::{AddressGenerator, App};
use cw_multi_test::{AppResponse, BankKeeper, BasicAppBuilder, CosmosRouter, DistributionKeeper, Module, Router, StakeKeeper, WasmKeeper};
use injective_cosmwasm::{InjectiveMsgWrapper, InjectiveQueryWrapper};

use std::{
    cell::{Ref, RefCell},
    marker::PhantomData,
    ops::Deref,
    rc::Rc,
    u8,
};

use crate::InjectiveAddressGenerator;

fn no_init<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>(
    _: &mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>,
    _: &dyn Api,
    _: &mut dyn Storage,
) {
}

pub type MockedInjectiveApp = App<
    BankKeeper,
    MockApi,
    MemoryStorage,
    CustomInjectiveHandler,
    WasmKeeper<InjectiveMsgWrapper, InjectiveQueryWrapper>,
    StakeKeeper,
    DistributionKeeper,
>;

#[derive(Clone)]
pub struct InitialBalance {
    pub amounts: Vec<Coin>,
    pub address: Addr,
}

pub struct CachingCustomHandlerState<CustomModule, Msg, Query> {
    pub execs: Rc<RefCell<Vec<Msg>>>,
    pub queries: Rc<RefCell<Vec<Query>>>,
    _p: PhantomData<CustomModule>,
}

impl<CustomModule, Msg, Query> CachingCustomHandlerState<CustomModule, Msg, Query>
where
    CustomModule: Module,
    CustomModule::ExecT: Clone + 'static,
    CustomModule::QueryT: CustomQuery + 'static,
{
    pub fn execs(&self) -> impl Deref<Target = [Msg]> + '_ {
        Ref::map(self.execs.borrow(), Vec::as_slice)
    }

    pub fn queries(&self) -> impl Deref<Target = [Query]> + '_ {
        Ref::map(self.queries.borrow(), Vec::as_slice)
    }

    pub fn reset(&self) {
        self.execs.borrow_mut().clear();
        self.queries.borrow_mut().clear();
    }
}

impl Default for CachingCustomHandlerState<CustomInjectiveHandler, InjectiveMsgWrapper, InjectiveQueryWrapper> {
    fn default() -> Self {
        Self {
            execs: Rc::new(RefCell::new(vec![])),
            queries: Rc::new(RefCell::new(vec![])),
            _p: PhantomData,
        }
    }
}

pub type ExecuteResponse = Result<Option<Binary>, anyhow::Error>;
pub type QueryResponse = Result<Binary, anyhow::Error>;

pub struct ExecuteResponseContainer {
    response: Option<ExecuteResponse>,
}

impl ExecuteResponseContainer {
    pub fn with_ok_response<T: serde::ser::Serialize + Sized>(payload: &T) -> Self {
        ExecuteResponseContainer {
            response: Some(Ok(Some(to_binary(payload).unwrap()))),
        }
    }

    pub fn with_error(error: anyhow::Error) -> Self {
        ExecuteResponseContainer { response: Some(Err(error)) }
    }

    pub fn empty() -> Self {
        ExecuteResponseContainer { response: None }
    }

    pub fn is_empty(&self) -> bool {
        self.response.is_none()
    }
}

pub struct QueryResponseContainer {
    response: Option<QueryResponse>,
}

impl QueryResponseContainer {
    pub fn with_ok_response<T: serde::ser::Serialize + Sized>(payload: &T) -> Self {
        QueryResponseContainer {
            response: Some(Ok(to_binary(payload).unwrap())),
        }
    }

    pub fn with_error(error: anyhow::Error) -> Self {
        QueryResponseContainer { response: Some(Err(error)) }
    }

    pub fn empty() -> Self {
        QueryResponseContainer { response: None }
    }

    pub fn is_empty(&self) -> bool {
        self.response.is_none()
    }
}

pub type ExecuteAssertion<Msg> = fn(message: &Msg);
pub type QueryAssertion<Query> = fn(query: &Query);

pub struct ExecuteAssertionContainer<Msg> {
    pub assertion: Option<ExecuteAssertion<Msg>>,
}

impl<Msg> ExecuteAssertionContainer<Msg> {
    pub fn new(assertion: ExecuteAssertion<Msg>) -> Self {
        ExecuteAssertionContainer { assertion: Some(assertion) }
    }

    pub fn empty() -> Self {
        ExecuteAssertionContainer { assertion: None }
    }

    pub fn is_empty(&self) -> bool {
        self.assertion.is_none()
    }
}

pub struct QueryAssertionContainer<Query> {
    pub assertion: Option<QueryAssertion<Query>>,
}

impl<Query> QueryAssertionContainer<Query> {
    pub fn new(assertion: QueryAssertion<Query>) -> Self {
        QueryAssertionContainer { assertion: Some(assertion) }
    }

    pub fn empty() -> Self {
        QueryAssertionContainer { assertion: None }
    }

    pub fn is_empty(&self) -> bool {
        self.assertion.is_none()
    }
}

pub struct CustomInjectiveHandlerAssertions<Msg, Query> {
    pub executes: Vec<ExecuteAssertionContainer<Msg>>,
    pub queries: Vec<QueryAssertionContainer<Query>>,
}

impl<InjectiveMsgWrapper, InjectiveQueryWrapper> Default for CustomInjectiveHandlerAssertions<InjectiveMsgWrapper, InjectiveQueryWrapper> {
    fn default() -> Self {
        Self {
            executes: vec![],
            queries: vec![],
        }
    }
}

#[derive(Default)]
pub struct CustomInjectiveHandlerResponses {
    pub executes: Vec<ExecuteResponseContainer>,
    pub queries: Vec<QueryResponseContainer>,
}

#[derive(Default)]
pub struct CustomInjectiveHandler {
    pub state: CachingCustomHandlerState<CustomInjectiveHandler, InjectiveMsgWrapper, InjectiveQueryWrapper>,
    pub responses: CustomInjectiveHandlerResponses,
    pub assertions: CustomInjectiveHandlerAssertions<InjectiveMsgWrapper, InjectiveQueryWrapper>,
    pub enable_debug: bool,
}

impl Module for CustomInjectiveHandler {
    type ExecT = InjectiveMsgWrapper;
    type QueryT = InjectiveQueryWrapper;
    type SudoT = Empty;

    fn execute<ExecC, QueryC>(
        &self,
        _api: &dyn Api,
        _storage: &mut dyn Storage,
        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
        _block: &BlockInfo,
        _sender: Addr,
        msg: Self::ExecT,
    ) -> AnyResult<AppResponse> {
        let mut exec_calls_count = self.state.execs.borrow().len();

        if !self.assertions.executes.is_empty()
            && exec_calls_count < self.assertions.executes.len()
            && !self.assertions.executes[exec_calls_count].is_empty()
        {
            self.assertions.executes[exec_calls_count].assertion.unwrap()(&msg);
        }

        self.state.execs.borrow_mut().push(msg.clone());
        exec_calls_count += 1;

        if self.enable_debug {
            println!("[{exec_calls_count}] Execute message: {msg:?}");
        }

        if self.responses.executes.is_empty()
            || exec_calls_count > self.responses.executes.len()
            || self.responses.executes[exec_calls_count - 1].is_empty()
        {
            return Ok(AppResponse::default());
        }

        let stored_result = self.responses.executes.get(exec_calls_count - 1).unwrap().response.as_ref().unwrap();

        // In order to implement the trait that method has to receive &self and neither Result nor Binary implements Copy
        // and that's the reason why I'm manually copying the underlying [u8] in order to return owned data
        match &stored_result {
            Ok(optional_data) => match &optional_data {
                Some(binary) => Ok(AppResponse {
                    events: vec![],
                    data: Some(copy_binary(binary)),
                }),
                &None => Ok(AppResponse::default()),
            },
            Err(e) => Err(anyhow::Error::new(StdError::generic_err(e.to_string()))),
        }
    }

    fn query(&self, _api: &dyn Api, _storage: &dyn Storage, _querier: &dyn Querier, _block: &BlockInfo, request: Self::QueryT) -> AnyResult<Binary> {
        let mut query_calls_count = self.state.queries.borrow().len();

        if !self.assertions.queries.is_empty()
            && query_calls_count < self.assertions.queries.len()
            && !self.assertions.queries[query_calls_count].is_empty()
        {
            self.assertions.queries[query_calls_count].assertion.unwrap()(&request);
        }

        self.state.queries.borrow_mut().push(request.clone());
        query_calls_count += 1;

        if self.enable_debug {
            println!("[{query_calls_count}] Query request: {request:?}");
        }

        if self.responses.queries.is_empty()
            || query_calls_count > self.responses.queries.len()
            || self.responses.queries[query_calls_count - 1].is_empty()
        {
            Ok(Binary::default())
        } else {
            let stored_result = self.responses.queries.get(query_calls_count - 1).unwrap().response.as_ref().unwrap();

            // In order to implement the trait that method has to receive &self and neither Result nor Binary implements Copy
            // and that's the reason why I'm manually copying the underlying [u8] in order to return owned data
            match &stored_result {
                Ok(optional_data) => Ok(copy_binary(optional_data)),
                Err(e) => Err(anyhow::Error::new(StdError::generic_err(e.to_string()))),
            }
        }
    }

    fn sudo<ExecC, QueryC>(
        &self,
        _api: &dyn Api,
        _storage: &mut dyn Storage,
        _router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
        _block: &BlockInfo,
        msg: Self::SudoT,
    ) -> AnyResult<AppResponse> {
        bail!("Unexpected sudo msg {:?}", msg)
    }
}

pub fn mock_custom_injective_chain_app(
    initial_balances: Vec<InitialBalance>,
    execute_responses: Vec<ExecuteResponseContainer>,
    query_responses: Vec<QueryResponseContainer>,
    execute_assertions: Vec<ExecuteAssertionContainer<InjectiveMsgWrapper>>,
    query_assertions: Vec<QueryAssertionContainer<InjectiveQueryWrapper>>,
    address_generator: Option<impl AddressGenerator + 'static>,
    enable_debug: bool,
) -> MockedInjectiveApp {
    let inj_handler = CustomInjectiveHandler {
        responses: CustomInjectiveHandlerResponses {
            executes: execute_responses,
            queries: query_responses,
        },
        assertions: CustomInjectiveHandlerAssertions {
            executes: execute_assertions,
            queries: query_assertions,
        },
        enable_debug,
        ..Default::default()
    };

    let inj_wasm_keeper = match address_generator {
        Some(generator) => WasmKeeper::<InjectiveMsgWrapper, InjectiveQueryWrapper>::new_with_custom_address_generator(generator),
        None => WasmKeeper::<InjectiveMsgWrapper, InjectiveQueryWrapper>::new_with_custom_address_generator(InjectiveAddressGenerator()),
    };

    BasicAppBuilder::new()
        .with_custom(inj_handler)
        .with_wasm::<CustomInjectiveHandler, WasmKeeper<InjectiveMsgWrapper, InjectiveQueryWrapper>>(inj_wasm_keeper)
        .build(|router, _, storage| {
            initial_balances.into_iter().for_each(|balance| {
                router
                    .bank
                    .init_balance(storage, &balance.address, balance.amounts)
                    .expect("balances added")
            })
        })
}

pub fn mock_default_injective_chain_app() -> MockedInjectiveApp {
    let inj_wasm_keeper = WasmKeeper::<InjectiveMsgWrapper, InjectiveQueryWrapper>::new_with_custom_address_generator(InjectiveAddressGenerator());

    let inj_handler = CustomInjectiveHandler::default();

    BasicAppBuilder::new()
        .with_custom(inj_handler)
        .with_wasm::<CustomInjectiveHandler, WasmKeeper<InjectiveMsgWrapper, InjectiveQueryWrapper>>(inj_wasm_keeper)
        .build(no_init)
}

fn copy_binary(binary: &Binary) -> Binary {
    let mut c: Vec<u8> = vec![0; binary.0.len()];
    c.clone_from_slice(&binary.0);
    Binary(c)
}