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
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
//! Entrypoint messages for the market
use super::deferred_execution::DeferredExecId;
use super::order::LimitOrder;
use super::position::{ClosedPosition, PositionId};
use super::spot_price::SpotPriceConfigInit;
use super::{config::ConfigUpdate, crank::CrankWorkInfo};
use crate::contracts::market::order::OrderId;
use crate::{contracts::liquidity_token::LiquidityTokenKind, token::TokenInit};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Binary, BlockInfo, Decimal256, Uint128};
use pyth_sdk_cw::PriceIdentifier;
use schemars::schema::{InstanceType, SchemaObject};
use schemars::JsonSchema;
use shared::prelude::*;
use std::collections::BTreeMap;
use std::fmt::Formatter;

/// The InstantiateMsg comes from Factory only
#[cw_serde]
pub struct InstantiateMsg {
    /// The factory address
    pub factory: RawAddr,
    /// Modifications to the default config value
    pub config: Option<ConfigUpdate>,
    /// Mandatory spot price config
    pub spot_price: SpotPriceConfigInit,
    /// Initial price to use in the contract
    ///
    /// This is required when doing manual price updates, and prohibited for oracle based price updates. It would make more sense to include this in [SpotPriceConfigInit], but that will create more complications in config update logic.
    pub initial_price: Option<InitialPrice>,
    /// Base, quote, and market type
    pub market_id: MarketId,
    /// The token used for collateral
    pub token: TokenInit,
    /// Initial borrow fee rate when launching the protocol, annualized
    pub initial_borrow_fee_rate: Decimal256,
}

/// Initial price when instantiating a contract
#[cw_serde]
#[derive(Copy)]
pub struct InitialPrice {
    /// Price of base in terms of quote
    pub price: PriceBaseInQuote,
    /// Price of collateral in terms of USD
    pub price_usd: PriceCollateralInUsd,
}

/// Config info passed on to all sub-contracts in order to
/// add a new market.
#[cw_serde]
pub struct NewMarketParams {
    /// Base, quote, and market type
    pub market_id: MarketId,

    /// The token used for collateral
    pub token: TokenInit,

    /// config
    pub config: Option<ConfigUpdate>,

    /// mandatory spot price config
    pub spot_price: SpotPriceConfigInit,

    /// Initial borrow fee rate, annualized
    pub initial_borrow_fee_rate: Decimal256,

    /// Initial price, only provided for manual price updates
    pub initial_price: Option<InitialPrice>,
}

/// There are two sources of slippage in the protocol:
/// - Change in the oracle price from creation of the message to execution of the message.
/// - Change in delta neutrality fee from creation of the message to execution of the message.
/// Slippage assert tolerance is the tolerance to the sum of the two sources of slippage.
#[cw_serde]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Eq)]
pub struct SlippageAssert {
    /// Expected effective price from the sender. To incorporate tolerance on delta neutrality fee,
    /// the expected price should be modified by expected fee rate:
    /// `price = oracle_price * (1 + fee_rate)`
    /// `fee_rate` here is the ratio between the delta neutrality fee amount and notional size delta (in collateral asset).
    pub price: PriceBaseInQuote,
    /// Max ratio tolerance of actual trade price differing in an unfavorable direction from expected price.
    /// Tolerance of 0.01 means max 1% difference.
    pub tolerance: Number,
}

/// Execute message for the market contract
#[allow(clippy::large_enum_variant)]
#[cw_serde]
pub enum ExecuteMsg {
    /// Owner-only executions
    Owner(ExecuteOwnerMsg),

    /// cw20
    Receive {
        /// Owner of funds sent to the contract
        sender: RawAddr,
        /// Amount of funds sent
        amount: Uint128,
        /// Must parse to a [ExecuteMsg]
        msg: Binary,
    },

    /// Open a new position
    OpenPosition {
        /// 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,
        /// Maximum gains of new position
        #[deprecated(note = "Use take_profit instead")]
        max_gains: Option<MaxGainsInQuote>,
        /// Stop loss price of new position
        stop_loss_override: Option<PriceBaseInQuote>,
        /// Take profit price of new position
        /// if max_gains is `None`, this *must* be `Some`
        #[serde(alias = "take_profit_override")]
        take_profit: Option<TakeProfitTrader>,
    },

    /// Add collateral to a position, causing leverage to decrease
    ///
    /// The amount of collateral to add must be attached as funds
    UpdatePositionAddCollateralImpactLeverage {
        /// ID of position to update
        id: PositionId,
    },

    /// Add collateral to a position, causing notional size to increase
    ///
    /// The amount of collateral to add must be attached as funds
    UpdatePositionAddCollateralImpactSize {
        /// ID of position to update
        id: PositionId,
        /// Assertion that the price has not moved too far
        slippage_assert: Option<SlippageAssert>,
    },

    /// 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 notional size to decrease
    UpdatePositionRemoveCollateralImpactSize {
        /// ID of position to update
        id: PositionId,
        /// Amount of funds to remove from the position
        amount: NonZero<Collateral>,
        /// Assertion that the price has not moved too far
        slippage_assert: Option<SlippageAssert>,
    },

    /// Modify the leverage of the position
    ///
    /// This will impact the notional size of the position
    UpdatePositionLeverage {
        /// ID of position to update
        id: PositionId,
        /// New leverage of the position
        leverage: LeverageToBase,
        /// Assertion that the price has not moved too far
        slippage_assert: Option<SlippageAssert>,
    },

    /// Modify the max gains of a position
    UpdatePositionMaxGains {
        /// ID of position to update
        id: PositionId,
        /// New max gains of the position
        max_gains: MaxGainsInQuote,
    },

    /// 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 stop loss or take profit override.
    /// Deprecated, use UpdatePositionStopLossPrice instead
    // not sure why this causes a warning here...
    // #[deprecated(note = "Use UpdatePositionStopLossPrice instead")]
    SetTriggerOrder {
        /// ID of position to modify
        id: PositionId,
        /// New stop loss price of the position
        /// Passing None will remove the override.
        stop_loss_override: Option<PriceBaseInQuote>,
        /// New take profit price of the position, merely as a trigger.
        /// Passing None will bypass changing this
        /// This does not affect the locked up counter collateral (or borrow fees etc.).
        /// if this override is further away than the position's take profit price, the position's will be triggered first
        /// if you want to update the position itself, use [ExecuteMsg::UpdatePositionTakeProfitPrice]
        #[serde(alias = "take_profit_override")]
        take_profit: Option<TakeProfitTrader>,
    },

