Skip to content

Commit

Permalink
Converted validation checks to ensures
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Feb 18, 2024
1 parent 1820519 commit 33ae708
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
30 changes: 17 additions & 13 deletions contracts/orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::error::ContractError;
use crate::state::*;
use crate::state::{MAX_TICK, MIN_TICK, ORDERBOOKS};
use crate::types::{LimitOrder, OrderDirection};
use cosmwasm_std::{coin, BankMsg, DepsMut, Env, MessageInfo, Response, Uint128};
use cosmwasm_std::{coin, ensure, BankMsg, DepsMut, Env, MessageInfo, Response, Uint128};
use cw_utils::must_pay;

#[allow(clippy::manual_range_contains, clippy::uninlined_format_args)]
pub fn place_limit(
deps: DepsMut,
env: Env,
Expand All @@ -20,14 +21,16 @@ pub fn place_limit(
.map_err(|_| ContractError::InvalidBookId { book_id })?;

// Validate tick_id is within valid range
if tick_id < MIN_TICK || tick_id > MAX_TICK {
return Err(ContractError::InvalidTickId { tick_id });
}
ensure!(
tick_id >= MIN_TICK && tick_id <= MAX_TICK,
ContractError::InvalidTickId { tick_id }
);

// Validate order_quantity is positive
if quantity <= Uint128::zero() {
return Err(ContractError::InvalidQuantity { quantity });
}
// Ensure order_quantity is positive
ensure!(
quantity > Uint128::zero(),
ContractError::InvalidQuantity { quantity }
);

// Determine the correct denom based on order direction
let expected_denom = match order_direction {
Expand All @@ -38,12 +41,13 @@ pub fn place_limit(
// Verify the funds sent with the message match the `quantity` for the correct denom
// We reject any quantity that is not exactly equal to the amount in the limit order being placed
let received = must_pay(&info, &expected_denom)?;
if received != quantity {
return Err(ContractError::InsufficientFunds {
ensure!(
received == quantity,
ContractError::InsufficientFunds {
sent: received,
required: quantity,
});
}
}
);

// Generate a new order ID
let order_id = new_order_id(deps.storage)?;
Expand All @@ -53,7 +57,7 @@ pub fn place_limit(
book_id,
tick_id,
order_id,
order_direction.clone(),
order_direction,
info.sender.clone(),
quantity,
);
Expand Down
1 change: 1 addition & 0 deletions contracts/orderbook/src/tests/test_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct PlaceLimitTestCase {
expected_error: Option<ContractError>,
}

#[allow(clippy::uninlined_format_args)]
fn format_test_name(name: &str) -> String {
format!("\n\nTest case failed: {}\n", name)
}
Expand Down

0 comments on commit 33ae708

Please sign in to comment.