This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4848a09
commit 6a55533
Showing
23 changed files
with
351 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::trade_shield::types::PerpetualOrderV2; | ||
use cw_storage_plus::Map; | ||
|
||
pub const PERPETUAL_ORDER_V2: Map<u64, PerpetualOrderV2> = Map::new("perpetual order2_v2"); | ||
|
||
pub const PENDING_PERPETUAL_ORDER_V2: Map<u64, PerpetualOrderV2> = | ||
Map::new("unprocess perpetual order_v2"); |
23 changes: 23 additions & 0 deletions
23
bindings/src/trade_shield/types/from_perpetual_order_to_v2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use super::{PerpetualOrder, PerpetualOrderV2}; | ||
|
||
impl Into<PerpetualOrderV2> for PerpetualOrder { | ||
fn into(self) -> PerpetualOrderV2 { | ||
PerpetualOrderV2 { | ||
order_id: self.order_id, | ||
owner: self.owner, | ||
order_type: self.order_type, | ||
position: self.position, | ||
trigger_price: self.trigger_price, | ||
collateral: self.collateral, | ||
trading_asset: self.trading_asset, | ||
leverage: self.leverage, | ||
take_profit_price: self.take_profit_price, | ||
position_id: self.position_id, | ||
status: self.status, | ||
size: None, | ||
liquidation: None, | ||
borrow_fee: None, | ||
funding_fee: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::{trade_shield::states::PENDING_PERPETUAL_ORDER_V2, types::PerpetualPosition}; | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{ | ||
Coin, DecCoin, Decimal256, OverflowError, SignedDecimal, SignedDecimal256, StdError, StdResult, | ||
Storage, | ||
}; | ||
|
||
use super::{Fee, OrderPrice, PerpetualOrderType, Status}; | ||
|
||
#[cw_serde] | ||
pub struct PerpetualOrderV2 { | ||
pub order_id: u64, | ||
pub owner: String, | ||
pub order_type: PerpetualOrderType, | ||
pub position: PerpetualPosition, | ||
pub trigger_price: Option<OrderPrice>, | ||
pub collateral: Coin, | ||
pub trading_asset: String, | ||
pub leverage: SignedDecimal, | ||
pub take_profit_price: Option<SignedDecimal256>, | ||
pub position_id: Option<u64>, | ||
pub status: Status, | ||
pub size: Option<DecCoin>, | ||
pub liquidation: Option<SignedDecimal>, | ||
pub borrow_fee: Option<Fee>, | ||
pub funding_fee: Option<Fee>, | ||
} | ||
|
||
impl PerpetualOrderV2 { | ||
pub fn new_open( | ||
owner: impl Into<String>, | ||
position: &PerpetualPosition, | ||
order_type: &PerpetualOrderType, | ||
collateral: &Coin, | ||
trading_asset: impl Into<String>, | ||
leverage: &SignedDecimal, | ||
take_profit_price: &Option<SignedDecimal256>, | ||
trigger_price: &Option<OrderPrice>, | ||
order_vec: &Vec<PerpetualOrderV2>, | ||
size: DecCoin, | ||
liquidation: SignedDecimal, | ||
borrow_fee: Fee, | ||
funding_fee: Fee, | ||
) -> StdResult<Self> { | ||
let status = if order_type == &PerpetualOrderType::MarketOpen { | ||
Status::Executed | ||
} else { | ||
Status::Pending | ||
}; | ||
|
||
let order_id = get_new_id(&order_vec)?; | ||
|
||
let order = Self { | ||
order_id, | ||
owner: owner.into(), | ||
position: position.to_owned(), | ||
collateral: collateral.to_owned(), | ||
trading_asset: trading_asset.into(), | ||
leverage: leverage.to_owned(), | ||
take_profit_price: take_profit_price.to_owned(), | ||
order_type: order_type.to_owned(), | ||
trigger_price: trigger_price.to_owned(), | ||
status, | ||
position_id: None, | ||
size: Some(size), | ||
liquidation: Some(liquidation), | ||
borrow_fee: Some(borrow_fee), | ||
funding_fee: Some(funding_fee), | ||
}; | ||
|
||
return Ok(order); | ||
} | ||
pub fn new_close( | ||
owner: impl Into<String>, | ||
position: i32, | ||
order_type: &PerpetualOrderType, | ||
collateral: &Coin, | ||
trading_asset: impl Into<String>, | ||
leverage: &SignedDecimal, | ||
position_id: u64, | ||
trigger_price: &Option<OrderPrice>, | ||
take_profit_price: &Option<SignedDecimal256>, | ||
order_vec: &Vec<PerpetualOrderV2>, | ||
) -> StdResult<Self> { | ||
let order_id: u64 = get_new_id(&order_vec)?; | ||
|
||
let status = if order_type == &PerpetualOrderType::MarketClose { | ||
Status::Executed | ||
} else { | ||
Status::Pending | ||
}; | ||
|
||
let position = PerpetualPosition::try_from_i32(position)?; | ||
|
||
let order: PerpetualOrderV2 = Self { | ||
order_id, | ||
status, | ||
order_type: order_type.to_owned(), | ||
position, | ||
owner: owner.into(), | ||
trigger_price: trigger_price.to_owned(), | ||
collateral: collateral.to_owned(), | ||
trading_asset: trading_asset.into(), | ||
position_id: Some(position_id), | ||
leverage: leverage.to_owned(), | ||
take_profit_price: take_profit_price.to_owned(), | ||
size: Some(DecCoin::new(Decimal256::zero(), "")), | ||
liquidation: Some(SignedDecimal::zero()), | ||
borrow_fee: Some(Fee::default()), | ||
funding_fee: Some(Fee::default()), | ||
}; | ||
|
||
Ok(order) | ||
} | ||
|
||
pub fn binary_search( | ||
trigger_price: &Option<OrderPrice>, | ||
storage: &dyn Storage, | ||
list: &Vec<u64>, | ||
) -> StdResult<usize> { | ||
let mut low = 0; | ||
let mut high = list.len(); | ||
let rate = match trigger_price { | ||
Some(price) => &price.rate, | ||
None => { | ||
return Err(StdError::generic_err( | ||
"perpetual: binary search: price not found", | ||
)) | ||
} | ||
}; | ||
|
||
while low < high { | ||
let mid = low + (high - low) / 2; | ||
let PerpetualOrderV2 { trigger_price, .. } = | ||
match PENDING_PERPETUAL_ORDER_V2.may_load(storage, list[mid])? { | ||
Some(order) => order, | ||
None => { | ||
return Err(StdError::generic_err( | ||
"perpetual: binary search: order not found", | ||
)) | ||
} | ||
}; | ||
if trigger_price.is_none() { | ||
return Err(StdError::generic_err( | ||
"perpetual: binary search: price not found", | ||
)); | ||
} | ||
|
||
if trigger_price.unwrap().rate < *rate { | ||
low = mid + 1; | ||
} else { | ||
high = mid; | ||
} | ||
} | ||
Ok(low) | ||
} | ||
|
||
pub fn gen_key(&self) -> StdResult<String> { | ||
if self.order_type == PerpetualOrderType::MarketClose | ||
|| self.order_type == PerpetualOrderType::MarketOpen | ||
{ | ||
return Err(StdError::generic_err("gen a key on a market order")); | ||
} | ||
if let Some(price) = &self.trigger_price { | ||
Ok(self.position.to_string() | ||
+ "\n" | ||
+ &self.order_type.to_string() | ||
+ "\n" | ||
+ &price.base_denom | ||
+ "\n" | ||
+ &price.quote_denom) | ||
} else { | ||
Err(StdError::not_found("trigger price not found")) | ||
} | ||
} | ||
|
||
pub fn from_key( | ||
key: &str, | ||
) -> StdResult<(PerpetualPosition, PerpetualOrderType, String, String)> { | ||
let vec: Vec<&str> = key.split('\n').collect(); | ||
if vec.len() != 4 { | ||
return Err(StdError::generic_err("Wrong Key")); | ||
} | ||
|
||
let order_position = PerpetualPosition::from_str(vec[0])?; | ||
let order_type = PerpetualOrderType::from_str(vec[1])?; | ||
if order_type == PerpetualOrderType::MarketClose | ||
|| order_type == PerpetualOrderType::MarketOpen | ||
{ | ||
return Err(StdError::generic_err("Market Order")); | ||
} | ||
|
||
Ok(( | ||
order_position, | ||
order_type, | ||
vec[2].to_string(), | ||
vec[3].to_string(), | ||
)) | ||
} | ||
} | ||
|
||
fn get_new_id(orders: &[PerpetualOrderV2]) -> 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, | ||
"perpetual_order_max_id", | ||
"increment one", | ||
))), | ||
}, | ||
None => Ok(0), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.