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
use asset_agnostic_orderbook::state::{orderbook::CallbackInfo, OrderSummary};
use borsh::{BorshDeserialize, BorshSerialize};
use bytemuck::{try_cast_slice_mut, try_from_bytes_mut, Pod, Zeroable};
use num_derive::{FromPrimitive, ToPrimitive};
use solana_program::{
    account_info::AccountInfo, msg, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey,
};
use std::{cell::RefMut, convert::TryInto, mem::size_of};

use crate::{
    error::DexError,
    processor::{MSRM_MINT, REFERRAL_MASK, SRM_MINT},
    utils::{fp32_div, fp32_mul, FP_32_ONE},
};

#[derive(Clone, Debug, PartialEq, Copy)]
#[allow(missing_docs)]
#[repr(u64)]
pub enum AccountTag {
    Uninitialized,
    DexState,
    UserAccount,
    Closed,
}

#[derive(Clone, Copy, PartialEq, FromPrimitive, ToPrimitive)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum Side {
    Bid,
    Ask,
}

/// This enum describes different supported behaviors for handling self trading scenarios
#[derive(PartialEq, Clone, Copy)]
#[repr(u64)]
pub enum SelfTradeBehavior {
    /// Decrement take means that both the maker and taker sides of the matched orders are decremented.
    ///
    /// This is equivalent to a normal order match, except for the fact that no fees are applies.
    DecrementTake,
    /// Cancels the maker side of the order.
    CancelProvide,
    /// Cancels the whole transaction as soon as a self-matching scenario is encountered.
    AbortTransaction,
}

/// The primary market state object
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct DexState {
    /// This u64 is used to verify and version the dex state
    pub tag: u64,
    /// The mint key of the base token
    pub base_mint: Pubkey,
    /// The mint key of the quote token
    pub quote_mint: Pubkey,
    /// The SPL token account holding the market's base tokens
    pub base_vault: Pubkey,
    /// The SPL token account holding the market's quote tokens
    pub quote_vault: Pubkey,
    /// The asset agnostic orderbook address
    pub orderbook: Pubkey,
    /// The market admin which can recuperate all transaction fees
    pub admin: Pubkey,
    /// The market's creation timestamp on the Solana runtime clock.
    pub creation_timestamp: i64,
    /// The market's total historical volume in base token
    pub base_volume: u64,
    /// The market's total historical volume in quote token
    pub quote_volume: u64,
    /// The market's fees which are available for extraction by the market admin
    pub accumulated_fees: u64,
    /// The market's minimum allowed order size in base token amount
    pub min_base_order_size: u64,
    /// Royalties bps
    pub royalties_bps: u64,
    /// Accumulated royalties fees
    pub accumulated_royalties: u64,
    /// The base currency multiplier
    pub base_currency_multiplier: u64,
    /// The quote currency multiplier
    pub quote_currency_multiplier: u64,
    /// The signer nonce is necessary for the market to perform as a signing entity
    pub signer_nonce: u8,
    /// Fee type (e.g. default or stable)
    pub fee_type: u8,
    /// Padding
    pub _padding: [u8; 6],
}

/// Size in bytes of the dex state object
pub const DEX_STATE_LEN: usize = size_of::<DexState>();

