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

Commit

Permalink
Merge branch 'main' into price-rate-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond authored Dec 15, 2023
2 parents 844f664 + 8e85597 commit 9a2c006
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 32 deletions.
35 changes: 30 additions & 5 deletions src/action/execute/create_margin_order.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::msg::ReplyType;

use super::*;
use cosmwasm_std::{to_json_binary, Decimal, StdError, StdResult, SubMsg};
use cosmwasm_std::{
to_json_binary, Decimal, OverflowError, OverflowOperation, StdError, StdResult, SubMsg,
};
use cw_utils;
use MarginOrderType::*;

Expand Down Expand Up @@ -92,7 +94,6 @@ fn check_order_type(
fn create_margin_open_order(
info: MessageInfo,
deps: DepsMut<ElysQuery>,

order_type: MarginOrderType,
position: MarginPosition,
trading_asset: String,
Expand Down Expand Up @@ -142,7 +143,7 @@ fn create_margin_open_order(
&take_profit_price,
&trigger_price,
&orders,
);
)?;

let order_id = order.order_id;

Expand All @@ -166,7 +167,19 @@ fn create_margin_open_order(
take_profit_price,
);

let reply_id = MAX_REPLY_ID.load(deps.storage)? + 1;
let reply_info_max_id = MAX_REPLY_ID.load(deps.storage)?;

let reply_id = match reply_info_max_id.checked_add(1) {
Some(id) => id,
None => {
return Err(StdError::overflow(OverflowError::new(
OverflowOperation::Add,
"reply_info_max_id",
"increment one",
))
.into())
}
};
MAX_REPLY_ID.save(deps.storage, &reply_id)?;

let reply_info = ReplyInfo {
Expand Down Expand Up @@ -253,7 +266,19 @@ fn create_margin_close_order(
mtp.custodies[0].amount.u128() as i128,
);

let reply_id = MAX_REPLY_ID.load(deps.storage)? + 1;
let reply_info_max_id = MAX_REPLY_ID.load(deps.storage)?;

let reply_id = match reply_info_max_id.checked_add(1) {
Some(id) => id,
None => {
return Err(StdError::overflow(OverflowError::new(
OverflowOperation::Add,
"reply_info_max_id",
"increment one",
))
.into())
}
};
MAX_REPLY_ID.save(deps.storage, &reply_id)?;

let reply_info = ReplyInfo {
Expand Down
33 changes: 29 additions & 4 deletions src/action/execute/create_spot_order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use cosmwasm_std::{to_json_binary, Decimal, Int128, StdError, StdResult, Storage, SubMsg};
use cosmwasm_std::{
to_json_binary, Decimal, Int128, OverflowError, StdError, StdResult, Storage, SubMsg,
};
use elys_bindings::query_resp::AmmSwapEstimationByDenomResponse;

use crate::msg::ReplyType;
Expand Down Expand Up @@ -42,8 +44,18 @@ pub fn create_spot_order(
&order_target_denom,
&Decimal::zero(),
)?;

let order_id = SPOT_ORDER_MAX_ID.load(deps.storage)? + 1;
let spot_order_max_id = SPOT_ORDER_MAX_ID.load(deps.storage)?;
let order_id = match spot_order_max_id.checked_add(1) {
Some(id) => id,
None => {
return Err(StdError::overflow(OverflowError::new(
cosmwasm_std::OverflowOperation::Add,
"spot_order_max_id",
"increment one",
))
.into())
}
};
SPOT_ORDER_MAX_ID.save(deps.storage, &order_id)?;

let new_order: SpotOrder = SpotOrder::new(
Expand Down Expand Up @@ -124,7 +136,20 @@ fn create_resp(
return Ok(resp);
}

let reply_id = MAX_REPLY_ID.load(storage)? + 1;
let reply_info_max_id = MAX_REPLY_ID.load(storage)?;

let reply_id = match reply_info_max_id.checked_add(1) {
Some(id) => id,
None => {
return Err(StdError::overflow(OverflowError::new(
cosmwasm_std::OverflowOperation::Add,
"reply_info_max_id",
"increment one",
))
.into())
}
};

MAX_REPLY_ID.save(storage, &reply_id)?;

let swap_msg = ElysMsg::amm_swap_exact_amount_in(
Expand Down
31 changes: 27 additions & 4 deletions src/action/sudo/process_orders.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::msg::ReplyType;
use cosmwasm_std::{to_json_binary, Decimal, Int128, StdResult, Storage, SubMsg};
use cosmwasm_std::{
to_json_binary, Decimal, Int128, OverflowError, StdError, StdResult, Storage, SubMsg,
};
use elys_bindings::query_resp::AmmSwapEstimationByDenomResponse;
use std::ops::Div;

Expand Down Expand Up @@ -97,6 +99,18 @@ fn process_margin_order(
)
};

*reply_info_id = match reply_info_id.checked_add(1) {
Some(id) => id,
None => {
return Err(StdError::overflow(OverflowError::new(
cosmwasm_std::OverflowOperation::Add,
"reply_info_max_id",
"increment one",
))
.into())
}
};

let reply_info = ReplyInfo {
id: *reply_info_id,
reply_type,
Expand All @@ -106,7 +120,6 @@ fn process_margin_order(

REPLY_INFO.save(storage, *reply_info_id, &reply_info)?;

*reply_info_id += 1;
Ok(())
}

Expand Down Expand Up @@ -206,6 +219,18 @@ fn process_spot_order(
&order.owner_address,
);

*reply_info_id = match reply_info_id.checked_add(1) {
Some(id) => id,
None => {
return Err(StdError::overflow(OverflowError::new(
cosmwasm_std::OverflowOperation::Add,
"reply_info_max_id",
"increment one",
))
.into())
}
};

let reply_info = ReplyInfo {
id: *reply_info_id,
reply_type: ReplyType::SpotOrder,
Expand All @@ -216,8 +241,6 @@ fn process_spot_order(

REPLY_INFO.save(storage, *reply_info_id, &reply_info)?;

*reply_info_id += 1;

Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion src/tests/cancel_margin_order/succesful_cancel_an_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fn succesful_cancel_an_order() {
rate: Decimal::from_str("20000.0").unwrap(),
}),
&vec![],
)],
)
.unwrap()],
};

// Create a contract wrapper and store its code.
Expand Down
3 changes: 2 additions & 1 deletion src/tests/cancel_margin_order/unauthorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fn unauthorize() {
rate: Decimal::from_str("20000.0").unwrap(),
}),
&vec![],
)],
)
.unwrap()],
};

