Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
feat: add a pending map for orders to reduce gas price for process order
Browse files Browse the repository at this point in the history
  • Loading branch information
politeWall committed Dec 22, 2023
1 parent 7707c4c commit 9c8978c
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/action/execute/cancel_margin_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions src/action/execute/cancel_margin_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = orders.iter().map(|order| order.order_id).collect();
Expand Down
4 changes: 3 additions & 1 deletion src/action/execute/cancel_spot_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions src/action/execute/cancel_spot_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = orders.iter().map(|order| order.order_id).collect();
Expand Down
6 changes: 6 additions & 0 deletions src/action/execute/create_margin_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/action/execute/create_spot_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/action/reply/close_margin_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElysMsg> = Response::new().add_event(
Expand Down
1 change: 1 addition & 0 deletions src/action/reply/open_margin_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElysMsg> = Response::new().add_event(
Expand Down
1 change: 1 addition & 0 deletions src/action/reply/spot_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElysMsg> = Response::new().add_event(
Event::new("reply_to_spot_order").add_attribute("order_id", order_id.to_string()),
Expand Down
4 changes: 2 additions & 2 deletions src/action/sudo/process_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub fn process_orders(
deps: DepsMut<ElysQuery>,
env: Env,
) -> Result<Response<ElysMsg>, ContractError> {
let spot_orders: Vec<SpotOrder> = SPOT_ORDER
let spot_orders: Vec<SpotOrder> = 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<MarginOrder> = MARGIN_ORDER
let margin_orders: Vec<MarginOrder> = PENDING_MARGIN_ORDER
.prefix_range(deps.storage, None, None, Order::Ascending)
.filter_map(|res| res.ok().map(|r| r.1))
.collect();
Expand Down
2 changes: 2 additions & 0 deletions src/states/margin_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ use crate::types::MarginOrder;
use cw_storage_plus::Map;

pub const MARGIN_ORDER: Map<u64, MarginOrder> = Map::new("margin order");

pub const PENDING_MARGIN_ORDER: Map<u64, MarginOrder> = Map::new("unprocess margin order");
4 changes: 2 additions & 2 deletions src/states/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
2 changes: 2 additions & 0 deletions src/states/spot_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ use cw_storage_plus::{Item, Map};
pub const SPOT_ORDER: Map<u64, SpotOrder> = Map::new("spot order");

pub const SPOT_ORDER_MAX_ID: Item<u64> = Item::new("spot order max id");

pub const PENDING_SPOT_ORDER: Map<u64, SpotOrder> = Map::new("unprocess spot order");
8 changes: 7 additions & 1 deletion src/tests/mock/instantiate.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -20,9 +20,15 @@ pub fn instantiate(
) -> StdResult<Response<ElysMsg>> {
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)?;
Expand Down

0 comments on commit 9c8978c

Please sign in to comment.