    /// Set a limit order to open a position when the price of the asset hits
    /// the specified trigger price.
    PlaceLimitOrder {
        /// Price when the order should trigger
        trigger_price: PriceBaseInQuote,
        /// Leverage of new position
        leverage: LeverageToBase,
        /// Direction of new position
        direction: DirectionToBase,

        /// Maximum gains of new position
        #[deprecated(note = "Use take_profit instead")]
        max_gains: Option<MaxGainsInQuote>,
        /// Stop loss price of new position
        stop_loss_override: Option<PriceBaseInQuote>,
        /// Take profit price of new position
        /// if max_gains is `None`, this *must* be `Some`
        #[serde(alias = "take_profit_override")]
        take_profit: Option<TakeProfitTrader>,
    },

    /// 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>,
    },

    /// Deposits send funds into the unlocked liquidity fund
    /// Returns [LiquidityDepositResponseData] as response data
    DepositLiquidity {
        /// Should we stake the resulting LP tokens into xLP?
        ///
        /// Defaults to `false`.
        #[serde(default)]
        stake_to_xlp: bool,
    },

    /// Like [ExecuteMsg::DepositLiquidity], but reinvests pending yield instead of receiving new funds.
    ReinvestYield {
        /// Should we stake the resulting LP tokens into xLP?
        ///
        /// Defaults to `false`.
        #[serde(default)]
        stake_to_xlp: bool,
        /// Amount of rewards to reinvest.
        ///
        /// If `None`, reinvests all pending rewards.
        amount: Option<NonZero<Collateral>>,
    },

    /// Withdraw liquidity calculated from specified `lp_amount`
    WithdrawLiquidity {
        /// Amount of LP tokens to burn
        lp_amount: Option<NonZero<LpToken>>,
    },

    /// Claims accrued yield based on LP share allocation
    ClaimYield {},

    /// Stake some existing LP tokens into xLP
    ///
    /// [None] means stake all LP tokens.
    StakeLp {
        /// Amount of LP tokens to convert into xLP.
        amount: Option<NonZero<LpToken>>,
    },

    /// Begin unstaking xLP into LP
    ///
    /// [None] means unstake all xLP tokens.
    UnstakeXlp {
        /// Amount of xLP tokens to convert into LP
        amount: Option<NonZero<LpToken>>,
    },

    /// Stop an ongoing xLP unstaking process.
    StopUnstakingXlp {},

    /// Collect any LP tokens that have been unstaked from xLP.
    CollectUnstakedLp {},

    /// Crank a number of times
    Crank {
        /// Total number of crank executions to do
        /// None: config default
        execs: Option<u32>,
        /// Which wallet receives crank rewards.
        ///
        /// If unspecified, sender receives the rewards.
        rewards: Option<RawAddr>,
    },

    /// Nft proxy messages.
    /// Only allowed to be called by this market's position_token contract
    NftProxy {
        /// Original caller of the NFT proxy.
        sender: RawAddr,
        /// Message sent to the NFT proxy
        msg: crate::contracts::position_token::entry::ExecuteMsg,
    },

    /// liquidity token cw20 proxy messages.
    /// Only allowed to be called by this market's liquidity_token contract
    LiquidityTokenProxy {
        /// Original caller of the liquidity token proxy.
        sender: RawAddr,
        /// Whether this was the LP or xLP proxy.
        kind: LiquidityTokenKind,
        /// Message sent to the liquidity token proxy.
        msg: crate::contracts::liquidity_token::entry::ExecuteMsg,
    },

    /// Transfer all available protocol fees to the dao account
    TransferDaoFees {},

    /// Begin force-closing all positions in the protocol.
    ///
    /// This can only be performed by the market wind down wallet.
    CloseAllPositions {},

    /// Provide funds directly to the crank fees.
    ///
    /// The person who calls this receives no benefits. It's intended for the
    /// DAO to use to incentivize cranking.
    ProvideCrankFunds {},

    /// Set manual price (mostly for testing)
    SetManualPrice {
        /// Price of the base asset in terms of the quote.
        price: PriceBaseInQuote,
        /// Price of the collateral asset in terms of USD.
        ///
        /// This is generally used for reporting of values like PnL and trade
        /// volume.
        price_usd: PriceCollateralInUsd,
    },

    /// Perform a deferred exec
    ///
    /// This should only ever be called from the market contract itself, any
    /// other call is guaranteed to fail.
    PerformDeferredExec {
        /// Which ID to execute
        id: DeferredExecId,
        /// Which price point to use for this execution.
        price_point_timestamp: Timestamp,
    },
}

/// Owner-only messages
#[cw_serde]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ExecuteOwnerMsg {
    /// Update the config
    ConfigUpdate {
        /// New configuration parameters
        update: Box<ConfigUpdate>,
    },
}

/// Fees held within the market contract.
#[cw_serde]
pub struct Fees {
    /// Fees available for individual wallets to withdraw.
    pub wallets: Collateral,
    /// Fees available for the protocol overall to withdraw.
    pub protocol: Collateral,
    /// Crank fees collected and waiting to be allocated to crankers.
    pub crank: Collateral,
    /// Referral fees collected and waiting to be allocated to crankers.
    #[serde(default)]
    pub referral: Collateral,
}

/// Return value from [QueryMsg::ClosedPositionHistory]
#[cw_serde]
pub struct ClosedPositionsResp {
    /// Closed positions
    pub positions: Vec<ClosedPosition>,
    /// the next cursor to start from
    /// if we've reached the end, it's a None
    pub cursor: Option<ClosedPositionCursor>,
}

/// A cursor used for paginating
/// the closed position history
#[cw_serde]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ClosedPositionCursor {
    /// Last close timestamp
    pub time: Timestamp,
    /// Last closed position ID
    pub position: PositionId,
}

