Skip to content

Commit

Permalink
feat: all traits finished but testing still required
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrobot committed Sep 16, 2024
1 parent 5b47c27 commit b6d8206
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 70 deletions.
32 changes: 25 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ ethereum-types = { version = "0.5.2" }
hex = { version = "0.4.3", features = [ "serde" ] }
injective-cosmwasm = { version = "0.3.0", path = "./packages/injective-cosmwasm" }
injective-math = { version = "0.3.0", path = "./packages/injective-math" }
injective-std = { version = "1.13.0", path = "./packages/injective-std" }
injective-std = { version = "=1.13.2-rc1", path = "./packages/injective-std" }
injective-std-derive = { version = "1.13.0", path = "./packages/injective-std-derive" }
injective-test-tube = { version = "1.13.2" }
injective-testing = { version = "1.1.0", path = "./packages/injective-testing" }
injective-testing = { version = "1.1.2", path = "./packages/injective-testing" }
itertools = { version = "0.10.3" }
primitive-types = { version = "0.12.2", default-features = false }
proc-macro2 = { version = "1.0.40" }
Expand Down
4 changes: 3 additions & 1 deletion packages/injective-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT OR Apache-2.0"
name = "injective-std"
readme = "README.md"
repository = "https://github.com/InjectiveLabs/cw-injective/tree/dev/packages/injective-std"
version = "1.13.2"
version = "1.13.2-rc1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -14,6 +14,7 @@ chrono = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
ethereum-types = { workspace = true }
hex = { workspace = true }
injective-math = { workspace = true, path = "../injective-math" }
injective-std-derive = { workspace = true, path = "../injective-std-derive" }
Expand All @@ -23,6 +24,7 @@ schemars = { workspace = true }
serde = { workspace = true }
serde-cw-value = { workspace = true }
serde_repr = { workspace = true }
subtle-encoding = { workspace = true }

[dev-dependencies]
serde_test = { workspace = true }
124 changes: 106 additions & 18 deletions packages/injective-std/src/traits/exchange/market.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
traits::general::MarketId,
types::injective::exchange::v1beta1::{DerivativeMarket, ExchangeQuerier, SpotMarket},
traits::{general::MarketId, oracle::OracleType},
types::injective::exchange::v1beta1::{Deposit, DerivativeMarket, ExchangeQuerier, MidPriceAndTob, PerpetualMarketFunding, SpotMarket},
};

use cosmwasm_schema::cw_serde;
Expand All @@ -9,7 +9,53 @@ use injective_math::FPDecimal;
use schemars::JsonSchema;
use std::fmt;

impl GenericMarket for DerivativeMarket {
pub trait MarketExtension {
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 get_min_quantity_tick_size(&self) -> FPDecimal;
}

pub trait DerivativeMarketExtension {
fn get_initial_margin_ratio(&self) -> FPDecimal;
fn get_maintenance_margin_ratio(&self) -> FPDecimal;
fn get_oracle_type(&self) -> OracleType;
}

pub trait SpotMarketExtension {
fn get_base_denom(&self) -> &str;
}

pub trait PerpetualMarketFundingExtension {
fn get_cumulative_funding(&self) -> FPDecimal;
fn get_cumulative_price(&self) -> FPDecimal;
fn get_last_timestamp(&self) -> i64;
}

pub trait PerpetualMarketInfoExtension {
fn get_market_id(&self) -> MarketId;
fn get_hourly_funding_rate_cap(&self) -> FPDecimal;
fn get_hourly_interest_rate(&self) -> FPDecimal;
fn get_next_funding_timestamp(&self) -> i64;
fn get_funding_interval(&self) -> i64;
}

pub trait MidPriceAndTobExtension {
fn get_mid_price(&self) -> FPDecimal;
fn get_best_buy_price(&self) -> FPDecimal;
fn get_best_sell_price(&self) -> FPDecimal;
}

pub trait DepositExtension {
fn get_available_balance(&self) -> FPDecimal;
fn get_total_balance(&self) -> FPDecimal;
}

impl MarketExtension for DerivativeMarket {
fn get_ticker(&self) -> &str {
&self.ticker
}
Expand Down Expand Up @@ -38,12 +84,32 @@ impl GenericMarket for DerivativeMarket {
FPDecimal::must_from_str(&self.min_price_tick_size)
}

fn min_quantity_tick_size(&self) -> FPDecimal {
fn get_min_quantity_tick_size(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.min_quantity_tick_size)
}
}

impl GenericMarket for SpotMarket {
impl DerivativeMarketExtension for DerivativeMarket {
fn get_initial_margin_ratio(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.initial_margin_ratio)
}

fn get_maintenance_margin_ratio(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.maintenance_margin_ratio)
}

fn get_oracle_type(&self) -> OracleType {
OracleType::from_i32(self.oracle_type)
}
}

