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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
//! Copy trading contract

use std::{fmt::Display, num::ParseIntError, str::FromStr};

use super::market::{
    entry::{ExecuteMsg as MarketExecuteMsg, SlippageAssert, StopLoss},
    order::OrderId,
    position::PositionId,
};
use crate::{
    number::{Collateral, LpToken, NonZero},
    price::{PriceBaseInQuote, PricePoint, TakeProfitTrader},
    storage::{DirectionToBase, LeverageToBase, MarketId, RawAddr},
    time::Timestamp,
};
use anyhow::{anyhow, bail};
use cosmwasm_std::{Addr, Binary, Decimal256, StdError, StdResult, Uint128, Uint64};
use cw_storage_plus::{IntKey, Key, KeyDeserialize, Prefixer, PrimaryKey};
use thiserror::Error;

/// Message for instantiating a new copy trading contract.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct InstantiateMsg {
    /// Leader of the contract
    pub leader: RawAddr,
    /// Initial configuration values
    pub config: ConfigUpdate,
    /// Initial parameters
    pub parameters: FactoryConfigUpdate,
}

/// Full configuration
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
/// Updates to configuration values.
pub struct Config {
    /// Factory we will allow trading on.
    pub factory: Addr,
    /// Administrator of the contract. Should be the factory contract
    /// which initializes this.
    pub admin: Addr,
    /// Pending administrator, ready to be accepted, if any.
    pub pending_admin: Option<Addr>,
    /// Leader of the contract
    pub leader: Addr,
    /// Name given to this copy_trading pool
    pub name: String,
    /// Description of the copy_trading pool. Not more than 128
    /// characters.
    pub description: String,
    /// Commission rate for the leader. Must be within 1-30%.
    pub commission_rate: Decimal256,
    /// Creation time of contract
    pub created_at: Timestamp,
    /// Allowed smart queries during Rebalance operation
    pub allowed_rebalance_queries: u32,
    /// Allowed smart queries during token value computation
    pub allowed_lp_token_queries: u32,
}

impl Config {
    /// Check validity of config values
    pub fn check(&self) -> anyhow::Result<()> {
        if self.name.len() > 128 {
            Err(anyhow!(
                "Description should not be more than 128 characters"
            ))
        } else if self.commission_rate < Decimal256::from_ratio(1u32, 100u32) {
            Err(anyhow!("Commission rate less than 1 percent"))
        } else if self.commission_rate > Decimal256::from_ratio(30u32, 100u32) {
            Err(anyhow!("Commission rate greater than 30 percent"))
        } else {
            Ok(())
        }
    }

    /// Check leader
    pub fn ensure_leader(&self, sender: &Addr) -> anyhow::Result<()> {
        if self.leader != sender {
            bail!("Unautorized access, only {} allowed", self.leader)
        }
        Ok(())
    }

    /// Ensure it is factory contract
    pub fn ensure_factory(&self, sender: &Addr) -> anyhow::Result<()> {
        if self.factory != sender {
            bail!("Unautorized access, only {} allowed", self.factory)
        }
        Ok(())
    }
}

/// Updates to configuration values.
///
/// See [Config] for field meanings.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub struct ConfigUpdate {
    pub name: Option<String>,
    pub description: Option<String>,
    pub commission_rate: Option<Decimal256>,
}

/// Updates to configuration values.
///
/// See [Config] for field meanings.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub struct FactoryConfigUpdate {
    pub allowed_rebalance_queries: Option<u32>,
    pub allowed_lp_token_queries: Option<u32>,
}

/// Executions available on the copy trading contract.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    /// Cw20 interface
    Receive {
        /// Owner of funds sent to the contract
        sender: RawAddr,
        /// Amount of funds sent
        amount: Uint128,
        /// Must parse to a [ExecuteMsg]
        msg: Binary,
    },
    /// Deposit funds to the contract
    Deposit {},
    /// Withdraw funds from a given market
    Withdraw {
        /// The number of LP shares to remove
        shares: NonZero<LpToken>,
        /// Token type in which amount should be withdrawn
        token: Token,
    },
    /// Update configuration values that is allowed for leader.
    LeaderUpdateConfig(ConfigUpdate),
    /// Update configuration values that is allowed for facotr.
    FactoryUpdateConfig(FactoryConfigUpdate),
    /// Leader specific execute message for the market
    LeaderMsg {
        /// Market id that message is for
        market_id: MarketId,
        /// Message
        message: Box<MarketExecuteMsg>,
        /// Collateral to use for the action
        collateral: Option<NonZero<Collateral>>,
    },
    /// Leader withdrawal (for commission).
    LeaderWithdrawal {
        /// Fund to be withdrawn
        requested_funds: NonZero<Collateral>,
        /// Token type in which amount should be withdrawn
        token: Token,
    },
    /// Perform queue work
    DoWork {},
}