/// Use this price as the current price during a query.
#[cw_serde]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Eq, Copy)]
pub struct PriceForQuery {
    /// Price of the base asset in terms of quote
    pub base: PriceBaseInQuote,
    /// Price of the collateral asset in terms of USD
    ///
    /// This is optional if the notional asset is USD and required otherwise.
    pub collateral: PriceCollateralInUsd,
}

impl PriceForQuery {
    /// Create a PriceForQuery with a base price and USD-market, without specifying the USD price
    pub fn from_usd_market(base: PriceBaseInQuote, market_id: &MarketId) -> Result<Self> {
        Ok(Self {
            base,
            collateral: base
                .try_into_usd(market_id)
                .context("cannot derive price query for non-USD market")?,
        })
    }
}

/// Query messages on the market contract
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    /// * returns [cw2::ContractVersion]
    #[returns(cw2::ContractVersion)]
    Version {},

    /// Provides overall information about this market.
    ///
    /// This is intended as catch-all for protocol wide information, both static
    /// (like market ID) and dynamic (like notional interest). The goal is to
    /// limit the total number of queries callers have to make to get relevant
    /// information.
    ///
    /// * returns [StatusResp]
    #[returns(StatusResp)]
    Status {
        /// Price to be used as the current price
        price: Option<PriceForQuery>,
    },

    /// * returns [shared::prelude::PricePoint]
    ///
    /// Gets the spot price, if no time is supplied, then it's current
    /// This is the spot price as seen by the contract storage
    /// i.e. the price that was pushed via execution messages
    #[returns(shared::prelude::PricePoint)]
    SpotPrice {
        /// Timestamp when the price should be effective.
        ///
        /// [None] means "give the most recent price."
        timestamp: Option<Timestamp>,
    },

    /// * returns [SpotPriceHistoryResp]
    ///
    /// Gets a collection of historical spot prices
    #[returns(SpotPriceHistoryResp)]
    SpotPriceHistory {
        /// Last timestamp we saw
        start_after: Option<Timestamp>,
        /// How many prices to query
        limit: Option<u32>,
        /// Order to sort by, if None then it will be descending
        order: Option<OrderInMessage>,
    },

    /// * returns [OraclePriceResp]
    ///
    /// Gets the current price from the oracle (for markets configured with an oracle)
    ///
    /// Also returns prices for each feed used to compose the final price
    ///
    /// This may be more up-to-date than the spot price which was
    /// validated and pushed into the contract storage via execution messages
    #[returns(OraclePriceResp)]
    OraclePrice {
        /// If true then it will validate the publish_time age as though it were
        /// used to push a new spot_price update
        /// Otherwise, it just returns the oracle price as-is, even if it's old
        #[serde(default)]
        validate_age: bool,
    },

    /// * returns [super::position::PositionsResp]
    ///
    /// Maps the given PositionIds into Positions
    #[returns(super::position::PositionsResp)]
    Positions {
        /// Positions to query.
        position_ids: Vec<PositionId>,
        /// Should we skip calculating pending fees?
        ///
        /// This field is ignored if `fees` is set.
        ///
        /// The default for this field is `false`. The behavior of this field is:
        ///
        /// * `true`: the same as [PositionsQueryFeeApproach::NoFees]
        ///
        /// * `false`: the same as [PositionsQueryFeeApproach::AllFees] (though see note on that variant, this default will likely change in the future).
        ///
        /// It is recommended _not_ to use this field going forward, and to instead use `fees`.
        skip_calc_pending_fees: Option<bool>,
        /// How do we calculate fees for this position?
        ///
        /// Any value here will override the `skip_calc_pending_fees` field.
        fees: Option<PositionsQueryFeeApproach>,
        /// Price to be used as the current price
        price: Option<PriceForQuery>,
    },

    /// * returns [LimitOrderResp]
    ///
    /// Returns the specified Limit Order
    #[returns(LimitOrderResp)]
    LimitOrder {
        /// Limit order ID to query
        order_id: OrderId,
    },

    /// * returns [LimitOrdersResp]
    ///
    /// Returns the Limit Orders for the specified addr
    #[returns(LimitOrdersResp)]
    LimitOrders {
        /// Owner of limit orders
        owner: RawAddr,
        /// Last limit order seen
        start_after: Option<OrderId>,
        /// Number of order to return
        limit: Option<u32>,
        /// Whether to return ascending or descending
        order: Option<OrderInMessage>,
    },

    /// * returns [ClosedPositionsResp]
    #[returns(ClosedPositionsResp)]
    ClosedPositionHistory {
        /// Owner of the positions to get history for
        owner: RawAddr,
        /// Cursor to start from, for pagination
        cursor: Option<ClosedPositionCursor>,
        /// limit pagination
        limit: Option<u32>,
        /// order is default Descending
        order: Option<OrderInMessage>,
    },

    /// * returns [cosmwasm_std::QueryResponse]
    ///
    /// Nft proxy messages. Not meant to be called directly
    /// but rather for internal cross-contract calls
    ///
    /// however, these are merely queries, and can be called by anyone
    /// and clients may take advantage of this to save query gas
    /// by calling the market directly
    #[returns(cosmwasm_std::QueryResponse)]
    NftProxy {
        /// NFT message to process
        nft_msg: crate::contracts::position_token::entry::QueryMsg,
    },

    /// * returns [cosmwasm_std::QueryResponse]
    ///
    /// Liquidity token cw20 proxy messages. Not meant to be called directly
    /// but rather for internal cross-contract calls
    ///
    /// however, these are merely queries, and can be called by anyone
    /// and clients may take advantage of this to save query gas
    /// by calling the market directly
    #[returns(cosmwasm_std::QueryResponse)]
    LiquidityTokenProxy {
        /// Whether to query LP or xLP tokens
        kind: LiquidityTokenKind,
        /// Query to run
        msg: crate::contracts::liquidity_token::entry::QueryMsg,
    },

    /// * returns [TradeHistorySummary] for a given wallet addr
    #[returns(TradeHistorySummary)]
    TradeHistorySummary {
        /// Which wallet's history are we querying?
        addr: RawAddr,
    },

    /// * returns [PositionActionHistoryResp]
    #[returns(PositionActionHistoryResp)]
    PositionActionHistory {
        /// Which position's history are we querying?
        id: PositionId,
        /// Last action ID we saw
        start_after: Option<String>,
        /// How many actions to query
        limit: Option<u32>,
        /// Order to sort by
        order: Option<OrderInMessage>,
    },

    /// Actions taken by a trader.
    ///
    /// Similar to [Self::PositionActionHistory], but provides all details for
    /// an individual trader, not an individual position.
    ///
    /// * returns [TraderActionHistoryResp]
    #[returns(TraderActionHistoryResp)]
    TraderActionHistory {
        /// Which trader's history are we querying?
        owner: RawAddr,
        /// Last action ID we saw
        start_after: Option<String>,
        /// How many actions to query
        limit: Option<u32>,
        /// Order to sort by
        order: Option<OrderInMessage>,
    },

    /// * returns [LpActionHistoryResp]
    #[returns(LpActionHistoryResp)]
    LpActionHistory {
        /// Which provider's history are we querying?
        addr: RawAddr,
        /// Last action ID we saw
        start_after: Option<String>,
        /// How many actions to query
        limit: Option<u32>,
        /// Order to sort by
        order: Option<OrderInMessage>,
    },

    /// * returns [LimitOrderHistoryResp]
    ///
    /// Provides information on triggered limit orders.
    #[returns(LimitOrderHistoryResp)]
    LimitOrderHistory {
        /// Trader's address for history we are querying
        addr: RawAddr,
        /// Last order ID we saw
        start_after: Option<String>,
        /// How many orders to query
        limit: Option<u32>,
        /// Order to sort the order IDs by
        order: Option<OrderInMessage>,
    },

    /// * returns [LpInfoResp]
    ///
    /// Provides the data needed by the earn page.
    #[returns(LpInfoResp)]
    LpInfo {
        /// Which provider's information are we querying?
        liquidity_provider: RawAddr,
    },

    /// * returns [ReferralStatsResp]
    ///
    /// Returns the referral rewards generated and received by this wallet.
    #[returns(ReferralStatsResp)]
    ReferralStats {
        /// Which address to check
        addr: RawAddr,
    },

    /// * returns [DeltaNeutralityFeeResp]
    ///
    /// Gets the delta neutrality fee
    /// at the current price, for a given change in terms of net notional
    #[returns(DeltaNeutralityFeeResp)]
    DeltaNeutralityFee {
        /// the amount of notional that would be changed
        notional_delta: Signed<Notional>,
        /// for real delta neutrality fees, this is calculated internally
        /// should only be supplied if querying the fee for close or update
        pos_delta_neutrality_fee_margin: Option<Collateral>,
    },

    /// Check if a price update would trigger a liquidation/take profit/etc.
    ///
    /// * returns [PriceWouldTriggerResp]
    #[returns(PriceWouldTriggerResp)]
    PriceWouldTrigger {
        /// The new price of the base asset in terms of quote
        price: PriceBaseInQuote,
    },

    /// Enumerate deferred execution work items for the given trader.
    ///
    /// Always begins enumeration from the most recent.
    ///
    /// * returns [ListDeferredExecsResp]
    #[returns(crate::contracts::market::deferred_execution::ListDeferredExecsResp)]
    ListDeferredExecs {
        /// Trader wallet address
        addr: RawAddr,
        /// Previously seen final ID.
        start_after: Option<DeferredExecId>,
        /// How many items to request per batch.
        limit: Option<u32>,
    },

    /// Get a single deferred execution item, if available.
    ///
    /// * returns [GetDeferredExecResp]
    #[returns(crate::contracts::market::deferred_execution::GetDeferredExecResp)]
    GetDeferredExec {
        /// ID
        id: DeferredExecId,
    },
}

