Skip to content

Commit

Permalink
F/deposit msg (#28)
Browse files Browse the repository at this point in the history
* feat: add deposit message

* chore: add further address utils

* feat: add more Msgs and reorganize files

Co-authored-by: Markus Waas <[email protected]>
  • Loading branch information
albertchon and gorgos authored May 5, 2022
1 parent cb5b322 commit 09b1925
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 252 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 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.9"
version = "0.1.12"
authors = ["Albert Chon <[email protected]>"]
edition = "2018"
description = "Bindings for CosmWasm contracts to call into custom modules of Injective Core"
Expand Down
168 changes: 168 additions & 0 deletions packages/injective-cosmwasm/src/derivative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
use crate::order::OrderInfo;
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Position {
#[serde(default)]
pub isLong: bool,
pub quantity: FPDecimal,
pub entry_price: FPDecimal,
#[serde(default)]
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: FPDecimal,
pub entry_price: FPDecimal,
#[serde(default)]
pub effective_margin: FPDecimal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativePosition {
pub subaccount_id: String,
pub market_id: String,
pub position: Position,
}

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

impl DerivativeOrder {
pub fn new(
price: FPDecimal,
quantity: FPDecimal,
margin: FPDecimal,
is_buy: bool,
market_id: &str,
subaccount_id: &str,
fee_recipient: &str,
) -> Self {
DerivativeOrder {
market_id: market_id.to_string(),
order_info: OrderInfo {
subaccount_id: subaccount_id.to_string(),
fee_recipient: fee_recipient.to_string(),
price,
quantity,
},
order_type: if is_buy { 1 } else { 2 }, // TODO PO-orders
margin,
trigger_price: None,
}
}
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
pub fn get_price(&self) -> FPDecimal {
self.order_info.price
}
pub fn get_qty(&self) -> FPDecimal {
self.order_info.quantity
}
pub fn get_val(&self) -> FPDecimal {
self.get_price() * self.get_qty()
}
pub fn get_margin(&self) -> FPDecimal {
self.margin
}
pub fn non_reduce_only_is_invalid(&self) -> bool {
self.get_margin().is_zero() || self.get_price().is_zero() || self.get_qty().is_zero()
}
pub fn reduce_only_price_is_invalid(&self) -> bool {
self.get_price().is_zero()
}
pub fn reduce_only_qty_is_invalid(&self) -> bool {
self.get_qty().is_zero()
}
}

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

impl DerivativeLimitOrder {
pub fn new(
margin: FPDecimal,
fillable: FPDecimal,
order_hash: String,
trigger_price: Option<FPDecimal>,
order_type: i32,
order_info: OrderInfo,
) -> Self {
DerivativeLimitOrder {
margin,
fillable,
order_hash,
trigger_price,
order_type,
order_info,
}
}
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
}

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TrimmedDerivativeLimitOrder {
pub price: FPDecimal,
pub quantity: FPDecimal,
pub margin: FPDecimal,
pub fillable: FPDecimal,
pub isBuy: bool,
pub order_hash: String,
}

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

impl DerivativeMarketOrder {
pub fn new(
order_info: OrderInfo,
order_type: i32,
margin: FPDecimal,
fillable: FPDecimal,
trigger_price: Option<FPDecimal>,
order_hash: String,
) -> Self {
DerivativeMarketOrder {
margin,
fillable,
order_hash,
trigger_price,
order_type,
order_info,
}
}
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
}
67 changes: 67 additions & 0 deletions packages/injective-cosmwasm/src/derivative_market.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct PerpetualMarketInfo {
pub market_id: String,
#[serde(default)]
pub hourly_funding_rate_cap: FPDecimal,
#[serde(default)]
pub hourly_interest_rate: FPDecimal,
#[serde(default)]
pub next_funding_timestamp: i64,
pub funding_interval: i64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct PerpetualMarketFunding {
#[serde(default)]
pub cumulative_funding: FPDecimal,
#[serde(default)]
pub cumulative_price: FPDecimal,
#[serde(default)]
pub last_timestamp: i64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct PerpetualMarketState {
pub market_info: PerpetualMarketInfo,
pub funding_info: PerpetualMarketFunding,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct FullDerivativeMarketPerpetualInfo {
pub perpetual_info: PerpetualMarketState,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct FullDerivativeMarket {
pub market: Option<DerivativeMarket>,
pub info: Option<FullDerivativeMarketPerpetualInfo>,
pub mark_price: FPDecimal,
}

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeMarket {
pub ticker: String,
pub oracle_base: String,
pub oracle_quote: String,
#[serde(default)]
pub oracle_type: i32,
#[serde(default)]
pub oracle_scale_factor: u32,
pub quote_denom: String,
pub market_id: String,
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: FPDecimal,
pub min_quantity_tick_size: FPDecimal,
}
12 changes: 12 additions & 0 deletions packages/injective-cosmwasm/src/exchange.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Deposit is data format for the subaccount deposit
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Deposit {
#[serde(default)]
pub available_balance: FPDecimal,
#[serde(default)]
pub total_balance: FPDecimal,
}
37 changes: 30 additions & 7 deletions packages/injective-cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
mod derivative;
mod derivative_market;
mod exchange;
mod msg;
mod order;
mod querier;
mod query;
mod route;
mod spot;
mod spot_market;
mod subaccount;

pub use msg::{
create_batch_update_orders_msg, create_derivative_market_order_msg, create_subaccount_transfer_msg, DerivativeOrder, InjectiveMsg,
InjectiveMsgWrapper, OrderData, OrderInfo, SpotOrder,
create_batch_update_orders_msg, create_deposit_msg, create_derivative_market_order_msg, create_external_transfer_msg,
create_increase_position_margin_msg, create_liquidate_position_msg, create_register_as_dmm_msg, create_spot_market_order_msg,
create_subaccount_transfer_msg, create_withdraw_msg, InjectiveMsg, InjectiveMsgWrapper,
};

pub use querier::InjectiveQuerier;
pub use query::{
Deposit, DerivativeLimitOrder, DerivativeMarket, EffectivePosition, InjectiveQuery, InjectiveQueryWrapper, PerpetualMarketFunding,
PerpetualMarketInfo, SubaccountDepositResponse,
};
pub use query::{InjectiveQuery, InjectiveQueryWrapper, SubaccountDepositResponse};
pub use route::InjectiveRoute;
pub use subaccount::{address_to_subaccount_id, bech32_to_hex, default_subaccount_id};
pub use subaccount::{
addr_to_bech32, address_to_subaccount_id, bech32_to_hex, default_subaccount_id, subaccount_id_to_ethereum_address,
subaccount_id_to_injective_address,
};

pub use order::{OrderData, OrderInfo};

pub use exchange::Deposit;

pub use spot::{SpotLimitOrder, SpotMarketOrder, SpotOrder, TrimmedSpotLimitOrder};

pub use spot_market::SpotMarket;

pub use derivative::{
DerivativeLimitOrder, DerivativeMarketOrder, DerivativeOrder, DerivativePosition, EffectivePosition, Position, TrimmedDerivativeLimitOrder,
};

pub use derivative_market::{
DerivativeMarket, FullDerivativeMarket, FullDerivativeMarketPerpetualInfo, PerpetualMarketFunding, PerpetualMarketInfo, PerpetualMarketState,
};

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

0 comments on commit 09b1925

Please sign in to comment.