Skip to content
13 changes: 4 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lazer/publisher_sdk/proto/publisher_update.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ message PriceUpdate {

// feed update containing data relating to funding rate
message FundingRateUpdate {
enum FundingRateInterval {
INTERVAL_1H = 0;
INTERVAL_4H = 1;
INTERVAL_8H = 2;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't seem like a good idea to restrict the possible set of values here. Why not use a Duration?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, maybe in the future people have half an hour too :D


// [optional] price for which the funding rate applies to
optional int64 price = 1;

// [optional] perpetual future funding rate
optional int64 rate = 2;

// [optional] funding rate update
optional FundingRateInterval funding_rate_interval = 3;
}
7 changes: 1 addition & 6 deletions lazer/publisher_sdk/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyth-lazer-publisher-sdk"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Pyth Lazer Publisher SDK types."
license = "Apache-2.0"
Expand All @@ -10,12 +10,7 @@ repository = "https://github.com/pyth-network/pyth-crosschain"
pyth-lazer-protocol = { version = "0.9.1", path = "../../sdk/rust/protocol" }
anyhow = "1.0.98"
protobuf = "3.7.2"
humantime = "2.2.0"
tracing = "0.1.41"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
derive_more = { version = "2.0.1", features = ["from"] }
hex = "0.4.3"

[build-dependencies]
fs-err = "3.1.0"
Expand Down
36 changes: 30 additions & 6 deletions lazer/publisher_sdk/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::publisher_update::feed_update::Update;
use crate::publisher_update::{FeedUpdate, FundingRateUpdate, PriceUpdate};
use crate::state::FeedState;
use ::protobuf::EnumOrUnknown;
use pyth_lazer_protocol::jrpc::{FeedUpdateParams, UpdateParams};
use pyth_lazer_protocol::symbol_state::SymbolState;
use pyth_lazer_protocol::FeedKind;
Expand Down Expand Up @@ -60,12 +61,35 @@ impl From<UpdateParams> for Update {
best_ask_price: best_ask_price.map(|p| p.0.into()),
special_fields: Default::default(),
}),
UpdateParams::FundingRateUpdate { price, rate } => {
Update::FundingRateUpdate(FundingRateUpdate {
price: price.map(|p| p.0.into()),
rate: Some(rate.0),
special_fields: Default::default(),
})
UpdateParams::FundingRateUpdate {
price,
rate,
funding_rate_interval,
} => Update::FundingRateUpdate(FundingRateUpdate {
price: price.map(|p| p.0.into()),
rate: Some(rate.0),
funding_rate_interval: funding_rate_interval
.map(From::from)
.map(EnumOrUnknown::new),
special_fields: Default::default(),
}),
}
}
}

