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
//! Entrypoint messages for the factory
use crate::prelude::*;
use crate::{
    contracts::market::entry::{NewCopyTradingParams, NewMarketParams},
    shutdown::{ShutdownEffect, ShutdownImpact},
};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Addr;
use cw_storage_plus::{KeyDeserialize, Prefixer, PrimaryKey};
use schemars::JsonSchema;

/// Instantiate a new factory contract.
#[cw_serde]
pub struct InstantiateMsg {
    /// The code id for the market contract
    pub market_code_id: String,
    /// The code id for the position_token contract
    pub position_token_code_id: String,
    /// The code id for the liquidity_token contract
    pub liquidity_token_code_id: String,
    /// The code id for the copy trading contract
    pub copy_trading_code_id: Option<String>,
    /// Migration admin, needed for instantiating/migrating sub-contracts
    pub migration_admin: RawAddr,
    /// Perpetual swap admin address
    pub owner: RawAddr,
    /// DAO address
    pub dao: RawAddr,
    /// Kill switch address
    pub kill_switch: RawAddr,
    /// Wind down address
    pub wind_down: RawAddr,
    /// Suffix attached to all contracts instantiated by the factory
    pub label_suffix: Option<String>,
}

/// Execute a message on the factory.
#[allow(clippy::large_enum_variant)]
#[cw_serde]
pub enum ExecuteMsg {
    /// Add a new market
    AddMarket {
        /// Parameters for the new market
        new_market: NewMarketParams,
    },
    /// Set the market code id, i.e. if it's been migrated
    SetMarketCodeId {
        /// Code ID to use for future market contracts
        code_id: String,
    },
    /// Set the position token code id, i.e. if it's been migrated
    SetPositionTokenCodeId {
        /// Code ID to use for future position token contracts
        code_id: String,
    },
    /// Set the liquidity token code id, i.e. if it's been migrated
    SetLiquidityTokenCodeId {
        /// Code ID to use for future liquidity token contracts
        code_id: String,
    },

    /// Change the owner addr
    SetOwner {
        /// New owner
        owner: RawAddr,
    },

    /// Change the migration admin
    SetMigrationAdmin {
        /// New migration admin
        migration_admin: RawAddr,
    },

    /// Change the dao addr
    SetDao {
        /// New DAO
        dao: RawAddr,
    },

    /// Change the kill switch addr
    SetKillSwitch {
        /// New kill switch administrator
        kill_switch: RawAddr,
    },

    /// Change the wind down addr
    SetWindDown {
        /// New wind down administrator
        wind_down: RawAddr,
    },

    /// Convenience mechanism to transfer all dao fees from all markets
    TransferAllDaoFees {},

    /// Perform a shutdown on the given markets with the given impacts
    Shutdown {
        /// Which markets to impact? Empty list means impact all markets
        markets: Vec<MarketId>,
        /// Which impacts to have? Empty list means shut down all activities
        impacts: Vec<ShutdownImpact>,
        /// Are we disabling these impacts, or reenabling them?
        effect: ShutdownEffect,
    },

    /// Register a referrer for the given account.
    ///
    /// Can only be performed once.
    RegisterReferrer {
        /// The wallet address of the referrer
        addr: RawAddr,
    },
    /// Add new copy trading contract
    AddCopyTrading {
        /// Parameters for the contract
        new_copy_trading: NewCopyTradingParams,
    },
    /// Set the copy trading code id, i.e. if it's been migrated
    SetCopyTradingCodeId {
        /// Code ID to use for future copy trading contracts
        code_id: String,
    },
}

/// Response from [QueryMsg::Markets]
///
/// Use [QueryMsg::MarketInfo] for details on each market.
#[cw_serde]
pub struct MarketsResp {
    /// Markets maintained by this factory
    pub markets: Vec<MarketId>,
}

/// Response from [QueryMsg::Markets]
///
/// Use [QueryMsg::CopyTrading] for details on copy trading contract.
#[cw_serde]
pub struct CopyTradingResp {
    /// Copy trading contracts maintained by this factory
    pub addresses: Vec<CopyTradingInfo>,
}

/// Response from [QueryMsg::AddrIsContract]
#[cw_serde]
pub struct AddrIsContractResp {
    /// Boolean indicating whether this is a success for failure.
    pub is_contract: bool,
    /// If this is a contract: what type of contract is it?
    pub contract_type: Option<ContractType>,
}

/// The type of contract identified by [QueryMsg::AddrIsContract].
#[cw_serde]
pub enum ContractType {
    /// The factory contract
    Factory,
    /// An LP or xLP liquidity token proxy
    LiquidityToken,
    /// A position NFT proxy
    PositionToken,
    /// A market
    Market,
    /// Copy trading contract
    CopyTrading,
}

/// Default limit for [QueryMsg::Markets]
pub const MARKETS_QUERY_LIMIT_DEFAULT: u32 = 15;