// Create a contract wrapper and store its code.
Expand Down
3 changes: 2 additions & 1 deletion src/tests/get_margin_order/successful_query_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ fn successful_query_message() {
rate: Decimal::one(),
}),
&vec![],
);
)
.unwrap();

// Create a mock message to instantiate the contract with an initial dummy order.
let instantiate_msg = InstantiateMockMsg {
Expand Down
3 changes: 2 additions & 1 deletion src/tests/process_margin_order/process_limit_open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ fn successful_process_limit_buy_order() {
rate: Decimal::from_atomics(Uint128::new(38), 0).unwrap(), // Rate at which ubtc will be bought (38 USDC per ubtc).
}),
&vec![],
);
)
.unwrap();

// Create a mock message to instantiate the contract with the dummy order.
let instantiate_msg = InstantiateMockMsg {
Expand Down
42 changes: 27 additions & 15 deletions src/types/margin_order.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, Decimal, StdResult};
use cosmwasm_std::{Coin, Decimal, OverflowError, StdError, StdResult};
use elys_bindings::types::MarginPosition;

use super::{MarginOrderType, OrderPrice, Status};
Expand Down Expand Up @@ -30,15 +30,12 @@ impl MarginOrder {
take_profit_price: &Decimal,
trigger_price: &Option<OrderPrice>,
order_vec: &Vec<MarginOrder>,
) -> Self {
let order_id: u64 = match order_vec.iter().max_by_key(|s| s.order_id) {
Some(x) => x.order_id + 1,
None => 0,
};

) -> StdResult<Self> {
let status = Status::Pending;

Self {
let order_id = get_new_id(&order_vec)?;

let order = Self {
order_id,
owner: owner.into(),
position: position.to_owned(),
Expand All @@ -50,7 +47,9 @@ impl MarginOrder {
trigger_price: trigger_price.to_owned(),
status,
position_id: None,
}
};

return Ok(order);
}
pub fn new_close(
owner: impl Into<String>,
Expand All @@ -64,16 +63,13 @@ impl MarginOrder {
take_profit_price: &Decimal,
order_vec: &Vec<MarginOrder>,
) -> StdResult<Self> {
let order_id: u64 = match order_vec.iter().max_by_key(|s| s.order_id) {
Some(x) => x.order_id + 1,
None => 0,
};
let order_id: u64 = get_new_id(&order_vec)?;

let status = Status::Pending;

let position = MarginPosition::try_from_i32(position)?;

Ok(Self {
let order = Self {
order_id,
status,
order_type: order_type.to_owned(),
Expand All @@ -85,6 +81,22 @@ impl MarginOrder {
position_id: Some(position_id),
leverage: leverage.to_owned(),
take_profit_price: take_profit_price.to_owned(),
})
};

Ok(order)
}
}

fn get_new_id(orders: &[MarginOrder]) -> StdResult<u64> {
match orders.iter().max_by_key(|s| s.order_id) {
Some(order) => match order.order_id.checked_add(1) {
Some(id) => Ok(id),
None => Err(StdError::overflow(OverflowError::new(
cosmwasm_std::OverflowOperation::Add,
"margin_order_max_id",
"increment one",
))),
},
None => Ok(0),
}
}

0 comments on commit 9a2c006

Please sign in to comment.