From f50b3cc3d417426509323d6da94c1811911c1cd8 Mon Sep 17 00:00:00 2001 From: jelysn <129082781+jelysn@users.noreply.github.com> Date: Sun, 3 Dec 2023 17:28:16 +0800 Subject: [PATCH] Event enhancement (#57) * event enhancement * typo fix & process_spot_orders to process_orders for accurate meaning * fix: replace attributes event_type to event --------- Co-authored-by: politeWall <138504353+politeWall@users.noreply.github.com> --- src/action/execute/cancel_margin_order.rs | 5 ++++- src/action/execute/cancel_spot_order.rs | 2 +- src/action/execute/create_margin_order.rs | 10 ++++++++-- src/action/execute/create_spot_order.rs | 5 ++++- src/action/mod.rs | 5 +++-- src/action/query/get_margin_position.rs | 2 +- src/action/reply/close_margin_position.rs | 8 +++++--- src/action/reply/create_margin_order_market_close.rs | 6 ++++-- src/action/reply/create_margin_order_market_open.rs | 6 ++++-- src/action/reply/open_margin_position.rs | 8 +++++--- src/action/reply/spot_order.rs | 5 +++-- src/action/reply/spot_order_market.rs | 5 +++-- .../sudo/{process_spot_orders.rs => process_orders.rs} | 2 +- src/entry_point/sudo.rs | 2 +- 14 files changed, 47 insertions(+), 24 deletions(-) rename src/action/sudo/{process_spot_orders.rs => process_orders.rs} (99%) diff --git a/src/action/execute/cancel_margin_order.rs b/src/action/execute/cancel_margin_order.rs index 558a5d4..d49c3dc 100644 --- a/src/action/execute/cancel_margin_order.rs +++ b/src/action/execute/cancel_margin_order.rs @@ -34,7 +34,10 @@ pub fn cancel_margin_order( amount: vec![order.collateral.clone()], }; - let resp = Response::new().add_attribute("order_id", order.order_id.to_string()); + let resp = Response::new().add_event( + Event::new("cancel_margin_order") + .add_attribute("margin_order_id", order.order_id.to_string()), + ); MARGIN_ORDER.save(deps.storage, &orders)?; diff --git a/src/action/execute/cancel_spot_order.rs b/src/action/execute/cancel_spot_order.rs index f75da06..aae5e44 100644 --- a/src/action/execute/cancel_spot_order.rs +++ b/src/action/execute/cancel_spot_order.rs @@ -36,7 +36,7 @@ pub fn cancel_spot_order( let resp = Response::new() .add_message(CosmosMsg::Bank(refund_msg)) - .add_attribute("canceled_spot_order_id", order_id.to_string()); + .add_event(Event::new("cancel_spot_order").add_attribute("order_id", order_id.to_string())); SPOT_ORDER.save(deps.storage, &orders_list)?; diff --git a/src/action/execute/create_margin_order.rs b/src/action/execute/create_margin_order.rs index 8263e3a..4de2213 100644 --- a/src/action/execute/create_margin_order.rs +++ b/src/action/execute/create_margin_order.rs @@ -136,7 +136,10 @@ fn create_margin_open_order( MARGIN_ORDER.save(deps.storage, &orders)?; - let resp = Response::new().add_attribute("margin_order_id", order_id.to_string()); + let resp = Response::new().add_event( + Event::new("create_margin_open_order") + .add_attribute("margin_order_id", order_id.to_string()), + ); if order_type != MarketOpen { return Ok(resp); @@ -223,7 +226,10 @@ fn create_margin_close_order( MARGIN_ORDER.save(deps.storage, &orders)?; - let resp = Response::new().add_attribute("margin_order_id", order_id.to_string()); + let resp = Response::new().add_event( + Event::new("create_margin_close_order") + .add_attribute("margin_order_id", order_id.to_string()), + ); if order_type != MarketClose { return Ok(resp); diff --git a/src/action/execute/create_spot_order.rs b/src/action/execute/create_spot_order.rs index e777975..b28179b 100644 --- a/src/action/execute/create_spot_order.rs +++ b/src/action/execute/create_spot_order.rs @@ -112,7 +112,10 @@ fn create_resp( in_route: Vec, ) -> StdResult> { let resp = Response::new() - .add_attribute("order_id", new_order.order_id.to_string()) + .add_event( + Event::new("create_spot_order") + .add_attribute("order_id", new_order.order_id.to_string()), + ) .add_message(bank_msg); // information message if new_order.order_type != SpotOrderType::MarketBuy { diff --git a/src/action/mod.rs b/src/action/mod.rs index 9a8604c..822e415 100644 --- a/src/action/mod.rs +++ b/src/action/mod.rs @@ -1,4 +1,5 @@ use crate::{states::*, types::*, ContractError}; +use cosmwasm_std::Event; use cosmwasm_std::{BankMsg, CosmosMsg, DepsMut, Env, MessageInfo, Response}; use elys_bindings::*; @@ -71,6 +72,6 @@ pub mod reply { pub mod sudo { use super::*; - mod process_spot_orders; - pub use process_spot_orders::process_spot_orders; + mod process_orders; + pub use process_orders::process_orders; } diff --git a/src/action/query/get_margin_position.rs b/src/action/query/get_margin_position.rs index 4f96fff..a43974e 100644 --- a/src/action/query/get_margin_position.rs +++ b/src/action/query/get_margin_position.rs @@ -14,6 +14,6 @@ pub fn get_margin_position( if let Some(_) = &resp.mtp { Ok(resp) } else { - Err(StdError::not_found("margin trading prosition").into()) + Err(StdError::not_found("margin trading position").into()) } } diff --git a/src/action/reply/close_margin_position.rs b/src/action/reply/close_margin_position.rs index 86929af..0acddd2 100644 --- a/src/action/reply/close_margin_position.rs +++ b/src/action/reply/close_margin_position.rs @@ -31,9 +31,11 @@ pub fn reply_to_close_margin_order( MARGIN_ORDER.save(deps.storage, &orders)?; - let resp: Response = Response::new() - .add_attribute("processed_margin_order_id", order_id.to_string()) - .add_attribute("margin_trading_position_closed_id", res.id.to_string()); + let resp: Response = Response::new().add_event( + Event::new("reply_to_close_margin_order") + .add_attribute("margin_order_id", order_id.to_string()) + .add_attribute("margin_trading_position_closed_id", res.id.to_string()), + ); Ok(resp) } diff --git a/src/action/reply/create_margin_order_market_close.rs b/src/action/reply/create_margin_order_market_close.rs index 8684cc5..3dcd716 100644 --- a/src/action/reply/create_margin_order_market_close.rs +++ b/src/action/reply/create_margin_order_market_close.rs @@ -34,8 +34,10 @@ pub fn reply_to_create_margin_market_close( order.status = Status::Processed; - let resp = - Response::new().add_attribute("margin_trading_position_id", margin_resp.id.to_string()); + let resp = Response::new().add_event( + Event::new("reply_to_create_margin_market_close") + .add_attribute("margin_trading_position_id", margin_resp.id.to_string()), + ); Ok(resp) } diff --git a/src/action/reply/create_margin_order_market_open.rs b/src/action/reply/create_margin_order_market_open.rs index 837152b..7dfae2b 100644 --- a/src/action/reply/create_margin_order_market_open.rs +++ b/src/action/reply/create_margin_order_market_open.rs @@ -35,8 +35,10 @@ pub fn reply_to_create_margin_market_open( order.status = Status::Processed; order.position_id = Some(margin_resp.id); - let resp = - Response::new().add_attribute("margin_trading_position_id", margin_resp.id.to_string()); + let resp = Response::new().add_event( + Event::new("reply_to_create_margin_market_open") + .add_attribute("margin_trading_position_id", margin_resp.id.to_string()), + ); Ok(resp) } diff --git a/src/action/reply/open_margin_position.rs b/src/action/reply/open_margin_position.rs index 7387091..65efc28 100644 --- a/src/action/reply/open_margin_position.rs +++ b/src/action/reply/open_margin_position.rs @@ -31,9 +31,11 @@ pub fn reply_to_open_margin_position( MARGIN_ORDER.save(deps.storage, &orders)?; - let resp: Response = Response::new() - .add_attribute("processed_margin_order_id", order_id.to_string()) - .add_attribute("margin_trading_position_opened_id", res.id.to_string()); + let resp: Response = Response::new().add_event( + Event::new("reply_to_open_margin_position") + .add_attribute("margin_order_id", order_id.to_string()) + .add_attribute("margin_trading_position_opened_id", res.id.to_string()), + ); Ok(resp) } diff --git a/src/action/reply/spot_order.rs b/src/action/reply/spot_order.rs index 2384d90..9281514 100644 --- a/src/action/reply/spot_order.rs +++ b/src/action/reply/spot_order.rs @@ -31,8 +31,9 @@ pub fn reply_to_spot_order( SPOT_ORDER.save(deps.storage, &orders)?; - let resp: Response = - Response::new().add_attribute("processed_order_id", order_id.to_string()); + let resp: Response = Response::new().add_event( + Event::new("reply_to_spot_order").add_attribute("order_id", order_id.to_string()), + ); Ok(resp) } diff --git a/src/action/reply/spot_order_market.rs b/src/action/reply/spot_order_market.rs index 879f4ec..a7fcf16 100644 --- a/src/action/reply/spot_order_market.rs +++ b/src/action/reply/spot_order_market.rs @@ -26,8 +26,9 @@ pub fn reply_to_spot_order_market( SPOT_ORDER.save(deps.storage, &orders)?; - let resp: Response = - Response::new().add_attribute("processed_order_id", order_id.to_string()); + let resp: Response = Response::new().add_event( + Event::new("reply_to_spot_order_market").add_attribute("order_id", order_id.to_string()), + ); Ok(resp) } diff --git a/src/action/sudo/process_spot_orders.rs b/src/action/sudo/process_orders.rs similarity index 99% rename from src/action/sudo/process_spot_orders.rs rename to src/action/sudo/process_orders.rs index e6a0a6b..8841ef2 100644 --- a/src/action/sudo/process_spot_orders.rs +++ b/src/action/sudo/process_orders.rs @@ -5,7 +5,7 @@ use std::ops::Div; use super::*; -pub fn process_spot_orders( +pub fn process_orders( deps: DepsMut, env: Env, ) -> Result, ContractError> { diff --git a/src/entry_point/sudo.rs b/src/entry_point/sudo.rs index a378c15..10b934b 100644 --- a/src/entry_point/sudo.rs +++ b/src/entry_point/sudo.rs @@ -9,6 +9,6 @@ pub fn sudo( msg: SudoMsg, ) -> Result, ContractError> { match msg { - SudoMsg::ClockEndBlock {} => process_spot_orders(deps, env), + SudoMsg::ClockEndBlock {} => process_orders(deps, env), } }