Skip to content

Commit

Permalink
wip: normalizing stargate dex responses
Browse files Browse the repository at this point in the history
  • Loading branch information
sotnikov-s committed Dec 4, 2023
1 parent 5a8bcfd commit 67459a0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ thiserror = "1.0.49"
protobuf = { version = "3.3.0" }
hex = "0.4.3"
tendermint-proto = "0.34"
speedate = "0.13.0"
1 change: 1 addition & 0 deletions packages/neutron-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cosmwasm-schema = { workspace = true }
prost = { workspace = true }
prost-types = { workspace = true }
tendermint-proto = { workspace = true }
speedate = { workspace = true }

[dev-dependencies]
base64 = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions packages/neutron-sdk/src/stargate/aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pub(crate) fn create_stargate_msg<Req: prost::Message>(req: Req, path: &str) ->
}
}

pub(crate) fn convert_timestamp(timestamp: u64) -> TimestampGen {
pub(crate) fn convert_timestamp(timestamp: i64) -> TimestampGen {
TimestampGen {
seconds: timestamp as i64,
seconds: timestamp,
nanos: 0,
}
}
2 changes: 1 addition & 1 deletion packages/neutron-sdk/src/stargate/msg_dex.rs
Original file line number Diff line number Diff line change
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<u64>,
expiration_time: Option<i64>,
max_amount_out: Option<String>,
) -> CosmosMsg {
let msg = MsgPlaceLimitOrder {
Expand Down
40 changes: 33 additions & 7 deletions packages/neutron-sdk/src/stargate/types_dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use crate::stargate::proto_types::neutron::dex::{
QueryPoolByIdRequest, QueryPoolRequest,
};
use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest as PageRequestGen;
use cosmwasm_std::{Coin, Int128, Int64, Timestamp, Uint64};
use cosmwasm_std::{Coin, Int128, Int64, Uint64};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use serde::{Deserialize, Deserializer, Serialize};
use speedate::DateTime;

// Params query

Expand Down Expand Up @@ -340,7 +340,7 @@ pub struct EstimatePlaceLimitOrderRequest {
pub tick_index_in_to_out: i64,
pub amount_in: String,
pub order_type: LimitOrderType,
pub expiration_time: Option<u64>,
pub expiration_time: Option<i64>,
pub max_amount_out: Option<String>,
}

Expand Down Expand Up @@ -469,8 +469,9 @@ pub struct Params {
pub max_true_taker_spread: String,
}

#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[repr(i32)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[schemars(with = "String")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum LimitOrderType {
GoodTilCancelled = 0,
FillOrKill = 1,
Expand Down Expand Up @@ -523,7 +524,8 @@ pub struct LimitOrderTranche {
pub reserves_taker_denom: Int128,
pub total_maker_denom: Int128,
pub total_taker_denom: Int128,
pub expiration_time: Option<Timestamp>,
#[serde(deserialize_with = "deserialize_expiration_time")]
pub expiration_time: Option<i64>,
pub price_taker_to_maker: String, // TODO: refactor to PrecDec
}

Expand All @@ -544,6 +546,7 @@ pub struct PairID {
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Liquidity {
PoolReserves(PoolReserves),
LimitOrderTranche(LimitOrderTranche),
Expand Down Expand Up @@ -597,3 +600,26 @@ fn convert_page_request(page_request: Option<PageRequest>) -> Option<PageRequest
None => None,
}
}

fn deserialize_expiration_time<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize the field as an Option<&str>
let opt_string: Option<&str> = Option::deserialize(deserializer)?;

// Convert the &str to a i64 or return None if it's None or an invalid format
match opt_string {
Some(v) => Ok(Some(
DateTime::parse_str_rfc3339(v)
.map_err(|e| {
serde::de::Error::invalid_value(
serde::de::Unexpected::Str(v),
&"an RFC 3339 formatted date time",
)
})?
.timestamp(),
)),
None => Ok(None),
}
}

0 comments on commit 67459a0

Please sign in to comment.