impl SpotMarketExtension for SpotMarket {
fn get_base_denom(&self) -> &str {
&self.base_denom
}
}

impl MarketExtension for SpotMarket {
fn get_ticker(&self) -> &str {
&self.ticker
}
Expand Down Expand Up @@ -72,23 +138,45 @@ impl GenericMarket for SpotMarket {
FPDecimal::must_from_str(&self.min_price_tick_size)
}

fn min_quantity_tick_size(&self) -> FPDecimal {
fn get_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;
impl PerpetualMarketFundingExtension for PerpetualMarketFunding {
fn get_cumulative_funding(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.cumulative_funding)
}

fn get_cumulative_price(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.cumulative_price)
}

fn get_last_timestamp(&self) -> i64 {
self.last_timestamp
}
}

pub enum MarketType {
Spot,
Derivative,
impl MidPriceAndTobExtension for MidPriceAndTob {
fn get_mid_price(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.mid_price)
}

fn get_best_buy_price(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.best_buy_price)
}

fn get_best_sell_price(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.best_sell_price)
}
}

impl DepositExtension for Deposit {
fn get_available_balance(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.available_balance)
}

fn get_total_balance(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.total_balance)
}
}
1 change: 1 addition & 0 deletions packages/injective-std/src/traits/exchange/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod market;
pub mod order;
pub mod position;
72 changes: 68 additions & 4 deletions packages/injective-std/src/traits/exchange/order.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
traits::general::{MarketId, SubaccountId},
types::injective::exchange::v1beta1::{DerivativeLimitOrder, DerivativeOrder, OrderData, OrderInfo, SpotLimitOrder, SpotOrder},
types::injective::exchange::v1beta1::{
DerivativeLimitOrder, DerivativeOrder, OrderData, OrderInfo, SpotLimitOrder, SpotOrder, TrimmedDerivativeLimitOrder, TrimmedSpotLimitOrder,
},
};

use cosmwasm_std::Addr;
Expand Down Expand Up @@ -52,14 +54,26 @@ impl OrderType {
}
}

pub trait GenericOrder {
pub trait OrderExtension {
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;
}

pub trait TrimmedOrderExtension {
fn get_price(&self) -> FPDecimal;
fn get_fillable_quantity(&self) -> FPDecimal;
fn is_buy(&self) -> bool;
fn is_sell(&self) -> bool;
fn get_order_hash(&self) -> String;
}

pub trait TrimmedDerivativeOrderExtension {
fn get_margin(&self) -> FPDecimal;
}

impl SpotLimitOrder {
pub fn new(order_info: OrderInfo, order_type: OrderType, fillable: FPDecimal, trigger_price: FPDecimal, order_hash: String) -> Self {
SpotLimitOrder {
Expand All @@ -72,7 +86,7 @@ impl SpotLimitOrder {
}
}

impl GenericOrder for SpotLimitOrder {
impl OrderExtension 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
}
Expand Down Expand Up @@ -119,7 +133,7 @@ impl SpotOrder {
}
}

impl GenericOrder for SpotOrder {
impl OrderExtension 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
}
Expand All @@ -140,3 +154,53 @@ impl GenericOrder for SpotOrder {
Some(FPDecimal::must_from_str(&self.trigger_price))
}
}

impl TrimmedOrderExtension for TrimmedSpotLimitOrder {
fn is_buy(&self) -> bool {
self.is_buy
}

fn is_sell(&self) -> bool {
!self.is_buy
}

fn get_price(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.price)
}

fn get_fillable_quantity(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.fillable)
}

fn get_order_hash(&self) -> String {
self.order_hash.to_owned()
}
}

impl TrimmedOrderExtension for TrimmedDerivativeLimitOrder {
fn is_buy(&self) -> bool {
self.is_buy
}

fn is_sell(&self) -> bool {
!self.is_buy
}

fn get_price(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.price)
}

fn get_fillable_quantity(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.fillable)
}

fn get_order_hash(&self) -> String {
self.order_hash.to_owned()
}
}

impl TrimmedDerivativeOrderExtension for TrimmedDerivativeLimitOrder {
fn get_margin(&self) -> FPDecimal {
FPDecimal::must_from_str(&self.margin)
}
}
Loading

0 comments on commit b6d8206

Please sign in to comment.