Skip to content

Commit 09b1925

Browse files
albertchongorgos
andauthored
F/deposit msg (#28)
* feat: add deposit message * chore: add further address utils * feat: add more Msgs and reorganize files Co-authored-by: Markus Waas <mail@markuswaas.com>
1 parent cb5b322 commit 09b1925

12 files changed

Lines changed: 572 additions & 252 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "injective-cosmwasm"
3-
version = "0.1.9"
3+
version = "0.1.12"
44
authors = ["Albert Chon <albert@injectivelabs.org>"]
55
edition = "2018"
66
description = "Bindings for CosmWasm contracts to call into custom modules of Injective Core"
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
use crate::order::OrderInfo;
2+
use injective_math::FPDecimal;
3+
use schemars::JsonSchema;
4+
use serde::{Deserialize, Serialize};
5+
6+
#[allow(non_snake_case)]
7+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
8+
pub struct Position {
9+
#[serde(default)]
10+
pub isLong: bool,
11+
pub quantity: FPDecimal,
12+
pub entry_price: FPDecimal,
13+
#[serde(default)]
14+
pub margin: FPDecimal,
15+
pub cumulative_funding_entry: FPDecimal,
16+
}
17+
18+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
19+
pub struct EffectivePosition {
20+
#[serde(default)]
21+
pub is_long: bool,
22+
pub quantity: FPDecimal,
23+
pub entry_price: FPDecimal,
24+
#[serde(default)]
25+
pub effective_margin: FPDecimal,
26+
}
27+
28+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
29+
pub struct DerivativePosition {
30+
pub subaccount_id: String,
31+
pub market_id: String,
32+
pub position: Position,
33+
}
34+
35+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
36+
pub struct DerivativeOrder {
37+
pub market_id: String,
38+
pub order_info: OrderInfo,
39+
pub order_type: i32,
40+
pub margin: FPDecimal,
41+
pub trigger_price: Option<String>,
42+
}
43+
44+
impl DerivativeOrder {
45+
pub fn new(
46+
price: FPDecimal,
47+
quantity: FPDecimal,
48+
margin: FPDecimal,
49+
is_buy: bool,
50+
market_id: &str,
51+
subaccount_id: &str,
52+
fee_recipient: &str,
53+
) -> Self {
54+
DerivativeOrder {
55+
market_id: market_id.to_string(),
56+
order_info: OrderInfo {
57+
subaccount_id: subaccount_id.to_string(),
58+
fee_recipient: fee_recipient.to_string(),
59+
price,
60+
quantity,
61+
},
62+
order_type: if is_buy { 1 } else { 2 }, // TODO PO-orders
63+
margin,
64+
trigger_price: None,
65+
}
66+
}
67+
pub fn is_reduce_only(&self) -> bool {
68+
self.margin.is_zero()
69+
}
70+
pub fn get_price(&self) -> FPDecimal {
71+
self.order_info.price
72+
}
73+
pub fn get_qty(&self) -> FPDecimal {
74+
self.order_info.quantity
75+
}
76+
pub fn get_val(&self) -> FPDecimal {
77+
self.get_price() * self.get_qty()
78+
}
79+
pub fn get_margin(&self) -> FPDecimal {
80+
self.margin
81+
}
82+
pub fn non_reduce_only_is_invalid(&self) -> bool {
83+
self.get_margin().is_zero() || self.get_price().is_zero() || self.get_qty().is_zero()
84+
}
85+
pub fn reduce_only_price_is_invalid(&self) -> bool {
86+
self.get_price().is_zero()
87+
}
88+
pub fn reduce_only_qty_is_invalid(&self) -> bool {
89+
self.get_qty().is_zero()
90+
}
91+
}
92+
93+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
94+
pub struct DerivativeLimitOrder {
95+
pub order_info: OrderInfo,
96+
pub order_type: i32,
97+
pub margin: FPDecimal,
98+
pub fillable: FPDecimal,
99+
pub trigger_price: Option<FPDecimal>,
100+
pub order_hash: String,
101+
}
102+
103+
impl DerivativeLimitOrder {
104+
pub fn new(
105+
margin: FPDecimal,
106+
fillable: FPDecimal,
107+
order_hash: String,
108+
trigger_price: Option<FPDecimal>,
109+
order_type: i32,
110+
order_info: OrderInfo,
111+
) -> Self {
112+
DerivativeLimitOrder {
113+
margin,
114+
fillable,
115+
order_hash,
116+
trigger_price,
117+
order_type,
118+
order_info,
119+
}
120+
}
121+
pub fn is_reduce_only(&self) -> bool {
122+
self.margin.is_zero()
123+
}
124+
}
125+
126+
#[allow(non_snake_case)]
127+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
128+
pub struct TrimmedDerivativeLimitOrder {
129+
pub price: FPDecimal,
130+
pub quantity: FPDecimal,
131+
pub margin: FPDecimal,
132+
pub fillable: FPDecimal,
133+
pub isBuy: bool,
134+
pub order_hash: String,
135+
}
136+
137+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
138+
pub struct DerivativeMarketOrder {
139+
pub order_info: OrderInfo,
140+
pub order_type: i32,
141+
pub margin: FPDecimal,
142+
pub fillable: FPDecimal,
143+
pub trigger_price: Option<FPDecimal>,
144+
pub order_hash: String,
145+
}
146+
147+
impl DerivativeMarketOrder {
148+
pub fn new(
149+
order_info: OrderInfo,
150+
order_type: i32,
151+
margin: FPDecimal,
152+
fillable: FPDecimal,
153+
trigger_price: Option<FPDecimal>,
154+
order_hash: String,
155+
) -> Self {
156+
DerivativeMarketOrder {
157+
margin,
158+
fillable,
159+
order_hash,
160+
trigger_price,
161+
order_type,
162+
order_info,
163+
}
164+
}
165+
pub fn is_reduce_only(&self) -> bool {
166+
self.margin.is_zero()
167+
}
168+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use injective_math::FPDecimal;
2+
use schemars::JsonSchema;
3+
use serde::{Deserialize, Serialize};
4+
5+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
6+
pub struct PerpetualMarketInfo {
7+
pub market_id: String,
8+
#[serde(default)]
9+
pub hourly_funding_rate_cap: FPDecimal,
10+
#[serde(default)]
11+
pub hourly_interest_rate: FPDecimal,
12+
#[serde(default)]
13+
pub next_funding_timestamp: i64,
14+
pub funding_interval: i64,
15+
}
16+
17+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
18+
pub struct PerpetualMarketFunding {
19+
#[serde(default)]
20+
pub cumulative_funding: FPDecimal,
21+
#[serde(default)]
22+
pub cumulative_price: FPDecimal,
23+
#[serde(default)]
24+
pub last_timestamp: i64,
25+
}
26+
27+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
28+
pub struct PerpetualMarketState {
29+
pub market_info: PerpetualMarketInfo,
30+
pub funding_info: PerpetualMarketFunding,
31+
}
32+
33+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
34+
pub struct FullDerivativeMarketPerpetualInfo {
35+
pub perpetual_info: PerpetualMarketState,
36+
}
37+
38+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
39+
pub struct FullDerivativeMarket {
40+
pub market: Option<DerivativeMarket>,
41+
pub info: Option<FullDerivativeMarketPerpetualInfo>,
42+
pub mark_price: FPDecimal,
43+
}
44+
45+
#[allow(non_snake_case)]
46+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
47+
pub struct DerivativeMarket {
48+
pub ticker: String,
49+
pub oracle_base: String,
50+
pub oracle_quote: String,
51+
#[serde(default)]
52+
pub oracle_type: i32,
53+
#[serde(default)]
54+
pub oracle_scale_factor: u32,
55+
pub quote_denom: String,
56+
pub market_id: String,
57+
pub initial_margin_ratio: FPDecimal,
58+
pub maintenance_margin_ratio: FPDecimal,
59+
pub maker_fee_rate: FPDecimal,
60+
pub taker_fee_rate: FPDecimal,
61+
#[serde(default)]
62+
pub isPerpetual: bool,
63+
#[serde(default)]
64+
pub status: i32,
65+
pub min_price_tick_size: FPDecimal,
66+
pub min_quantity_tick_size: FPDecimal,
67+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use injective_math::FPDecimal;
2+
use schemars::JsonSchema;
3+
use serde::{Deserialize, Serialize};
4+
5+
/// Deposit is data format for the subaccount deposit
6+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
7+
pub struct Deposit {
8+
#[serde(default)]
9+
pub available_balance: FPDecimal,
10+
#[serde(default)]
11+
pub total_balance: FPDecimal,
12+
}

packages/injective-cosmwasm/src/lib.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
1+
mod derivative;
2+
mod derivative_market;
3+
mod exchange;
14
mod msg;
5+
mod order;
26
mod querier;
37
mod query;
48
mod route;
9+
mod spot;
10+
mod spot_market;
511
mod subaccount;
612

713
pub use msg::{
8-
create_batch_update_orders_msg, create_derivative_market_order_msg, create_subaccount_transfer_msg, DerivativeOrder, InjectiveMsg,
9-
InjectiveMsgWrapper, OrderData, OrderInfo, SpotOrder,
14+
create_batch_update_orders_msg, create_deposit_msg, create_derivative_market_order_msg, create_external_transfer_msg,
15+
create_increase_position_margin_msg, create_liquidate_position_msg, create_register_as_dmm_msg, create_spot_market_order_msg,
16+
create_subaccount_transfer_msg, create_withdraw_msg, InjectiveMsg, InjectiveMsgWrapper,
1017
};
1118

1219
pub use querier::InjectiveQuerier;
13-
pub use query::{
14-
Deposit, DerivativeLimitOrder, DerivativeMarket, EffectivePosition, InjectiveQuery, InjectiveQueryWrapper, PerpetualMarketFunding,
15-
PerpetualMarketInfo, SubaccountDepositResponse,
16-
};
20+
pub use query::{InjectiveQuery, InjectiveQueryWrapper, SubaccountDepositResponse};
1721
pub use route::InjectiveRoute;
18-
pub use subaccount::{address_to_subaccount_id, bech32_to_hex, default_subaccount_id};
22+
pub use subaccount::{
23+
addr_to_bech32, address_to_subaccount_id, bech32_to_hex, default_subaccount_id, subaccount_id_to_ethereum_address,
24+
subaccount_id_to_injective_address,
25+
};
26+
27+
pub use order::{OrderData, OrderInfo};
28+
29+
pub use exchange::Deposit;
30+
31+
pub use spot::{SpotLimitOrder, SpotMarketOrder, SpotOrder, TrimmedSpotLimitOrder};
32+
33+
pub use spot_market::SpotMarket;
34+
35+
pub use derivative::{
36+
DerivativeLimitOrder, DerivativeMarketOrder, DerivativeOrder, DerivativePosition, EffectivePosition, Position, TrimmedDerivativeLimitOrder,
37+
};
38+
39+
pub use derivative_market::{
40+
DerivativeMarket, FullDerivativeMarket, FullDerivativeMarketPerpetualInfo, PerpetualMarketFunding, PerpetualMarketInfo, PerpetualMarketState,
41+
};
1942

2043
// This export is added to all contracts that import this package, signifying that they require
2144
// "injective" support on the chain they run on.

0 commit comments

Comments
 (0)