impl DexState {
    pub(crate) fn get<'a, 'b: 'a>(
        account_info: &'a AccountInfo<'b>,
    ) -> Result<RefMut<'a, Self>, ProgramError> {
        let a = Self::get_unchecked(account_info);
        if a.tag != AccountTag::DexState as u64 {
            return Err(ProgramError::InvalidAccountData);
        };
        Ok(a)
    }

    pub(crate) fn get_unchecked<'a, 'b: 'a>(account_info: &'a AccountInfo<'b>) -> RefMut<'a, Self> {
        let a = RefMut::map(account_info.data.borrow_mut(), |s| {
            try_from_bytes_mut::<Self>(&mut s[0..DEX_STATE_LEN]).unwrap()
        });
        a
    }

    pub(crate) fn scale_quote_amount(&self, raw_quote_amount: u64) -> u64 {
        raw_quote_amount / self.quote_currency_multiplier
    }

    pub(crate) fn scale_base_amount(&self, raw_base_amount: u64) -> u64 {
        raw_base_amount / self.base_currency_multiplier
    }

    pub(crate) fn unscale_quote_amount(&self, scaled_quote_amount: u64) -> Option<u64> {
        scaled_quote_amount.checked_mul(self.quote_currency_multiplier)
    }

    pub(crate) fn unscale_base_amount(&self, scaled_base_amount: u64) -> Option<u64> {
        scaled_base_amount.checked_mul(self.base_currency_multiplier)
    }

    pub(crate) fn unscale_order_summary(&self, order_summary: &mut OrderSummary) -> Option<()> {
        order_summary.total_base_qty = self.unscale_base_amount(order_summary.total_base_qty)?;
        order_summary.total_base_qty_posted =
            self.unscale_base_amount(order_summary.total_base_qty_posted)?;
        order_summary.total_quote_qty = self.unscale_quote_amount(order_summary.total_quote_qty)?;
        Some(())
    }

    pub(crate) fn get_quote_from_base(
        &self,
        raw_base_amount: u64,
        scaled_price_fp32: u64,
    ) -> Option<u64> {
        fp32_mul(raw_base_amount, scaled_price_fp32)
            .and_then(|n| (n as u128).checked_mul(self.quote_currency_multiplier as u128))
            .and_then(|n| n.checked_div(self.base_currency_multiplier as u128))
            .and_then(|n| n.try_into().ok())
    }
}

/// This header describes a user account's state
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct UserAccountHeader {
    /// This byte is used to verify and version the dex state
    pub tag: u64,
    /// The user account's assocatied DEX market
    pub market: Pubkey,
    /// The user account owner's wallet
    pub owner: Pubkey,
    /// The amount of base token available for settlement
    pub base_token_free: u64,
    /// The amount of base token currently locked in the orderbook
    pub base_token_locked: u64,
    /// The amount of quote token available for settlement
    pub quote_token_free: u64,
    /// The amount of quote token currently locked in the orderbook
    pub quote_token_locked: u64,
    /// The all time quantity of rebates accumulated by this user account.
    ///
    /// The actual rebates will always be transfer to the user account's main balance. This field is just a metric.
    pub accumulated_rebates: u64,
    /// The accumulated maker quote volume of the user. This field is just a metric.
    pub accumulated_maker_quote_volume: u64,
    /// The accumulated maker quote volume of the user. This field is just a metric.
    pub accumulated_maker_base_volume: u64,
    /// The accumulated taker quote volume of the user. This field is just a metric.
    pub accumulated_taker_quote_volume: u64,
    /// The accumulated taker quote volume of the user. This field is just a metric.
    pub accumulated_taker_base_volume: u64,
    /// We are forced to add padding here to keep the subsequent field as a u32 which maintains Borsh compatibility while respecting alignment constraints
    _padding: u32,
    /// The user account's number of active orders.
    pub number_of_orders: u32,
}

/// Represents and order in the user account. The client id offers an alias which can be used off-chain to map custom ids to an actual order id.
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct Order {
    /// The raw order id
    pub id: u128,
    /// The client-defined order id. Care should be taken off-chain to only create new orders with new client_ids.
    pub client_id: u128,
}

impl Order {
    /// The length in bytes of the order's binary representation
    pub const LEN: usize = std::mem::size_of::<Self>();
}

#[allow(missing_docs)]
pub struct UserAccount<'a> {
    pub header: &'a mut UserAccountHeader,
    orders: &'a mut [Order],
}

/// Size in bytes of the user account header object
pub const USER_ACCOUNT_HEADER_LEN: usize = 152;

