Skip to content

Commit

Permalink
Changed reply to use ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Feb 18, 2024
1 parent 6ce5e83 commit 254ad52
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
11 changes: 6 additions & 5 deletions contracts/orderbook/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult};
use cosmwasm_std::{ensure, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult};
use cw2::set_contract_version;

use crate::error::ContractError;
Expand Down Expand Up @@ -100,11 +100,12 @@ pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
// With `Response` type, it is still possible to dispatch message to invoke external logic.
// See: https://github.com/CosmWasm/cosmwasm/blob/main/SEMANTICS.md#dispatching-messages
if msg.result.is_err() {
return Err(ContractError::ReplyError {
ensure!(
msg.result.is_ok(),
ContractError::ReplyError {
id: msg.id,
error: msg.result.unwrap_err(),
});
}
}
);
todo!()
}
4 changes: 1 addition & 3 deletions contracts/orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ pub fn cancel_limit(
orders().remove(deps.storage, &key)?;

// Update tick liquidity
TICK_LIQUIDITY.update(deps.storage, &(book_id, tick_id), |liquidity| {
Ok::<Uint128, ContractError>(liquidity.unwrap_or_default().checked_sub(order.quantity)?)
})?;
reduce_tick_liquidity(deps.storage, book_id, tick_id, order.quantity)?;

// Get orderbook info for correct denomination
let orderbook =
Expand Down
21 changes: 20 additions & 1 deletion contracts/orderbook/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ pub fn new_order_id(storage: &mut dyn Storage) -> Result<u64, ContractError> {
Ok(id)
}

/// Reduces the liquidity of a tick by the specified amount and removes it if no liquidity remains.
pub fn reduce_tick_liquidity(
storage: &mut dyn Storage,
book_id: u64,
tick_id: i64,
amount: Uint128,
) -> Result<(), ContractError> {
let tick_liquidity = TICK_LIQUIDITY
.may_load(storage, &(book_id, tick_id))?
.ok_or(ContractError::InvalidTickId { tick_id })?;
let new_liquidity = tick_liquidity.checked_sub(amount)?;
if new_liquidity.is_zero() {
TICK_LIQUIDITY.remove(storage, &(book_id, tick_id));
} else {
TICK_LIQUIDITY.save(storage, &(book_id, tick_id), &new_liquidity)?;
}
Ok(())
}

/// Retrieves a list of `LimitOrder` filtered by the specified `FilterOwnerOrders`.
///
/// This function allows for filtering orders based on the owner's address, optionally further
Expand Down Expand Up @@ -119,6 +138,6 @@ pub fn get_orders_by_owner(
.filter_map(|item| item.ok())
.map(|(_, order)| order)
.collect();

Ok(orders)
}

0 comments on commit 254ad52

Please sign in to comment.