/// Queries that can be performed on the copy contract.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    /// Get the current config
    ///
    /// Returns [Config]
    Config {},
    /// Get the queue status of a particular wallet
    ///
    /// Returns [QueueResp]
    QueueStatus {
        /// Address of the wallet
        address: RawAddr,
        /// Value from [QueueResp]
        start_after: Option<QueuePositionId>,
        /// How many values to return
        limit: Option<u32>,
    },
    /// Returns the share held by the wallet
    ///
    /// Returns [BalanceResp]
    Balance {
        /// Address of the token holder
        address: RawAddr,
        /// Value from [BalanceResp]
        start_after: Option<Token>,
        /// How many values to return
        limit: Option<u32>,
    },
    /// Check the status of the copy trading contract for all the
    /// markets that it's trading on
    ///
    /// Returns [LeaderStatusResp]
    LeaderStatus {
        /// Value from [TokenStatus::token]
        start_after: Option<Token>,
        /// How many values to return
        limit: Option<u32>,
    },
    /// Does it have any pending work ?
    ///
    /// Returns [WorkResp]
    HasWork {},
}

/// Individual response from [QueryMsg::QueueStatus]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct QueueResp {
    /// Items in queue for the wallet
    pub items: Vec<QueueItemStatus>,
    /// Last processed [QueuePositionId]
    pub inc_processed_till: Option<IncQueuePositionId>,
    /// Last processed [QueuePositionId]
    pub dec_processed_till: Option<DecQueuePositionId>,
}

/// Queue item status
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct QueueItemStatus {
    /// Queue item
    pub item: QueueItem,
    /// Status of processing
    pub status: ProcessingStatus,
}

/// Queue item Processing status
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ProcessingStatus {
    /// Not started processing yet
    NotProcessed,
    /// Successfully finished processing
    Finished,
    /// In progress
    InProgress,
    /// Failed during processing
    Failed(FailedReason),
}

impl ProcessingStatus {
    /// Did the processing fail ?
    pub fn failed(&self) -> bool {
        match self {
            ProcessingStatus::NotProcessed => false,
            ProcessingStatus::Finished => false,
            ProcessingStatus::Failed(_) => true,
            ProcessingStatus::InProgress => false,
        }
    }

    /// Is any status pending ?
    pub fn pending(&self) -> bool {
        match self {
            ProcessingStatus::NotProcessed => true,
            ProcessingStatus::Finished => false,
            ProcessingStatus::Failed(_) => false,
            ProcessingStatus::InProgress => true,
        }
    }

    /// Did the status finish ?
    pub fn finish(&self) -> bool {
        match self {
            ProcessingStatus::NotProcessed => false,
            ProcessingStatus::Finished => true,
            ProcessingStatus::Failed(_) => false,
            ProcessingStatus::InProgress => false,
        }
    }

    /// Is any status currently in progres ?
    pub fn in_progress(&self) -> bool {
        match self {
            ProcessingStatus::NotProcessed => false,
            ProcessingStatus::Finished => false,
            ProcessingStatus::Failed(_) => false,
            ProcessingStatus::InProgress => true,
        }
    }
}