/// Response for [QueryMsg::OraclePrice]
#[cw_serde]
pub struct OraclePriceResp {
    /// A map of each pyth id used in this market to the price and publish time
    pub pyth: BTreeMap<PriceIdentifier, OraclePriceFeedPythResp>,
    /// A map of each sei denom used in this market to the price
    pub sei: BTreeMap<String, OraclePriceFeedSeiResp>,
    /// A map of each stride denom used in this market to the redemption price
    pub stride: BTreeMap<String, OraclePriceFeedStrideResp>,
    /// A map of each simple contract used in this market to the contract price
    #[serde(default)]
    pub simple: BTreeMap<RawAddr, OraclePriceFeedSimpleResp>,
    /// The final, composed price. See [QueryMsg::OraclePrice] for more information about this value
    pub composed_price: PricePoint,
}

/// Part of [OraclePriceResp]
#[cw_serde]
pub struct OraclePriceFeedPythResp {
    /// The pyth price
    pub price: NumberGtZero,
    /// The pyth publish time
    pub publish_time: Timestamp,
    /// Is this considered a volatile feed?
    pub volatile: bool,
}

/// Part of [OraclePriceResp]
#[cw_serde]
pub struct OraclePriceFeedSeiResp {
    /// The Sei price
    pub price: NumberGtZero,
    /// The Sei publish time
    pub publish_time: Timestamp,
    /// Is this considered a volatile feed?
    pub volatile: bool,
}

/// Part of [OraclePriceResp]
#[cw_serde]
pub struct OraclePriceFeedStrideResp {
    /// The redemption rate
    pub redemption_rate: NumberGtZero,
    /// The redemption price publish time
    pub publish_time: Timestamp,
    /// Is this considered a volatile feed?
    pub volatile: bool,
}

/// Part of [OraclePriceResp]
#[cw_serde]
pub struct OraclePriceFeedSimpleResp {
    /// The price value
    pub value: NumberGtZero,
    /// The block info when this price was set
    pub block_info: BlockInfo,
    /// Optional timestamp for the price, independent of block_info.time
    pub timestamp: Option<Timestamp>,
    /// Is this considered a volatile feed?
    #[serde(default)]
    pub volatile: bool,
}

