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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use anyhow::Result as AnyResult;
use cosmwasm_std::{
    from_binary, to_binary, Addr, Api, BankMsg, Binary, BlockInfo, Coin, CosmosMsg, CustomQuery,
    Decimal, Querier, Storage, Uint128, Uint64,
};
use cw_multi_test::{AppResponse, BankSudo, CosmosRouter, Module, SudoMsg};
use schemars::JsonSchema;
use sei_cosmwasm::{
    DenomOracleExchangeRatePair, DexPair, DexTwap, DexTwapsResponse, Epoch, EpochResponse,
    ExchangeRatesResponse, GetOrderByIdResponse, GetOrdersResponse, OracleTwap,
    OracleTwapsResponse, Order, OrderResponse, OrderSimulationResponse, OrderStatus,
    PositionDirection, SeiMsg, SeiQuery, SeiQueryWrapper, SudoMsg as SeiSudoMsg,
};
use serde::de::DeserializeOwned;
use std::{
    collections::HashMap,
    fmt::Debug,
    ops::{Add, Div, Mul, Sub},
};

pub struct SeiModule {
    epoch: Epoch,
    exchange_rates: HashMap<String, Vec<DenomOracleExchangeRatePair>>,
}

const GENESIS_EPOCH: Epoch = Epoch {
    genesis_time: String::new(),
    duration: 60,
    current_epoch: 1,
    current_epoch_start_time: String::new(),
    current_epoch_height: 1,
};

impl SeiModule {
    pub fn new() -> Self {
        SeiModule {
            epoch: GENESIS_EPOCH,
            exchange_rates: HashMap::new(),
        }
    }

    pub fn new_with_oracle_exchange_rates(rates: Vec<DenomOracleExchangeRatePair>) -> Self {
        let mut exchange_rates: HashMap<String, Vec<DenomOracleExchangeRatePair>> = HashMap::new();

        for rate in rates {
            let arr = exchange_rates
                .entry(rate.denom.clone())
                .or_insert_with(Vec::new);

            match arr.binary_search_by(|x| {
                rate.oracle_exchange_rate
                    .last_update
                    .cmp(&x.oracle_exchange_rate.last_update)
            }) {
                Ok(_) => {}
                Err(pos) => arr.insert(pos, rate.clone()),
            };
        }

        SeiModule {
            epoch: GENESIS_EPOCH,
            exchange_rates: exchange_rates,
        }
    }

    pub fn set_epoch(&self, new_epoch: Epoch) -> Self {
        SeiModule {
            epoch: new_epoch,
            exchange_rates: (&self.exchange_rates).clone(),
        }
    }
}

impl Default for SeiModule {
    fn default() -> Self {
        Self::new()
    }
}

impl Module for SeiModule {
    type ExecT = SeiMsg;
    type QueryT = SeiQueryWrapper;
    type SudoT = SeiSudoMsg;

    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>
    where
        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    {
        match msg {
            SeiMsg::PlaceOrders {
                orders,
                funds,
                contract_address,
            } => {
                return execute_place_orders_helper(
                    storage,
                    block,
                    orders,
                    funds,
                    contract_address,
                );
            }
            SeiMsg::CancelOrders {
                order_ids,
                contract_address,
            } => {
                return execute_cancel_orders_helper(storage, order_ids, contract_address);
            }
            SeiMsg::CreateDenom { subdenom } => {
                return execute_create_denom_helper(storage, sender, subdenom);
            }
            SeiMsg::MintTokens { amount } => {
                return execute_mint_tokens_helper(api, storage, router, block, sender, amount);
            }
            SeiMsg::BurnTokens { amount } => {
                return execute_burn_tokens_helper(api, storage, router, block, sender, amount);
            }
            _ => panic!("Unexpected custom exec msg"),
        }
    }

