Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lazer/sdk/rust/protocol/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::router::{
Channel, Format, JsonBinaryEncoding, JsonUpdate, PriceFeedId, PriceFeedProperty,
Channel, Format, JsonBinaryEncoding, JsonUpdate, PriceFeedId, PriceFeedProperty, TimestampUs,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -21,6 +21,24 @@ pub struct LatestPriceRequest {
pub channel: Channel,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PriceRequest {
Copy link
Contributor

@darunrs darunrs Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this API different from the LatestPrice API that router already has? Can we just extend it instead? Granted this is a little late to say it, but I am confused on the positioning of this API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok I see. We take in a timestamp to fetch the price for. Theoretically we could unify them but I guess that's not a big deal right now.

pub timestamp: TimestampUs,
pub price_feed_ids: Vec<PriceFeedId>,
pub properties: Vec<PriceFeedProperty>,
// "chains" was renamed to "formats". "chains" is still supported for compatibility.
#[serde(alias = "chains")]
pub formats: Vec<Format>,
#[serde(default)]
pub json_binary_encoding: JsonBinaryEncoding,
/// If `true`, the stream update will contain a JSON object containing
/// all data of the update.
#[serde(default = "default_parsed")]
pub parsed: bool,
pub channel: Channel,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReducePriceRequest {
Expand All @@ -30,6 +48,7 @@ pub struct ReducePriceRequest {

pub type LatestPriceResponse = JsonUpdate;
pub type ReducePriceResponse = JsonUpdate;
pub type PriceResponse = JsonUpdate;

pub fn default_parsed() -> bool {
true
Expand Down
18 changes: 18 additions & 0 deletions lazer/sdk/rust/protocol/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
rust_decimal::{prelude::FromPrimitive, Decimal},
serde::{de::Error, Deserialize, Serialize},
std::{
fmt::Display,
num::NonZeroI64,
ops::{Add, Deref, DerefMut, Div, Sub},
time::{SystemTime, UNIX_EPOCH},
Expand Down Expand Up @@ -66,6 +67,12 @@ impl Rate {
let value: i64 = value.try_into().context("overflow")?;
Ok(Self(value))
}

pub fn from_integer(value: i64, exponent: u32) -> anyhow::Result<Self> {
let coef = 10i64.checked_pow(exponent).context("overflow")?;
let value = value.checked_mul(coef).context("overflow")?;
Ok(Self(value))
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
Expand Down Expand Up @@ -236,6 +243,17 @@ mod channel_ids {
pub const FIXED_RATE_200: ChannelId = ChannelId(3);
}

impl Display for Channel {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably just use serde to serialize it as a string, which gets you the same result without a duplicate implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into it but couldn't find a good way of handling it. If you want please give it a try and let me know if it worked.

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Channel::FixedRate(fixed_rate) => match *fixed_rate {
FixedRate::MIN => write!(f, "real_time"),
rate => write!(f, "fixed_rate@{}ms", rate.value_ms()),
},
}
}
}

impl Channel {
pub fn id(&self) -> ChannelId {
match self {
Expand Down
Loading