/// Failure reason on why queue processing failed
#[derive(Error, Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum FailedReason {
    /// Not enough collateral available
    #[error("Collateral not available. Requested {requested}, but only available {available}")]
    NotEnoughCollateral {
        /// Available collateral
        available: Collateral,
        /// Requested collateral
        requested: NonZero<Collateral>,
    },
    /// Not enough crank fee
    #[error("Crank fee not available. Requested {requested}, but only available {available}")]
    NotEnoughCrankFee {
        /// Available collateral
        available: Collateral,
        /// Requested collateral
        requested: Collateral,
    },
    /// Fund less than chain's minimum representation
    #[error("Collateral amount {funds} is less than chain's minimum representation.not available")]
    FundLessThanMinChain {
        /// Requested collateral
        funds: NonZero<Collateral>,
    },
    /// Wallet does not have enough shares
    #[error("Shares not available. Requested {requested}, but only available {available}")]
    NotEnoughShares {
        /// Available shares
        available: LpToken,
        /// Requested shares
        requested: LpToken,
    },
    /// Received error from Market contract
    #[error("{market_id} result in error: {message}")]
    MarketError {
        /// Market ID which result in error
        market_id: MarketId,
        /// Error message
        message: String,
    },
    /// Deferred exec failure
    #[error("Deferred exec failure at {executed} because of: {reason}")]
    DeferredExecFailure {
        /// Reason it didn't apply successfully
        reason: String,
        /// Timestamp when it failed execution
        executed: Timestamp,
        /// Price point when it was cranked, if applicable
        crank_price: Option<PricePoint>,
    },
}

/// Queue Item
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum QueueItem {
    /// Item that will lead to increase or no change of collateral
    IncCollaleteral {
        /// Item type
        item: IncQueueItem,
        /// Queue position id
        id: IncQueuePositionId,
    },
    /// Item that will lead to decrease of collateral
    DecCollateral {
        /// Item type
        item: Box<DecQueueItem>,
        /// Queue position id
        id: DecQueuePositionId,
    },
}

/// Queue item that needs to be processed
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum IncQueueItem {
    /// Deposit the fund and get some [LpToken]
    Deposit {
        /// Funds to be deposited
        funds: NonZero<Collateral>,
        /// Token
        token: Token,
    },
    /// Market action items
    MarketItem {
        /// Market id
        id: MarketId,
        /// Market token
        token: Token,
        /// Market item
        item: Box<IncMarketItem>,
    },
}

/// Queue item that needs to be processed
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DecQueueItem {
    /// Withdraw via LpToken
    Withdrawal {
        /// Tokens to be withdrawn
        tokens: NonZero<LpToken>,
        /// Token type
        token: Token,
    },
    /// Market action items
    MarketItem {
        /// Market id
        id: MarketId,
        /// Market token
        token: Token,
        /// Market item
        item: Box<DecMarketItem>,
    },
}

/// Queue item that needs to be processed
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum IncMarketItem {
    /// Remove collateral from a position, causing leverage to increase
    UpdatePositionRemoveCollateralImpactLeverage {
        /// ID of position to update
        id: PositionId,
        /// Amount of funds to remove from the position
        amount: NonZero<Collateral>,
    },
    /// Remove collateral from a position, causing leverage to increase
    UpdatePositionRemoveCollateralImpactSize {
        /// ID of position to update
        id: PositionId,
        /// Amount of funds to remove from the position
        amount: NonZero<Collateral>,
        /// Slippage alert
        slippage_assert: Option<SlippageAssert>,
    },
    /// Cancel an open limit order
    CancelLimitOrder {
        /// ID of the order
        order_id: OrderId,
    },
    /// Close a position
    ClosePosition {
        /// ID of position to close
        id: PositionId,
        /// Assertion that the price has not moved too far
        slippage_assert: Option<SlippageAssert>,
    },
}

