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
use std::rc::Rc;

use agnostic_orderbook::{
    state::{EventQueue, EventQueueHeader, OrderSummary, SelfTradeBehavior, Side},
    CRANKER_REWARD,
};
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint::ProgramResult,
    msg,
    program::{invoke, invoke_signed},
    program_error::ProgramError,
    pubkey::Pubkey,
    rent::Rent,
    system_instruction, system_program,
    sysvar::Sysvar,
};

use crate::{
    error::DexError,
    state::{AccountTag, CallBackInfo, DexState, FeeTier, UserAccount},
    utils::{check_account_key, check_signer},
    utils::{check_account_owner, fp32_mul},
};

use super::CALLBACK_INFO_LEN;

#[derive(BorshDeserialize, BorshSerialize)]
/**
The required arguments for a new_order instruction.
*/
pub struct Params {
    /// The order's side (Bid or Ask)
    pub side: Side,
    /// The order's limit price (as a FP32)
    pub limit_price: u64,
    /// The max quantity of base token to match and post
    pub max_base_qty: u64,
    /// The max quantity of quote token to match and post
    pub max_quote_qty: u64,
    /// The order type (supported types include Limit, FOK, IOC and PostOnly)
    pub order_type: OrderType,
    /// Configures what happens when this order is at least partially matched against an order belonging to the same user account
    pub self_trade_behavior: SelfTradeBehavior,
    /// The maximum number of orders to be matched against.
    ///
    /// Setting this number too high can sometimes lead to excessive resource consumption which can cause a failure.
    pub match_limit: u64,
}

/// This enum describes all supported order types
#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq)]
pub enum OrderType {
    #[allow(missing_docs)]
    Limit,
    #[allow(missing_docs)]
    ImmediateOrCancel,
    #[allow(missing_docs)]
    FillOrKill,
    #[allow(missing_docs)]
    PostOnly,
}

struct Accounts<'a, 'b: 'a> {
    aaob_program: &'a AccountInfo<'b>,
    spl_token_program: &'a AccountInfo<'b>,
    system_program: &'a AccountInfo<'b>,
    rent_sysvar: &'a AccountInfo<'b>,
    market: &'a AccountInfo<'b>,
    market_signer: &'a AccountInfo<'b>,
    orderbook: &'a AccountInfo<'b>,
    event_queue: &'a AccountInfo<'b>,
    bids: &'a AccountInfo<'b>,
    asks: &'a AccountInfo<'b>,
    base_vault: &'a AccountInfo<'b>,
    quote_vault: &'a AccountInfo<'b>,
    user: &'a AccountInfo<'b>,
    user_token_account: &'a AccountInfo<'b>,
    user_owner: &'a AccountInfo<'b>,
    discount_token_account: Option<&'a AccountInfo<'b>>,
}

impl<'a, 'b: 'a> Accounts<'a, 'b> {
    pub fn parse(
        program_id: &Pubkey,
        accounts: &'a [AccountInfo<'b>],
    ) -> Result<Self, ProgramError> {
        let accounts_iter = &mut accounts.iter();
        let a = Self {
            aaob_program: next_account_info(accounts_iter)?,
            spl_token_program: next_account_info(accounts_iter)?,
            system_program: next_account_info(accounts_iter)?,
            rent_sysvar: next_account_info(accounts_iter)?,
            market: next_account_info(accounts_iter)?,
            market_signer: next_account_info(accounts_iter)?,
            orderbook: next_account_info(accounts_iter)?,
            event_queue: next_account_info(accounts_iter)?,
            bids: next_account_info(accounts_iter)?,
            asks: next_account_info(accounts_iter)?,
            base_vault: next_account_info(accounts_iter)?,
            quote_vault: next_account_info(accounts_iter)?,
            user: next_account_info(accounts_iter)?,
            user_token_account: next_account_info(accounts_iter)?,
            user_owner: next_account_info(accounts_iter)?,
            discount_token_account: next_account_info(accounts_iter).ok(),
        };
        check_signer(&a.user_owner).unwrap();
        check_account_key(a.spl_token_program, &spl_token::ID).unwrap();
        check_account_key(a.system_program, &system_program::ID).unwrap();
        check_account_owner(a.user, program_id).unwrap();

        Ok(a)
    }

    pub fn load_user_account(&self) -> Result<UserAccount<'b>, ProgramError> {
        let user_account =
            match AccountTag::deserialize(&mut (&self.user.data.borrow() as &[u8])).unwrap() {
                AccountTag::UserAccount => {
                    let u = UserAccount::parse(&self.user)?;
                    if &u.header.owner != self.user_owner.key {
                        msg!("Invalid user account owner provided!");
                        return Err(ProgramError::InvalidArgument);
                    }
                    if &u.header.market != self.market.key {
                        msg!("The provided user account doesn't match the current market");
                        return Err(ProgramError::InvalidArgument);
                    };
                    u
                }
                AccountTag::Uninitialized => {
                    msg!("Invalid user account!");
                    return Err(ProgramError::InvalidArgument);
                }
                _ => return Err(ProgramError::InvalidArgument),
            };
        if &user_account.header.owner != self.user_owner.key {
            msg!("Invalid user account owner provided!");
            return Err(ProgramError::InvalidArgument);
        }
        if &user_account.header.market != self.market.key {
            msg!("The provided user account doesn't match the current market");
            return Err(ProgramError::InvalidArgument);
        };
        Ok(user_account)
    }
}

