Skip to content

Commit cb5b322

Browse files
authored
feat: add FPDecimals (#24)
* 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
1 parent aced8d3 commit cb5b322

17 files changed

Lines changed: 1820 additions & 54 deletions

File tree

Cargo.lock

Lines changed: 677 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/injective-cosmwasm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "injective-cosmwasm"
3-
version = "0.1.6"
3+
version = "0.1.9"
44
authors = ["Albert Chon <albert@injectivelabs.org>"]
55
edition = "2018"
66
description = "Bindings for CosmWasm contracts to call into custom modules of Injective Core"
@@ -16,6 +16,7 @@ schemars = "0.8.8"
1616
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
1717
ethereum-types = "0.5.2"
1818
subtle-encoding = { version = "0.5.1", features = ["bech32-preview"] }
19+
injective-math = { path = "../injective-math", version = "0.1.2" }
1920

2021
[dev-dependencies]
2122
cosmwasm-schema = { version = "1.0.0-beta10" }

packages/injective-cosmwasm/src/msg.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use injective_math::FPDecimal;
12
use schemars::JsonSchema;
23
use serde::{Deserialize, Serialize};
34

45
use crate::route::InjectiveRoute;
5-
use cosmwasm_std::{Addr, Coin, CosmosMsg, CustomMsg, Decimal256 as Decimal};
6+
use cosmwasm_std::{Addr, Coin, CosmosMsg, CustomMsg};
67

78
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
89
#[serde(rename_all = "snake_case")]
@@ -30,8 +31,8 @@ pub struct OrderData {
3031
pub struct OrderInfo {
3132
pub subaccount_id: String,
3233
pub fee_recipient: String,
33-
pub price: Decimal,
34-
pub quantity: Decimal,
34+
pub price: FPDecimal,
35+
pub quantity: FPDecimal,
3536
}
3637

3738
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
@@ -47,15 +48,15 @@ pub struct DerivativeOrder {
4748
pub market_id: String,
4849
pub order_info: OrderInfo,
4950
pub order_type: i32,
50-
pub margin: Decimal,
51+
pub margin: FPDecimal,
5152
pub trigger_price: Option<String>,
5253
}
5354

5455
impl DerivativeOrder {
5556
pub fn new(
56-
price: Decimal,
57-
quantity: Decimal,
58-
margin: Decimal,
57+
price: FPDecimal,
58+
quantity: FPDecimal,
59+
margin: FPDecimal,
5960
is_buy: bool,
6061
market_id: &str,
6162
subaccount_id: &str,
@@ -77,16 +78,16 @@ impl DerivativeOrder {
7778
pub fn is_reduce_only(&self) -> bool {
7879
self.margin.is_zero()
7980
}
80-
pub fn get_price(&self) -> Decimal {
81+
pub fn get_price(&self) -> FPDecimal {
8182
self.order_info.price
8283
}
83-
pub fn get_qty(&self) -> Decimal {
84+
pub fn get_qty(&self) -> FPDecimal {
8485
self.order_info.quantity
8586
}
86-
pub fn get_val(&self) -> Decimal {
87+
pub fn get_val(&self) -> FPDecimal {
8788
self.get_price() * self.get_qty()
8889
}
89-
pub fn get_margin(&self) -> Decimal {
90+
pub fn get_margin(&self) -> FPDecimal {
9091
self.margin
9192
}
9293
pub fn non_reduce_only_is_invalid(&self) -> bool {

packages/injective-cosmwasm/src/query.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use injective_math::FPDecimal;
12
use schemars::JsonSchema;
23
use serde::{Deserialize, Serialize};
34

45
use crate::{msg::OrderInfo, route::InjectiveRoute};
5-
use cosmwasm_std::{CustomQuery, Decimal256 as Decimal};
6+
use cosmwasm_std::CustomQuery;
67

78
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
89
#[serde(rename_all = "snake_case")]
@@ -65,39 +66,39 @@ pub struct SubaccountDepositResponse {
6566
pub struct Position {
6667
#[serde(default)]
6768
pub isLong: bool,
68-
pub quantity: Decimal,
69-
pub entry_price: Decimal,
69+
pub quantity: FPDecimal,
70+
pub entry_price: FPDecimal,
7071
#[serde(default)]
71-
pub margin: Decimal,
72-
pub cumulative_funding_entry: Decimal,
72+
pub margin: FPDecimal,
73+
pub cumulative_funding_entry: FPDecimal,
7374
}
7475

7576
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
7677
pub struct EffectivePosition {
7778
#[serde(default)]
7879
pub is_long: bool,
79-
pub quantity: Decimal,
80-
pub entry_price: Decimal,
80+
pub quantity: FPDecimal,
81+
pub entry_price: FPDecimal,
8182
#[serde(default)]
82-
pub effective_margin: Decimal,
83+
pub effective_margin: FPDecimal,
8384
}
8485

8586
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
8687
pub struct DerivativeLimitOrder {
8788
pub order_info: OrderInfo,
8889
pub order_type: i32,
89-
pub margin: Decimal,
90-
pub fillable: Decimal,
91-
pub trigger_price: Option<Decimal>,
90+
pub margin: FPDecimal,
91+
pub fillable: FPDecimal,
92+
pub trigger_price: Option<FPDecimal>,
9293
pub order_hash: String,
9394
}
9495

9596
impl DerivativeLimitOrder {
9697
pub fn new(
97-
margin: Decimal,
98-
fillable: Decimal,
98+
margin: FPDecimal,
99+
fillable: FPDecimal,
99100
order_hash: String,
100-
trigger_price: Option<Decimal>,
101+
trigger_price: Option<FPDecimal>,
101102
order_type: i32,
102103
order_info: OrderInfo,
103104
) -> DerivativeLimitOrder {
@@ -130,10 +131,10 @@ pub struct SubaccountEffectivePositionInMarketResponse {
130131
#[allow(non_snake_case)]
131132
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
132133
pub struct TrimmedDerivativeLimitOrder {
133-
pub price: Decimal,
134-
pub quantity: Decimal,
135-
pub margin: Decimal,
136-
pub fillable: Decimal,
134+
pub price: FPDecimal,
135+
pub quantity: FPDecimal,
136+
pub margin: FPDecimal,
137+
pub fillable: FPDecimal,
137138
pub isBuy: bool,
138139
pub order_hash: String,
139140
}
@@ -155,12 +156,12 @@ pub struct PerpetualMarketFundingResponse {
155156

156157
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
157158
pub struct DerivativeMarketVolatilityResponse {
158-
pub volatility: Option<Decimal>,
159+
pub volatility: Option<FPDecimal>,
159160
}
160161

161162
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
162163
pub struct SpotMarketVolatilityResponse {
163-
pub volatility: Option<Decimal>,
164+
pub volatility: Option<FPDecimal>,
164165
}
165166

166167
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
@@ -172,18 +173,18 @@ pub struct DerivativeMarketResponse {
172173
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
173174
pub struct Deposit {
174175
#[serde(default)]
175-
pub available_balance: Decimal,
176+
pub available_balance: FPDecimal,
176177
#[serde(default)]
177-
pub total_balance: Decimal,
178+
pub total_balance: FPDecimal,
178179
}
179180

180181
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
181182
pub struct PerpetualMarketInfo {
182183
pub market_id: String,
183184
#[serde(default)]
184-
pub hourly_funding_rate_cap: Decimal,
185+
pub hourly_funding_rate_cap: FPDecimal,
185186
#[serde(default)]
186-
pub hourly_interest_rate: Decimal,
187+
pub hourly_interest_rate: FPDecimal,
187188
#[serde(default)]
188189
pub next_funding_timestamp: i64,
189190
pub funding_interval: i64,
@@ -192,9 +193,9 @@ pub struct PerpetualMarketInfo {
192193
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
193194
pub struct PerpetualMarketFunding {
194195
#[serde(default)]
195-
pub cumulative_funding: Decimal,
196+
pub cumulative_funding: FPDecimal,
196197
#[serde(default)]
197-
pub cumulative_price: Decimal,
198+
pub cumulative_price: FPDecimal,
198199
#[serde(default)]
199200
pub last_timestamp: i64,
200201
}
@@ -214,7 +215,7 @@ pub struct FullDerivativeMarketPerpetualInfo {
214215
pub struct FullDerivativeMarket {
215216
pub market: Option<DerivativeMarket>,
216217
pub info: Option<FullDerivativeMarketPerpetualInfo>,
217-
pub mark_price: Decimal,
218+
pub mark_price: FPDecimal,
218219
}
219220

220221
#[allow(non_snake_case)]
@@ -229,14 +230,14 @@ pub struct DerivativeMarket {
229230
pub oracle_scale_factor: u32,
230231
pub quote_denom: String,
231232
pub market_id: String,
232-
pub initial_margin_ratio: Decimal,
233-
pub maintenance_margin_ratio: Decimal,
234-
pub maker_fee_rate: Decimal,
235-
pub taker_fee_rate: Decimal,
233+
pub initial_margin_ratio: FPDecimal,
234+
pub maintenance_margin_ratio: FPDecimal,
235+
pub maker_fee_rate: FPDecimal,
236+
pub taker_fee_rate: FPDecimal,
236237
#[serde(default)]
237238
pub isPerpetual: bool,
238239
#[serde(default)]
239240
pub status: i32,
240-
pub min_price_tick_size: Decimal,
241-
pub min_quantity_tick_size: Decimal,
241+
pub min_price_tick_size: FPDecimal,
242+
pub min_quantity_tick_size: FPDecimal,
242243
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[alias]
2+
wasm = "build --release --target wasm32-unknown-unknown"
3+
wasm-debug = "build --target wasm32-unknown-unknown"
4+
unit-test = "test --lib --features backtraces"
5+
schema = "run --example schema"

packages/injective-math/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "injective-math"
3+
version = "0.1.2"
4+
authors = ["Markus Waas <markus@injectivelabs.org>"]
5+
edition = "2018"
6+
description = "Math library for CosmWasm contracts in Injective Protocol"
7+
repository = "https://github.com/InjectiveLabs/cw-injective/tree/master/packages/injective-math"
8+
license = "Apache-2.0"
9+
readme = "README.md"
10+
11+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
13+
[features]
14+
# for more explicit tests, cargo test --features=backtraces
15+
backtraces = ["cosmwasm-std/backtraces"]
16+
17+
[dependencies]
18+
cosmwasm-std = { version = "1.0.0-beta10" }
19+
schemars = "0.8.8"
20+
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
21+
ethereum-types = "0.5.2"
22+
subtle-encoding = { version = "0.5.1", features = ["bech32-preview"] }
23+
bigint = "4"
24+
25+
[dev-dependencies]
26+
cosmwasm-schema = { version = "1.0.0-beta10" }
27+
plotters = "^0.3.0"

packages/injective-math/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# injective-math

0 commit comments

Comments
 (0)