/// Default limit for queries.
pub const QUERY_LIMIT_DEFAULT: u32 = 15;

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

    /// * returns [MarketsResp]
    ///
    /// All the markets
    #[returns(MarketsResp)]
    Markets {
        /// Last seen market ID in a [MarketsResp] for enumeration
        start_after: Option<MarketId>,
        /// Defaults to [MARKETS_QUERY_LIMIT_DEFAULT]
        limit: Option<u32>,
    },

    /// * returns [MarketInfoResponse]
    ///
    /// Combined query to get the market related addresses
    #[returns(MarketInfoResponse)]
    MarketInfo {
        /// Market ID to look up
        market_id: MarketId,
    },

    /// * returns [AddrIsContractResp]
    ///
    /// given an address, checks if it's any of the registered protocol contracts.
    #[returns(AddrIsContractResp)]
    AddrIsContract {
        /// Address to check
        addr: RawAddr,
    },

    /// * returns [FactoryOwnerResp]
    ///
    /// Returns information about the owners of the factory
    #[returns(FactoryOwnerResp)]
    FactoryOwner {},

    /// * returns [ShutdownStatus]
    #[returns(ShutdownStatus)]
    ShutdownStatus {
        /// Market to look up
        market_id: MarketId,
    },

    /// * returns [CodeIds]
    #[returns(CodeIds)]
    CodeIds {},

    /// Who referred this user, if anyone?
    ///
    /// * returns [GetReferrerResp]
    #[returns(GetReferrerResp)]
    GetReferrer {
        /// Referee address
        addr: RawAddr,
    },

    /// Enumerated query: who was referred by this user?
    ///
    /// * returns [ListRefereesResp]
    #[returns(ListRefereesResp)]
    ListReferees {
        /// Referrer address
        addr: RawAddr,
        /// How many addresses to return at once
        limit: Option<u32>,
        /// Taken from [ListRefereesResp::next_start_after]
        start_after: Option<String>,
    },

    /// Enumerated query: referee counts for all referrers.
    ///
    /// * returns [ListRefereeCountResp]
    #[returns(ListRefereeCountResp)]
    ListRefereeCount {
        /// How many records to return at once
        limit: Option<u32>,
        /// Take from [ListRefereeCountResp::next_start_after]
        start_after: Option<ListRefereeCountStartAfter>,
    },

    /// Fetch copy trading contracts
    ///
    /// Returns [CopyTradingResp]
    #[returns(CopyTradingResp)]
    CopyTrading {
        /// Last seen [CopyTradingInfo] in a [CopyTradingResp] for enumeration
        start_after: Option<CopyTradingInfoRaw>,
        /// Defaults to [QUERY_LIMIT_DEFAULT]
        limit: Option<u32>,
    },
    /// Fetch copy trading contract belonging to a specfic leader
    ///
    /// Returns [CopyTradingResp]
    #[returns(CopyTradingResp)]
    CopyTradingForLeader {
        /// Leader of the contract
        leader: RawAddr,
        /// Last seen copy trading contract address for enumeration
        start_after: Option<RawAddr>,
        /// Defaults to [QUERY_LIMIT_DEFAULT]
        limit: Option<u32>,
    },
}

/// Information on owners and other protocol-wide special addresses
#[cw_serde]
pub struct FactoryOwnerResp {
    /// Owner of the factory
    pub owner: Addr,
    /// Migration admin of the factory
    pub admin_migration: Addr,
    /// Wallet that receives DAO/protocol fees for all markets
    pub dao: Addr,
    /// Wallet that can activate kill switch shutdowns
    pub kill_switch: Addr,
    /// Wallet that can activate market wind downs
    pub wind_down: Addr,
}

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

/// Information about a specific market, returned from [QueryMsg::MarketInfo].
#[cw_serde]
pub struct MarketInfoResponse {
    /// Address of the market
    pub market_addr: Addr,
    /// Address of the position token
    pub position_token: Addr,
    /// Address of the LP liquidity token
    pub liquidity_token_lp: Addr,
    /// Address of the xLP liquidity token
    pub liquidity_token_xlp: Addr,
}

/// Return value from [QueryMsg::Shutdown]
#[cw_serde]
pub struct ShutdownStatus {
    /// Any parts of the market which have been disabled.
    pub disabled: Vec<ShutdownImpact>,
}

impl ExecuteMsg {
    /// Does this message require owner permissions?
    pub fn requires_owner(&self) -> bool {
        match self {
            ExecuteMsg::AddMarket { .. } => true,
            ExecuteMsg::SetMarketCodeId { .. } => true,
            ExecuteMsg::SetPositionTokenCodeId { .. } => true,
            ExecuteMsg::SetLiquidityTokenCodeId { .. } => true,
            ExecuteMsg::SetOwner { .. } => true,
            ExecuteMsg::SetMigrationAdmin { .. } => true,
            ExecuteMsg::SetDao { .. } => true,
            ExecuteMsg::SetKillSwitch { .. } => true,
            ExecuteMsg::SetWindDown { .. } => true,
            ExecuteMsg::TransferAllDaoFees {} => true,
            ExecuteMsg::RegisterReferrer { .. } => false,
            // Uses its own auth mechanism internally
            ExecuteMsg::Shutdown { .. } => false,
            ExecuteMsg::AddCopyTrading { .. } => false,
            ExecuteMsg::SetCopyTradingCodeId { .. } => true,
        }
    }
}

