Skip to content

Commit

Permalink
Merge pull request #143 from neutron-org/fix/add_price_to_lo
Browse files Browse the repository at this point in the history
Fix/add price to lo
  • Loading branch information
pr0n00gler authored May 31, 2024
2 parents 8c30912 + f4a2945 commit ab2fa67
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/neutron-sdk/schema/neutron_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@
"type": "object",
"required": [
"amount_in",
"limit_sell_price",
"order_type",
"receiver",
"tick_index_in_to_out",
Expand All @@ -1064,6 +1065,10 @@
"format": "uint64",
"minimum": 0.0
},
"limit_sell_price": {
"description": "Accepts standard decimals and decimals with scientific notation (ie. 1234.23E-7)",
"type": "string"
},
"max_amount_out": {
"description": "Maximum amount of TokenB can be bought. For everything except JUST_IN_TIME OrderType",
"anyOf": [
Expand Down
2 changes: 2 additions & 0 deletions packages/neutron-sdk/src/bindings/dex/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum DexMsg {
expiration_time: Option<u64>,
/// Maximum amount of TokenB can be bought. For everything except JUST_IN_TIME OrderType
max_amount_out: Option<Uint128>,
/// Accepts standard decimals and decimals with scientific notation (ie. 1234.23E-7)
limit_sell_price: String,
},
/// WithdrawFilledLimitOrder. Once a limit order has been filled – either partially or in
/// its entirety, it can be withdrawn at any time. Withdrawing from a limit order credits
Expand Down
2 changes: 1 addition & 1 deletion packages/neutron-sdk/src/proto_types/NEUTRON_COMMIT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2bf5ac94f7dbb8c31985c8f789bc8e49094adce0
3f3c8f4adf20a9f142e1b4a55c83f5d16aa2cef6
4 changes: 4 additions & 0 deletions packages/neutron-sdk/src/proto_types/neutron.dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ pub struct MsgPlaceLimitOrder {
pub token_in: ::prost::alloc::string::String,
#[prost(string, tag = "4")]
pub token_out: ::prost::alloc::string::String,
/// DEPRECATED: tick_index_in_to_out will be removed in future release; limit_sell_price should be used instead.
#[deprecated]
#[prost(int64, tag = "5")]
pub tick_index_in_to_out: i64,
#[prost(string, tag = "7")]
Expand All @@ -175,6 +177,8 @@ pub struct MsgPlaceLimitOrder {
pub expiration_time: ::core::option::Option<::prost_types::Timestamp>,
#[prost(string, tag = "10")]
pub max_amount_out: ::prost::alloc::string::String,
#[prost(string, tag = "11")]
pub limit_sell_price: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MsgPlaceLimitOrderResponse {
Expand Down
37 changes: 37 additions & 0 deletions packages/neutron-sdk/src/stargate/dex/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pub struct PlaceLimitOrderRequest {
pub token_out: String,
/// Limit tick for a limit order, specified in terms of token_in to token_out.
pub tick_index_in_to_out: i64,
/// limit sell price when selling token_in.
/// Accepts standard decimals and decimals with scientific notation (ie. 1234.23E-7)
pub limit_sell_price: String,
/// Amount of TokenIn to be traded.
pub amount_in: String,
/// Type of limit order to be used.
Expand All @@ -123,8 +126,41 @@ pub struct PlaceLimitOrderRequest {
pub max_amount_out: Option<String>,
}

const PREC_DEC_PRECISION: usize = 27;

fn serialize_prec_dec(decimal_str: String) -> String {
// The proto marshaller expects the decimal to come as an integer that will be divided by 10^PREC_DEC_PRECISION to produce a PrecDec
// There is no available decimal type that can hold 27 decimals of precision. So instead we use string manipulation to serialize the PrecDec into an integer
let parts: Vec<&str> = decimal_str.split('.').collect();
let integer_part = parts[0];
let mut fractional_part = if parts.len() > 1 {
String::from(parts[1])
} else {
String::new()
};
// Remove trailing zeros from the fractional_part
fractional_part = fractional_part.trim_end_matches('0').to_string();

// Remove leading zeros from the integer_part
let mut result = integer_part.trim_start_matches('0').to_string();

// combine integer part and fractional part
result.push_str(&fractional_part.to_owned());

// Add zeros to the end. This is the equivalent of multiplying by 10^PREC_DEC_PRECISION
let zeros_to_add = PREC_DEC_PRECISION
.checked_sub(fractional_part.len())
.expect("Cannot retain precision when serializing PrecDec");
for _ in 0..zeros_to_add {
result.push('0');
}

result
}

impl From<PlaceLimitOrderRequest> for MsgPlaceLimitOrder {
fn from(v: PlaceLimitOrderRequest) -> MsgPlaceLimitOrder {
#[allow(deprecated)] // tick_index_in_to_out will be removed in the next release
MsgPlaceLimitOrder {
creator: v.sender,
receiver: v.receiver,
Expand All @@ -135,6 +171,7 @@ impl From<PlaceLimitOrderRequest> for MsgPlaceLimitOrder {
order_type: v.order_type as i32,
expiration_time: v.expiration_time.map(proto_timestamp_from_i64),
max_amount_out: v.max_amount_out.unwrap_or_default(),
limit_sell_price: serialize_prec_dec(v.limit_sell_price),
}
}
}
Expand Down

0 comments on commit ab2fa67

Please sign in to comment.