Skip to content

Commit

Permalink
Add check liquidity query (#18)
Browse files Browse the repository at this point in the history
* Add check liquidity query

* Resolve lints

Co-authored-by: Cyson <[email protected]>
  • Loading branch information
LCyson and Cyson authored Aug 9, 2022
1 parent 841f6cd commit c1c84d2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
15 changes: 13 additions & 2 deletions contracts/sei-tester/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::msg::{
};
use sei_cosmwasm::{
DexTwapsResponse, EpochResponse, ExchangeRatesResponse, GetOrderByIdResponse,
GetOrdersResponse, OracleTwapsResponse, Order, OrderType, PositionDirection, SeiMsg,
SeiQuerier, SeiQueryWrapper,
GetOrdersResponse, OracleTwapsResponse, Order, OrderSimulationResponse, OrderType,
PositionDirection, SeiMsg, SeiQuerier, SeiQueryWrapper,
};

#[entry_point]
Expand Down Expand Up @@ -212,6 +212,7 @@ pub fn query(deps: Deps<SeiQueryWrapper>, _env: Env, msg: QueryMsg) -> StdResult
contract_address,
lookback_seconds,
} => to_binary(&query_dex_twaps(deps, contract_address, lookback_seconds)?),
QueryMsg::OrderSimulation { order } => to_binary(&query_order_simulation(deps, order)?),
QueryMsg::Epoch {} => to_binary(&query_epoch(deps)?),
QueryMsg::GetOrders {
contract_address,
Expand Down Expand Up @@ -261,6 +262,16 @@ pub fn query_dex_twaps(
Ok(res)
}

pub fn query_order_simulation(
deps: Deps<SeiQueryWrapper>,
order: Order,
) -> StdResult<OrderSimulationResponse> {
let querier = SeiQuerier::new(&deps.querier);
let res: OrderSimulationResponse = querier.query_order_simulation(order)?;

Ok(res)
}

pub fn query_epoch(deps: Deps<SeiQueryWrapper>) -> StdResult<EpochResponse> {
let querier = SeiQuerier::new(&deps.querier);
let res: EpochResponse = querier.query_epoch()?;
Expand Down
3 changes: 3 additions & 0 deletions contracts/sei-tester/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ pub enum QueryMsg {
contract_address: String,
lookback_seconds: u64,
},
OrderSimulation {
order: Order,
},
Epoch {},
GetOrders {
contract_address: String,
Expand Down
2 changes: 1 addition & 1 deletion packages/sei-cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use proto_structs::{
pub use querier::SeiQuerier;
pub use query::{
DexTwapsResponse, EpochResponse, ExchangeRatesResponse, GetOrderByIdResponse,
GetOrdersResponse, OracleTwapsResponse, SeiQuery, SeiQueryWrapper,
GetOrdersResponse, OracleTwapsResponse, OrderSimulationResponse, SeiQuery, SeiQueryWrapper,
};
pub use route::SeiRoute;
pub use sei_types::{Order, OrderResponse, OrderType, PositionDirection};
Expand Down
28 changes: 24 additions & 4 deletions packages/sei-cosmwasm/src/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use cosmwasm_std::{Addr, QuerierWrapper, StdResult};

use crate::query::{
DexTwapsResponse, EpochResponse, ExchangeRatesResponse, GetOrderByIdResponse,
GetOrdersResponse, OracleTwapsResponse, SeiQuery, SeiQueryWrapper,
GetOrdersResponse, OracleTwapsResponse, OrderSimulationResponse, SeiQuery, SeiQueryWrapper,
};
use crate::route::SeiRoute;
use crate::Order;

/// This is a helper wrapper to easily use our custom queries
pub struct SeiQuerier<'a> {
Expand All @@ -16,6 +17,9 @@ impl<'a> SeiQuerier<'a> {
SeiQuerier { querier }
}

/*
query oracle module
*/
pub fn query_exchange_rates(&self) -> StdResult<ExchangeRatesResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Oracle,
Expand All @@ -36,6 +40,9 @@ impl<'a> SeiQuerier<'a> {
self.querier.query(&request)
}

/*
query dex module
*/
pub fn query_dex_twaps(
&self,
lookback_seconds: u64,
Expand All @@ -53,12 +60,13 @@ impl<'a> SeiQuerier<'a> {
self.querier.query(&request)
}

pub fn query_epoch(&self) -> StdResult<EpochResponse> {
pub fn query_order_simulation(&self, order: Order) -> StdResult<OrderSimulationResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Epoch,
query_data: SeiQuery::Epoch {},
route: SeiRoute::Dex,
query_data: SeiQuery::OrderSimulation { order },
}
.into();

self.querier.query(&request)
}

Expand Down Expand Up @@ -97,4 +105,16 @@ impl<'a> SeiQuerier<'a> {
.into();
self.querier.query(&request)
}

/*
query epoch module
*/
pub fn query_epoch(&self) -> StdResult<EpochResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Epoch,
query_data: SeiQuery::Epoch {},
}
.into();
self.querier.query(&request)
}
}
10 changes: 10 additions & 0 deletions packages/sei-cosmwasm/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::proto_structs::{DenomOracleExchangeRatePair, DexTwap, Epoch, OracleTwap};
use crate::route::SeiRoute;
use crate::sei_types::OrderResponse;
use crate::Order;

/// SeiQueryWrapper is an override of QueryRequest::Custom to access Sei-specific modules
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -40,6 +41,9 @@ pub enum SeiQuery {
asset_denom: String,
id: u64,
},
OrderSimulation {
order: Order,
},
}

/// ExchangeRatesResponse is data format returned from OracleRequest::ExchangeRates query
Expand Down Expand Up @@ -77,3 +81,9 @@ pub struct GetOrdersResponse {
pub struct GetOrderByIdResponse {
pub order: OrderResponse,
}

/// OrderSimulationResponse is data format returned from OrderSimulation query
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OrderSimulationResponse {
pub executed_quantity: String,
}

0 comments on commit c1c84d2

Please sign in to comment.