/// When querying an open position, how do we calculate PnL vis-a-vis fees?
#[cw_serde]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Eq)]
pub enum PositionsQueryFeeApproach {
    /// Do not include any pending fees
    NoFees,
    /// Include accumulated fees (borrow and funding rates), but do not include future fees (specifically DNF).
    Accumulated,
    /// Include the DNF fee in addition to accumulated fees.
    ///
    /// This gives an idea of "what will be my PnL if I close my position right
    /// now." To keep compatibility with previous contract APIs, this is the
    /// default behavior. However, going forward, `Accumulated` should be
    /// preferred, and will eventually become the default.
    AllFees,
}

/// Placeholder migration message
#[cw_serde]
pub struct MigrateMsg {}

/// The summary for trade history
#[cw_serde]
#[derive(Default)]
pub struct TradeHistorySummary {
    /// Given in usd
    pub trade_volume: Usd,
    /// Given in usd
    pub realized_pnl: Signed<Usd>,
}

/// Response for [QueryMsg::PositionActionHistory]
#[cw_serde]
pub struct PositionActionHistoryResp {
    /// list of position actions that happened historically
    pub actions: Vec<PositionAction>,
    /// Next start_after value to continue pagination
    ///
    /// None means no more pagination
    pub next_start_after: Option<String>,
}

/// Response for [QueryMsg::TraderActionHistory]
#[cw_serde]
pub struct TraderActionHistoryResp {
    /// list of position actions that this trader performed
    pub actions: Vec<PositionAction>,
    /// Next start_after value to continue pagination
    ///
    /// None means no more pagination
    pub next_start_after: Option<String>,
}

/// A distinct position history action
#[cw_serde]
pub struct PositionAction {
    /// ID of the position impacted
    ///
    /// For ease of migration, we allow for a missing position ID.
    pub id: Option<PositionId>,
    /// Kind of action taken by the trader
    pub kind: PositionActionKind,
    /// Timestamp when the action occurred
    pub timestamp: Timestamp,
    /// Timestamp of the PricePoint used for this action, if relevant
    pub price_timestamp: Option<Timestamp>,
    /// the amount of collateral at the time of the action
    #[serde(alias = "active_collateral")]
    pub collateral: Collateral,
    /// The amount of collateral transferred to or from the trader
    #[serde(default)]
    pub transfer_collateral: Signed<Collateral>,
    /// Leverage of the position at the time of the action, if relevant
    pub leverage: Option<LeverageToBase>,
    /// max gains in quote
    pub max_gains: Option<MaxGainsInQuote>,
    /// the trade fee in USD
    pub trade_fee: Option<Usd>,
    /// The delta neutrality fee paid (or, if negative, received) in USD
    pub delta_neutrality_fee: Option<Signed<Usd>>,
    /// If this is a position transfer, the previous owner.
    pub old_owner: Option<Addr>,
    /// If this is a position transfer, the new owner.
    pub new_owner: Option<Addr>,
    /// The take profit price set by the trader.
    /// For historical reasons this is optional, i.e. if the trader had set max gains price instead
    #[serde(rename = "take_profit_override")]
    pub take_profit_trader: Option<TakeProfitTrader>,
    /// The stop loss override, if set.
    pub stop_loss_override: Option<PriceBaseInQuote>,
}

/// Action taken by trader for a [PositionAction]
#[cw_serde]
pub enum PositionActionKind {
    /// Open a new position
    Open,
    /// Updated an existing position
    Update,
    /// Close a position
    Close,
    /// Position was transferred between wallets
    //FIXME need distinct "Received Position" and "Sent Position" cases
    Transfer,
}

//todo does it make sense for PositionActionKind to impl Display
impl Display for PositionActionKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let str = match self {
            PositionActionKind::Open => "Open",
            PositionActionKind::Update => "Update",
            PositionActionKind::Close => "Close",
            PositionActionKind::Transfer => "Transfer",
        };

        f.write_str(str)
    }
}

/// Returned by [QueryMsg::LpInfo]
#[cw_serde]
pub struct LpInfoResp {
    /// This LP amount includes both actual LP tokens and xLP unstaked to LP but
    /// not yet collected.
    pub lp_amount: LpToken,
    /// Collateral backing the LP tokens
    pub lp_collateral: Collateral,
    /// This shows the balance of xLP minus any xLP already unstaked.
    pub xlp_amount: LpToken,
    /// Collateral backing the xLP tokens
    pub xlp_collateral: Collateral,
    /// Total available yield, sum of the available LP, xLP, crank rewards, and referral rewards.
    pub available_yield: Collateral,
    /// Available yield from LP tokens
    pub available_yield_lp: Collateral,
    /// Available yield from xLP tokens
    pub available_yield_xlp: Collateral,
    /// Available crank rewards
    pub available_crank_rewards: Collateral,
    #[serde(default)]
    /// Available referrer rewards
    pub available_referrer_rewards: Collateral,
    /// Current status of an unstaking, if under way
    ///
    /// This will return `Some` from the time the provider begins an unstaking process until either:
    ///
    /// 1. They either cancel it, _or_
    /// 2. They unstake all request xLP into LP _and_ collect that LP within the contract.
    pub unstaking: Option<UnstakingStatus>,
    /// Historical information on LP activity
    pub history: LpHistorySummary,
    /// Liquidity cooldown information, if active.
    pub liquidity_cooldown: Option<LiquidityCooldown>,
}

/// Returned by [QueryMsg::ReferralStats]
#[cw_serde]
#[derive(Default)]
pub struct ReferralStatsResp {
    /// Rewards generated by this wallet, in collateral.
    pub generated: Collateral,
    /// Rewards generated by this wallet, converted to USD at time of generation.
    pub generated_usd: Usd,
    /// Rewards received by this wallet, in collateral.
    pub received: Collateral,
    /// Rewards received by this wallet, converted to USD at time of generation.
    pub received_usd: Usd,
    /// Total number of referees associated with this wallet.
    ///
    /// Note that this is a factory-wide value. It will be identical
    /// across all markets for a given address.
    pub referees: u32,
    /// Who referred this account, if anyone.
    pub referrer: Option<Addr>,
}

