Skip to content

Commit

Permalink
define req and resp types for dex stargate queries and refactor respe…
Browse files Browse the repository at this point in the history
…ctive helpers
  • Loading branch information
sotnikov-s committed Dec 1, 2023
1 parent eb0b075 commit 4d3e10e
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 244 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cosmwasm-std = "1.4.0"
cw2 = "1.1.1"
schemars = "0.8.15"
serde = { version = "1.0.188", default-features = false }
serde_repr = { version = "0.1.17", default-features = false }
serde-json-wasm = "1.0.0"
cw-storage-plus = "1.1.0"
cosmwasm-schema = { version = "1.4.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions packages/neutron-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ readme = "README.md"
cosmwasm-std = { workspace = true }
cosmos-sdk-proto = { workspace = true }
serde = { workspace = true }
serde_repr = { workspace = true }
schemars = { workspace = true }
serde-json-wasm = { workspace = true }
bech32 = { workspace = true }
Expand Down
12 changes: 12 additions & 0 deletions packages/neutron-sdk/src/bindings/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ pub struct PageRequest {
pub reverse: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct PageResponse {
/// **next_key** is the key to be passed to PageRequest.key to
/// query the next page most efficiently. It will be empty if
/// there are no more results.
pub next_key: Option<Binary>,
/// **total** is total number of results available if PageRequest.count_total
/// was set, its value is undefined otherwise
pub total: Option<u64>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct QueryRegisteredQueriesResponse {
Expand Down
56 changes: 8 additions & 48 deletions packages/neutron-sdk/src/stargate/aux.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,28 @@
use cosmwasm_std::{
Binary, ContractResult, CosmosMsg, Deps, Empty, QueryRequest, StdError, StdResult,
SystemResult, Timestamp,
};
use prost::bytes::Bytes;
use cosmwasm_std::{Binary, CosmosMsg, Deps, QueryRequest, StdResult};
use prost_types::Timestamp as TimestampGen;
use serde_json_wasm::to_vec;
use serde::de::DeserializeOwned;

pub(crate) fn make_stargate_query<Req, Res>(deps: Deps, req: Req, path: &str) -> StdResult<Res>
where
Req: prost::Message,
Res: prost::Message + Default,
Res: DeserializeOwned,
{
let raw = to_vec::<QueryRequest<Empty>>(&QueryRequest::Stargate {
deps.querier.query(&QueryRequest::Stargate {
path: path.to_string(),
data: req.encode_to_vec().into(),
})
.map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err))
})?;

match deps.querier.raw_query(&raw) {
SystemResult::Err(system_err) => Err(StdError::generic_err(format!(
"Querier system error: {}",
system_err
))),
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(format!(
"Querier contract error: {}",
contract_err
))),
SystemResult::Ok(ContractResult::Ok(value)) => {
deps.api.debug(
format!(
"WASMDEBUG: stargate query raw resp: {:?}",
value.to_string()
)
.as_str(),
);
deps.api.debug(
format!(
"WASMDEBUG: stargate query to_base_64 resp: {:?}",
value.to_base64()
)
.as_str(),
);

Res::decode(Bytes::copy_from_slice(&value))
.map_err(|e| StdError::generic_err(e.to_string()))
}
}
}

pub(crate) fn create_stargate_msg<Req>(req: Req, path: &str) -> CosmosMsg
where
Req: prost::Message,
{
pub(crate) fn create_stargate_msg<Req: prost::Message>(req: Req, path: &str) -> CosmosMsg {
cosmwasm_std::CosmosMsg::Stargate {
type_url: path.to_string(),
value: Binary::from(req.encode_to_vec()),
}
}

pub(crate) fn convert_timestamp(timestamp: Timestamp) -> TimestampGen {
pub(crate) fn convert_timestamp(timestamp: u64) -> TimestampGen {
TimestampGen {
seconds: i64::try_from(timestamp.seconds()).unwrap(),
nanos: i32::try_from(timestamp.subsec_nanos()).unwrap(),
seconds: timestamp as i64,
nanos: 0,
}
}
2 changes: 2 additions & 0 deletions packages/neutron-sdk/src/stargate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub(crate) mod msg_dex;
pub(crate) mod proto_types;
pub(crate) mod query_dex;

pub mod types_dex;

pub mod query {
pub mod neutron {
pub mod dex {
Expand Down
31 changes: 9 additions & 22 deletions packages/neutron-sdk/src/stargate/msg_dex.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::proto_types::neutron::dex::{
DepositOptions as DepositOptionsGen, LimitOrderType, MsgCancelLimitOrder, MsgDeposit,
MsgMultiHopSwap, MsgPlaceLimitOrder, MsgWithdrawFilledLimitOrder, MsgWithdrawal, MultiHopRoute,
};
use crate::stargate::aux::{convert_timestamp, create_stargate_msg};
use cosmwasm_std::{CosmosMsg, Timestamp};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::stargate::proto_types::neutron::dex::{
MsgCancelLimitOrder, MsgDeposit, MsgMultiHopSwap, MsgPlaceLimitOrder,
MsgWithdrawFilledLimitOrder, MsgWithdrawal, MultiHopRoute,
};
use crate::stargate::types_dex::DepositOptions;
use crate::stargate::types_dex::LimitOrderType;
use cosmwasm_std::CosmosMsg;

const DEPOSIT_MSG_PATH: &str = "/neutron.dex.MsgDeposit";
const WITHDRAWAL_MSG_PATH: &str = "/neutron.dex.MsgWithdrawal";
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn msg_place_limit_order(
tick_index_in_to_out: i64,
amount_in: String,
order_type: LimitOrderType,
expiration_time: Option<Timestamp>,
expiration_time: Option<u64>,
max_amount_out: Option<String>,
) -> CosmosMsg {
let msg = MsgPlaceLimitOrder {
Expand All @@ -80,7 +80,7 @@ pub fn msg_place_limit_order(
token_out,
tick_index_in_to_out,
amount_in,
order_type: i32::from(order_type),
order_type: order_type as i32,
expiration_time: expiration_time.map(convert_timestamp),
max_amount_out: max_amount_out.unwrap_or_default(),
};
Expand Down Expand Up @@ -124,16 +124,3 @@ pub fn msg_multi_hop_swap(
};
create_stargate_msg(msg, MULTI_HOP_SWAP_MSG_PATH)
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct DepositOptions {
pub disable_autoswap: bool,
}

impl From<DepositOptions> for DepositOptionsGen {
fn from(o: DepositOptions) -> DepositOptionsGen {
DepositOptionsGen {
disable_autoswap: o.disable_autoswap,
}
}
}
Loading

0 comments on commit 4d3e10e

Please sign in to comment.