/// Which code IDs are currently set for new markets
#[cw_serde]
pub struct CodeIds {
    /// Market code ID
    pub market: Uint64,
    /// Position token proxy code ID
    pub position_token: Uint64,
    /// Liquidity token proxy code ID
    pub liquidity_token: Uint64,
}

/// Response from [QueryMsg::GetReferrer]
#[cw_serde]
pub enum GetReferrerResp {
    /// No referrer registered
    NoReferrer {},
    /// Has a registered referrer
    HasReferrer {
        /// Referrer address
        referrer: Addr,
    },
}

/// Response from [QueryMsg::ListReferees]
#[cw_serde]
pub struct ListRefereesResp {
    /// Next batch of referees
    pub referees: Vec<Addr>,
    /// Next value to start after
    ///
    /// Returns `None` if we've seen all referees
    pub next_start_after: Option<String>,
}

/// Make a lookup key for the given referee
///
/// We don't follow the normal Map pattern to simplify raw queries.
pub fn make_referrer_key(referee: &Addr) -> String {
    format!("ref__{}", referee.as_str())
}

/// Make a lookup key for the count of referees for a referrer.
///
/// We don't follow the normal Map pattern to simplify raw queries.
pub fn make_referee_count_key(referrer: &Addr) -> String {
    format!("refcount__{}", referrer.as_str())
}

/// Response from [QueryMsg::ListRefereeCount]
#[cw_serde]
pub struct ListRefereeCountResp {
    /// Counts for individual wallets
    pub counts: Vec<RefereeCount>,
    /// Next value to start after
    ///
    /// Returns `None` if we've seen all referees
    pub next_start_after: Option<ListRefereeCountStartAfter>,
}

/// The count of referees for an individual referrer.
#[cw_serde]
pub struct RefereeCount {
    /// Referrer address
    pub referrer: Addr,
    /// Number of referees
    pub count: u32,
}

/// Helper for enumerated referee count queries.
#[cw_serde]
pub struct ListRefereeCountStartAfter {
    /// Last referrer seen.
    pub referrer: RawAddr,
    /// Last count seen.
    pub count: u32,
}

#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
/// Leader address
pub struct LeaderAddr(pub Addr);

#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
/// Leader address
pub struct CopyTradingAddr(pub Addr);

#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
/// Copy trading contract information
pub struct CopyTradingInfo {
    /// Leader of the contract
    pub leader: LeaderAddr,
    /// Address of the copy trading contract
    pub contract: CopyTradingAddr,
}

#[derive(Clone, serde::Serialize, serde::Deserialize, JsonSchema, PartialEq, Debug)]
/// Same as [CopyTradingInfo], but has raw addresses
pub struct CopyTradingInfoRaw {
    /// Leader of the contract
    pub leader: RawAddr,
    /// Address of the copy trading contract
    pub contract: RawAddr,
}

impl KeyDeserialize for LeaderAddr {
    type Output = LeaderAddr;

    const KEY_ELEMS: u16 = Addr::KEY_ELEMS;

    fn from_vec(value: Vec<u8>) -> cosmwasm_std::StdResult<Self::Output> {
        Addr::from_vec(value).map(LeaderAddr)
    }
}

impl<'a> Prefixer<'a> for LeaderAddr {
    fn prefix(&self) -> Vec<cw_storage_plus::Key> {
        self.0.prefix()
    }
}

impl<'a> PrimaryKey<'a> for LeaderAddr {
    type Prefix = <Addr as PrimaryKey<'a>>::Prefix;
    type SubPrefix = <Addr as PrimaryKey<'a>>::SubPrefix;
    type Suffix = <Addr as PrimaryKey<'a>>::Suffix;
    type SuperSuffix = <Addr as PrimaryKey<'a>>::SuperSuffix;

    fn key(&self) -> Vec<cw_storage_plus::Key> {
        self.0.key()
    }
}

impl KeyDeserialize for CopyTradingAddr {
    type Output = CopyTradingAddr;

    const KEY_ELEMS: u16 = Addr::KEY_ELEMS;

    fn from_vec(value: Vec<u8>) -> cosmwasm_std::StdResult<Self::Output> {
        Addr::from_vec(value).map(CopyTradingAddr)
    }
}
impl<'a> Prefixer<'a> for CopyTradingAddr {
    fn prefix(&self) -> Vec<cw_storage_plus::Key> {
        self.0.prefix()
    }
}

impl<'a> PrimaryKey<'a> for CopyTradingAddr {
    type Prefix = <Addr as PrimaryKey<'a>>::Prefix;
    type SubPrefix = <Addr as PrimaryKey<'a>>::SubPrefix;
    type Suffix = <Addr as PrimaryKey<'a>>::Suffix;
    type SuperSuffix = <Addr as PrimaryKey<'a>>::SuperSuffix;

    fn key(&self) -> Vec<cw_storage_plus::Key> {
        self.0.key()
    }
}