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
//! Data types for limit orders
use crate::prelude::*;
use cosmwasm_std::{Addr, StdResult};
use cw_storage_plus::{IntKey, Key, KeyDeserialize, Prefixer, PrimaryKey};
use std::fmt;
use std::hash::Hash;
use std::num::ParseIntError;

/// A limit order
#[cw_serde]
pub struct LimitOrder {
    /// ID of the order
    pub order_id: OrderId,
    /// Owner of the order
    pub owner: Addr,
    /// Price where the order will trigger
    pub trigger_price: PriceBaseInQuote,
    /// Deposit collateral
    pub collateral: NonZero<Collateral>,
    /// Leverage
    pub leverage: LeverageToBase,
    /// Direction of the position
    pub direction: DirectionToNotional,
    /// Maximum gains
    #[deprecated(note = "Use take_profit instead")]
    pub max_gains: Option<MaxGainsInQuote>,
    /// Stop loss price
    pub stop_loss_override: Option<PriceBaseInQuote>,
    /// Take profit price
    // TODO - this should eventually become non-optional, but that would require a migration
    // it is, however, non-optional in LimitOrderResp
    #[serde(alias = "take_profit_override")]
    pub take_profit: Option<TakeProfitTrader>,
    /// Crank fee charged during deferred execution and placing the limit order
    #[serde(default)]
    pub crank_fee_collateral: Collateral,
    /// Same as [Self::crank_fee_collateral] but cost-basis expressed in USD.
    #[serde(default)]
    pub crank_fee_usd: Usd,
}

/// A unique numeric ID for each order in the protocol.
#[cw_serde]
#[derive(Copy, PartialOrd, Ord, Eq)]
pub struct OrderId(Uint64);

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for OrderId {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        u64::arbitrary(u).map(|x| OrderId(Uint64::new(x)))
    }
}

#[allow(clippy::derived_hash_with_manual_eq)]
impl Hash for OrderId {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.u64().hash(state);
    }
}

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

    /// Get the underlying `u64` representation of the order ID.
    pub fn u64(self) -> u64 {
        self.0.u64()
    }
}

impl<'a> PrimaryKey<'a> for OrderId {
    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 OrderId {
    fn prefix(&self) -> Vec<Key> {
        vec![Key::Val64(self.0.u64().to_cw_bytes())]
    }
}

impl KeyDeserialize for OrderId {
    type Output = OrderId;

    const KEY_ELEMS: u16 = 1;

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

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

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

/// Events
pub mod events {
    use crate::constants::{event_key, event_val};
    use crate::contracts::market::order::OrderId;
    use crate::contracts::market::position::PositionId;
    use crate::prelude::MarketType::{CollateralIsBase, CollateralIsQuote};
    use crate::prelude::*;

    /// Event when a limit order is placed
    pub struct PlaceLimitOrderEvent {
        /// Unique order ID
        pub order_id: OrderId,
        /// Owner of the order
        pub owner: Addr,
        /// Trigger price
        pub trigger_price: PriceBaseInQuote,
        /// Market type of the contract
        pub market_type: MarketType,
        /// Deposit collateral
        pub collateral: NonZero<Collateral>,
        /// Deposit collateral in USD at current exchange rate
        pub collateral_usd: NonZero<Usd>,
        /// Signed leverage to base (negative == short, positive == long)
        pub leverage: SignedLeverageToBase,
        /// Direction of the position
        pub direction: DirectionToBase,
        /// Maximum gains
        #[deprecated(note = "Use take_profit_override instead")]
        pub max_gains: Option<MaxGainsInQuote>,
        /// Stop loss price
        pub stop_loss_override: Option<PriceBaseInQuote>,
        /// Take profit price
        pub take_profit_override: Option<TakeProfitTrader>,
    }

