diff --git a/src/action/execute/cancel_margin_order.rs b/src/action/execute/cancel_margin_order.rs index bea1411..7fae102 100644 --- a/src/action/execute/cancel_margin_order.rs +++ b/src/action/execute/cancel_margin_order.rs @@ -38,6 +38,7 @@ pub fn cancel_margin_order( ); MARGIN_ORDER.save(deps.storage, order_id, &order)?; + PENDING_MARGIN_ORDER.remove(deps.storage, order.order_id); if order_type == MarginOrderType::LimitOpen { Ok(resp.add_message(CosmosMsg::Bank(refund_msg))) diff --git a/src/action/execute/cancel_margin_orders.rs b/src/action/execute/cancel_margin_orders.rs index 1aa89cd..c72a1cf 100644 --- a/src/action/execute/cancel_margin_orders.rs +++ b/src/action/execute/cancel_margin_orders.rs @@ -69,6 +69,7 @@ pub fn cancel_margin_orders( for order in orders.iter_mut() { order.status = Status::Canceled; MARGIN_ORDER.save(deps.storage, order.order_id, order)?; + PENDING_MARGIN_ORDER.remove(deps.storage, order.order_id); } let order_ids: Vec = orders.iter().map(|order| order.order_id).collect(); diff --git a/src/action/execute/cancel_spot_order.rs b/src/action/execute/cancel_spot_order.rs index 01639f4..978b910 100644 --- a/src/action/execute/cancel_spot_order.rs +++ b/src/action/execute/cancel_spot_order.rs @@ -19,11 +19,12 @@ pub fn cancel_spot_order( if order.status != Status::Pending { return Err(ContractError::CancelStatusError { order_id, - status: order.status.clone(), + status: order.status, }); } order.status = Status::Canceled; + let refund_msg = BankMsg::Send { to_address: order.owner_address.to_string(), amount: vec![order.order_amount.clone()], @@ -34,6 +35,7 @@ pub fn cancel_spot_order( .add_event(Event::new("cancel_spot_order").add_attribute("order_id", order_id.to_string())); SPOT_ORDER.save(deps.storage, order_id, &order)?; + PENDING_SPOT_ORDER.remove(deps.storage, order_id); Ok(resp) } diff --git a/src/action/execute/cancel_spot_orders.rs b/src/action/execute/cancel_spot_orders.rs index 9175175..579a956 100644 --- a/src/action/execute/cancel_spot_orders.rs +++ b/src/action/execute/cancel_spot_orders.rs @@ -72,6 +72,7 @@ pub fn cancel_spot_orders( for order in orders.iter_mut() { order.status = Status::Canceled; SPOT_ORDER.save(deps.storage, order.order_id, &order)?; + PENDING_SPOT_ORDER.remove(deps.storage, order.order_id); } let order_ids: Vec = orders.iter().map(|order| order.order_id).collect(); diff --git a/src/action/execute/create_margin_order.rs b/src/action/execute/create_margin_order.rs index 51fae5a..aa85f01 100644 --- a/src/action/execute/create_margin_order.rs +++ b/src/action/execute/create_margin_order.rs @@ -148,6 +148,9 @@ fn create_margin_open_order( let order_id = order.order_id; MARGIN_ORDER.save(deps.storage, order_id, &order)?; + if order.order_type != MarginOrderType::MarketOpen { + PENDING_MARGIN_ORDER.save(deps.storage, order_id, &order)?; + } let resp = Response::new().add_event( Event::new("create_margin_open_order") @@ -250,6 +253,9 @@ fn create_margin_close_order( let order_id = order.order_id; MARGIN_ORDER.save(deps.storage, order_id, &order)?; + if order.order_type != MarginOrderType::MarketClose { + PENDING_MARGIN_ORDER.save(deps.storage, order_id, &order)?; + } let resp = Response::new().add_event( Event::new("create_margin_close_order") diff --git a/src/action/execute/create_spot_order.rs b/src/action/execute/create_spot_order.rs index 87a0edc..450d018 100644 --- a/src/action/execute/create_spot_order.rs +++ b/src/action/execute/create_spot_order.rs @@ -82,6 +82,9 @@ pub fn create_spot_order( )?; SPOT_ORDER.save(deps.storage, new_order.order_id, &new_order)?; + if new_order.order_type != SpotOrderType::MarketBuy { + PENDING_SPOT_ORDER.save(deps.storage, new_order.order_id, &new_order)?; + } Ok(resp) } diff --git a/src/action/reply/close_margin_position.rs b/src/action/reply/close_margin_position.rs index a1e1313..ef9842e 100644 --- a/src/action/reply/close_margin_position.rs +++ b/src/action/reply/close_margin_position.rs @@ -24,6 +24,7 @@ pub fn reply_to_close_margin_order( order.status = Status::Executed; + PENDING_MARGIN_ORDER.remove(deps.storage, order.order_id); MARGIN_ORDER.save(deps.storage, order_id, &order)?; let resp: Response = Response::new().add_event( diff --git a/src/action/reply/open_margin_position.rs b/src/action/reply/open_margin_position.rs index 77bca57..47433fb 100644 --- a/src/action/reply/open_margin_position.rs +++ b/src/action/reply/open_margin_position.rs @@ -24,6 +24,7 @@ pub fn reply_to_open_margin_position( order.status = Status::Executed; + PENDING_MARGIN_ORDER.remove(deps.storage, order.order_id); MARGIN_ORDER.save(deps.storage, order_id, &order)?; let resp: Response = Response::new().add_event( diff --git a/src/action/reply/spot_order.rs b/src/action/reply/spot_order.rs index d62c699..7238888 100644 --- a/src/action/reply/spot_order.rs +++ b/src/action/reply/spot_order.rs @@ -23,6 +23,7 @@ pub fn reply_to_spot_order( order.status = Status::Executed; SPOT_ORDER.save(deps.storage, order_id, &order)?; + PENDING_SPOT_ORDER.remove(deps.storage, order.order_id); let resp: Response = Response::new().add_event( Event::new("reply_to_spot_order").add_attribute("order_id", order_id.to_string()), diff --git a/src/action/sudo/process_orders.rs b/src/action/sudo/process_orders.rs index f7358a0..e8307a8 100644 --- a/src/action/sudo/process_orders.rs +++ b/src/action/sudo/process_orders.rs @@ -11,12 +11,12 @@ pub fn process_orders( deps: DepsMut, env: Env, ) -> Result, ContractError> { - let spot_orders: Vec = SPOT_ORDER + let spot_orders: Vec = PENDING_SPOT_ORDER .prefix_range(deps.storage, None, None, Order::Ascending) .filter_map(|res| res.ok().map(|r| r.1)) .collect(); - let margin_orders: Vec = MARGIN_ORDER + let margin_orders: Vec = PENDING_MARGIN_ORDER .prefix_range(deps.storage, None, None, Order::Ascending) .filter_map(|res| res.ok().map(|r| r.1)) .collect(); diff --git a/src/states/margin_order.rs b/src/states/margin_order.rs index f970542..008cd97 100644 --- a/src/states/margin_order.rs +++ b/src/states/margin_order.rs @@ -2,3 +2,5 @@ use crate::types::MarginOrder; use cw_storage_plus::Map; pub const MARGIN_ORDER: Map = Map::new("margin order"); + +pub const PENDING_MARGIN_ORDER: Map = Map::new("unprocess margin order"); diff --git a/src/states/mod.rs b/src/states/mod.rs index 0846e32..3e98fca 100644 --- a/src/states/mod.rs +++ b/src/states/mod.rs @@ -2,6 +2,6 @@ mod margin_order; mod reply_info; mod spot_order; -pub use margin_order::MARGIN_ORDER; +pub use margin_order::{MARGIN_ORDER, PENDING_MARGIN_ORDER}; pub use reply_info::{MAX_REPLY_ID, REPLY_INFO}; -pub use spot_order::{SPOT_ORDER, SPOT_ORDER_MAX_ID}; +pub use spot_order::{PENDING_SPOT_ORDER, SPOT_ORDER, SPOT_ORDER_MAX_ID}; diff --git a/src/states/spot_order.rs b/src/states/spot_order.rs index 4e1a846..b208131 100644 --- a/src/states/spot_order.rs +++ b/src/states/spot_order.rs @@ -4,3 +4,5 @@ use cw_storage_plus::{Item, Map}; pub const SPOT_ORDER: Map = Map::new("spot order"); pub const SPOT_ORDER_MAX_ID: Item = Item::new("spot order max id"); + +pub const PENDING_SPOT_ORDER: Map = Map::new("unprocess spot order"); diff --git a/src/tests/mock/instantiate.rs b/src/tests/mock/instantiate.rs index 912a056..3c1910b 100644 --- a/src/tests/mock/instantiate.rs +++ b/src/tests/mock/instantiate.rs @@ -1,6 +1,6 @@ use crate::{ states::*, - types::{MarginOrder, SpotOrder}, + types::{MarginOrder, SpotOrder, Status}, }; use cosmwasm_schema::cw_serde; use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdResult}; @@ -20,9 +20,15 @@ pub fn instantiate( ) -> StdResult> { for order in msg.spot_orders.iter() { SPOT_ORDER.save(deps.storage, order.order_id, order)?; + if order.status == Status::Pending { + PENDING_SPOT_ORDER.save(deps.storage, order.order_id, order)?; + } } for order in msg.margin_orders.iter() { MARGIN_ORDER.save(deps.storage, order.order_id, order)?; + if order.status == Status::Pending { + PENDING_MARGIN_ORDER.save(deps.storage, order.order_id, order)?; + } } MAX_REPLY_ID.save(deps.storage, &0)?; SPOT_ORDER_MAX_ID.save(deps.storage, &0)?;