    fn query(
        &self,
        _api: &dyn Api,
        storage: &dyn Storage,
        _querier: &dyn Querier,
        block: &BlockInfo,
        request: Self::QueryT,
    ) -> AnyResult<Binary> {
        match request.query_data {
            SeiQuery::ExchangeRates {} => {
                Ok(to_binary(&get_exchange_rates(self.exchange_rates.clone()))?)
            }
            SeiQuery::OracleTwaps { lookback_seconds } => Ok(to_binary(&get_oracle_twaps(
                block,
                self.exchange_rates.clone(),
                lookback_seconds,
            ))?),
            SeiQuery::DexTwaps {
                contract_address,
                lookback_seconds,
            } => Ok(to_binary(&get_dex_twaps(
                storage,
                block,
                contract_address,
                lookback_seconds,
            ))?),
            SeiQuery::OrderSimulation {
                order,
                contract_address,
            } => Ok(to_binary(&get_order_simulation(
                storage,
                order,
                contract_address,
            ))?),
            SeiQuery::Epoch {} => return query_get_epoch_helper(self.epoch.clone()),
            SeiQuery::GetOrders {
                contract_address,
                account,
            } => {
                return query_get_orders_helper(storage, contract_address, account);
            }
            // TODO: Implement Set and Get latest price in integration tests
            SeiQuery::GetLatestPrice { .. } => {
                panic!("Get Latest Price Query not implemented")
            }
            SeiQuery::GetOrderById {
                contract_address,
                price_denom,
                asset_denom,
                id,
            } => {
                return query_get_order_by_id_helper(
                    storage,
                    contract_address,
                    price_denom,
                    asset_denom,
                    id,
                );
            }
        }
    }

    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>
    where
        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    {
        match msg {
            SeiSudoMsg::Settlement {
                epoch: _,
                entries: _,
            } => Ok(AppResponse {
                events: vec![],
                data: None,
            }),
            SeiSudoMsg::NewBlock { epoch } => {
                let new_epoch = Epoch {
                    genesis_time: self.epoch.clone().genesis_time,
                    duration: self.epoch.duration,
                    current_epoch: epoch as u64,
                    current_epoch_start_time: self.epoch.clone().current_epoch_start_time,
                    current_epoch_height: self.epoch.current_epoch_height + 1,
                };
                self.set_epoch(new_epoch);
                return Ok(AppResponse {
                    events: vec![],
                    data: None,
                });
            }
            SeiSudoMsg::BulkOrderPlacements {
                orders: _,
                deposits: _,
            } => Ok(AppResponse {
                events: vec![],
                data: None,
            }),
            SeiSudoMsg::BulkOrderCancellations { ids: _ } => Ok(AppResponse {
                events: vec![],
                data: None,
            }),
            SeiSudoMsg::Liquidation { requests: _ } => Ok(AppResponse {
                events: vec![],
                data: None,
            }),
            SeiSudoMsg::FinalizeBlock {
                contract_order_results: _,
            } => Ok(AppResponse {
                events: vec![],
                data: None,
            }),
        }
    }
}

// Helper functions

// Dex Module Msg

// Execute: PlaceOrders()
fn execute_place_orders_helper(
    storage: &mut dyn Storage,
    block: &BlockInfo,
    orders: Vec<Order>,
    _funds: Vec<Coin>,
    contract_address: Addr,
) -> AnyResult<AppResponse> {
    // Storage:
    // OrderIdCounter -> OrderId
    // contract_address + "-" + OrderResponses -> OrderResponse[]
    // contract_address + "-" + OrderResponseById + "-" + Price Denom + "-" + Asset Denom + "-" + OrderId -> OrderResponse
    // "OrderTimestamp-" + OrderId -> OrderTimestamp

    // Get latest order id
    let mut latest_order_id: u64 = 0;
    let curr = storage.get("OrderIdCounter".as_bytes());
    if storage.get("OrderIdCounter".as_bytes()).is_some() {
        latest_order_id = String::from_utf8(curr.unwrap_or_default())
            .unwrap_or_default()
            .parse::<u64>()
            .unwrap();
    }

    // get existing orders
    let order_responses_key = contract_address.to_string() + "-" + "OrderResponses";

    let mut order_responses: Vec<OrderResponse> = Vec::new();
    let existing_order_responses = storage.get(order_responses_key.as_bytes());
    if existing_order_responses.is_some() {
        //let order_responses_key = contract_address.to_string() + "-" + "OrderResponses";
        let responses_json: String =
            serde_json::from_slice(&existing_order_responses.clone().unwrap()).unwrap();
        order_responses = serde_json::from_str(&responses_json).unwrap();
    }
    // Iterate through orders, make OrderResponse
    for order in orders.iter() {
        let order_response = OrderResponse {
            id: latest_order_id,
            status: OrderStatus::Placed,
            price: order.price,
            quantity: order.quantity,
            price_denom: order.price_denom.clone(),
            asset_denom: order.asset_denom.clone(),
            order_type: order.order_type,
            position_direction: order.position_direction,
            data: order.data.clone(),
        };
        order_responses.push(order_response.clone());

        // update GetOrderById() -> OrderResponse storage
        let response_json = serde_json::to_string(&order_response);
        let order_id_key = contract_address.to_string()
            + "-"
            + "OrderResponseById"
            + "-"
            + &order.price_denom.clone()
            + "-"
            + &order.asset_denom.clone()
            + "-"
            + &latest_order_id.to_string();
        storage.set(
            order_id_key.as_bytes(),
            &serde_json::to_vec(&response_json.unwrap_or_default()).unwrap(),
        );
        storage.set(
            format!("OrderTimestamp-{}", latest_order_id).as_bytes(),
            &block.time.seconds().to_be_bytes(),
        );

        latest_order_id += 1;
    }

    let responses_json = serde_json::to_string(&order_responses);

    // update GetOrders() -> OrderResponse[] storage
    storage.set(
        order_responses_key.as_bytes(),
        &serde_json::to_vec(&responses_json.unwrap_or_default()).unwrap(),
    );
    // update OrderIdCounter -> latest_order_id storage
    storage.set(
        "OrderIdCounter".as_bytes(),
        latest_order_id.to_string().as_bytes(),
    );

    Ok(AppResponse {
        events: vec![],
        data: Some(to_binary(&contract_address).unwrap()),
    })
}