/// When a liquidity cooldown period will end
#[cw_serde]
pub struct LiquidityCooldown {
    /// Timestamp when it will end
    pub at: Timestamp,
    /// Number of seconds until it will end
    pub seconds: u64,
}

/// Status of an ongoing unstaking process.
#[cw_serde]
pub struct UnstakingStatus {
    /// When the unstaking began
    pub start: Timestamp,
    /// This will be in the future if unstaking is incomplete
    pub end: Timestamp,
    /// Total amount requested to be unstaked
    ///
    /// Note that this value must be the sum of collected, available, and pending.
    pub xlp_unstaking: NonZero<LpToken>,
    /// Collateral, at current exchange rate, underlying the [UnstakingStatus::xlp_unstaking]
    pub xlp_unstaking_collateral: Collateral,
    /// Total amount of LP tokens that have been unstaked and collected
    pub collected: LpToken,
    /// Total amount of LP tokens that have been unstaked and not yet collected
    pub available: LpToken,
    /// Total amount of xLP tokens that are still pending unstaking
    pub pending: LpToken,
}

/// The summary for LP history
#[cw_serde]
#[derive(Default)]
pub struct LpHistorySummary {
    /// How much collateral was deposited in total
    pub deposit: Collateral,
    /// Value of the collateral in USD at time of deposit
    #[serde(alias = "deposit_in_usd")]
    pub deposit_usd: Usd,
    /// Cumulative yield claimed by the provider
    ///
    /// Note that this field includes crank and referral rewards.
    pub r#yield: Collateral,
    /// Cumulative yield expressed in USD at time of claiming
    ///
    /// Note that this field includes crank and referral rewards.
    #[serde(alias = "yield_in_usd")]
    pub yield_usd: Usd,
}

/// Response for [QueryMsg::LpActionHistory]
#[cw_serde]
pub struct LpActionHistoryResp {
    /// list of earn actions that happened historically
    pub actions: Vec<LpAction>,
    /// Next start_after value to continue pagination
    ///
    /// None means no more pagination
    pub next_start_after: Option<String>,
}

/// A distinct lp history action
#[cw_serde]
pub struct LpAction {
    /// Kind of action
    pub kind: LpActionKind,
    /// When the action happened
    pub timestamp: Timestamp,
    /// How many tokens were involved, if relevant
    pub tokens: Option<LpToken>,
    /// Amount of collateral
    pub collateral: Collateral,
    /// Value of that collateral in USD at the time
    #[serde(alias = "collateral_in_usd")]
    pub collateral_usd: Usd,
}

/// Kind of action for a [LpAction].
#[cw_serde]
pub enum LpActionKind {
    /// via [ExecuteMsg::DepositLiquidity]
    DepositLp,
    /// via [ExecuteMsg::DepositLiquidity]
    DepositXlp,
    /// via [ExecuteMsg::ReinvestYield]
    ReinvestYieldLp,
    /// via [ExecuteMsg::ReinvestYield]
    ReinvestYieldXlp,
    /// via [ExecuteMsg::UnstakeXlp]
    /// the amount of collateral is determined by the time they send their message
    /// [ExecuteMsg::CollectUnstakedLp] is *not* accounted for here
    UnstakeXlp,
    /// Some amount of unstaked LP has been collected into actual LP.
    CollectLp,
    /// via [ExecuteMsg::WithdrawLiquidity]
    Withdraw,
    /// via [ExecuteMsg::ClaimYield]
    ClaimYield,
}

#[cw_serde]
/// Return value from [QueryMsg::LimitOrder].
pub struct LimitOrderResp {
    /// The order identifier
    pub order_id: OrderId,
    /// The price at which the order will trigger
    pub trigger_price: PriceBaseInQuote,
    /// Amount of deposit collateral on the order
    pub collateral: NonZero<Collateral>,
    /// Leverage to open the position at
    pub leverage: LeverageToBase,
    /// Direction of the new position
    pub direction: DirectionToBase,
    /// Max gains of the new position
    #[deprecated(note = "Use take_profit instead")]
    pub max_gains: Option<MaxGainsInQuote>,
    /// Stop loss of the new position
    pub stop_loss_override: Option<PriceBaseInQuote>,
    #[serde(alias = "take_profit_override")]
    /// Take profit of the new position
    pub take_profit: TakeProfitTrader,
}

/// Response for [QueryMsg::LimitOrders]
#[cw_serde]
pub struct LimitOrdersResp {
    /// The list of limit orders
    pub orders: Vec<LimitOrderResp>,
    /// Next start_after value to continue pagination
    ///
    /// None means no more pagination
    pub next_start_after: Option<OrderId>,
}

/// Response for [QueryMsg::DeltaNeutralityFee]
#[cw_serde]
pub struct DeltaNeutralityFeeResp {
    /// the amount charged
    pub amount: Signed<Collateral>,
    /// the amount in the fund currently
    pub fund_total: Collateral,
    /// Expected effective price after slippage, can be used for the slippage assert.
    pub slippage_assert_price: PriceBaseInQuote,
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for QueryMsg {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Self::arbitrary_with_user(u, None)
    }
}

#[cfg(feature = "arbitrary")]
impl QueryMsg {
    /// Generate an arbitrary [QueryMsg] using the given default user address.
    pub fn arbitrary_with_user(
        u: &mut arbitrary::Unstructured<'_>,
        user: Option<RawAddr>,
    ) -> arbitrary::Result<Self> {
        let user_arb = |u: &mut arbitrary::Unstructured<'_>| -> arbitrary::Result<RawAddr> {
            match user {
                Some(user) => Ok(user),
                None => u.arbitrary(),
            }
        };
        // only allow messages for *this* contract - no proxies or cw20 submessages

