1use crate::contracts::market::entry::NewCounterTradeParams;
3use crate::prelude::*;
4use crate::{
5 contracts::market::entry::{NewCopyTradingParams, NewMarketParams},
6 shutdown::{ShutdownEffect, ShutdownImpact},
7};
8use cosmwasm_schema::{cw_serde, QueryResponses};
9use cosmwasm_std::Addr;
10use cw_storage_plus::{KeyDeserialize, Prefixer, PrimaryKey};
11use schemars::JsonSchema;
12
13#[cw_serde]
15pub struct InstantiateMsg {
16 pub market_code_id: String,
18 pub position_token_code_id: String,
20 pub liquidity_token_code_id: String,
22 pub copy_trading_code_id: Option<String>,
24 pub counter_trade_code_id: Option<String>,
26 pub migration_admin: RawAddr,
28 pub owner: RawAddr,
30 pub dao: RawAddr,
32 pub kill_switch: RawAddr,
34 pub wind_down: RawAddr,
36 pub label_suffix: Option<String>,
38}
39
40#[allow(clippy::large_enum_variant)]
42#[cw_serde]
43pub enum ExecuteMsg {
44 AddMarket {
46 new_market: NewMarketParams,
48 },
49 SetMarketCodeId {
51 code_id: String,
53 },
54 SetPositionTokenCodeId {
56 code_id: String,
58 },
59 SetLiquidityTokenCodeId {
61 code_id: String,
63 },
64
65 SetOwner {
67 owner: RawAddr,
69 },
70
71 SetMigrationAdmin {
73 migration_admin: RawAddr,
75 },
76
77 SetDao {
79 dao: RawAddr,
81 },
82
83 SetKillSwitch {
85 kill_switch: RawAddr,
87 },
88
89 SetWindDown {
91 wind_down: RawAddr,
93 },
94
95 TransferAllDaoFees {},
97
98 Shutdown {
100 markets: Vec<MarketId>,
102 impacts: Vec<ShutdownImpact>,
104 effect: ShutdownEffect,
106 },
107
108 RegisterReferrer {
112 addr: RawAddr,
114 },
115 AddCopyTrading {
117 new_copy_trading: NewCopyTradingParams,
119 },
120 AddCounterTrade {
122 new_counter_trade: NewCounterTradeParams,
124 },
125 SetCopyTradingCodeId {
127 code_id: String,
129 },
130 SetCounterTradeCodeId {
132 code_id: String,
134 },
135 RemoveOwner {},
137}
138
139#[cw_serde]
143pub struct MarketsResp {
144 pub markets: Vec<MarketId>,
146}
147
148#[cw_serde]
152pub struct CopyTradingResp {
153 pub addresses: Vec<CopyTradingInfo>,
155}
156
157#[cw_serde]
161pub struct CounterTradeResp {
162 pub addresses: Vec<CounterTradeInfo>,
164}
165
166#[cw_serde]
168pub struct AddrIsContractResp {
169 pub is_contract: bool,
171 pub contract_type: Option<ContractType>,
173}
174
175#[cw_serde]
177pub enum ContractType {
178 Factory,
180 LiquidityToken,
182 PositionToken,
184 Market,
186 CopyTrading,
188 CounterTrade,
190 Vault,
192}
193
194pub const MARKETS_QUERY_LIMIT_DEFAULT: u32 = 15;
196
197pub const QUERY_LIMIT_DEFAULT: u32 = 15;
199
200#[cw_serde]
202#[derive(QueryResponses)]
203pub enum QueryMsg {
204 #[returns(cw2::ContractVersion)]
206 Version {},
207
208 #[returns(MarketsResp)]
212 Markets {
213 start_after: Option<MarketId>,
215 limit: Option<u32>,
217 },
218
219 #[returns(MarketInfoResponse)]
223 MarketInfo {
224 market_id: MarketId,
226 },
227
228 #[returns(AddrIsContractResp)]
232 AddrIsContract {
233 addr: RawAddr,
235 },
236
237 #[returns(FactoryOwnerResp)]
241 FactoryOwner {},
242
243 #[returns(ShutdownStatus)]
245 ShutdownStatus {
246 market_id: MarketId,
248 },
249
250 #[returns(CodeIds)]
252 CodeIds {},
253
254 #[returns(GetReferrerResp)]
258 GetReferrer {
259 addr: RawAddr,
261 },
262
263 #[returns(ListRefereesResp)]
267 ListReferees {
268 addr: RawAddr,
270 limit: Option<u32>,
272 start_after: Option<String>,
274 },
275
276 #[returns(ListRefereeCountResp)]
280 ListRefereeCount {
281 limit: Option<u32>,
283 start_after: Option<ListRefereeCountStartAfter>,
285 },
286
287 #[returns(CopyTradingResp)]
291 CopyTrading {
292 start_after: Option<CopyTradingInfoRaw>,
294 limit: Option<u32>,
296 },
297 #[returns(CopyTradingResp)]
301 CopyTradingForLeader {
302 leader: RawAddr,
304 start_after: Option<RawAddr>,
306 limit: Option<u32>,
308 },
309 #[returns(CounterTradeResp)]
311 CounterTrade {
312 start_after: Option<MarketId>,
314 limit: Option<u32>,
316 },
317}
318
319#[cw_serde]
321pub struct FactoryOwnerResp {
322 pub owner: Option<Addr>,
324 pub admin_migration: Addr,
326 pub dao: Addr,
328 pub kill_switch: Addr,
330 pub wind_down: Addr,
332}
333
334#[cw_serde]
336pub struct MigrateMsg {}
337
338#[cw_serde]
340pub struct MarketInfoResponse {
341 pub market_addr: Addr,
343 pub position_token: Addr,
345 pub liquidity_token_lp: Addr,
347 pub liquidity_token_xlp: Addr,
349}
350
351#[cw_serde]
353pub struct ShutdownStatus {
354 pub disabled: Vec<ShutdownImpact>,
356}
357
358impl ExecuteMsg {
359 pub fn requires_owner(&self) -> bool {
361 match self {
362 ExecuteMsg::AddMarket { .. } => true,
363 ExecuteMsg::SetMarketCodeId { .. } => true,
364 ExecuteMsg::SetPositionTokenCodeId { .. } => true,
365 ExecuteMsg::SetLiquidityTokenCodeId { .. } => true,
366 ExecuteMsg::SetCounterTradeCodeId { .. } => true,
367 ExecuteMsg::SetOwner { .. } => true,
368 ExecuteMsg::SetMigrationAdmin { .. } => true,
369 ExecuteMsg::SetDao { .. } => true,
370 ExecuteMsg::SetKillSwitch { .. } => true,
371 ExecuteMsg::SetWindDown { .. } => true,
372 ExecuteMsg::TransferAllDaoFees {} => true,
373 ExecuteMsg::RegisterReferrer { .. } => false,
374 ExecuteMsg::AddCounterTrade { .. } => false,
375 ExecuteMsg::Shutdown { .. } => false,
377 ExecuteMsg::AddCopyTrading { .. } => false,
378 ExecuteMsg::SetCopyTradingCodeId { .. } => true,
379 ExecuteMsg::RemoveOwner {} => true,
380 }
381 }
382}
383
384#[cw_serde]
386pub struct CodeIds {
387 pub market: Uint64,
389 pub position_token: Uint64,
391 pub liquidity_token: Uint64,
393 pub counter_trade: Option<Uint64>,
395}
396
397#[cw_serde]
399pub enum GetReferrerResp {
400 NoReferrer {},
402 HasReferrer {
404 referrer: Addr,
406 },
407}
408
409#[cw_serde]
411pub struct ListRefereesResp {
412 pub referees: Vec<Addr>,
414 pub next_start_after: Option<String>,
418}
419
420pub fn make_referrer_key(referee: &Addr) -> String {
424 format!("ref__{}", referee.as_str())
425}
426
427pub fn make_referee_count_key(referrer: &Addr) -> String {
431 format!("refcount__{}", referrer.as_str())
432}
433
434#[cw_serde]
436pub struct ListRefereeCountResp {
437 pub counts: Vec<RefereeCount>,
439 pub next_start_after: Option<ListRefereeCountStartAfter>,
443}
444
445#[cw_serde]
447pub struct RefereeCount {
448 pub referrer: Addr,
450 pub count: u32,
452}
453
454#[cw_serde]
456pub struct ListRefereeCountStartAfter {
457 pub referrer: RawAddr,
459 pub count: u32,
461}
462
463#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
464pub struct LeaderAddr(pub Addr);
466
467#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
468pub struct CopyTradingAddr(pub Addr);
470
471#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
472pub struct CopyTradingInfo {
474 pub leader: LeaderAddr,
476 pub contract: CopyTradingAddr,
478}
479
480#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
481pub struct CounterTradeInfo {
483 pub contract: CounterTradeAddr,
485 pub market_id: MarketId,
487}
488
489#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
490pub struct CopyTradingInfoRaw {
492 pub leader: RawAddr,
494 pub contract: RawAddr,
496}
497
498impl KeyDeserialize for LeaderAddr {
499 type Output = LeaderAddr;
500
501 const KEY_ELEMS: u16 = Addr::KEY_ELEMS;
502
503 fn from_vec(value: Vec<u8>) -> cosmwasm_std::StdResult<Self::Output> {
504 Addr::from_vec(value).map(LeaderAddr)
505 }
506}
507
508impl<'a> Prefixer<'a> for LeaderAddr {
509 fn prefix(&self) -> Vec<cw_storage_plus::Key> {
510 self.0.prefix()
511 }
512}
513
514impl<'a> PrimaryKey<'a> for LeaderAddr {
515 type Prefix = <Addr as PrimaryKey<'a>>::Prefix;
516 type SubPrefix = <Addr as PrimaryKey<'a>>::SubPrefix;
517 type Suffix = <Addr as PrimaryKey<'a>>::Suffix;
518 type SuperSuffix = <Addr as PrimaryKey<'a>>::SuperSuffix;
519
520 fn key(&self) -> Vec<cw_storage_plus::Key> {
521 self.0.key()
522 }
523}
524
525impl KeyDeserialize for CopyTradingAddr {
526 type Output = CopyTradingAddr;
527
528 const KEY_ELEMS: u16 = Addr::KEY_ELEMS;
529
530 fn from_vec(value: Vec<u8>) -> cosmwasm_std::StdResult<Self::Output> {
531 Addr::from_vec(value).map(CopyTradingAddr)
532 }
533}
534impl<'a> Prefixer<'a> for CopyTradingAddr {
535 fn prefix(&self) -> Vec<cw_storage_plus::Key> {
536 self.0.prefix()
537 }
538}
539
540impl<'a> PrimaryKey<'a> for CopyTradingAddr {
541 type Prefix = <Addr as PrimaryKey<'a>>::Prefix;
542 type SubPrefix = <Addr as PrimaryKey<'a>>::SubPrefix;
543 type Suffix = <Addr as PrimaryKey<'a>>::Suffix;
544 type SuperSuffix = <Addr as PrimaryKey<'a>>::SuperSuffix;
545
546 fn key(&self) -> Vec<cw_storage_plus::Key> {
547 self.0.key()
548 }
549}
550
551#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
552pub struct CounterTradeAddr(pub Addr);
554
555impl KeyDeserialize for CounterTradeAddr {
556 type Output = CounterTradeAddr;
557
558 const KEY_ELEMS: u16 = Addr::KEY_ELEMS;
559
560 fn from_vec(value: Vec<u8>) -> cosmwasm_std::StdResult<Self::Output> {
561 Addr::from_vec(value).map(CounterTradeAddr)
562 }
563}
564impl<'a> Prefixer<'a> for CounterTradeAddr {
565 fn prefix(&self) -> Vec<cw_storage_plus::Key> {
566 self.0.prefix()
567 }
568}
569
570impl<'a> PrimaryKey<'a> for CounterTradeAddr {
571 type Prefix = <Addr as PrimaryKey<'a>>::Prefix;
572 type SubPrefix = <Addr as PrimaryKey<'a>>::SubPrefix;
573 type Suffix = <Addr as PrimaryKey<'a>>::Suffix;
574 type SuperSuffix = <Addr as PrimaryKey<'a>>::SuperSuffix;
575
576 fn key(&self) -> Vec<cw_storage_plus::Key> {
577 self.0.key()
578 }
579}
580
581#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
582pub struct VaultAddr(pub Addr);
584
585impl KeyDeserialize for VaultAddr {
586 type Output = VaultAddr;
587
588 const KEY_ELEMS: u16 = Addr::KEY_ELEMS;
589
590 fn from_vec(value: Vec<u8>) -> cosmwasm_std::StdResult<Self::Output> {
591 Addr::from_vec(value).map(VaultAddr)
592 }
593}
594
595impl<'a> Prefixer<'a> for VaultAddr {
596 fn prefix(&self) -> Vec<cw_storage_plus::Key> {
597 self.0.prefix()
598 }
599}
600
601impl<'a> PrimaryKey<'a> for VaultAddr {
602 type Prefix = <Addr as PrimaryKey<'a>>::Prefix;
603 type SubPrefix = <Addr as PrimaryKey<'a>>::SubPrefix;
604 type Suffix = <Addr as PrimaryKey<'a>>::Suffix;
605 type SuperSuffix = <Addr as PrimaryKey<'a>>::SuperSuffix;
606
607 fn key(&self) -> Vec<cw_storage_plus::Key> {
608 self.0.key()
609 }
610}