Skip to content

Commit

Permalink
Remove duplicated cancel by id logic
Browse files Browse the repository at this point in the history
  • Loading branch information
brittcyr committed Oct 13, 2024
1 parent 694fc0a commit cc36ad6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 42 deletions.
7 changes: 2 additions & 5 deletions programs/manifest/src/program/processor/batch_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,8 @@ pub(crate) fn process_batch_update(
"Invalid cancel hint sequence number index {}",
hinted_cancel_index,
)?;
dynamic_account.cancel_order_by_index(
trader_index,
hinted_cancel_index,
&global_trade_accounts_opts,
)?;
dynamic_account
.cancel_order_by_index(hinted_cancel_index, &global_trade_accounts_opts)?;
}
};

Expand Down
6 changes: 1 addition & 5 deletions programs/manifest/src/program/processor/global_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ pub(crate) fn process_global_clean(
// Should drop global, but cancel_order_by_index actually does not need to
// borrow in this case.

market_dynamic_account.cancel_order_by_index(
maker_index,
order_index,
&global_trade_accounts,
)?;
market_dynamic_account.cancel_order_by_index(order_index, &global_trade_accounts)?;

// The global account itself only accounting on remove_order is that it
// tracks unclaimed gas deposits for informational purposes and this is
Expand Down
34 changes: 2 additions & 32 deletions programs/manifest/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,7 @@ impl<
}
if index_to_remove != NIL {
// Cancel order by index will update balances.
self.cancel_order_by_index(
trader_index,
index_to_remove,
global_trade_accounts_opts,
)?;
self.cancel_order_by_index(index_to_remove, global_trade_accounts_opts)?;
return Ok(());
}
}
Expand All @@ -1114,38 +1110,12 @@ impl<

pub fn cancel_order_by_index(
&mut self,
trader_index: DataIndex,
order_index: DataIndex,
global_trade_accounts_opts: &[Option<GlobalTradeAccounts>; 2],
) -> ProgramResult {
let DynamicAccount { fixed, dynamic } = self.borrow_mut();

let resting_order: &RestingOrder =
get_helper::<RBNode<RestingOrder>>(dynamic, order_index).get_value();
let is_bid: bool = resting_order.get_is_bid();
let amount_atoms: u64 = if is_bid {
(resting_order
.get_price()
.checked_quote_for_base(resting_order.get_num_base_atoms(), true)
.unwrap())
.into()
} else {
resting_order.get_num_base_atoms().into()
};

// Update the accounting for the order that was just canceled.
if resting_order.is_global() {
let global_trade_accounts_opt: &Option<GlobalTradeAccounts> = if is_bid {
&global_trade_accounts_opts[1]
} else {
&global_trade_accounts_opts[0]
};
remove_from_global(&global_trade_accounts_opt)?
} else {
update_balance(dynamic, trader_index, !is_bid, true, amount_atoms)?;
}
remove_order_from_tree_and_free(fixed, dynamic, order_index, is_bid)?;

remove_and_update_balances(fixed, dynamic, order_index, global_trade_accounts_opts)?;
Ok(())
}
}
Expand Down

0 comments on commit cc36ad6

Please sign in to comment.