gmsol_store/states/
shift.rs

1use anchor_lang::prelude::*;
2
3use crate::events::ShiftRemoved;
4
5use super::{
6    common::{
7        action::{Action, ActionHeader, Closable},
8        token::TokenAndAccount,
9    },
10    Seed,
11};
12
13/// Shift.
14#[account(zero_copy)]
15#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17pub struct Shift {
18    /// Action header.
19    pub(crate) header: ActionHeader,
20    /// Token accounts.
21    pub(crate) tokens: ShiftTokenAccounts,
22    /// Shift params.
23    pub(crate) params: ShiftActionParams,
24    #[cfg_attr(feature = "debug", debug(skip))]
25    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
26    reserved: [u8; 128],
27}
28
29impl Seed for Shift {
30    const SEED: &'static [u8] = b"shift";
31}
32
33impl Action for Shift {
34    const MIN_EXECUTION_LAMPORTS: u64 = 200_000;
35
36    fn header(&self) -> &ActionHeader {
37        &self.header
38    }
39}
40
41impl Closable for Shift {
42    type ClosedEvent = ShiftRemoved;
43
44    fn to_closed_event(&self, address: &Pubkey, reason: &str) -> Result<Self::ClosedEvent> {
45        ShiftRemoved::new(
46            self.header.id,
47            self.header.store,
48            *address,
49            self.tokens().from_market_token(),
50            self.header.owner,
51            self.header.action_state()?,
52            reason,
53        )
54    }
55}
56
57impl gmsol_utils::InitSpace for Shift {
58    const INIT_SPACE: usize = std::mem::size_of::<Self>();
59}
60
61impl Shift {
62    /// Get token infos.
63    pub fn tokens(&self) -> &ShiftTokenAccounts {
64        &self.tokens
65    }
66}
67
68#[zero_copy]
69#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub struct ShiftTokenAccounts {
72    pub(crate) from_market_token: TokenAndAccount,
73    pub(crate) to_market_token: TokenAndAccount,
74    pub(crate) long_token: Pubkey,
75    pub(crate) short_token: Pubkey,
76    #[cfg_attr(feature = "debug", debug(skip))]
77    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
78    reserved: [u8; 128],
79}
80
81impl ShiftTokenAccounts {
82    /// Get from market token.
83    pub fn from_market_token(&self) -> Pubkey {
84        self.from_market_token.token().expect("must exist")
85    }
86
87    /// Get from market token account.
88    pub fn from_market_token_account(&self) -> Pubkey {
89        self.from_market_token.account().expect("msut exist")
90    }
91
92    /// Get to market token.
93    pub fn to_market_token(&self) -> Pubkey {
94        self.to_market_token.token().expect("must exist")
95    }
96
97    /// Get to market token account.
98    pub fn to_market_token_account(&self) -> Pubkey {
99        self.to_market_token.account().expect("msut exist")
100    }
101
102    /// Get long token.
103    pub fn long_token(&self) -> &Pubkey {
104        &self.long_token
105    }
106
107    /// Get short token.
108    pub fn short_token(&self) -> &Pubkey {
109        &self.short_token
110    }
111}
112
113#[zero_copy]
114#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
115#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
116pub struct ShiftActionParams {
117    pub(crate) from_market_token_amount: u64,
118    pub(crate) min_to_market_token_amount: u64,
119    #[cfg_attr(feature = "debug", debug(skip))]
120    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
121    reserved: [u8; 64],
122}
123
124impl ShiftActionParams {
125    /// Get from market token amount.
126    pub fn from_market_token_amount(&self) -> u64 {
127        self.from_market_token_amount
128    }
129
130    /// Get min to market token amount.
131    pub fn min_to_market_token_amount(&self) -> u64 {
132        self.min_to_market_token_amount
133    }
134}