    impl From<PlaceLimitOrderEvent> for Event {
        fn from(src: PlaceLimitOrderEvent) -> Self {
            let mut event = Event::new(event_key::PLACE_LIMIT_ORDER)
                .add_attribute(
                    event_key::MARKET_TYPE,
                    match src.market_type {
                        CollateralIsQuote => event_val::NOTIONAL_BASE,
                        CollateralIsBase => event_val::COLLATERAL_BASE,
                    },
                )
                .add_attribute(event_key::ORDER_ID, src.order_id.to_string())
                .add_attribute(event_key::POS_OWNER, src.owner.to_string())
                .add_attribute(event_key::TRIGGER_PRICE, src.trigger_price.to_string())
                .add_attribute(event_key::DEPOSIT_COLLATERAL, src.collateral.to_string())
                .add_attribute(
                    event_key::DEPOSIT_COLLATERAL_USD,
                    src.collateral_usd.to_string(),
                )
                .add_attribute(event_key::LEVERAGE_TO_BASE, src.leverage.to_string())
                .add_attribute(event_key::DIRECTION, src.direction.as_str());

            if let Some(stop_loss_override) = src.stop_loss_override {
                event = event.add_attribute(
                    event_key::STOP_LOSS_OVERRIDE,
                    stop_loss_override.to_string(),
                );
            }

            if let Some(take_profit_override) = src.take_profit_override {
                event = event.add_attribute(
                    event_key::TAKE_PROFIT_OVERRIDE,
                    take_profit_override.to_string(),
                );
            }
            #[allow(deprecated)]
            if let Some(max_gains) = src.max_gains {
                event = event.add_attribute(event_key::MAX_GAINS, max_gains.to_string());
            }

            event
        }
    }
    impl TryFrom<Event> for PlaceLimitOrderEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> Result<Self, Self::Error> {
            #[allow(deprecated)]
            Ok(Self {
                market_type: evt.map_attr_result(event_key::MARKET_TYPE, |s| match s {
                    event_val::NOTIONAL_BASE => Ok(CollateralIsQuote),
                    event_val::COLLATERAL_BASE => Ok(CollateralIsBase),
                    _ => Err(PerpError::unimplemented().into()),
                })?,
                collateral: evt
                    .string_attr(event_key::DEPOSIT_COLLATERAL)?
                    .as_str()
                    .try_into()?,
                collateral_usd: evt
                    .string_attr(event_key::DEPOSIT_COLLATERAL_USD)?
                    .as_str()
                    .try_into()?,
                leverage: SignedLeverageToBase::from_str(
                    &(evt.string_attr(event_key::LEVERAGE_TO_BASE)?),
                )?,
                direction: evt.direction_attr(event_key::DIRECTION)?,
                order_id: OrderId::new(evt.u64_attr(event_key::ORDER_ID)?),
                owner: evt.unchecked_addr_attr(event_key::POS_OWNER)?,
                trigger_price: PriceBaseInQuote::try_from_number(
                    evt.number_attr(event_key::TRIGGER_PRICE)?,
                )?,
                stop_loss_override: match evt.try_number_attr(event_key::STOP_LOSS_OVERRIDE)? {
                    None => None,
                    Some(stop_loss_override) => {
                        Some(PriceBaseInQuote::try_from_number(stop_loss_override)?)
                    }
                },
                take_profit_override: evt
                    .try_map_attr(event_key::TAKE_PROFIT_OVERRIDE, |s| {
                        TakeProfitTrader::try_from(s)
                    })
                    .transpose()?,

                max_gains: evt
                    .try_map_attr(event_key::MAX_GAINS, MaxGainsInQuote::from_str)
                    .transpose()?,
            })
        }
    }

    /// A limit order was canceled
    pub struct CancelLimitOrderEvent {
        /// ID of the canceled order
        pub order_id: OrderId,
    }

    impl From<CancelLimitOrderEvent> for Event {
        fn from(src: CancelLimitOrderEvent) -> Self {
            Event::new(event_key::PLACE_LIMIT_ORDER)
                .add_attribute(event_key::ORDER_ID, src.order_id.to_string())
        }
    }
    impl TryFrom<Event> for CancelLimitOrderEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> Result<Self, Self::Error> {
            Ok(Self {
                order_id: OrderId::new(evt.u64_attr(event_key::ORDER_ID)?),
            })
        }
    }

    /// A limit order was triggered
    pub struct ExecuteLimitOrderEvent {
        /// ID of the order
        pub order_id: OrderId,
        /// ID of the position, if it successfully opened
        pub pos_id: Option<PositionId>,
        /// The error message for a failed limit order, if it failed
        pub error: Option<String>,
    }

    impl From<ExecuteLimitOrderEvent> for Event {
        fn from(src: ExecuteLimitOrderEvent) -> Self {
            let mut event = Event::new(event_key::EXECUTE_LIMIT_ORDER)
                .add_attribute(event_key::ORDER_ID, src.order_id.to_string());

            if let Some(pos_id) = src.pos_id {
                event = event.add_attribute(event_key::POS_ID, pos_id.to_string());
            }

            if let Some(error) = src.error {
                event = event.add_attribute(event_key::EXECUTE_LIMIT_ORDER_ERROR, error);
            }

            event
        }
    }
    impl TryFrom<Event> for ExecuteLimitOrderEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> Result<Self, Self::Error> {
            Ok(Self {
                order_id: OrderId::new(evt.u64_attr(event_key::ORDER_ID)?),
                pos_id: evt.try_u64_attr(event_key::POS_ID)?.map(PositionId::new),
                error: evt.try_map_attr(event_key::EXECUTE_LIMIT_ORDER_ERROR, |x| x.to_owned()),
            })
        }
    }
}