// Execute: CancelOrders()
fn execute_cancel_orders_helper(
    storage: &mut dyn Storage,
    order_ids: Vec<u64>,
    contract_address: Addr,
) -> AnyResult<AppResponse> {
    // get existing orders
    let order_responses_key = contract_address.to_string() + "-" + "OrderResponses";

    let existing_order_responses = storage.get(order_responses_key.as_bytes());
    if !existing_order_responses.is_some() {
        return Err(anyhow::anyhow!(
            "CancelOrders: orders for contract_address do not exist"
        ));
    }

    let responses_json: String =
        serde_json::from_slice(&existing_order_responses.clone().unwrap()).unwrap();
    let mut order_responses: Vec<OrderResponse> = serde_json::from_str(&responses_json).unwrap();

    for order_id in &order_ids.clone() {
        let order_response: Vec<OrderResponse> = order_responses
            .clone()
            .into_iter()
            .filter(|o| order_id.clone() == o.id)
            .collect();
        let order_id_key = contract_address.to_string()
            + "-"
            + "OrderResponseById"
            + "-"
            + &order_response[0].price_denom.clone()
            + "-"
            + &order_response[0].asset_denom.clone()
            + "-"
            + &order_id.to_string();
        // Remove individual for GetOrderById()
        storage.remove(order_id_key.as_bytes());
    }

    order_responses = order_responses
        .into_iter()
        .filter(|o| !order_ids.contains(&o.id))
        .collect();

    let responses_json = serde_json::to_string(&order_responses);

    // update GetOrders() -> OrderResponse[] storage
    storage.set(
        order_responses_key.as_bytes(),
        &serde_json::to_vec(&responses_json.unwrap_or_default()).unwrap(),
    );

    Ok(AppResponse {
        events: vec![],
        data: Some(to_binary(&contract_address).unwrap()),
    })
}

// Oracle Module

fn get_exchange_rates(
    rates: HashMap<String, Vec<DenomOracleExchangeRatePair>>,
) -> ExchangeRatesResponse {
    let mut exchange_rates: Vec<DenomOracleExchangeRatePair> = Vec::new();

    for key in rates.keys() {
        let rate = rates.get(key).unwrap();
        exchange_rates.push(rate[0].clone());
    }

    ExchangeRatesResponse {
        denom_oracle_exchange_rate_pairs: exchange_rates,
    }
}

