MCPcopy Create free account
hub / github.com/Bonfida/dex-v4 / process

Function process

program/src/processor/swap.rs:163–388  ·  view source on GitHub ↗
(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
)

Source from the content-addressed store, hash-verified

161}
162
163pub(crate) fn process(
164 program_id: &Pubkey,
165 accounts: &[AccountInfo],
166 instruction_data: &[u8],
167) -> ProgramResult {
168 let Params {
169 side,
170 base_qty,
171 mut quote_qty,
172 match_limit,
173 has_discount_token_account,
174 _padding: _,
175 } = try_from_bytes(instruction_data).map_err(|_| ProgramError::InvalidInstructionData)?;
176 let accounts = Accounts::parse(program_id, accounts, *has_discount_token_account != 0)?;
177
178 let market_state = DexState::get(accounts.market)?;
179
180 // Check the order size
181 if base_qty < &market_state.min_base_order_size {
182 msg!("The base order size is too small.");
183 return Err(ProgramError::InvalidArgument);
184 }
185
186 check_accounts(program_id, &market_state, &accounts).unwrap();
187 let fee_tier = accounts
188 .discount_token_account
189 .map(|a| FeeTier::get(&market_state, a, accounts.user_owner.key))
190 .unwrap_or(Ok(FeeTier::Base))?;
191 let callback_info = CallBackInfo {
192 user_account: Pubkey::default(),
193 fee_tier: fee_tier as u8
194 | ((accounts.fee_referral_account.is_some() as u8) * REFERRAL_MASK),
195 };
196 if *side == Side::Bid as u8 {
197 // We make sure to leave enough quote quantity to pay for taker fees in the worst case
198 quote_qty = fee_tier.remove_taker_fee(quote_qty);
199 }
200
201 let mut orderbook_guard = accounts.orderbook.data.borrow_mut();
202 let orderbook = asset_agnostic_orderbook::state::market_state::MarketState::from_buffer(
203 &mut orderbook_guard,
204 AccountTag::Market,
205 )?;
206 let tick_size = orderbook.tick_size;
207 drop(orderbook_guard);
208
209 let (max_base_qty_scaled, max_quote_qty_scaled, limit_price) =
210 match FromPrimitive::from_u8(*side).unwrap() {
211 Side::Bid => (
212 u64::MAX,
213 market_state.scale_quote_amount(quote_qty),
214 u64::MAX - (u64::MAX % tick_size),
215 ),
216 Side::Ask => (market_state.scale_base_amount(*base_qty), u64::MAX, 0),
217 };
218
219 let invoke_params = asset_agnostic_orderbook::instruction::new_order::Params {
220 max_base_qty: max_base_qty_scaled,

Callers 1

process_instructionMethod · 0.50

Calls 7

remove_taker_feeMethod · 0.80
scale_quote_amountMethod · 0.80
scale_base_amountMethod · 0.80
unscale_order_summaryMethod · 0.80
referral_feeMethod · 0.80
taker_feeMethod · 0.80
check_accountsFunction · 0.70

Tested by

no test coverage detected