Skip to content

Commit

Permalink
Merge pull request #175 from InjectiveLabs/f/exchange-params
Browse files Browse the repository at this point in the history
added exchange params to injective-cosmwasm
  • Loading branch information
maxrobot authored Sep 19, 2023
2 parents fd10d48 + 69eca97 commit 5f7faed
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 21 deletions.
4 changes: 2 additions & 2 deletions 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
Expand Up @@ -11,7 +11,7 @@ license = "Apache-2.0"
name = "injective-cosmwasm"
readme = "README.md"
repository = "https://github.com/InjectiveLabs/cw-injective/tree/master/packages/bindings"
version = "0.2.12"
version = "0.2.13"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
7 changes: 6 additions & 1 deletion packages/injective-cosmwasm/src/exchange/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ use crate::exchange::{
derivative_market::{FullDerivativeMarket, PerpetualMarketFunding, PerpetualMarketInfo},
spot::TrimmedSpotLimitOrder,
spot_market::SpotMarket,
types::{DenomDecimals, Deposit, MarketVolume, PriceLevel, VolumeByType},
types::{DenomDecimals, Deposit, MarketVolume, Params, PriceLevel, VolumeByType},
};
use crate::oracle::volatility::{MetadataStatistics, TradeRecord};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ExchangeParamsResponse {
pub params: Option<Params>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct SubaccountDepositResponse {
pub deposits: Deposit,
Expand Down
43 changes: 42 additions & 1 deletion packages/injective-cosmwasm/src/exchange/types.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
use cosmwasm_std::{Empty, StdError, StdResult};
use cosmwasm_std::{Coin, Empty, StdError, StdResult};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::ser::Error as SerError;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt;

use crate::InjectiveQuerier;

pub const UNSORTED_CANCELLATION_STRATEGY: i32 = 0;
pub const FROM_WORST_TO_BEST_CANCELLATION_STRATEGY: i32 = 1;

/// Params is the response type for the exchange params
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Params {
pub spot_market_instant_listing_fee: Coin,
pub derivative_market_instant_listing_fee: Coin,
pub default_spot_maker_fee_rate: FPDecimal,
pub default_spot_taker_fee_rate: FPDecimal,
pub default_derivative_maker_fee_rate: FPDecimal,
pub default_derivative_taker_fee_rate: FPDecimal,
pub default_initial_margin_ratio: FPDecimal,
pub default_maintenance_margin_ratio: FPDecimal,
pub default_funding_interval: i64,
pub relayer_fee_share_rate: FPDecimal,
pub default_hourly_funding_rate_cap: FPDecimal,
pub default_hourly_interest_rate: FPDecimal,
pub max_derivative_order_side_count: u32,
pub inj_reward_staked_requirement_threshold: FPDecimal,
pub trading_rewards_vesting_duration: i64,
pub liquidator_reward_share_rate: FPDecimal,
pub binary_options_market_instant_listing_fee: Coin,
#[serde(default)]
pub atomic_market_order_access_level: AtomicMarketOrderAccessLevel,
pub spot_atomic_market_order_fee_multiplier: FPDecimal,
pub derivative_atomic_market_order_fee_multiplier: FPDecimal,
pub binary_options_atomic_market_order_fee_multiplier: FPDecimal,
pub minimal_protocol_fee_rate: FPDecimal,
pub is_instant_derivative_market_launch_enabled: bool,
}

/// Deposit is data format for the subaccount deposit
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Deposit {
Expand Down Expand Up @@ -57,6 +88,16 @@ pub enum MarketType {
Derivative,
}

#[derive(Serialize_repr, Deserialize_repr, Default, Clone, Debug, PartialEq, Eq, JsonSchema, Copy)]
#[repr(i32)]
pub enum AtomicMarketOrderAccessLevel {
#[default]
Nobody = 0,
BeginBlockerSmartContractsOnly = 1,
SmartContractsOnly = 2,
Everyone = 3,
}

#[derive(Serialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, JsonSchema)]
pub struct MarketId(String);

Expand Down
57 changes: 51 additions & 6 deletions packages/injective-cosmwasm/src/exchange_mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use injective_math::FPDecimal;
use crate::exchange::{
derivative_market::DerivativeMarket,
response::QueryOrderbookResponse,
types::{MarketVolume, PriceLevel, VolumeByType},
types::{AtomicMarketOrderAccessLevel, MarketVolume, Params, PriceLevel, VolumeByType},
};
use crate::oracle::{
types::{OracleHistoryOptions, OracleType, PriceState, PythPriceState},
Expand All @@ -21,11 +21,12 @@ use crate::oracle::{
use crate::tokenfactory::response::{TokenFactoryCreateDenomFeeResponse, TokenFactoryDenomSupplyResponse};
use crate::wasmx::response::QueryContractRegistrationInfoResponse;
use crate::{
Deposit, DerivativeMarketResponse, FullDerivativeMarket, InjectiveQuery, InjectiveQueryWrapper, MarketMidPriceAndTOBResponse, MarketStatus,
MarketVolatilityResponse, OracleInfo, OracleVolatilityResponse, OrderSide, PerpetualMarketFundingResponse, PerpetualMarketInfoResponse,
PythPriceResponse, QueryAggregateMarketVolumeResponse, QueryAggregateVolumeResponse, QueryDenomDecimalResponse, QueryDenomDecimalsResponse,
QueryMarketAtomicExecutionFeeMultiplierResponse, SpotMarket, SpotMarketResponse, SubaccountDepositResponse,
SubaccountEffectivePositionInMarketResponse, SubaccountPositionInMarketResponse, TraderDerivativeOrdersResponse, TraderSpotOrdersResponse,
Deposit, DerivativeMarketResponse, ExchangeParamsResponse, FullDerivativeMarket, InjectiveQuery, InjectiveQueryWrapper,
MarketMidPriceAndTOBResponse, MarketStatus, MarketVolatilityResponse, OracleInfo, OracleVolatilityResponse, OrderSide,
PerpetualMarketFundingResponse, PerpetualMarketInfoResponse, PythPriceResponse, QueryAggregateMarketVolumeResponse, QueryAggregateVolumeResponse,
QueryDenomDecimalResponse, QueryDenomDecimalsResponse, QueryMarketAtomicExecutionFeeMultiplierResponse, SpotMarket, SpotMarketResponse,
SubaccountDepositResponse, SubaccountEffectivePositionInMarketResponse, SubaccountPositionInMarketResponse, TraderDerivativeOrdersResponse,
TraderSpotOrdersResponse,
};
use crate::{MarketId, SubaccountId};

Expand Down Expand Up @@ -279,6 +280,40 @@ fn default_all_balances_bank_query_handler() -> QuerierResult {
SystemResult::Ok(ContractResult::from(to_binary(&response)))
}

fn default_exchange_params_response_handler() -> QuerierResult {
let denom = "inj";

let response = ExchangeParamsResponse {
params: Some(Params {
spot_market_instant_listing_fee: Coin::new(100000000000000000000, denom),
derivative_market_instant_listing_fee: Coin::new(1000000000000000000000, denom),
default_spot_maker_fee_rate: FPDecimal::must_from_str("-0.0001"),
default_spot_taker_fee_rate: FPDecimal::must_from_str("0.001"),
default_derivative_maker_fee_rate: FPDecimal::must_from_str("-0.0001"),
default_derivative_taker_fee_rate: FPDecimal::must_from_str("0.001"),
default_initial_margin_ratio: FPDecimal::must_from_str("0.05"),
default_maintenance_margin_ratio: FPDecimal::must_from_str("0.02"),
default_funding_interval: 3600i64,
// default_multiple: 3600i64,
relayer_fee_share_rate: FPDecimal::must_from_str("0.4"),
default_hourly_funding_rate_cap: FPDecimal::must_from_str("0.000625"),
default_hourly_interest_rate: FPDecimal::must_from_str("0.00000416666"),
max_derivative_order_side_count: 20u32,
inj_reward_staked_requirement_threshold: FPDecimal::must_from_str("25000000000000000000"),
trading_rewards_vesting_duration: 1209600i64,
liquidator_reward_share_rate: FPDecimal::must_from_str("0.05"),
binary_options_market_instant_listing_fee: Coin::new(10000000000000000000, denom),
atomic_market_order_access_level: AtomicMarketOrderAccessLevel::SmartContractsOnly,
spot_atomic_market_order_fee_multiplier: FPDecimal::must_from_str("2.0"),
derivative_atomic_market_order_fee_multiplier: FPDecimal::must_from_str("2.0"),
binary_options_atomic_market_order_fee_multiplier: FPDecimal::must_from_str("2.0"),
minimal_protocol_fee_rate: FPDecimal::must_from_str("0.00001"),
is_instant_derivative_market_launch_enabled: true,
}),
};
SystemResult::Ok(ContractResult::from(to_binary(&response)))
}

fn default_spot_market_orderbook_response_handler() -> QuerierResult {
let response = QueryOrderbookResponse {
buys_price_level: vec![PriceLevel::new(9u128.into(), 10u128.into()), PriceLevel::new(8u128.into(), 10u128.into())],
Expand Down Expand Up @@ -409,9 +444,14 @@ pub trait HandlesPriceLevelsQuery {
fn handle(&self, market_id: MarketId, order_side: OrderSide) -> QuerierResult;
}

pub trait HandlesExchangeParamsQuery {
fn handle(&self) -> QuerierResult;
}

pub struct WasmMockQuerier {
pub smart_query_handler: Option<Box<dyn HandlesSmartQuery>>,
pub subaccount_deposit_response_handler: Option<Box<dyn HandlesSubaccountAndDenomQuery>>,
pub exchange_params_response_handler: Option<Box<dyn HandlesExchangeParamsQuery>>,
pub spot_market_response_handler: Option<Box<dyn HandlesMarketIdQuery>>,
pub trader_spot_orders_response_handler: Option<Box<dyn HandlesMarketAndSubaccountQuery>>,
pub trader_spot_orders_to_cancel_up_to_amount_response_handler: Option<Box<dyn HandlesTraderSpotOrdersToCancelUpToAmountQuery>>,
Expand Down Expand Up @@ -492,6 +532,10 @@ impl WasmMockQuerier {
Some(handler) => handler.handle(subaccount_id, denom),
None => default_subaccount_deposit_response_handler(),
},
InjectiveQuery::ExchangeParams {} => match &self.exchange_params_response_handler {
Some(handler) => handler.handle(),
None => default_exchange_params_response_handler(),
},
InjectiveQuery::SpotMarket { market_id } => match &self.spot_market_response_handler {
Some(handler) => handler.handle(market_id),
None => default_spot_market_response_handler(market_id),
Expand Down Expand Up @@ -656,6 +700,7 @@ impl WasmMockQuerier {
WasmMockQuerier {
smart_query_handler: None,
subaccount_deposit_response_handler: None,
exchange_params_response_handler: None,
spot_market_response_handler: None,
trader_spot_orders_response_handler: None,
trader_spot_orders_to_cancel_up_to_amount_response_handler: None,
Expand Down
13 changes: 7 additions & 6 deletions packages/injective-cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ pub use exchange::{
market::MarketStatus,
order::{GenericOrder, GenericTrimmedOrder, OrderData, OrderInfo, OrderSide, OrderType},
response::{
DerivativeMarketResponse, MarketMidPriceAndTOBResponse, MarketVolatilityResponse, OracleVolatilityResponse, PerpetualMarketFundingResponse,
PerpetualMarketInfoResponse, QueryAggregateMarketVolumeResponse, QueryAggregateVolumeResponse, QueryDenomDecimalResponse,
QueryDenomDecimalsResponse, QueryMarketAtomicExecutionFeeMultiplierResponse, SpotMarketResponse, SubaccountDepositResponse,
SubaccountEffectivePositionInMarketResponse, SubaccountPositionInMarketResponse, TraderDerivativeOrdersResponse, TraderSpotOrdersResponse,
DerivativeMarketResponse, ExchangeParamsResponse, MarketMidPriceAndTOBResponse, MarketVolatilityResponse, OracleVolatilityResponse,
PerpetualMarketFundingResponse, PerpetualMarketInfoResponse, QueryAggregateMarketVolumeResponse, QueryAggregateVolumeResponse,
QueryDenomDecimalResponse, QueryDenomDecimalsResponse, QueryMarketAtomicExecutionFeeMultiplierResponse, SpotMarketResponse,
SubaccountDepositResponse, SubaccountEffectivePositionInMarketResponse, SubaccountPositionInMarketResponse, TraderDerivativeOrdersResponse,
TraderSpotOrdersResponse,
},
spot::{MsgCreateSpotMarketOrderResponse, SpotLimitOrder, SpotMarketOrder, SpotOrder, TrimmedSpotLimitOrder},
spot_market::SpotMarket,
Expand All @@ -20,8 +21,8 @@ pub use exchange::{
subaccount_id_to_ethereum_address, subaccount_id_to_injective_address, subaccount_id_to_unchecked_injective_address,
},
types::{
DenomDecimals, Deposit, Hash, MarketId, MarketType, PriceLevel, ShortSubaccountId, SubaccountId, FROM_WORST_TO_BEST_CANCELLATION_STRATEGY,
UNSORTED_CANCELLATION_STRATEGY,
DenomDecimals, Deposit, Hash, MarketId, MarketType, Params, PriceLevel, ShortSubaccountId, SubaccountId,
FROM_WORST_TO_BEST_CANCELLATION_STRATEGY, UNSORTED_CANCELLATION_STRATEGY,
},
};
pub use oracle::{
Expand Down
19 changes: 15 additions & 4 deletions packages/injective-cosmwasm/src/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::exchange::response::StakedAmountResponse;
use crate::exchange::{
order::OrderSide,
response::{
DerivativeMarketResponse, MarketMidPriceAndTOBResponse, MarketVolatilityResponse, OracleVolatilityResponse, PerpetualMarketFundingResponse,
PerpetualMarketInfoResponse, QueryAggregateVolumeResponse, QueryDenomDecimalResponse, QueryDenomDecimalsResponse,
QueryMarketAtomicExecutionFeeMultiplierResponse, QueryOrderbookResponse, SpotMarketResponse, SubaccountDepositResponse,
SubaccountEffectivePositionInMarketResponse, SubaccountPositionInMarketResponse, TraderDerivativeOrdersResponse, TraderSpotOrdersResponse,
DerivativeMarketResponse, ExchangeParamsResponse, MarketMidPriceAndTOBResponse, MarketVolatilityResponse, OracleVolatilityResponse,
PerpetualMarketFundingResponse, PerpetualMarketInfoResponse, QueryAggregateVolumeResponse, QueryDenomDecimalResponse,
QueryDenomDecimalsResponse, QueryMarketAtomicExecutionFeeMultiplierResponse, QueryOrderbookResponse, SpotMarketResponse,
SubaccountDepositResponse, SubaccountEffectivePositionInMarketResponse, SubaccountPositionInMarketResponse, TraderDerivativeOrdersResponse,
TraderSpotOrdersResponse,
},
types::{MarketId, SubaccountId},
};
Expand Down Expand Up @@ -76,6 +77,16 @@ impl<'a> InjectiveQuerier<'a> {
}

// Exchange
pub fn query_exchange_params(&self) -> StdResult<ExchangeParamsResponse> {
let request = InjectiveQueryWrapper {
route: InjectiveRoute::Exchange,
query_data: InjectiveQuery::ExchangeParams {},
};

let res: ExchangeParamsResponse = self.querier.query(&request.into())?;
Ok(res)
}

pub fn query_subaccount_deposit<T: Into<SubaccountId> + Clone, P: Into<String> + Clone>(
&self,
subaccount_id: &'a T,
Expand Down
1 change: 1 addition & 0 deletions packages/injective-cosmwasm/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum InjectiveQuery {
pagination: Option<u32>,
},
// Exchange
ExchangeParams {},
SubaccountDeposit {
subaccount_id: SubaccountId,
denom: String,
Expand Down

0 comments on commit 5f7faed

Please sign in to comment.