Skip to content

Commit

Permalink
take into account open orders when placing reduce only order on luna …
Browse files Browse the repository at this point in the history
…perp; use perp market pubkey instead of market index for determining if luna market in price bands check
  • Loading branch information
dafyddd committed May 14, 2022
1 parent a25df7d commit 8b52fdd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Deployed: | Slot:
1. Force reduce only for perp market advanced order
2. Allow LUNA-PERP bids to be as high as 9c
3. Fix LUNA spot orders
4. Fix LUNA-PERP reduce only to take into account open orders

## v3.4.5
Deployed: May 12, 2022 at 14:29:36 UTC | Slot: 133,529,809
Expand Down
3 changes: 3 additions & 0 deletions program/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub mod luna_spot_market {

pub mod luna_perp_market {
use solana_program::declare_id;
#[cfg(feature = "devnet")]
declare_id!("CuYEjB3vneuzpimL5iZXeiQRHuDwRefcVbJSsBPoxgwF");
#[cfg(not(feature = "devnet"))]
declare_id!("BCJrpvsB2BJtqiDgKVC4N6gyX1y24Jz96C6wMraYmXss");
}

Expand Down
28 changes: 15 additions & 13 deletions program/src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ const NODE_SIZE: usize = 88;
/// This exists as a guard against excessive compute use.
const DROP_EXPIRED_ORDER_LIMIT: usize = 5;

const LUNA_MARKET_INDEX: usize = 13;

#[derive(IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum NodeTag {
Expand Down Expand Up @@ -949,6 +947,7 @@ impl<'a> Book<'a> {
now_ts: u64,
referrer_mango_account_ai: Option<&AccountInfo>,
limit: u8,
is_luna_market: bool,
) -> MangoResult {
match side {
Side::Bid => self.new_bid(
Expand All @@ -971,6 +970,7 @@ impl<'a> Book<'a> {
now_ts,
referrer_mango_account_ai,
limit,
is_luna_market,
),
Side::Ask => self.new_ask(
program_id,
Expand Down Expand Up @@ -1008,7 +1008,7 @@ impl<'a> Book<'a> {
max_quote_quantity: i64, // guaranteed to be greater than zero due to initial check
order_type: OrderType,
now_ts: u64,
market_index: usize,
is_luna_market: bool,
) -> MangoResult<(i64, i64, i64, i64)> {
let (mut taker_base, mut taker_quote, mut bids_quantity, asks_quantity) = (0, 0, 0i64, 0);

Expand All @@ -1032,11 +1032,11 @@ impl<'a> Book<'a> {

// Temporary hard coding LUNA price limit for bid to be below 10c.
// This is safe because it's already in reduce only mode
if market_index == LUNA_MARKET_INDEX && native_price >= market.lot_to_native_price(10) {
msg!(
"Posting on book disallowed due to price limits. Price must be below 10 cents."
);
post_allowed = false;
if is_luna_market {
if native_price >= market.lot_to_native_price(10) {
msg!("Posting on book disallowed due to price limits. Price must be below 10 cents.");
post_allowed = false;
}
} else if native_price.checked_div(oracle_price).unwrap() > info.maint_liab_weight {
msg!("Posting on book disallowed due to price limits");
post_allowed = false;
Expand Down Expand Up @@ -1164,6 +1164,7 @@ impl<'a> Book<'a> {
now_ts: u64,
referrer_mango_account_ai: Option<&AccountInfo>,
mut limit: u8, // max number of FillEvents allowed; guaranteed to be greater than 0
is_luna_market: bool,
) -> MangoResult {
// TODO proper error handling
// TODO handle the case where we run out of compute (right now just fails)
Expand All @@ -1188,11 +1189,11 @@ impl<'a> Book<'a> {

// Temporary hard coding LUNA price limit for bid to be below 10c.
// This is safe because it's already in reduce only mode
if market_index == LUNA_MARKET_INDEX && native_price >= market.lot_to_native_price(10) {
msg!(
"Posting on book disallowed due to price limits. Price must be below 10 cents."
);
post_allowed = false;
if is_luna_market {
if native_price >= market.lot_to_native_price(10) {
msg!("Posting on book disallowed due to price limits. Price must be below 10 cents.");
post_allowed = false;
}
} else if native_price.checked_div(oracle_price).unwrap() > info.maint_liab_weight {
msg!("Posting on book disallowed due to price limits");
post_allowed = false;
Expand Down Expand Up @@ -2407,6 +2408,7 @@ mod tests {
now_ts,
None,
u8::MAX,
false,
)
.unwrap();
mango_account.orders[0]
Expand Down
19 changes: 18 additions & 1 deletion program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,13 @@ impl Processor {

if (side == Side::Bid && base_pos > 0) || (side == Side::Ask && base_pos < 0) {
0
} else if is_luna_market {
// Take into account outstanding open orders as well
let on_orders = match side {
Side::Bid => mango_account.perp_accounts[market_index].bids_quantity,
Side::Ask => mango_account.perp_accounts[market_index].asks_quantity,
};
base_pos.abs().checked_sub(on_orders).unwrap().min(quantity).max(0)
} else {
base_pos.abs().min(quantity)
}
Expand Down Expand Up @@ -2592,6 +2599,7 @@ impl Processor {
now_ts,
referrer_mango_account_ai,
u8::MAX,
is_luna_market,
)?;

health_cache.update_perp_val(&mango_group, &mango_cache, &mango_account, market_index)?;
Expand Down Expand Up @@ -2724,6 +2732,13 @@ impl Processor {

if (side == Side::Bid && base_pos > 0) || (side == Side::Ask && base_pos < 0) {
0
} else if is_luna_market {
// Take into account outstanding open orders as well
let on_orders = match side {
Side::Bid => mango_account.perp_accounts[market_index].bids_quantity,
Side::Ask => mango_account.perp_accounts[market_index].asks_quantity,
};
base_pos.abs().checked_sub(on_orders).unwrap().min(max_base_quantity).max(0)
} else {
base_pos.abs().min(max_base_quantity)
}
Expand Down Expand Up @@ -2755,6 +2770,7 @@ impl Processor {
now_ts,
referrer_mango_account_ai,
limit,
is_luna_market,
)?;

health_cache.update_perp_val(&mango_group, &mango_cache, &mango_account, market_index)?;
Expand Down Expand Up @@ -5527,7 +5543,7 @@ impl Processor {
i64::MAX,
order.order_type,
now_ts,
market_index,
is_luna_market,
)?,
Side::Ask => book.sim_new_ask(
&perp_market,
Expand Down Expand Up @@ -5586,6 +5602,7 @@ impl Processor {
now_ts,
None,
u8::MAX,
is_luna_market,
)?;

// TODO OPT - unnecessary, remove after testing
Expand Down

0 comments on commit 8b52fdd

Please sign in to comment.