/// Queue item that needs to be processed
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DecMarketItem {
    /// Open position
    OpenPosition {
        /// Collateral for the position
        collateral: NonZero<Collateral>,
        /// Assertion that the price has not moved too far
        slippage_assert: Option<SlippageAssert>,
        /// Leverage of new position
        leverage: LeverageToBase,
        /// Direction of new position
        direction: DirectionToBase,
        /// Stop loss price of new position
        stop_loss_override: Option<PriceBaseInQuote>,
        /// Take profit price of new position
        #[serde(alias = "take_profit_override")]
        take_profit: TakeProfitTrader,
    },
    /// Add collateral to position, causing leverage to decrease
    UpdatePositionAddCollateralImpactLeverage {
        /// Collateral that will be added
        collateral: NonZero<Collateral>,
        /// ID of position to update
        id: PositionId,
    },
    /// Add collateral to position, causing notional size to increase
    UpdatePositionAddCollateralImpactSize {
        /// Collateral that will be added
        collateral: NonZero<Collateral>,
        /// ID of position to update
        id: PositionId,
        /// Slippage assert
        slippage_assert: Option<SlippageAssert>,
    },
    /// Update position leverage
    UpdatePositionLeverage {
        /// ID of position to update
        id: PositionId,
        /// New leverage of the position
        leverage: LeverageToBase,
        /// Slippage assert
        slippage_assert: Option<SlippageAssert>,
    },
    /// Modify the take profit price of a position
    UpdatePositionTakeProfitPrice {
        /// ID of position to update
        id: PositionId,
        /// New take profit price of the position
        price: TakeProfitTrader,
    },
    /// Update the stop loss price of a position
    UpdatePositionStopLossPrice {
        /// ID of position to update
        id: PositionId,
        /// New stop loss price of the position, or remove
        stop_loss: StopLoss,
    },
    /// Set a limit order to open a position when the price of the asset hits
    /// the specified trigger price.
    PlaceLimitOrder {
        /// Collateral for the position
        collateral: NonZero<Collateral>,
        /// Price when the order should trigger
        trigger_price: PriceBaseInQuote,
        /// Leverage of new position
        leverage: LeverageToBase,
        /// Direction of new position
        direction: DirectionToBase,
        /// Stop loss price of new position
        stop_loss_override: Option<PriceBaseInQuote>,
        /// Take profit price of new position
        #[serde(alias = "take_profit_override")]
        take_profit: TakeProfitTrader,
    },
}

/// Token required for the queue item
pub enum RequiresToken {
    /// Token required for LP token value computation
    Token {
        /// Token
        token: Token,
    },
    /// Token not requird
    NoToken {},
}

impl IncQueueItem {
    /// Does this queue item require computation of LP token value
    pub fn requires_token(self) -> RequiresToken {
        match self {
            IncQueueItem::Deposit { token, .. } => RequiresToken::Token { token },
            IncQueueItem::MarketItem { .. } => RequiresToken::NoToken {},
        }
    }
}

impl DecQueueItem {
    /// Does this queue item require computation of LP token value
    pub fn requires_token(self) -> RequiresToken {
        match self {
            DecQueueItem::Withdrawal { token, .. } => RequiresToken::Token { token },
            // Market item does not require computation of lp token value
            DecQueueItem::MarketItem { .. } => RequiresToken::NoToken {},
        }
    }
}

/// Individual response from [QueryMsg::LeaderStatus]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct LeaderStatusResp {
    /// Tokens for the leader
    pub tokens: Vec<TokenStatus>,
}

/// Individual response from [QueryMsg::LeaderStatus]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct TokenStatus {
    /// Token
    pub token: Token,
    /// Available collateral for the leader
    pub collateral: Collateral,
    /// Total shares so far. Represents AUM.
    pub shares: LpToken,
    /// Unclaimed commission
    pub unclaimed_commission: Collateral,
    /// Claimed commission
    pub claimed_commission: Collateral,
}

/// Individual market response from [QueryMsg::Balance]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct BalanceResp {
    /// Shares of the pool held by the wallet
    pub balance: Vec<BalanceRespItem>,
    /// Start after that should be passed for next iteration
    pub start_after: Option<Token>,
}

/// Individual market response inside [BalanceResp]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct BalanceRespItem {
    /// Shares of the pool held by the wallet
    pub shares: NonZero<LpToken>,
    /// Token type
    pub token: Token,
}

/// Token accepted by the contract
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Token {
    /// Native coin and its denom
    Native(String),
    /// CW20 contract and its address
    Cw20(Addr),
}

impl Token {
    /// Is it same as market token ?
    pub fn is_same(&self, token: &crate::token::Token) -> bool {
        match token {
            crate::token::Token::Cw20 { addr, .. } => match self {
                Token::Native(_) => false,
                Token::Cw20(cw20_addr) => {
                    let cw20_addr: &RawAddr = &cw20_addr.into();
                    cw20_addr == addr
                }
            },
            crate::token::Token::Native { denom, .. } => match self {
                Token::Native(native_denom) => *native_denom == *denom,
                Token::Cw20(_) => false,
            },
        }
    }
}

