-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: first order and market traits are implemented
- Loading branch information
Showing
8 changed files
with
985 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,94 @@ | ||
use crate::{ | ||
traits::general::MarketId, | ||
types::injective::exchange::v1beta1::{DerivativeMarket, ExchangeQuerier, SpotMarket}, | ||
}; | ||
|
||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{Deps, Empty, StdError, StdResult}; | ||
use injective_math::FPDecimal; | ||
use schemars::JsonSchema; | ||
use std::fmt; | ||
|
||
impl GenericMarket for DerivativeMarket { | ||
fn get_ticker(&self) -> &str { | ||
&self.ticker | ||
} | ||
|
||
fn get_quote_denom(&self) -> &str { | ||
&self.quote_denom | ||
} | ||
|
||
fn get_maker_fee_rate(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.maker_fee_rate) | ||
} | ||
|
||
fn get_taker_fee_rate(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.taker_fee_rate) | ||
} | ||
|
||
fn get_market_id(&self) -> MarketId { | ||
MarketId::new(self.market_id.clone()).unwrap() | ||
} | ||
|
||
fn get_status(&self) -> i32 { | ||
self.status | ||
} | ||
|
||
fn get_min_price_tick_size(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.min_price_tick_size) | ||
} | ||
|
||
fn min_quantity_tick_size(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.min_quantity_tick_size) | ||
} | ||
} | ||
|
||
impl GenericMarket for SpotMarket { | ||
fn get_ticker(&self) -> &str { | ||
&self.ticker | ||
} | ||
|
||
fn get_quote_denom(&self) -> &str { | ||
&self.quote_denom | ||
} | ||
|
||
fn get_maker_fee_rate(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.maker_fee_rate) | ||
} | ||
|
||
fn get_taker_fee_rate(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.taker_fee_rate) | ||
} | ||
|
||
fn get_market_id(&self) -> MarketId { | ||
MarketId::new(self.market_id.clone()).unwrap() | ||
} | ||
|
||
fn get_status(&self) -> i32 { | ||
self.status | ||
} | ||
|
||
fn get_min_price_tick_size(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.min_price_tick_size) | ||
} | ||
|
||
fn min_quantity_tick_size(&self) -> FPDecimal { | ||
FPDecimal::must_from_str(&self.min_quantity_tick_size) | ||
} | ||
} | ||
|
||
pub trait GenericMarket { | ||
fn get_ticker(&self) -> &str; | ||
fn get_quote_denom(&self) -> &str; | ||
fn get_maker_fee_rate(&self) -> FPDecimal; | ||
fn get_taker_fee_rate(&self) -> FPDecimal; | ||
fn get_market_id(&self) -> MarketId; | ||
fn get_status(&self) -> i32; | ||
fn get_min_price_tick_size(&self) -> FPDecimal; | ||
fn min_quantity_tick_size(&self) -> FPDecimal; | ||
} | ||
|
||
pub enum MarketType { | ||
Spot, | ||
Derivative, | ||
} |
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,2 @@ | ||
pub mod market; | ||
pub mod order; |
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,142 @@ | ||
use crate::{ | ||
traits::general::{MarketId, SubaccountId}, | ||
types::injective::exchange::v1beta1::{DerivativeLimitOrder, DerivativeOrder, OrderData, OrderInfo, SpotLimitOrder, SpotOrder}, | ||
}; | ||
|
||
use cosmwasm_std::Addr; | ||
use injective_math::FPDecimal; | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_repr::{Deserialize_repr, Serialize_repr}; | ||
|
||
#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq, JsonSchema)] | ||
#[repr(u8)] | ||
pub enum OrderSide { | ||
Unspecified = 0, | ||
Buy = 1, | ||
Sell = 2, | ||
} | ||
|
||
#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq, JsonSchema)] | ||
#[repr(u8)] | ||
pub enum OrderType { | ||
Undefined = 0, | ||
Buy = 1, | ||
Sell = 2, | ||
StopBuy = 3, | ||
StopSell = 4, | ||
TakeBuy = 5, | ||
TakeSell = 6, | ||
BuyPo = 7, | ||
SellPo = 8, | ||
BuyAtomic = 9, | ||
SellAtomic = 10, | ||
} | ||
|
||
impl OrderType { | ||
pub fn from_i32(value: i32) -> OrderType { | ||
match value { | ||
0 => OrderType::Undefined, | ||
1 => OrderType::Buy, | ||
2 => OrderType::Sell, | ||
3 => OrderType::StopBuy, | ||
4 => OrderType::StopSell, | ||
5 => OrderType::TakeBuy, | ||
6 => OrderType::TakeSell, | ||
7 => OrderType::BuyPo, | ||
8 => OrderType::SellPo, | ||
9 => OrderType::BuyAtomic, | ||
10 => OrderType::SellAtomic, | ||
_ => unimplemented!("Order type not supported!"), | ||
} | ||
} | ||
} | ||
|
||
pub trait GenericOrder { | ||
fn get_order_type(&self) -> OrderType; | ||
fn get_order_info(&self) -> &Option<OrderInfo>; | ||
fn get_trigger_price(&self) -> Option<FPDecimal>; | ||
fn is_buy(&self) -> bool; | ||
fn is_sell(&self) -> bool; | ||
} | ||
|
||
impl SpotLimitOrder { | ||
pub fn new(order_info: OrderInfo, order_type: OrderType, fillable: FPDecimal, trigger_price: FPDecimal, order_hash: String) -> Self { | ||
SpotLimitOrder { | ||
order_info: Some(order_info), | ||
order_type: order_type as i32, | ||
fillable: fillable.to_string(), | ||
trigger_price: trigger_price.to_string(), | ||
order_hash: order_hash.into(), | ||
} | ||
} | ||
} | ||
|
||
impl GenericOrder for SpotLimitOrder { | ||
fn is_buy(&self) -> bool { | ||
self.order_type == OrderType::Buy as i32 || self.order_type == OrderType::BuyPo as i32 || self.order_type == OrderType::BuyAtomic as i32 | ||
} | ||
|
||
fn is_sell(&self) -> bool { | ||
self.order_type == OrderType::Sell as i32 || self.order_type == OrderType::SellPo as i32 || self.order_type == OrderType::SellAtomic as i32 | ||
} | ||
|
||
fn get_order_type(&self) -> OrderType { | ||
OrderType::from_i32(self.order_type) | ||
} | ||
|
||
fn get_order_info(&self) -> &Option<OrderInfo> { | ||
&self.order_info | ||
} | ||
|
||
fn get_trigger_price(&self) -> Option<FPDecimal> { | ||
Some(FPDecimal::must_from_str(&self.trigger_price)) | ||
} | ||
} | ||
|
||
impl SpotOrder { | ||
pub fn new( | ||
price: FPDecimal, | ||
quantity: FPDecimal, | ||
order_type: OrderType, | ||
market_id: &MarketId, | ||
subaccount_id: SubaccountId, | ||
fee_recipient: String, | ||
cid: Option<String>, | ||
) -> Self { | ||
SpotOrder { | ||
market_id: market_id.to_string(), | ||
order_info: Some(OrderInfo { | ||
subaccount_id: subaccount_id.to_string(), | ||
fee_recipient, | ||
price: price.to_string(), | ||
quantity: quantity.to_string(), | ||
cid: cid.unwrap_or_default(), | ||
}), | ||
order_type: order_type as i32, | ||
trigger_price: "".to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl GenericOrder for SpotOrder { | ||
fn is_buy(&self) -> bool { | ||
self.order_type == OrderType::Buy as i32 || self.order_type == OrderType::BuyPo as i32 || self.order_type == OrderType::BuyAtomic as i32 | ||
} | ||
|
||
fn is_sell(&self) -> bool { | ||
self.order_type == OrderType::Sell as i32 || self.order_type == OrderType::SellPo as i32 || self.order_type == OrderType::SellAtomic as i32 | ||
} | ||
|
||
fn get_order_type(&self) -> OrderType { | ||
OrderType::from_i32(self.order_type) | ||
} | ||
|
||
fn get_order_info(&self) -> &Option<OrderInfo> { | ||
&self.order_info | ||
} | ||
|
||
fn get_trigger_price(&self) -> Option<FPDecimal> { | ||
Some(FPDecimal::must_from_str(&self.trigger_price)) | ||
} | ||
} |
Oops, something went wrong.