fn get_oracle_twaps(
    block: &BlockInfo,
    rates: HashMap<String, Vec<DenomOracleExchangeRatePair>>,
    lookback_seconds: i64,
) -> OracleTwapsResponse {
    let mut oracle_twaps: Vec<OracleTwap> = Vec::new();
    let lbs = lookback_seconds as u64;

    for key in rates.keys() {
        let pair_rates = rates.get(key).unwrap();
        let mut sum = Decimal::zero();
        let start: u64 = block.time.seconds();
        let mut time: u64 = block.time.seconds();
        let mut last_rate = Decimal::zero();

        if pair_rates[0].oracle_exchange_rate.last_update < Uint64::new(start - lbs) {
            oracle_twaps.push(OracleTwap {
                denom: key.clone(),
                twap: pair_rates[0].oracle_exchange_rate.exchange_rate,
                lookback_seconds: lookback_seconds,
            });
            continue;
        }

        // Average prices of rates for the past lookback_seconds
        for rate in pair_rates {
            last_rate = rate.oracle_exchange_rate.exchange_rate;
            if Uint64::new(start) - rate.oracle_exchange_rate.last_update < Uint64::new(lbs) {
                sum += last_rate.mul(Decimal::from_ratio(
                    Uint128::new((time - rate.oracle_exchange_rate.last_update.u64()).into()),
                    Uint128::one(),
                ));
                time = rate.oracle_exchange_rate.last_update.u64();
            } else {
                break;
            }
        }

        if Uint64::new(start - time) < Uint64::new(lbs) {
            let sec: u64 = lbs;
            let diff = sec.sub(start - time);
            sum += last_rate.mul(Decimal::from_ratio(
                Uint128::new(diff.into()),
                Uint128::one(),
            ));
        }

        oracle_twaps.push(OracleTwap {
            denom: key.clone(),
            twap: sum.div(Decimal::from_ratio(
                Uint128::new(lbs.into()),
                Uint128::one(),
            )),
            lookback_seconds: lookback_seconds,
        });
    }

    OracleTwapsResponse {
        oracle_twaps: oracle_twaps,
    }
}

fn get_dex_twaps(
    storage: &dyn Storage,
    block: &BlockInfo,
    contract_address: Addr,
    lookback_seconds: u64,
) -> DexTwapsResponse {
    let mut dex_twaps: HashMap<(String, String), Decimal> = HashMap::new();
    let mut prev_time = block.time.seconds();

    let order_response: GetOrdersResponse = from_binary(
        &query_get_orders_helper(storage, contract_address, Addr::unchecked("")).unwrap(),
    )
    .unwrap();

    let mut orders = order_response.orders.clone();
    orders.sort_by(|a, b| b.id.cmp(&a.id));

    for order in orders {
        let timestamp = u64::from_be_bytes(
            storage
                .get(format!("OrderTimestamp-{}", order.id).as_bytes())
                .unwrap()
                .try_into()
                .unwrap(),
        );

        let mut update_fn = |time: u64| {
            if !dex_twaps.contains_key(&(order.asset_denom.clone(), order.price_denom.clone())) {
                dex_twaps.insert(
                    (order.asset_denom.clone(), order.price_denom.clone()),
                    Decimal::zero(),
                );
            }

            let sum = dex_twaps
                .get(&(order.asset_denom.clone(), order.price_denom.clone()))
                .unwrap();

            let new_sum = sum.add(order.price.mul(Decimal::from_ratio(time, 1u64)));

            dex_twaps.insert(
                (order.asset_denom.clone(), order.price_denom.clone()),
                new_sum,
            );
        };

        if block.time.seconds() - timestamp >= lookback_seconds {
            update_fn(lookback_seconds - (block.time.seconds() - prev_time));
            prev_time = timestamp;
        } else if block.time.seconds() - prev_time < lookback_seconds {
            update_fn(prev_time - timestamp);
            prev_time = timestamp;
        }
    }

    let mut twaps: Vec<DexTwap> = Vec::new();
    for key in dex_twaps.keys() {
        let sum = dex_twaps.get(key).unwrap();
        twaps.push(DexTwap {
            pair: DexPair {
                asset_denom: key.0.clone(),
                price_denom: key.1.clone(),
                price_tick_size: Decimal::from_ratio(1u128, 10000u128),
                quantity_tick_size: Decimal::from_ratio(1u128, 10000u128),
            },
            twap: sum.div(Decimal::from_ratio(lookback_seconds, 1u64)),
            lookback_seconds: lookback_seconds,
        });
    }

    DexTwapsResponse { twaps }
}

fn get_order_simulation(
    storage: &dyn Storage,
    order: Order,
    contract_address: Addr,
) -> OrderSimulationResponse {
    let mut executed_quantity = Decimal::zero();

    let orders: GetOrdersResponse = from_binary(
        &query_get_orders_helper(storage, contract_address, Addr::unchecked("")).unwrap(),
    )
    .unwrap();

    let valid_orders = if order.position_direction == PositionDirection::Long {
        PositionDirection::Short
    } else {
        PositionDirection::Long
    };

    for order_response in orders.orders {
        if order_response.position_direction == valid_orders {
            if (order_response.position_direction == PositionDirection::Long
                && order.price <= order_response.price)
                || (order_response.position_direction == PositionDirection::Short
                    && order.price >= order_response.price)
            {
                executed_quantity += order_response.quantity;
            }
        }
    }

    OrderSimulationResponse {
        executed_quantity: if executed_quantity > order.quantity {
            order.quantity
        } else {
            executed_quantity
        },
    }
}