impl<'a> PrimaryKey<'a> for Token {
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        let (token_type, bytes) = match self {
            Token::Native(native) => (0u8, native.as_bytes()),
            Token::Cw20(cw20) => (1u8, cw20.as_bytes()),
        };
        let token_type = Key::Val8([token_type]);
        let key = Key::Ref(bytes);

        vec![token_type, key]
    }
}

impl<'a> Prefixer<'a> for Token {
    fn prefix(&self) -> Vec<Key> {
        let (token_type, bytes) = match self {
            Token::Native(native) => (0u8, native.as_bytes()),
            Token::Cw20(cw20) => (1u8, cw20.as_bytes()),
        };
        let token_type = Key::Val8([token_type]);
        let key = Key::Ref(bytes);
        vec![token_type, key]
    }
}

impl KeyDeserialize for Token {
    type Output = Token;

    const KEY_ELEMS: u16 = 2;

    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        let (token_type, token) = <(u8, Vec<u8>) as KeyDeserialize>::from_vec(value)?;
        let token = match token_type {
            0 => {
                let native_token = String::from_slice(&token)?;
                Token::Native(native_token)
            }
            1 => {
                let cw20_token = Addr::from_slice(&token)?;
                Token::Cw20(cw20_token)
            }
            _ => {
                return Err(StdError::serialize_err(
                    "Token",
                    "Invalid number in token_type",
                ))
            }
        };
        Ok(token)
    }
}

impl KeyDeserialize for &Token {
    type Output = Token;

    const KEY_ELEMS: u16 = 2;

    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        let token = <Token as KeyDeserialize>::from_vec(value)?;
        Ok(token)
    }
}

impl Display for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Token::Native(denom) => f.write_str(denom),
            Token::Cw20(addr) => f.write_str(addr.as_str()),
        }
    }
}

impl Token {
    /// Ensure that the two versions of the token are compatible.
    pub fn ensure_matches(&self, token: &crate::token::Token) -> anyhow::Result<()> {
        match (self, token) {
            (Token::Native(_), crate::token::Token::Cw20 { addr, .. }) => {
                anyhow::bail!("Provided native funds, but market requires a CW20 (contract {addr})")
            }
            (
                Token::Native(denom1),
                crate::token::Token::Native {
                    denom: denom2,
                    decimal_places: _,
                },
            ) => {
                if denom1 == denom2 {
                    Ok(())
                } else {
                    Err(anyhow::anyhow!("Wrong denom provided. You sent {denom1}, but the contract expects {denom2}"))
                }
            }
            (
                Token::Cw20(addr1),
                crate::token::Token::Cw20 {
                    addr: addr2,
                    decimal_places: _,
                },
            ) => {
                if addr1.as_str() == addr2.as_str() {
                    Ok(())
                } else {
                    Err(anyhow::anyhow!(
                        "Wrong CW20 used. You used {addr1}, but the contract expects {addr2}"
                    ))
                }
            }
            (Token::Cw20(_), crate::token::Token::Native { denom, .. }) => {
                anyhow::bail!(
                    "Provided CW20 funds, but market requires native funds with denom {denom}"
                )
            }
        }
    }
}

/// Work response
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum WorkResp {
    /// No work found
    NoWork,
    /// Has some work
    HasWork {
        /// Work description
        work_description: WorkDescription,
    },
}

impl WorkResp {
    /// Does it have work ?
    pub fn has_work(&self) -> bool {
        match self {
            WorkResp::NoWork => false,
            WorkResp::HasWork { .. } => true,
        }
    }

    /// Is it deferred work
    pub fn is_deferred_work(&self) -> bool {
        match self {
            WorkResp::NoWork => false,
            WorkResp::HasWork { work_description } => work_description.is_deferred_work(),
        }
    }

    /// Is it deferred work
    pub fn is_rebalance(&self) -> bool {
        match self {
            WorkResp::NoWork => false,
            WorkResp::HasWork { work_description } => work_description.is_rebalance(),
        }
    }

