Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edit order instructions for spot and perp #191

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
385 changes: 385 additions & 0 deletions program/src/instruction.rs

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions program/src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,37 @@ impl BookSide {
Some(removed_leaf)
}

fn get_by_key(&self, search_key: i128) -> Option<LeafNode> {
let parent_h = self.root()?;
let (mut child_h, _crit_bit) = match self.get(parent_h).unwrap().case().unwrap() {
NodeRef::Leaf(&leaf) if leaf.key == search_key => {
assert_eq!(self.leaf_count, 1);
return Some(leaf);
}
NodeRef::Leaf(_) => return None,
NodeRef::Inner(inner) => inner.walk_down(search_key),
};
dboures marked this conversation as resolved.
Show resolved Hide resolved

// walk down the tree until finding the key
loop {
match self.get(child_h).unwrap().case().unwrap() {
NodeRef::Inner(inner) => {
let (new_child_h, _new_crit_bit) = inner.walk_down(search_key);
child_h = new_child_h;
}
NodeRef::Leaf(leaf) => {
if leaf.key != search_key {
return None;
dboures marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
}
}
let leaf: LeafNode = *self.get(child_h).unwrap().as_leaf().unwrap();

Some(leaf)
}

fn remove(&mut self, key: NodeHandle) -> Option<AnyNode> {
let val = *self.get(key)?;

Expand Down Expand Up @@ -1674,6 +1705,17 @@ impl<'a> Book<'a> {
Ok(())
}

pub fn get_order(&self, order_id: i128, side: Side) -> MangoResult<LeafNode> {
match side {
Side::Bid => {
self.bids.get_by_key(order_id).ok_or(throw_err!(MangoErrorCode::InvalidOrderId))
}
Side::Ask => {
self.asks.get_by_key(order_id).ok_or(throw_err!(MangoErrorCode::InvalidOrderId))
}
}
}

pub fn cancel_order(&mut self, order_id: i128, side: Side) -> MangoResult<LeafNode> {
match side {
Side::Bid => {
Expand Down
Loading