        // prior art for this approach: https://github.com/rust-fuzz/arbitrary/blob/061ca86be699faf1fb584dd7a7843b3541cd5f2c/src/lib.rs#L724
        match u.int_in_range::<u8>(0..=11)? {
            0 => Ok(Self::Version {}),
            1 => Ok(Self::Status {
                price: u.arbitrary()?,
            }),
            2 => Ok(Self::SpotPrice {
                timestamp: u.arbitrary()?,
            }),
            3 => Ok(Self::Positions {
                position_ids: u.arbitrary()?,
                skip_calc_pending_fees: u.arbitrary()?,
                fees: u.arbitrary()?,
                price: u.arbitrary()?,
            }),

            4 => Ok(Self::LimitOrder {
                order_id: u.arbitrary()?,
            }),

            5 => Ok(Self::LimitOrders {
                owner: user_arb(u)?,
                start_after: u.arbitrary()?,
                limit: u.arbitrary()?,
                order: u.arbitrary()?,
            }),

            6 => Ok(Self::ClosedPositionHistory {
                owner: user_arb(u)?,
                cursor: u.arbitrary()?,
                limit: u.arbitrary()?,
                order: u.arbitrary()?,
            }),

            7 => Ok(Self::TradeHistorySummary { addr: user_arb(u)? }),

            8 => Ok(Self::PositionActionHistory {
                id: u.arbitrary()?,
                start_after: u.arbitrary()?,
                limit: u.arbitrary()?,
                order: u.arbitrary()?,
            }),

            9 => Ok(Self::LpActionHistory {
                addr: user_arb(u)?,
                start_after: u.arbitrary()?,
                limit: u.arbitrary()?,
                order: u.arbitrary()?,
            }),

            10 => Ok(Self::LpInfo {
                liquidity_provider: user_arb(u)?,
            }),

            11 => Ok(Self::DeltaNeutralityFee {
                notional_delta: u.arbitrary()?,
                pos_delta_neutrality_fee_margin: u.arbitrary()?,
            }),

            _ => unreachable!(),
        }
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for ExecuteMsg {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        // only allow messages for *this* contract - no proxies or cw20 submessages

        // prior art for this approach: https://github.com/rust-fuzz/arbitrary/blob/061ca86be699faf1fb584dd7a7843b3541cd5f2c/src/lib.rs#L724
        match u.int_in_range::<u8>(0..=23)? {
            //0 => Ok(ExecuteMsg::Owner(u.arbitrary()?)),
            0 => Ok(ExecuteMsg::Owner(ExecuteOwnerMsg::ConfigUpdate {
                update: Box::<ConfigUpdate>::default(),
            })),
            1 => Ok(ExecuteMsg::OpenPosition {
                slippage_assert: u.arbitrary()?,
                leverage: u.arbitrary()?,
                direction: u.arbitrary()?,
                max_gains: None,
                stop_loss_override: u.arbitrary()?,
                take_profit: Some(u.arbitrary()?),
            }),
            2 => Ok(ExecuteMsg::UpdatePositionAddCollateralImpactLeverage { id: u.arbitrary()? }),
            3 => Ok(ExecuteMsg::UpdatePositionAddCollateralImpactSize {
                id: u.arbitrary()?,
                slippage_assert: u.arbitrary()?,
            }),
            4 => Ok(ExecuteMsg::UpdatePositionRemoveCollateralImpactLeverage {
                id: u.arbitrary()?,
                amount: u.arbitrary()?,
            }),
            5 => Ok(ExecuteMsg::UpdatePositionRemoveCollateralImpactSize {
                id: u.arbitrary()?,
                amount: u.arbitrary()?,
                slippage_assert: u.arbitrary()?,
            }),
            6 => Ok(ExecuteMsg::UpdatePositionLeverage {
                id: u.arbitrary()?,
                leverage: u.arbitrary()?,
                slippage_assert: u.arbitrary()?,
            }),
            7 => Ok(ExecuteMsg::UpdatePositionMaxGains {
                id: u.arbitrary()?,
                max_gains: u.arbitrary()?,
            }),
            #[allow(deprecated)]
            8 => Ok(ExecuteMsg::SetTriggerOrder {
                id: u.arbitrary()?,
                stop_loss_override: u.arbitrary()?,
                take_profit: u.arbitrary()?,
            }),
            9 => Ok(ExecuteMsg::PlaceLimitOrder {
                trigger_price: u.arbitrary()?,
                leverage: u.arbitrary()?,
                direction: u.arbitrary()?,
                max_gains: None,
                stop_loss_override: u.arbitrary()?,
                take_profit: Some(u.arbitrary()?),
            }),
            10 => Ok(ExecuteMsg::CancelLimitOrder {
                order_id: u.arbitrary()?,
            }),
            11 => Ok(ExecuteMsg::ClosePosition {
                id: u.arbitrary()?,
                slippage_assert: u.arbitrary()?,
            }),
            12 => Ok(ExecuteMsg::DepositLiquidity {
                stake_to_xlp: u.arbitrary()?,
            }),
            13 => Ok(ExecuteMsg::ReinvestYield {
                stake_to_xlp: u.arbitrary()?,
                amount: None,
            }),
            14 => Ok(ExecuteMsg::WithdrawLiquidity {
                lp_amount: u.arbitrary()?,
            }),
            15 => Ok(ExecuteMsg::ClaimYield {}),
            16 => Ok(ExecuteMsg::StakeLp {
                amount: u.arbitrary()?,
            }),
            17 => Ok(ExecuteMsg::UnstakeXlp {
                amount: u.arbitrary()?,
            }),
            18 => Ok(ExecuteMsg::StopUnstakingXlp {}),
            19 => Ok(ExecuteMsg::CollectUnstakedLp {}),
            20 => Ok(ExecuteMsg::Crank {
                execs: u.arbitrary()?,
                rewards: None,
            }),

            21 => Ok(ExecuteMsg::TransferDaoFees {}),

            22 => Ok(ExecuteMsg::CloseAllPositions {}),
            23 => Ok(ExecuteMsg::ProvideCrankFunds {}),

            _ => unreachable!(),
        }
    }
}