impl UserAccountHeader {
    pub(crate) fn new(market: &Pubkey, owner: &Pubkey) -> Self {
        Self {
            tag: AccountTag::UserAccount as u64,
            market: *market,
            owner: *owner,
            base_token_free: 0,
            base_token_locked: 0,
            quote_token_free: 0,
            quote_token_locked: 0,
            number_of_orders: 0,
            accumulated_rebates: 0,
            _padding: 0,
            accumulated_maker_quote_volume: 0,
            accumulated_maker_base_volume: 0,
            accumulated_taker_quote_volume: 0,
            accumulated_taker_base_volume: 0,
        }
    }
}

impl<'a> UserAccount<'a> {
    #[allow(missing_docs)]
    pub fn from_buffer(buf: &'a mut [u8]) -> Result<Self, ProgramError> {
        let user_acc = UserAccount::from_buffer_unchecked(buf).unwrap();
        if user_acc.header.tag != AccountTag::UserAccount as u64 {
            return Err(ProgramError::InvalidAccountData);
        };
        Ok(user_acc)
    }

    #[allow(missing_docs)]
    pub fn from_buffer_unchecked(buf: &'a mut [u8]) -> Result<Self, ProgramError> {
        let (hd, tl) = buf.split_at_mut(USER_ACCOUNT_HEADER_LEN);
        let header: &mut UserAccountHeader = try_from_bytes_mut(hd).unwrap();
        let orders = try_cast_slice_mut(tl).unwrap();

        Ok(Self { header, orders })
    }
}

impl<'a> UserAccount<'a> {
    #[allow(missing_docs)]
    pub fn read_order(&self, order_index: usize) -> Result<Order, DexError> {
        if order_index >= self.header.number_of_orders as usize {
            return Err(DexError::InvalidOrderIndex);
        }
        Ok(self.orders[order_index])
    }

    #[allow(missing_docs)]
    pub fn remove_order(&mut self, order_index: usize) -> Result<(), DexError> {
        if order_index >= self.header.number_of_orders as usize {
            return Err(DexError::InvalidOrderIndex);
        }
        if self.header.number_of_orders - order_index as u32 != 1 {
            self.orders[order_index] = self.orders[self.header.number_of_orders as usize - 1];
        }
        self.header.number_of_orders -= 1;
        Ok(())
    }

    #[allow(missing_docs)]
    pub fn add_order(&mut self, order: Order) -> Result<(), DexError> {
        let slot = self
            .orders
            .get_mut(self.header.number_of_orders as usize)
            .ok_or(DexError::UserAccountFull)?;
        *slot = order;
        self.header.number_of_orders += 1;
        Ok(())
    }

    #[allow(missing_docs)]
    pub fn find_order_index(&self, order_id: u128) -> Result<usize, DexError> {
        let res = self
            .orders
            .iter()
            .enumerate()
            .find(|(_, b)| b.id == order_id)
            .ok_or(DexError::OrderNotFound)?
            .0;
        Ok(res)
    }

    #[allow(missing_docs)]
    pub fn find_order_id_and_index_by_client_id(
        &self,
        client_order_id: u128,
    ) -> Result<(u64, u128), DexError> {
        let res = self
            .orders
            .iter()
            .enumerate()
            .find(|(_, b)| b.client_id == client_order_id)
            .map(|(idx, b)| (idx as u64, b.id))
            .ok_or(DexError::OrderNotFound)?;
        Ok(res)
    }
}

#[doc(hidden)]
#[derive(BorshDeserialize, BorshSerialize, Debug, Clone, Copy)]
pub enum MarketFeeType {
    Default,
    Stable,
}

#[doc(hidden)]
#[derive(BorshDeserialize, BorshSerialize, Debug, Clone, Copy)]
pub enum FeeTier {
    Base,
    Srm2,
    Srm3,
    Srm4,
    Srm5,
    Srm6,
    MSrm,
    Stable,
}

