Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/injective std traits #243

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: first order and market traits are implemented
maxrobot committed Sep 3, 2024
commit 5b47c276af02ed2856c9561beb69c51444192f78
24 changes: 15 additions & 9 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion packages/injective-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,16 +5,24 @@ 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.0"
version = "1.13.2"

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

[dependencies]
chrono = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
hex = { workspace = true }
injective-math = { workspace = true, path = "../injective-math" }
injective-std-derive = { workspace = true, path = "../injective-std-derive" }
prost = { workspace = true }
prost-types = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
serde-cw-value = { workspace = true }
serde_repr = { workspace = true }

[dev-dependencies]
serde_test = { workspace = true }
94 changes: 94 additions & 0 deletions packages/injective-std/src/traits/exchange/market.rs
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,
}
2 changes: 2 additions & 0 deletions packages/injective-std/src/traits/exchange/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod market;
pub mod order;
142 changes: 142 additions & 0 deletions packages/injective-std/src/traits/exchange/order.rs
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))
}
}
721 changes: 721 additions & 0 deletions packages/injective-std/src/traits/general.rs

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions packages/injective-std/src/traits/market.rs

This file was deleted.

3 changes: 2 additions & 1 deletion packages/injective-std/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod market;
pub mod exchange;
pub mod general;