Skip to content

Commit

Permalink
Added sumtree test for cancellations
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Mar 19, 2024
1 parent 21bc366 commit b714cd8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contracts/sumtree-orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ pub fn cancel_limit(
&curr_tick_state,
)?;

tree.save(deps.storage)?;
TREE.save(deps.storage, &(book_id, tick_id), &tree)?;

Ok(Response::new()
.add_attribute("method", "cancelLimit")
.add_attribute("owner", info.sender)
Expand Down
32 changes: 32 additions & 0 deletions contracts/sumtree-orderbook/src/tests/test_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use crate::{
order::*,
orderbook::*,
state::*,
sumtree::{
node::{NodeType, TreeNode},
tree::TREE,
},
types::{OrderDirection, REPLY_ID_REFUND},
};
use cosmwasm_std::{coin, Addr, BankMsg, Coin, Empty, SubMsg, Uint128, Uint256};
Expand Down Expand Up @@ -296,6 +300,7 @@ fn test_place_limit() {
"{}",
format_test_name(test.name)
);
assert_eq!(order.etas, Decimal256::zero());

// Validate liquidity updated as intended
let state = TICK_STATE
Expand Down Expand Up @@ -569,5 +574,32 @@ fn test_cancel_limit() {
"{}",
format_test_name(test.name)
);

// -- Sumtree --

// Ensure tree is saved correctly
let tree = TREE
.load(deps.as_ref().storage, &(valid_book_id, test.tick_id))
.unwrap();

// Traverse the tree to check its form
let res = tree.traverse(deps.as_ref().storage).unwrap();
let mut root_node = TreeNode::new(
valid_book_id,
test.tick_id,
1,
NodeType::internal(test.quantity, (0u128, test.quantity)),
);
let mut cancelled_node = TreeNode::new(
valid_book_id,
test.tick_id,
2,
NodeType::leaf(0u128, test.quantity),
);
root_node.left = Some(cancelled_node.key);
cancelled_node.parent = Some(root_node.key);

// Ensure tree traversal returns expected ordering
assert_eq!(res, vec![root_node, cancelled_node])
}
}

0 comments on commit b714cd8

Please sign in to comment.