Skip to content

Commit

Permalink
Merge pull request #81 from osmosis-labs/connor/rebalancing
Browse files Browse the repository at this point in the history
[Sumtree]: Rebalancing
  • Loading branch information
crnbarr93 authored Mar 28, 2024
2 parents 243a6e9 + dd4de96 commit 840ea2e
Show file tree
Hide file tree
Showing 6 changed files with 1,870 additions and 224 deletions.
1 change: 1 addition & 0 deletions contracts/sumtree-orderbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ thiserror = { version = "1.0.49" }

[dev-dependencies]
cw-multi-test = "0.18.0"
rand = "0.8.4"
17 changes: 10 additions & 7 deletions contracts/sumtree-orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::constants::{MAX_TICK, MIN_TICK};
use crate::error::ContractError;
use crate::state::{new_order_id, orders, ORDERBOOKS, TICK_STATE};
use crate::sumtree::node::{generate_node_id, NodeType, TreeNode};
use crate::sumtree::tree::TREE;
use crate::sumtree::tree::{get_root_node, TREE};
use crate::types::{LimitOrder, OrderDirection, REPLY_ID_REFUND};
use cosmwasm_std::{
coin, ensure, ensure_eq, BankMsg, Decimal256, DepsMut, Env, MessageInfo, Response, SubMsg,
Expand Down Expand Up @@ -152,14 +152,18 @@ pub fn cancel_limit(
);

// Fetch the sumtree from storage, or create one if it does not exist
let mut tree = TREE
.load(deps.storage, &(order.book_id, order.tick_id))
.unwrap_or(TreeNode::new(
let mut tree = if let Ok(tree) = get_root_node(deps.storage, book_id, tick_id) {
tree
} else {
let new_root = TreeNode::new(
order.book_id,
order.tick_id,
generate_node_id(deps.storage, order.book_id, order.tick_id)?,
generate_node_id(deps.storage, book_id, tick_id)?,
NodeType::default(),
));
);
TREE.save(deps.storage, &(book_id, tick_id), &new_root.key)?;
new_root
};

// Generate info for new node to insert to sumtree
let node_id = generate_node_id(deps.storage, order.book_id, order.tick_id)?;
Expand Down Expand Up @@ -216,7 +220,6 @@ pub fn cancel_limit(
)?;

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

Ok(Response::new()
.add_attribute("method", "cancelLimit")
Expand Down
Loading

0 comments on commit 840ea2e

Please sign in to comment.