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
//! Different representations of the direction of a position.
//!
//! Positions can either be long or short, but due to the different
//! [MarketType]s supported by perps we need to distinguish between the
//! direction to the base asset versus the notional asset.
use std::array::TryFromSliceError;
use std::convert::From;

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{StdError, StdResult};
use cw_storage_plus::{IntKey, Key, KeyDeserialize, Prefixer, PrimaryKey};

use crate::{market_type::MarketType, prelude::*};

/// Direction in terms of notional
#[cw_serde]
#[derive(Eq, Copy)]
#[repr(u8)]
pub enum DirectionToNotional {
    /// Long versus notional
    Long,
    /// Short versus notional
    Short,
}

/// Direction in terms of base
#[cw_serde]
#[derive(Eq, Copy)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum DirectionToBase {
    /// Long versus base
    Long,
    /// Short versus base
    Short,
}

impl DirectionToBase {
    /// Represent as a string, either `long` or `short`
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Long => "long",
            Self::Short => "short",
        }
    }

    /// Swap to the opposite direction
    pub fn invert(self) -> Self {
        match self {
            Self::Long => Self::Short,
            Self::Short => Self::Long,
        }
    }

    /// Convert into the direction to notional
    pub fn into_notional(&self, market_type: MarketType) -> DirectionToNotional {
        match (market_type, self) {
            (MarketType::CollateralIsQuote, DirectionToBase::Long) => DirectionToNotional::Long,
            (MarketType::CollateralIsQuote, DirectionToBase::Short) => DirectionToNotional::Short,
            (MarketType::CollateralIsBase, DirectionToBase::Long) => DirectionToNotional::Short,
            (MarketType::CollateralIsBase, DirectionToBase::Short) => DirectionToNotional::Long,
        }
    }
}

impl DirectionToNotional {
    /// Represent as a string, either `long` or `short`
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Long => "long",
            Self::Short => "short",
        }
    }

    /// Convert into the direction to base
    pub fn into_base(&self, market_type: MarketType) -> DirectionToBase {
        match (market_type, self) {
            (MarketType::CollateralIsQuote, DirectionToNotional::Long) => DirectionToBase::Long,
            (MarketType::CollateralIsQuote, DirectionToNotional::Short) => DirectionToBase::Short,
            (MarketType::CollateralIsBase, DirectionToNotional::Long) => DirectionToBase::Short,
            (MarketType::CollateralIsBase, DirectionToNotional::Short) => DirectionToBase::Long,
        }
    }

    /// Return positive 1 for long, negative 1 for short
    pub fn sign(&self) -> Number {
        match self {
            DirectionToNotional::Long => Number::ONE,
            DirectionToNotional::Short => Number::NEG_ONE,
        }
    }
}

impl From<DirectionToNotional> for u8 {
    fn from(value: DirectionToNotional) -> Self {
        match value {
            DirectionToNotional::Long => 0,
            DirectionToNotional::Short => 1,
        }
    }
}

impl From<&str> for DirectionToNotional {
    fn from(s: &str) -> Self {
        match s {
            "long" => Self::Long,
            "short" => Self::Short,
            _ => unimplemented!(),
        }
    }
}

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

    fn key(&self) -> Vec<Key> {
        let val: u8 = u8::from(*self);
        let key = Key::Val8(val.to_cw_bytes());

        vec![key]
    }
}

impl<'a> Prefixer<'a> for DirectionToNotional {
    fn prefix(&self) -> Vec<Key> {
        let val: u8 = u8::from(*self);
        let key = Key::Val8(val.to_cw_bytes());
        vec![key]
    }
}

impl KeyDeserialize for DirectionToNotional {
    type Output = u8;

    const KEY_ELEMS: u16 = 1;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        Ok(u8::from_cw_bytes(value.as_slice().try_into().map_err(
            |err: TryFromSliceError| StdError::generic_err(err.to_string()),
        )?))
    }
}