impl From<pyth_lazer_protocol::router::FundingRateInterval>
for publisher_update::funding_rate_update::FundingRateInterval
{
fn from(value: pyth_lazer_protocol::router::FundingRateInterval) -> Self {
match value {
pyth_lazer_protocol::router::FundingRateInterval::Interval1Hour => {
publisher_update::funding_rate_update::FundingRateInterval::INTERVAL_1H
}
pyth_lazer_protocol::router::FundingRateInterval::Interval4Hours => {
publisher_update::funding_rate_update::FundingRateInterval::INTERVAL_4H
}
pyth_lazer_protocol::router::FundingRateInterval::Interval8Hours => {
publisher_update::funding_rate_update::FundingRateInterval::INTERVAL_8H
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lazer/sdk/rust/protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyth-lazer-protocol"
version = "0.9.1"
version = "0.9.2"
edition = "2021"
description = "Pyth Lazer SDK - protocol types."
license = "Apache-2.0"
Expand Down
64 changes: 61 additions & 3 deletions lazer/sdk/rust/protocol/src/jrpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::router::{Channel, Price, PriceFeedId, Rate};
use crate::router::{Channel, FundingRateInterval, Price, PriceFeedId, Rate};
use crate::symbol_state::SymbolState;
use crate::time::TimestampUs;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -37,7 +37,11 @@
best_ask_price: Option<Price>,
},
#[serde(rename = "funding_rate")]
FundingRateUpdate { price: Option<Price>, rate: Rate },
FundingRateUpdate {
price: Option<Price>,
rate: Rate,
funding_rate_interval: Option<FundingRateInterval>,
},
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
Expand All @@ -59,6 +63,7 @@
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(untagged)]
pub enum JrpcResponse<T> {
Success(JrpcSuccessResponse<T>),
Error(JrpcErrorResponse),
Expand Down Expand Up @@ -226,7 +231,8 @@
"update": {
"type": "funding_rate",
"price": 1234567890,
"rate": 1234567891
"rate": 1234567891,
"funding_rate_interval": "8h"
}
},
"id": 1
Expand All @@ -241,6 +247,7 @@
update: UpdateParams::FundingRateUpdate {
price: Some(Price::from_integer(1234567890, 0).unwrap()),
rate: Rate::from_integer(1234567891, 0).unwrap(),
funding_rate_interval: Some(FundingRateInterval::Interval8Hours),
},
}),
id: 1,
Expand Down Expand Up @@ -278,6 +285,7 @@
update: UpdateParams::FundingRateUpdate {
price: None,
rate: Rate::from_integer(1234567891, 0).unwrap(),
funding_rate_interval: None,
},
}),
id: 1,
Expand Down Expand Up @@ -396,4 +404,54 @@
}
);
}

#[test]
pub fn test_parse_response() {
let success_response = serde_json::from_str::<JrpcResponse<String>>(
r#"
{
"jsonrpc": "2.0",
"id": 2,
"result": "success"
}

Check warning on line 416 in lazer/sdk/rust/protocol/src/jrpc.rs

View workflow job for this annotation

GitHub Actions / Lazer Rust Test Suite

Diff in /home/runner/work/pyth-crosschain/pyth-crosschain/lazer/sdk/rust/protocol/src/jrpc.rs

Check warning on line 416 in lazer/sdk/rust/protocol/src/jrpc.rs

View workflow job for this annotation

GitHub Actions / Lazer Solana contract test

Diff in /home/runner/work/pyth-crosschain/pyth-crosschain/lazer/sdk/rust/protocol/src/jrpc.rs
"#,
)
.unwrap();

assert_eq!(
success_response,
JrpcResponse::Success(JrpcSuccessResponse::<String> {
jsonrpc: JsonRpcVersion::V2,
result: "success".to_string(),
id: 2,
})
);

let error_response = serde_json::from_str::<JrpcResponse<String>>(
r#"
{
"jsonrpc": "2.0",
"id": 3,
"error": {
"code": -32603,
"message": "Internal error"
}
}

Check warning on line 439 in lazer/sdk/rust/protocol/src/jrpc.rs

View workflow job for this annotation

GitHub Actions / Lazer Rust Test Suite

Diff in /home/runner/work/pyth-crosschain/pyth-crosschain/lazer/sdk/rust/protocol/src/jrpc.rs

Check warning on line 439 in lazer/sdk/rust/protocol/src/jrpc.rs

View workflow job for this annotation

GitHub Actions / Lazer Solana contract test

Diff in /home/runner/work/pyth-crosschain/pyth-crosschain/lazer/sdk/rust/protocol/src/jrpc.rs
"#,
)
.unwrap();

assert_eq!(
error_response,
JrpcResponse::Error(JrpcErrorResponse {
jsonrpc: JsonRpcVersion::V2,
error: JrpcErrorObject {
code: -32603,
message: "Internal error".to_string(),
data: None,
},
id: Some(3),
})
);
}
}
10 changes: 10 additions & 0 deletions lazer/sdk/rust/protocol/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ impl Channel {
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum FundingRateInterval {
#[serde(rename = "1h")]
Interval1Hour,
#[serde(rename = "4h")]
Interval4Hours,
#[serde(rename = "8h")]
Interval8Hours,
}

#[test]
fn id_supports_all_fixed_rates() {
for rate in FixedRate::ALL {
Expand Down
Loading