Skip to content

Commit

Permalink
feat: add FPDecimals (#24)
Browse files Browse the repository at this point in the history
* chore: make volatility query response optional

* chore: fix linting

* chore: remove schema and failed tests from CI until fixed

* feat: add math lib from nebula-contracts

* taken from https://github.com/nebula-protocol/nebula-contracts/tree/a5ff0e3e4285e38a310c7a6e024f6e3e2b75c538/libraries/cluster-math

* feat: use new FPDecimal

* fix: add cargo debug config

* fix: update injective math dependencies
  • Loading branch information
gorgos authored Apr 29, 2022
1 parent aced8d3 commit cb5b322
Show file tree
Hide file tree
Showing 17 changed files with 1,820 additions and 54 deletions.
686 changes: 677 additions & 9 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion packages/injective-cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "injective-cosmwasm"
version = "0.1.6"
version = "0.1.9"
authors = ["Albert Chon <[email protected]>"]
edition = "2018"
description = "Bindings for CosmWasm contracts to call into custom modules of Injective Core"
Expand All @@ -16,6 +16,7 @@ schemars = "0.8.8"
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
ethereum-types = "0.5.2"
subtle-encoding = { version = "0.5.1", features = ["bech32-preview"] }
injective-math = { path = "../injective-math", version = "0.1.2" }

[dev-dependencies]
cosmwasm-schema = { version = "1.0.0-beta10" }
23 changes: 12 additions & 11 deletions packages/injective-cosmwasm/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::route::InjectiveRoute;
use cosmwasm_std::{Addr, Coin, CosmosMsg, CustomMsg, Decimal256 as Decimal};
use cosmwasm_std::{Addr, Coin, CosmosMsg, CustomMsg};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -30,8 +31,8 @@ pub struct OrderData {
pub struct OrderInfo {
pub subaccount_id: String,
pub fee_recipient: String,
pub price: Decimal,
pub quantity: Decimal,
pub price: FPDecimal,
pub quantity: FPDecimal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand All @@ -47,15 +48,15 @@ pub struct DerivativeOrder {
pub market_id: String,
pub order_info: OrderInfo,
pub order_type: i32,
pub margin: Decimal,
pub margin: FPDecimal,
pub trigger_price: Option<String>,
}

impl DerivativeOrder {
pub fn new(
price: Decimal,
quantity: Decimal,
margin: Decimal,
price: FPDecimal,
quantity: FPDecimal,
margin: FPDecimal,
is_buy: bool,
market_id: &str,
subaccount_id: &str,
Expand All @@ -77,16 +78,16 @@ impl DerivativeOrder {
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
pub fn get_price(&self) -> Decimal {
pub fn get_price(&self) -> FPDecimal {
self.order_info.price
}
pub fn get_qty(&self) -> Decimal {
pub fn get_qty(&self) -> FPDecimal {
self.order_info.quantity
}
pub fn get_val(&self) -> Decimal {
pub fn get_val(&self) -> FPDecimal {
self.get_price() * self.get_qty()
}
pub fn get_margin(&self) -> Decimal {
pub fn get_margin(&self) -> FPDecimal {
self.margin
}
pub fn non_reduce_only_is_invalid(&self) -> bool {
Expand Down
67 changes: 34 additions & 33 deletions packages/injective-cosmwasm/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{msg::OrderInfo, route::InjectiveRoute};
use cosmwasm_std::{CustomQuery, Decimal256 as Decimal};
use cosmwasm_std::CustomQuery;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -65,39 +66,39 @@ pub struct SubaccountDepositResponse {
pub struct Position {
#[serde(default)]
pub isLong: bool,
pub quantity: Decimal,
pub entry_price: Decimal,
pub quantity: FPDecimal,
pub entry_price: FPDecimal,
#[serde(default)]
pub margin: Decimal,
pub cumulative_funding_entry: Decimal,
pub margin: FPDecimal,
pub cumulative_funding_entry: FPDecimal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct EffectivePosition {
#[serde(default)]
pub is_long: bool,
pub quantity: Decimal,
pub entry_price: Decimal,
pub quantity: FPDecimal,
pub entry_price: FPDecimal,
#[serde(default)]
pub effective_margin: Decimal,
pub effective_margin: FPDecimal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeLimitOrder {
pub order_info: OrderInfo,
pub order_type: i32,
pub margin: Decimal,
pub fillable: Decimal,
pub trigger_price: Option<Decimal>,
pub margin: FPDecimal,
pub fillable: FPDecimal,
pub trigger_price: Option<FPDecimal>,
pub order_hash: String,
}

impl DerivativeLimitOrder {
pub fn new(
margin: Decimal,
fillable: Decimal,
margin: FPDecimal,
fillable: FPDecimal,
order_hash: String,
trigger_price: Option<Decimal>,
trigger_price: Option<FPDecimal>,
order_type: i32,
order_info: OrderInfo,
) -> DerivativeLimitOrder {
Expand Down Expand Up @@ -130,10 +131,10 @@ pub struct SubaccountEffectivePositionInMarketResponse {
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TrimmedDerivativeLimitOrder {
pub price: Decimal,
pub quantity: Decimal,
pub margin: Decimal,
pub fillable: Decimal,
pub price: FPDecimal,
pub quantity: FPDecimal,
pub margin: FPDecimal,
pub fillable: FPDecimal,
pub isBuy: bool,
pub order_hash: String,
}
Expand All @@ -155,12 +156,12 @@ pub struct PerpetualMarketFundingResponse {

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeMarketVolatilityResponse {
pub volatility: Option<Decimal>,
pub volatility: Option<FPDecimal>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SpotMarketVolatilityResponse {
pub volatility: Option<Decimal>,
pub volatility: Option<FPDecimal>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand All @@ -172,18 +173,18 @@ pub struct DerivativeMarketResponse {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Deposit {
#[serde(default)]
pub available_balance: Decimal,
pub available_balance: FPDecimal,
#[serde(default)]
pub total_balance: Decimal,
pub total_balance: FPDecimal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct PerpetualMarketInfo {
pub market_id: String,
#[serde(default)]
pub hourly_funding_rate_cap: Decimal,
pub hourly_funding_rate_cap: FPDecimal,
#[serde(default)]
pub hourly_interest_rate: Decimal,
pub hourly_interest_rate: FPDecimal,
#[serde(default)]
pub next_funding_timestamp: i64,
pub funding_interval: i64,
Expand All @@ -192,9 +193,9 @@ pub struct PerpetualMarketInfo {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct PerpetualMarketFunding {
#[serde(default)]
pub cumulative_funding: Decimal,
pub cumulative_funding: FPDecimal,
#[serde(default)]
pub cumulative_price: Decimal,
pub cumulative_price: FPDecimal,
#[serde(default)]
pub last_timestamp: i64,
}
Expand All @@ -214,7 +215,7 @@ pub struct FullDerivativeMarketPerpetualInfo {
pub struct FullDerivativeMarket {
pub market: Option<DerivativeMarket>,
pub info: Option<FullDerivativeMarketPerpetualInfo>,
pub mark_price: Decimal,
pub mark_price: FPDecimal,
}

#[allow(non_snake_case)]
Expand All @@ -229,14 +230,14 @@ pub struct DerivativeMarket {
pub oracle_scale_factor: u32,
pub quote_denom: String,
pub market_id: String,
pub initial_margin_ratio: Decimal,
pub maintenance_margin_ratio: Decimal,
pub maker_fee_rate: Decimal,
pub taker_fee_rate: Decimal,
pub initial_margin_ratio: FPDecimal,
pub maintenance_margin_ratio: FPDecimal,
pub maker_fee_rate: FPDecimal,
pub taker_fee_rate: FPDecimal,
#[serde(default)]
pub isPerpetual: bool,
#[serde(default)]
pub status: i32,
pub min_price_tick_size: Decimal,
pub min_quantity_tick_size: Decimal,
pub min_price_tick_size: FPDecimal,
pub min_quantity_tick_size: FPDecimal,
}
5 changes: 5 additions & 0 deletions packages/injective-math/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib --features backtraces"
schema = "run --example schema"
27 changes: 27 additions & 0 deletions packages/injective-math/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "injective-math"
version = "0.1.2"
authors = ["Markus Waas <[email protected]>"]
edition = "2018"
description = "Math library for CosmWasm contracts in Injective Protocol"
repository = "https://github.com/InjectiveLabs/cw-injective/tree/master/packages/injective-math"
license = "Apache-2.0"
readme = "README.md"

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

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cosmwasm-std = { version = "1.0.0-beta10" }
schemars = "0.8.8"
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
ethereum-types = "0.5.2"
subtle-encoding = { version = "0.5.1", features = ["bech32-preview"] }
bigint = "4"

[dev-dependencies]
cosmwasm-schema = { version = "1.0.0-beta10" }
plotters = "^0.3.0"
1 change: 1 addition & 0 deletions packages/injective-math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# injective-math
Loading

0 comments on commit cb5b322

Please sign in to comment.