Skip to content

Commit

Permalink
Added liquidity tracking to place/cancel limit
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Feb 18, 2024
1 parent db8e5f1 commit b550181
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
3 changes: 1 addition & 2 deletions contracts/orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,10 @@ pub fn cancel_limit(

// Generate refund
let expected_denom = orderbook.get_expected_denom_for_direction(&order.order_direction);
let coin_to_send = coin(order.quantity.u128(), expected_denom);
let refund_msg = SubMsg::reply_on_error(
BankMsg::Send {
to_address: order.owner.to_string(),
amount: vec![coin_to_send],
amount: vec![coin(order.quantity.u128(), expected_denom)],
},
REPLY_ID_REFUND,
);
Expand Down
41 changes: 41 additions & 0 deletions contracts/orderbook/src/tests/test_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ fn test_place_limit() {
.may_load(&deps.storage, &(test.book_id, test.tick_id, 0))
.unwrap();
assert!(order_result.is_none(), "{}", format_test_name(test.name));

// Verifiy liquidity was not updated
let liquidity = TICK_LIQUIDITY
.load(&deps.storage, &(test.book_id, test.tick_id))
.unwrap_or_default();
assert!(liquidity.is_zero(), "{}", format_test_name(test.name));
continue;
}

Expand Down Expand Up @@ -280,6 +286,12 @@ fn test_place_limit() {
"{}",
format_test_name(test.name)
);

// Validate liquidity updated as intended
let liquidity = TICK_LIQUIDITY
.load(&deps.storage, &(test.book_id, test.tick_id))
.unwrap();
assert_eq!(liquidity, test.quantity, "{}", format_test_name(test.name));
}
}

Expand Down Expand Up @@ -395,6 +407,18 @@ fn test_cancel_limit() {
),
)
.unwrap();
// Update tick liquidity
TICK_LIQUIDITY
.update(
deps.as_mut().storage,
&(test.book_id, test.tick_id),
|liquidity| {
Ok::<Uint128, ContractError>(
liquidity.unwrap_or_default().checked_add(test.quantity)?,
)
},
)
.unwrap();
}

// --- System under test ---
Expand Down Expand Up @@ -428,6 +452,16 @@ fn test_cancel_limit() {
"{}",
format_test_name(test.name)
);

// Verify Liqudity was updated as intended
let liquidity = TICK_LIQUIDITY
.load(deps.as_ref().storage, &(test.book_id, test.tick_id))
.unwrap_or_default();
if test.place_order {
assert_eq!(liquidity, test.quantity, "{}", format_test_name(test.name));
} else {
assert!(liquidity.is_zero(), "{}", format_test_name(test.name));
}
continue;
}

Expand Down Expand Up @@ -500,5 +534,12 @@ fn test_cancel_limit() {

// Verify the order's fields
assert!(order.is_none(), "{}", format_test_name(test.name));

// Validate liquidity updated as intended
let liquidity = TICK_LIQUIDITY
.load(deps.as_ref().storage, &(test.book_id, test.tick_id))
.unwrap_or_default();

assert!(liquidity.is_zero(), "{}", format_test_name(test.name));
}
}

0 comments on commit b550181

Please sign in to comment.