// Query: GetOrders()
fn query_get_orders_helper(
    storage: &dyn Storage,
    contract_address: Addr,
    _account: Addr,
) -> AnyResult<Binary> {
    let order_responses_key = contract_address.to_string() + "-" + "OrderResponses";
    let existing_order_responses = storage.get(order_responses_key.as_bytes());
    if !existing_order_responses.is_some() {
        return Err(anyhow::anyhow!(
            "GetOrders: orders for contract_address do not exist"
        ));
    }
    let responses_json: String =
        serde_json::from_slice(&existing_order_responses.clone().unwrap()).unwrap();

    let order_responses: Vec<OrderResponse> = serde_json::from_str(&responses_json).unwrap();

    return Ok(to_binary(&GetOrdersResponse {
        orders: order_responses,
    })?);
}

// Query: GetOrderById()
fn query_get_order_by_id_helper(
    storage: &dyn Storage,
    contract_address: Addr,
    price_denom: String,
    asset_denom: String,
    id: u64,
) -> AnyResult<Binary> {
    let order_id_key = contract_address.to_string()
        + "-"
        + "OrderResponseById"
        + "-"
        + &price_denom
        + "-"
        + &asset_denom
        + "-"
        + &id.to_string();
    let existing_order_response = storage.get(order_id_key.as_bytes());

    if !existing_order_response.is_some() {
        return Err(anyhow::anyhow!("GetOrderById: order for id does not exist"));
    }

    let response_json: String =
        serde_json::from_slice(&existing_order_response.clone().unwrap()).unwrap();

    let order_response: OrderResponse = serde_json::from_str(&response_json).unwrap();

    return Ok(to_binary(&GetOrderByIdResponse {
        order: order_response,
    })?);
}

// Epoch Module Queries

fn get_epoch(epoch: Epoch) -> EpochResponse {
    EpochResponse { epoch: epoch }
}

// Query: GetEpoch()
fn query_get_epoch_helper(epoch: Epoch) -> AnyResult<Binary> {
    return Ok(to_binary(&get_epoch(epoch))?);
}

// TokenFactory Msg

// Execute: CreateDenom()
fn execute_create_denom_helper(
    storage: &mut dyn Storage,
    sender: Addr,
    subdenom: String,
) -> AnyResult<AppResponse> {
    let denom = format!("factory/{}/{}", sender, subdenom);
    if storage.get(denom.as_bytes()).is_some() {
        return Err(anyhow::anyhow!("denom already exists"));
    }
    storage.set(denom.as_bytes(), sender.to_string().as_bytes());
    Ok(AppResponse {
        events: vec![],
        data: Some(to_binary(&denom).unwrap()),
    })
}

// Execute: MintTokens()
fn execute_mint_tokens_helper<ExecC, QueryC>(
    api: &dyn Api,
    storage: &mut dyn Storage,
    router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
    block: &BlockInfo,
    sender: Addr,
    amount: Coin,
) -> AnyResult<AppResponse>
where
    ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
    QueryC: CustomQuery + DeserializeOwned + 'static,
{
    let owner = storage.get(amount.denom.as_bytes());
    if owner.is_none() || owner.unwrap() != sender.to_string().as_bytes() {
        return Err(anyhow::anyhow!(
            "Must be owner of coin factory denom to mint"
        ));
    }
    router.sudo(
        api,
        storage,
        block,
        SudoMsg::Bank(BankSudo::Mint {
            to_address: sender.to_string(),
            amount: vec![amount],
        }),
    )
}

// Execute: BurnTokens()
fn execute_burn_tokens_helper<ExecC, QueryC>(
    api: &dyn Api,
    storage: &mut dyn Storage,
    router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
    block: &BlockInfo,
    sender: Addr,
    amount: Coin,
) -> AnyResult<AppResponse>
where
    ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
    QueryC: CustomQuery + DeserializeOwned + 'static,
{
    let owner = storage.get(amount.denom.as_bytes());
    if owner.is_none() || owner.unwrap() != sender.to_string().as_bytes() {
        return Err(anyhow::anyhow!(
            "Must be owner of coin factory denom to burn"
        ));
    }
    Ok(router
        .execute(
            api,
            storage,
            block,
            sender,
            CosmosMsg::Bank(BankMsg::Burn {
                amount: vec![amount],
            }),
        )
        .unwrap())
}