-
Notifications
You must be signed in to change notification settings - Fork 32
/
cancel_order.rs
207 lines (178 loc) · 6.23 KB
/
cancel_order.rs
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
//! Cancel an existing order and remove it from the orderbook.
use std::rc::Rc;
use crate::{
error::DexError,
state::{DexState, UserAccount},
utils::{check_account_key, check_account_owner, check_signer},
};
use agnostic_orderbook::{
error::AoError,
state::{get_side_from_order_id, EventQueue, EventQueueHeader, OrderSummary, Side},
};
use bonfida_utils::BorshSize;
use bonfida_utils::InstructionsAccount;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use bytemuck::{try_from_bytes, Pod, Zeroable};
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program_error::{PrintProgramError, ProgramError},
pubkey::Pubkey,
};
use super::CALLBACK_INFO_LEN;
#[derive(Clone, Copy, Zeroable, Pod, BorshDeserialize, BorshSerialize, BorshSize)]
#[repr(C)]
/**
The required arguments for a cancel_order instruction.
*/
pub struct Params {
/// The index in the user account of the order to cancel
pub order_index: u64,
/// The order_id of the order to cancel. Redundancy is used here to avoid having to iterate over all
/// open orders on chain.
pub order_id: u128,
}
#[derive(InstructionsAccount)]
pub struct Accounts<'a, T> {
/// The DEX market
pub market: &'a T,
/// The orderbook
#[cons(writable)]
pub orderbook: &'a T,
/// The AOB event queue
#[cons(writable)]
pub event_queue: &'a T,
/// The AOB bids shared memory
#[cons(writable)]
pub bids: &'a T,
/// The AOB asks shared memory
#[cons(writable)]
pub asks: &'a T,
/// The DEX user account
#[cons(writable)]
pub user: &'a T,
/// The user wallet
#[cons(signer)]
pub user_owner: &'a T,
}
impl<'a, 'b: 'a> Accounts<'a, AccountInfo<'b>> {
pub fn parse(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'b>],
) -> Result<Self, ProgramError> {
let accounts_iter = &mut accounts.iter();
let a = Self {
market: 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)?,
user: next_account_info(accounts_iter)?,
user_owner: next_account_info(accounts_iter)?,
};
check_signer(a.user_owner).map_err(|e| {
msg!("The user account owner should be a signer for this transaction!");
e
})?;
check_account_owner(a.market, program_id, DexError::InvalidStateAccountOwner)?;
check_account_owner(a.user, program_id, DexError::InvalidStateAccountOwner)?;
Ok(a)
}
pub fn load_user_account(&self) -> Result<UserAccount<'a>, ProgramError> {
let user_account = UserAccount::get(self.user)?;
if user_account.header.owner != self.user_owner.key.to_bytes() {
msg!("Invalid user account owner provided!");
return Err(ProgramError::InvalidArgument);
}
if user_account.header.market != self.market.key.to_bytes() {
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],
instruction_data: &[u8],
) -> ProgramResult {
let params =
try_from_bytes(instruction_data).map_err(|_| ProgramError::InvalidInstructionData)?;
let accounts = Accounts::parse(program_id, accounts)?;
let Params {
order_index,
order_id,
} = params;
let market_state = DexState::get(accounts.market)?;
let mut user_account = accounts.load_user_account()?;
check_accounts(&market_state, &accounts).unwrap();
let order_id_from_index = user_account.read_order(*order_index as usize)?.id;
if order_id != &order_id_from_index {
msg!("Order id does not match with the order at the given index!");
return Err(ProgramError::InvalidArgument);
}
let invoke_params = agnostic_orderbook::instruction::cancel_order::Params {
order_id: *order_id,
};
let invoke_accounts = agnostic_orderbook::instruction::cancel_order::Accounts {
market: accounts.orderbook,
event_queue: accounts.event_queue,
bids: accounts.bids,
asks: accounts.asks,
};
if let Err(error) = agnostic_orderbook::instruction::cancel_order::process(
program_id,
invoke_accounts,
invoke_params,
) {
error.print::<AoError>();
return Err(DexError::AOBError.into());
}
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 order_summary: OrderSummary = event_queue.read_register().unwrap().unwrap();
let side = get_side_from_order_id(*order_id);
match side {
Side::Bid => {
user_account.header.quote_token_free = user_account
.header
.quote_token_free
.checked_add(order_summary.total_quote_qty)
.unwrap();
user_account.header.quote_token_locked = user_account
.header
.quote_token_locked
.checked_sub(order_summary.total_quote_qty)
.unwrap();
}
Side::Ask => {
user_account.header.base_token_free = user_account
.header
.base_token_free
.checked_add(order_summary.total_base_qty)
.unwrap();
user_account.header.base_token_locked = user_account
.header
.base_token_locked
.checked_sub(order_summary.total_base_qty)
.unwrap();
}
};
user_account.remove_order(*order_index as usize)?;
Ok(())
}
fn check_accounts(market_state: &DexState, accounts: &Accounts<AccountInfo>) -> ProgramResult {
check_account_key(
accounts.orderbook,
&market_state.orderbook,
DexError::InvalidOrderbookAccount,
)?;
Ok(())
}