#[doc(hidden)]
impl FeeTier {
    pub fn from_srm_and_msrm_balances(
        dex_state: &DexState,
        srm_held: u64,
        msrm_held: u64,
    ) -> FeeTier {
        let one_srm = 1_000_000;

        if dex_state.fee_type == MarketFeeType::Stable as u8 {
            return FeeTier::Stable;
        }

        match () {
            () if msrm_held >= 1 => FeeTier::MSrm,
            () if srm_held >= one_srm * 1_000_000 => FeeTier::Srm6,
            () if srm_held >= one_srm * 100_000 => FeeTier::Srm5,
            () if srm_held >= one_srm * 10_000 => FeeTier::Srm4,
            () if srm_held >= one_srm * 1_000 => FeeTier::Srm3,
            () if srm_held >= one_srm * 100 => FeeTier::Srm2,
            () => FeeTier::Base,
        }
    }

    pub fn from_u8(tag: u8) -> (Self, bool) {
        let is_referred = (tag & REFERRAL_MASK) != 0;
        let fee_tier = match tag & (!REFERRAL_MASK) {
            0 => FeeTier::Base,
            1 => FeeTier::Srm2,
            2 => FeeTier::Srm3,
            3 => FeeTier::Srm4,
            4 => FeeTier::Srm5,
            5 => FeeTier::Srm6,
            _ => unreachable!(),
        };
        (fee_tier, is_referred)
    }

    pub fn get(
        dex_state: &DexState,
        account: &AccountInfo,
        expected_owner: &Pubkey,
    ) -> Result<Self, ProgramError> {
        let parsed_token_account = spl_token::state::Account::unpack(&account.data.borrow())?;
        if &parsed_token_account.owner != expected_owner {
            msg!("The discount token account must share its owner with the user account.");
            return Err(ProgramError::InvalidArgument);
        }
        let (srm_held, msrm_held) = match parsed_token_account.mint {
            a if a == MSRM_MINT => (0, parsed_token_account.amount),
            a if a == SRM_MINT => (parsed_token_account.amount, 0),
            _ => {
                msg!("Invalid mint for discount token acccount.");
                return Err(ProgramError::InvalidArgument);
            }
        };
        Ok(Self::from_srm_and_msrm_balances(
            dex_state, srm_held, msrm_held,
        ))
    }

    pub fn taker_rate(self) -> u64 {
        match self {
            FeeTier::Base => (40 << 32) / 100_000,
            FeeTier::Srm2 => (39 << 32) / 100_000,
            FeeTier::Srm3 => (38 << 32) / 100_000,
            FeeTier::Srm4 => (36 << 32) / 100_000,
            FeeTier::Srm5 => (34 << 32) / 100_000,
            FeeTier::Srm6 => (32 << 32) / 100_000,
            FeeTier::MSrm => (30 << 32) / 100_000,
            FeeTier::Stable => (10 << 32) / 100_000,
        }
    }

    pub fn maker_rate(self) -> u64 {
        0
    }

    pub fn maker_rebate(self, _quote_qty: u64) -> u64 {
        0
    }

    pub fn remove_taker_fee(self, quote_qty: u64) -> u64 {
        let rate = self.taker_rate();
        fp32_div(quote_qty, FP_32_ONE + rate).unwrap()
    }

    pub fn taker_fee(self, quote_qty: u64) -> u64 {
        let rate = self.taker_rate();
        fp32_mul(quote_qty, rate).unwrap()
    }

    pub fn referral_rate(self) -> u64 {
        let taker_rate = self.taker_rate();
        let min_maker_rebate = Self::Base.maker_rate();
        taker_rate.saturating_sub(min_maker_rebate) / 5
    }

    pub fn referral_fee(self, quote_qty: u64) -> u64 {
        let rate = self.referral_rate();
        fp32_mul(quote_qty, rate).unwrap()
    }
}
#[derive(BorshDeserialize, BorshSerialize, Debug, Clone, Copy, Zeroable, Pod, PartialEq)]
#[repr(C)]
/// Information about a user involved in an orderbook matching event
pub struct CallBackInfo {
    #[allow(missing_docs)]
    pub user_account: Pubkey,
    #[allow(missing_docs)]
    pub fee_tier: u8,
}

impl CallbackInfo for CallBackInfo {
    type CallbackId = Pubkey;

    fn as_callback_id(&self) -> &Self::CallbackId {
        &self.user_account
    }
}