    /// Is it compute lp token work ?
    pub fn is_compute_lp_token(&self) -> bool {
        match self {
            WorkResp::NoWork => false,
            WorkResp::HasWork { work_description } => work_description.is_compute_lp_token(),
        }
    }

    /// Is it reset status work ?
    pub fn is_reset_status(&self) -> bool {
        match self {
            WorkResp::NoWork => false,
            WorkResp::HasWork { work_description } => work_description.is_reset_status(),
        }
    }
}

/// Work Description
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum WorkDescription {
    /// Load Market
    LoadMarket {},
    /// Calculate LP token value
    ComputeLpTokenValue {
        /// Token
        token: Token,
        /// Start from specific market id in the processing phase
        process_start_from: Option<MarketId>,
        /// Start from specific market id in the validate phase
        validate_start_from: Option<MarketId>,
    },
    /// Process Queue item
    ProcessQueueItem {
        /// Id to process
        id: QueuePositionId,
    },
    /// Reset market specific statistics
    ResetStats {
        /// Token
        token: Token,
    },
    /// Handle deferred exec id
    HandleDeferredExecId {},
    /// Rebalance is done when contract balance is not same as the one
    /// internally tracked by it.
    Rebalance {
        /// Token
        token: Token,
        /// Amount that needs to be balanced
        amount: NonZero<Collateral>,
        /// Start from specific market id
        start_from: Option<MarketId>,
    },
}

impl WorkDescription {
    /// Is it the rebalance work ?
    pub fn is_rebalance(&self) -> bool {
        match self {
            WorkDescription::LoadMarket {} => false,
            WorkDescription::ComputeLpTokenValue { .. } => false,
            WorkDescription::ProcessQueueItem { .. } => false,
            WorkDescription::ResetStats { .. } => false,
            WorkDescription::HandleDeferredExecId {} => false,
            WorkDescription::Rebalance { .. } => true,
        }
    }

    /// Is it compute lp token work ?
    pub fn is_compute_lp_token(&self) -> bool {
        match self {
            WorkDescription::LoadMarket {} => false,
            WorkDescription::ComputeLpTokenValue { .. } => true,
            WorkDescription::ProcessQueueItem { .. } => false,
            WorkDescription::ResetStats { .. } => false,
            WorkDescription::HandleDeferredExecId {} => false,
            WorkDescription::Rebalance { .. } => false,
        }
    }

    /// Is it reset stats ?
    pub fn is_reset_status(&self) -> bool {
        match self {
            WorkDescription::LoadMarket {} => false,
            WorkDescription::ComputeLpTokenValue { .. } => false,
            WorkDescription::ProcessQueueItem { .. } => false,
            WorkDescription::ResetStats { .. } => true,
            WorkDescription::HandleDeferredExecId {} => false,
            WorkDescription::Rebalance { .. } => false,
        }
    }

    /// Is deferred work ?
    pub fn is_deferred_work(&self) -> bool {
        match self {
            WorkDescription::LoadMarket {} => false,
            WorkDescription::ComputeLpTokenValue { .. } => false,
            WorkDescription::ProcessQueueItem { .. } => false,
            WorkDescription::ResetStats { .. } => false,
            WorkDescription::HandleDeferredExecId {} => true,
            WorkDescription::Rebalance { .. } => false,
        }
    }
}

/// Queue position id that needs to be processed
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum QueuePositionId {
    /// Queue position id corrsponding to the queue items that will
    /// increase or won't change the collateral
    IncQueuePositionId(IncQueuePositionId),
    /// Queue position id corresponding to the queue items that will
    /// decrease the collateral
    DecQueuePositionId(DecQueuePositionId),
}

impl<'a> PrimaryKey<'a> for QueuePositionId {
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        let (queue_type, key) = match self {
            QueuePositionId::IncQueuePositionId(id) => (0u8, id.key()),
            QueuePositionId::DecQueuePositionId(id) => (1u8, id.key()),
        };
        let mut keys = vec![Key::Val8([queue_type])];
        keys.extend(key);
        keys
    }
}

impl KeyDeserialize for QueuePositionId {
    type Output = QueuePositionId;