pub(crate) fn process(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    params: Params,
) -> ProgramResult {
    let accounts = Accounts::parse(program_id, accounts)?;

    let Params {
        side,
        limit_price,
        max_base_qty,
        mut max_quote_qty,
        order_type,
        self_trade_behavior,
        match_limit,
    } = params;
    let market_state =
        DexState::deserialize(&mut (&accounts.market.data.borrow() as &[u8]))?.check()?;
    let mut user_account = accounts.load_user_account()?;

    // Check the order size
    if max_base_qty < market_state.min_base_order_size {
        msg!("The base order size is too small.");
        return Err(ProgramError::InvalidArgument);
    }

    check_accounts(program_id, &market_state, &accounts).unwrap();
    let (post_only, post_allowed) = match order_type {
        OrderType::Limit => (false, true),
        OrderType::ImmediateOrCancel | OrderType::FillOrKill => (false, false),
        OrderType::PostOnly => (true, true),
    };
    let callback_info = CallBackInfo {
        user_account: *accounts.user.key,
        fee_tier: accounts
            .discount_token_account
            .map(|a| FeeTier::get(a, accounts.user_owner.key))
            .unwrap_or(Ok(FeeTier::Base))?,
    };
    if side == Side::Bid && order_type != OrderType::PostOnly {
        // We make sure to leave enough quote quantity to pay for taker fees in the worst case
        max_quote_qty = callback_info.fee_tier.remove_taker_fee(max_quote_qty);
    }

    //Transfer the cranking fee to the AAOB program
    let rent = Rent::from_account_info(accounts.rent_sysvar)?;
    if accounts.user_owner.lamports()
        < rent.minimum_balance(accounts.user_owner.data_len()) + CRANKER_REWARD
    {
        msg!("The user does not have enough lamports on his account.");
        return Err(DexError::OutofFunds.into());
    }
    let transfer_cranking_fee = system_instruction::transfer(
        accounts.user_owner.key,
        accounts.orderbook.key,
        agnostic_orderbook::CRANKER_REWARD,
    );
    invoke(
        &transfer_cranking_fee,
        &[
            accounts.system_program.clone(),
            accounts.user_owner.clone(),
            accounts.orderbook.clone(),
        ],
    )?;

    let new_order_instruction = agnostic_orderbook::instruction::new_order(
        *accounts.aaob_program.key,
        *accounts.orderbook.key,
        *accounts.market_signer.key,
        *accounts.event_queue.key,
        *accounts.bids.key,
        *accounts.asks.key,
        agnostic_orderbook::instruction::new_order::Params {
            max_base_qty,
            max_quote_qty,
            limit_price,
            side,
            match_limit,
            callback_info: callback_info.try_to_vec()?,
            post_only,
            post_allowed,
            self_trade_behavior,
        },
    );
    invoke_signed(
        &new_order_instruction,
        &[
            accounts.aaob_program.clone(),
            accounts.orderbook.clone(),
            accounts.event_queue.clone(),
            accounts.bids.clone(),
            accounts.asks.clone(),
            accounts.market_signer.clone(),
        ],
        &[&[
            &accounts.market.key.to_bytes(),
            &[market_state.signer_nonce],
        ]],
    )?;
    let event_queue_header =
        EventQueueHeader::deserialize(&mut (&accounts.event_queue.data.borrow() as &[u8]))?;
    let event_queue = EventQueue::new(
        event_queue_header,
        Rc::clone(&accounts.event_queue.data),
        CALLBACK_INFO_LEN as usize,
    );

    let mut order_summary: OrderSummary = event_queue.read_register().unwrap().unwrap();

    let (qty_to_transfer, transfer_destination) = match side {
        Side::Bid => {
            // We update the order summary to properly handle the FOK order type
            let posted_quote_qty = fp32_mul(order_summary.total_base_qty_posted, limit_price);
            order_summary.total_quote_qty += callback_info
                .fee_tier
                .taker_fee(order_summary.total_quote_qty - posted_quote_qty);
            let q = order_summary
                .total_quote_qty
                .saturating_sub(user_account.header.quote_token_free);
            user_account.header.quote_token_free = user_account
                .header
                .quote_token_free
                .saturating_sub(order_summary.total_quote_qty);
            user_account.header.quote_token_locked += posted_quote_qty;
            user_account.header.base_token_free +=
                order_summary.total_base_qty - order_summary.total_base_qty_posted;

            (q, accounts.quote_vault)
        }
        Side::Ask => {
            let q = order_summary
                .total_base_qty
                .saturating_sub(user_account.header.base_token_free);
            user_account.header.base_token_free = user_account
                .header
                .base_token_free
                .saturating_sub(order_summary.total_base_qty);
            user_account.header.base_token_locked += order_summary.total_base_qty_posted;
            let posted_quote_qty = fp32_mul(order_summary.total_base_qty_posted, limit_price);
            let taken_quote_qty = order_summary.total_quote_qty - posted_quote_qty;
            let taker_fee = callback_info.fee_tier.taker_fee(taken_quote_qty);
            user_account.header.quote_token_free += taken_quote_qty - taker_fee;
            (q, accounts.base_vault)
        }
    };

    let abort = match order_type {
        OrderType::ImmediateOrCancel => order_summary.total_base_qty == 0,
        OrderType::FillOrKill => {
            (order_summary.total_base_qty == max_base_qty)
                || (order_summary.total_quote_qty == max_quote_qty)
        }
        OrderType::PostOnly => order_summary.posted_order_id.is_none(),
        _ => false,
    };

    if abort {
        msg!(
            "The specified order type {:?} has caused an abort",
            order_type
        );
        return Err(DexError::TransactionAborted.into());
    }

    let token_transfer_instruction = spl_token::instruction::transfer(
        accounts.spl_token_program.key,
        accounts.user_token_account.key,
        transfer_destination.key,
        accounts.user_owner.key,
        &[],
        qty_to_transfer,
    )?;

    invoke(
        &token_transfer_instruction,
        &[
            accounts.spl_token_program.clone(),
            accounts.user_token_account.clone(),
            transfer_destination.clone(),
            accounts.user_owner.clone(),
        ],
    )?;

    if let Some(order_id) = order_summary.posted_order_id {
        user_account.add_order(order_id)?;
        msg!("Added new order with order_id {:?}", order_id);
    }

    user_account.write();

    Ok(())
}

fn check_accounts(
    program_id: &Pubkey,
    market_state: &DexState,
    accounts: &Accounts,
) -> ProgramResult {
    let market_signer = Pubkey::create_program_address(
        &[
            &accounts.market.key.to_bytes(),
            &[market_state.signer_nonce],
        ],
        program_id,
    )?;
    check_account_key(accounts.market_signer, &market_signer).unwrap();
    check_account_key(accounts.orderbook, &market_state.orderbook).unwrap();
    check_account_key(accounts.base_vault, &market_state.base_vault).unwrap();
    check_account_key(accounts.quote_vault, &market_state.quote_vault).unwrap();
    check_account_key(accounts.aaob_program, &market_state.aaob_program).unwrap();

    Ok(())
}