Skip to content

Commit

Permalink
Added limits for tick bound in run market order
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Feb 21, 2024
1 parent 60f9b78 commit 9ed7c6e
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 3 deletions.
10 changes: 7 additions & 3 deletions contracts/orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn run_limit_order(
Ok(fulfilment_msgs)
}

// TODO: Finish making this generic
#[allow(clippy::manual_range_contains)]
pub fn run_market_order(
storage: &mut dyn Storage,
order: &mut MarketOrder,
Expand All @@ -183,7 +183,9 @@ pub fn run_market_order(
OrderDirection::Ask => {
if let Some(tick_bound) = tick_bound {
ensure!(
tick_bound <= orderbook.next_bid_tick,
tick_bound <= orderbook.next_bid_tick
&& tick_bound <= MAX_TICK
&& tick_bound >= MIN_TICK,
ContractError::InvalidTickId {
tick_id: tick_bound
}
Expand All @@ -194,7 +196,9 @@ pub fn run_market_order(
OrderDirection::Bid => {
if let Some(tick_bound) = tick_bound {
ensure!(
tick_bound >= orderbook.next_ask_tick,
tick_bound >= orderbook.next_ask_tick
&& tick_bound <= MAX_TICK
&& tick_bound >= MIN_TICK,
ContractError::InvalidTickId {
tick_id: tick_bound
}
Expand Down
148 changes: 148 additions & 0 deletions contracts/orderbook/src/tests/test_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,72 @@ fn test_run_market_order() {
expected_remainder: Uint128::from(50u128),
expected_error: Some(ContractError::InvalidTickId { tick_id: 0 }),
},
RunMarketOrderTestCase {
name: "tick too large",
placed_order: MarketOrder::new(
valid_book_id,
Uint128::from(100u128),
OrderDirection::Bid,
Addr::unchecked("creator"),
),
extra_orders: vec![LimitOrder::new(
valid_book_id,
2,
1,
OrderDirection::Ask,
Addr::unchecked("creator"),
Uint128::from(150u128),
)],
tick_bound: Some(MAX_TICK + 1),
expected_fulfilments: vec![Fulfilment::new(
LimitOrder::new(
valid_book_id,
1,
0,
OrderDirection::Ask,
Addr::unchecked("creator"),
Uint128::from(50u128),
),
Uint128::from(50u128),
)],
expected_remainder: Uint128::from(50u128),
expected_error: Some(ContractError::InvalidTickId {
tick_id: MAX_TICK + 1,
}),
},
RunMarketOrderTestCase {
name: "tick too small",
placed_order: MarketOrder::new(
valid_book_id,
Uint128::from(100u128),
OrderDirection::Bid,
Addr::unchecked("creator"),
),
extra_orders: vec![LimitOrder::new(
valid_book_id,
2,
1,
OrderDirection::Ask,
Addr::unchecked("creator"),
Uint128::from(150u128),
)],
tick_bound: Some(MIN_TICK - 1),
expected_fulfilments: vec![Fulfilment::new(
LimitOrder::new(
valid_book_id,
1,
0,
OrderDirection::Ask,
Addr::unchecked("creator"),
Uint128::from(50u128),
),
Uint128::from(50u128),
)],
expected_remainder: Uint128::from(50u128),
expected_error: Some(ContractError::InvalidTickId {
tick_id: MIN_TICK - 1,
}),
},
];

for test in test_cases {
Expand Down Expand Up @@ -1957,6 +2023,88 @@ fn test_run_limit_order() {
expected_remainder: Uint128::zero(),
expected_error: Some(ContractError::MismatchedOrderDirection {}),
},
RunLimitOrderTestCase {
name: "tick too large",
order: LimitOrder::new(
valid_book_id,
MAX_TICK + 1,
0,
OrderDirection::Bid,
Addr::unchecked("creator"),
Uint128::from(100u128),
),
expected_fulfilments: vec![
Fulfilment::new(
LimitOrder::new(
valid_book_id,
1,
0,
OrderDirection::Ask,
Addr::unchecked("maker1"),
Uint128::from(25u128),
),
Uint128::from(25u128),
),
Fulfilment::new(
LimitOrder::new(
valid_book_id,
1,
1,
OrderDirection::Ask,
Addr::unchecked("maker2"),
Uint128::from(150u128),
),
Uint128::from(50u128),
),
],
expected_bank_msgs: vec![],
expected_liquidity: vec![],
expected_remainder: Uint128::zero(),
expected_error: Some(ContractError::InvalidTickId {
tick_id: MAX_TICK + 1,
}),
},
RunLimitOrderTestCase {
name: "tick too small",
order: LimitOrder::new(
valid_book_id,
MIN_TICK - 1,
0,
OrderDirection::Bid,
Addr::unchecked("creator"),
Uint128::from(100u128),
),
expected_fulfilments: vec![
Fulfilment::new(
LimitOrder::new(
valid_book_id,
1,
0,
OrderDirection::Ask,
Addr::unchecked("maker1"),
Uint128::from(25u128),
),
Uint128::from(25u128),
),
Fulfilment::new(
LimitOrder::new(
valid_book_id,
1,
1,
OrderDirection::Ask,
Addr::unchecked("maker2"),
Uint128::from(150u128),
),
Uint128::from(50u128),
),
],
expected_bank_msgs: vec![],
expected_liquidity: vec![],
expected_remainder: Uint128::zero(),
expected_error: Some(ContractError::InvalidTickId {
tick_id: MIN_TICK - 1,
}),
},
];

for test in test_cases {
Expand Down

0 comments on commit 9ed7c6e

Please sign in to comment.