    const KEY_ELEMS: u16 = 2;

    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        let (queue_type, queue_id) = <(u8, u64) as KeyDeserialize>::from_vec(value)?;
        let position_id = match queue_type {
            0 => QueuePositionId::IncQueuePositionId(IncQueuePositionId(queue_id.into())),
            1 => QueuePositionId::DecQueuePositionId(DecQueuePositionId(queue_id.into())),
            _ => {
                return Err(StdError::serialize_err(
                    "QueuePositionId",
                    "Invalid number in queue_type",
                ))
            }
        };
        Ok(position_id)
    }
}

impl<'a> Prefixer<'a> for QueuePositionId {
    fn prefix(&self) -> Vec<Key> {
        match self {
            QueuePositionId::IncQueuePositionId(id) => {
                let mut keys = vec![Key::Val8([0u8])];
                keys.extend(id.key());
                keys
            }
            QueuePositionId::DecQueuePositionId(id) => {
                let mut keys = vec![Key::Val8([1u8])];
                keys.extend(id.key());
                keys
            }
        }
    }
}

/// Queue position number
#[derive(
    Copy, PartialOrd, Ord, Eq, Clone, PartialEq, serde::Serialize, serde::Deserialize, Debug,
)]
#[serde(rename_all = "snake_case")]
pub struct IncQueuePositionId(Uint64);

impl IncQueuePositionId {
    /// Construct a new value from a [u64].
    pub fn new(x: u64) -> Self {
        IncQueuePositionId(x.into())
    }

    /// The underlying `u64` representation.
    pub fn u64(self) -> u64 {
        self.0.u64()
    }

    /// Generate the next position ID
    ///
    /// Panics on overflow
    pub fn next(self) -> Self {
        IncQueuePositionId((self.u64() + 1).into())
    }
}

impl<'a> PrimaryKey<'a> for IncQueuePositionId {
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        vec![Key::Val64(self.0.u64().to_cw_bytes())]
    }
}

impl<'a> Prefixer<'a> for IncQueuePositionId {
    fn prefix(&self) -> Vec<Key> {
        vec![Key::Val64(self.0.u64().to_cw_bytes())]
    }
}

impl KeyDeserialize for IncQueuePositionId {
    type Output = IncQueuePositionId;

    const KEY_ELEMS: u16 = 1;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        u64::from_vec(value).map(|x| IncQueuePositionId(Uint64::new(x)))
    }
}

impl std::fmt::Display for IncQueuePositionId {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for IncQueuePositionId {
    type Err = ParseIntError;
    fn from_str(src: &str) -> Result<Self, ParseIntError> {
        src.parse().map(|x| IncQueuePositionId(Uint64::new(x)))
    }
}

/// Queue position number
#[derive(
    Copy, PartialOrd, Ord, Eq, Clone, PartialEq, serde::Serialize, serde::Deserialize, Debug,
)]
#[serde(rename_all = "snake_case")]
pub struct DecQueuePositionId(Uint64);

impl DecQueuePositionId {
    /// Construct a new value from a [u64].
    pub fn new(x: u64) -> Self {
        DecQueuePositionId(x.into())
    }

    /// The underlying `u64` representation.
    pub fn u64(self) -> u64 {
        self.0.u64()
    }

    /// Generate the next position ID
    ///
    /// Panics on overflow
    pub fn next(self) -> Self {
        DecQueuePositionId((self.u64() + 1).into())
    }
}

impl<'a> PrimaryKey<'a> for DecQueuePositionId {
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        vec![Key::Val64(self.0.u64().to_cw_bytes())]
    }
}

impl<'a> Prefixer<'a> for DecQueuePositionId {
    fn prefix(&self) -> Vec<Key> {
        vec![Key::Val64(self.0.u64().to_cw_bytes())]
    }
}

impl KeyDeserialize for DecQueuePositionId {
    type Output = DecQueuePositionId;

    const KEY_ELEMS: u16 = 1;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        u64::from_vec(value).map(|x| DecQueuePositionId(Uint64::new(x)))
    }
}

impl std::fmt::Display for DecQueuePositionId {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for DecQueuePositionId {
    type Err = ParseIntError;
    fn from_str(src: &str) -> Result<Self, ParseIntError> {
        src.parse().map(|x| DecQueuePositionId(Uint64::new(x)))
    }
}

/// Migration message, currently no fields needed
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct MigrateMsg {}