/// Overall market status information
///
/// Returned from [QueryMsg::Status]
#[cw_serde]
pub struct StatusResp {
    /// This market's identifier
    pub market_id: MarketId,
    /// Base asset
    pub base: String,
    /// Quote asset
    pub quote: String,
    /// Type of market
    pub market_type: MarketType,
    /// The asset used for collateral within the system
    pub collateral: crate::token::Token,
    /// Config for this market
    pub config: super::config::Config,
    /// Current status of the liquidity pool
    pub liquidity: super::liquidity::LiquidityStats,
    /// Next bit of crank work available, if any
    pub next_crank: Option<CrankWorkInfo>,
    /// Timestamp of the last completed crank
    pub last_crank_completed: Option<Timestamp>,
    /// Earliest deferred execution price timestamp needed
    pub next_deferred_execution: Option<Timestamp>,
    /// Latest deferred execution price timestamp needed
    pub newest_deferred_execution: Option<Timestamp>,
    /// Next liquifunding work item timestamp
    pub next_liquifunding: Option<Timestamp>,
    /// Number of work items sitting in the deferred execution queue
    pub deferred_execution_items: u32,
    /// Last processed deferred execution ID, if any
    pub last_processed_deferred_exec_id: Option<DeferredExecId>,
    /// Overall borrow fee rate (annualized), combining LP and xLP
    pub borrow_fee: Decimal256,
    /// LP component of [Self::borrow_fee]
    pub borrow_fee_lp: Decimal256,
    /// xLP component of [Self::borrow_fee]
    pub borrow_fee_xlp: Decimal256,
    /// Long funding rate (annualized)
    pub long_funding: Number,
    /// Short funding rate (annualized)
    pub short_funding: Number,

    /// Total long interest, given in the notional asset.
    pub long_notional: Notional,
    /// Total short interest, given in the notional asset.
    pub short_notional: Notional,

    /// Total long interest, given in USD, converted at the current exchange rate.
    pub long_usd: Usd,
    /// Total short interest, given in USD, converted at the current exchange rate.
    pub short_usd: Usd,

    /// Instant delta neutrality fee value
    ///
    /// This is based on net notional and the sensitivity parameter
    pub instant_delta_neutrality_fee_value: Signed<Decimal256>,

    /// Amount of collateral in the delta neutrality fee fund.
    pub delta_neutrality_fee_fund: Collateral,

    /// Fees held by the market contract
    pub fees: Fees,
}

/// Response for [QueryMsg::LimitOrderHistory]
#[cw_serde]
pub struct LimitOrderHistoryResp {
    /// list of triggered limit orders that happened historically
    pub orders: Vec<ExecutedLimitOrder>,
    /// Next start_after value to continue pagination
    ///
    /// None means no more pagination
    pub next_start_after: Option<String>,
}

/// History information on a limit order which was triggered.
#[cw_serde]
pub struct ExecutedLimitOrder {
    /// The order itself
    pub order: LimitOrder,
    /// The result of triggering the order
    pub result: LimitOrderResult,
    /// When the order was triggered
    pub timestamp: Timestamp,
}

/// The result of triggering a limit order
#[cw_serde]
pub enum LimitOrderResult {
    /// Position was opened successfully
    Success {
        /// New position ID
        position: PositionId,
    },
    /// Position failed to open
    Failure {
        /// Error message
        reason: String,
    },
}

/// Response for [QueryMsg::SpotPriceHistory]
#[cw_serde]
pub struct SpotPriceHistoryResp {
    /// list of historical price points
    pub price_points: Vec<PricePoint>,
}

/// Would a price update trigger a liquidation/take profit/etc?
#[cw_serde]
pub struct PriceWouldTriggerResp {
    /// Would a price update trigger a liquidation/take profit/etc?
    pub would_trigger: bool,
}

/// String representation of remove.
const REMOVE_STR: &str = "remove";
/// Stop loss configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StopLoss {
    /// Remove stop loss price for the position
    Remove,
    /// Set the stop loss price for the position
    Price(PriceBaseInQuote),
}

impl FromStr for StopLoss {
    type Err = PerpError;
    fn from_str(src: &str) -> Result<StopLoss, PerpError> {
        match src {
            REMOVE_STR => Ok(StopLoss::Remove),
            _ => match src.parse() {
                Ok(number) => Ok(StopLoss::Price(number)),
                Err(err) => Err(perp_error!(
                    ErrorId::Conversion,
                    ErrorDomain::Default,
                    "error converting {} to StopLoss , {}",
                    src,
                    err
                )),
            },
        }
    }
}

impl TryFrom<&str> for StopLoss {
    type Error = PerpError;

    fn try_from(val: &str) -> Result<Self, Self::Error> {
        Self::from_str(val)
    }
}

impl serde::Serialize for StopLoss {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            StopLoss::Price(price) => price.serialize(serializer),
            StopLoss::Remove => serializer.serialize_str(REMOVE_STR),
        }
    }
}

impl<'de> serde::Deserialize<'de> for StopLoss {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(StopLossVisitor)
    }
}

impl JsonSchema for StopLoss {
    fn schema_name() -> String {
        "StopLoss".to_owned()
    }

    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        SchemaObject {
            instance_type: Some(InstanceType::String.into()),
            format: Some("stop-loss".to_owned()),
            ..Default::default()
        }
        .into()
    }
}

struct StopLossVisitor;
impl<'de> serde::de::Visitor<'de> for StopLossVisitor {
    type Value = StopLoss;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("StopLoss")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        v.parse()
            .map_err(|_| E::custom(format!("Invalid StopLoss: {v}")))
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        if let Some((key, value)) = map.next_entry()? {
            match key {
                REMOVE_STR => Ok(Self::Value::Remove),
                "price" => Ok(Self::Value::Price(value)),
                _ => Err(serde::de::Error::custom(format!(
                    "Invalid StopLoss field: {key}"
                ))),
            }
        } else {
            Err(serde::de::Error::custom("Empty StopLoss Object"))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::StopLoss;

    #[test]
    fn deserialize_stop_loss() {
        let go = serde_json::from_str::<StopLoss>;

        go(r#"{"price": "2.2"}"#).unwrap();
        go("\"remove\"").unwrap();
        go("\"2.2\"").unwrap();
        go("\"-2.2\"").unwrap_err();
        go(r#"{}"#).unwrap_err();
        go(r#"{"error-field": "2.2"}"#).unwrap_err();
        go("").unwrap_err();
    }
}