From 9038addba80802890f8908ace9216a8e14fb9448 Mon Sep 17 00:00:00 2001 From: Rodolfo Date: Fri, 28 Feb 2025 09:37:00 -0300 Subject: [PATCH 1/6] Using workspace dependencies --- Cargo.toml | 9 +++++++++ examples/sol-contract/Cargo.toml | 6 +++--- pyth-sdk-solana/Cargo.toml | 13 +++++++------ pyth-sdk/Cargo.toml | 6 +++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a988e2e..8ceb6ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,12 @@ members = [ "pyth-sdk-solana/test-contract", "examples/sol-contract", ] + +[workspace.dependencies] +pyth-sdk = { path = "./pyth-sdk",version = "0.8.0" } +pyth-sdk-solana = { path = "./pyth-sdk-solana", version = "0.10.2" } + +solana-program = ">= 1.10" +borsh = "0.10.3" +borsh-derive = "0.10.3" +serde = "1.0.136" diff --git a/examples/sol-contract/Cargo.toml b/examples/sol-contract/Cargo.toml index a17c66f..1572699 100644 --- a/examples/sol-contract/Cargo.toml +++ b/examples/sol-contract/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" crate-type = ["cdylib", "lib"] [dependencies] -borsh = "0.10.3" arrayref = "0.3.6" -solana-program = ">= 1.10" -pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.10.2" } +borsh.workspace = true +solana-program.workspace = true +pyth-sdk-solana.workspace = true diff --git a/pyth-sdk-solana/Cargo.toml b/pyth-sdk-solana/Cargo.toml index a2989c3..7be3c1b 100644 --- a/pyth-sdk-solana/Cargo.toml +++ b/pyth-sdk-solana/Cargo.toml @@ -12,15 +12,16 @@ keywords = [ "pyth", "solana", "oracle" ] readme = "README.md" [dependencies] -solana-program = ">= 1.9" -borsh = "0.10.3" -borsh-derive = "0.10.3" -bytemuck = {version ="1.7.2", features = ["derive"]} +pyth-sdk.workspace = true + +solana-program.workspace = true +borsh.workspace = true +borsh-derive.workspace = true +bytemuck = { version ="1.7.2", features = ["derive"] } num-derive = "0.3" num-traits = "0.2" thiserror = "1.0" -serde = { version = "1.0.136", features = ["derive"] } -pyth-sdk = { path = "../pyth-sdk", version = "0.8.0" } +serde = { workspace=true, features = ["derive"] } [dev-dependencies] solana-client = ">= 1.9" diff --git a/pyth-sdk/Cargo.toml b/pyth-sdk/Cargo.toml index 3814f50..c779502 100644 --- a/pyth-sdk/Cargo.toml +++ b/pyth-sdk/Cargo.toml @@ -16,9 +16,9 @@ crate-type = ["cdylib", "lib"] [dependencies] hex = { version = "0.4.3", features = ["serde"] } -borsh = "0.10.3" -borsh-derive = "0.10.3" -serde = { version = "1.0.136", features = ["derive"] } +borsh.workspace = true +borsh-derive.workspace = true +serde = { worksapce = true, features = ["derive"] } schemars = "0.8.8" getrandom = { version = "0.2.2", features = ["custom"] } From d2134d96e0c37d6d026cce89462bbc48bbbc7bb8 Mon Sep 17 00:00:00 2001 From: Rodolfo Date: Fri, 28 Feb 2025 09:37:59 -0300 Subject: [PATCH 2/6] Implementing Display for PriceStatus --- examples/sol-contract/src/processor.rs | 8 +++--- pyth-sdk-solana/src/state.rs | 38 +++++++++++-------------- pyth-sdk/src/lib.rs | 4 +-- pyth-sdk/src/price.rs | 39 +++++++++++++------------- 4 files changed, 42 insertions(+), 47 deletions(-) diff --git a/examples/sol-contract/src/processor.rs b/examples/sol-contract/src/processor.rs index e5d8cd3..067b658 100644 --- a/examples/sol-contract/src/processor.rs +++ b/examples/sol-contract/src/processor.rs @@ -128,14 +128,14 @@ pub fn process_instruction( // If the loan and collateral prices use different exponent, // normalize the value. if result1.expo > result2.expo { - let normalize = (10 as i64) + let normalize = 10_i64 .checked_pow((result1.expo - result2.expo) as u32) .ok_or(ProgramError::Custom(4))?; collateral_min_value = collateral_min_value .checked_mul(normalize) .ok_or(ProgramError::Custom(4))?; } else if result1.expo < result2.expo { - let normalize = (10 as i64) + let normalize = 10_i64 .checked_pow((result2.expo - result1.expo) as u32) .ok_or(ProgramError::Custom(4))?; loan_max_value = loan_max_value @@ -146,10 +146,10 @@ pub fn process_instruction( // Check whether the value of the collateral is higher. if collateral_min_value > loan_max_value { msg!("The value of the collateral is higher."); - return Ok(()); + Ok(()) } else { msg!("The value of the loan is higher!"); - return Err(ProgramError::Custom(5)); + Err(ProgramError::Custom(5)) } } } diff --git a/pyth-sdk-solana/src/state.rs b/pyth-sdk-solana/src/state.rs index c4fab5d..a853f02 100644 --- a/pyth-sdk-solana/src/state.rs +++ b/pyth-sdk-solana/src/state.rs @@ -46,21 +46,17 @@ pub const PROD_ATTR_SIZE: usize = PROD_ACCT_SIZE - PROD_HDR_SIZE; BorshDeserialize, serde::Serialize, serde::Deserialize, + Default, )] #[repr(u8)] pub enum AccountType { + #[default] Unknown, Mapping, Product, Price, } -impl Default for AccountType { - fn default() -> Self { - AccountType::Unknown - } -} - /// Status of any ongoing corporate actions. /// (still undergoing dev) #[derive( @@ -73,18 +69,14 @@ impl Default for AccountType { BorshDeserialize, serde::Serialize, serde::Deserialize, + Default, )] #[repr(u8)] pub enum CorpAction { + #[default] NoCorpAct, } -impl Default for CorpAction { - fn default() -> Self { - CorpAction::NoCorpAct - } -} - /// The type of prices associated with a product -- each product may have multiple price feeds of /// different types. #[derive( @@ -97,19 +89,15 @@ impl Default for CorpAction { BorshDeserialize, serde::Serialize, serde::Deserialize, + Default, )] #[repr(u8)] pub enum PriceType { + #[default] Unknown, Price, } -impl Default for PriceType { - fn default() -> Self { - PriceType::Unknown - } -} - /// Represents availability status of a price feed. #[derive( Copy, @@ -121,10 +109,12 @@ impl Default for PriceType { BorshDeserialize, serde::Serialize, serde::Deserialize, + Default, )] #[repr(u8)] pub enum PriceStatus { /// The price feed is not currently updating for an unknown reason. + #[default] Unknown, /// The price feed is updating as expected. Trading, @@ -136,9 +126,15 @@ pub enum PriceStatus { Ignored, } -impl Default for PriceStatus { - fn default() -> Self { - PriceStatus::Unknown +impl std::fmt::Display for PriceStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Unknown =>"unknown", + Self::Trading =>"trading", + Self::Halted => "halted", + Self::Auction =>"auction", + Self::Ignored =>"ignored", + }) } } diff --git a/pyth-sdk/src/lib.rs b/pyth-sdk/src/lib.rs index c22d29f..000ec81 100644 --- a/pyth-sdk/src/lib.rs +++ b/pyth-sdk/src/lib.rs @@ -167,7 +167,7 @@ impl PriceFeed { ) -> Option { let price = self.get_price_unchecked(); - let time_diff_abs = (price.publish_time - current_time).abs() as u64; + let time_diff_abs = (price.publish_time - current_time).unsigned_abs(); if time_diff_abs > age { return None; @@ -193,7 +193,7 @@ impl PriceFeed { ) -> Option { let price = self.get_ema_price_unchecked(); - let time_diff_abs = (price.publish_time - current_time).abs() as u64; + let time_diff_abs = (price.publish_time - current_time).unsigned_abs(); if time_diff_abs > age { return None; diff --git a/pyth-sdk/src/price.rs b/pyth-sdk/src/price.rs index 1dc411f..ba304b6 100644 --- a/pyth-sdk/src/price.rs +++ b/pyth-sdk/src/price.rs @@ -163,12 +163,12 @@ impl Price { .mul(&discount_interpolated)? .scale_to_exponent(expo_orig)?; - return Some(Price { + Some(Price { price: price_discounted.price, conf: conf_orig, expo: price_discounted.expo, publish_time: self.publish_time, - }); + }) } /// Get the valuation of a borrow position according to: @@ -243,12 +243,12 @@ impl Price { .mul(&premium_interpolated)? .scale_to_exponent(expo_orig)?; - return Some(Price { + Some(Price { price: price_premium.price, conf: conf_orig, expo: price_premium.expo, publish_time: self.publish_time, - }); + }) } /// affine_combination performs an affine combination of two prices located at x coordinates x1 @@ -345,7 +345,7 @@ impl Price { right = right.scale_to_exponent(pre_add_expo)?; // 8. compute H = F + G, Err(H) ~= 4x + 2*10^pre_add_expo - return left.add(&right); + left.add(&right) } /// Get the price of a basket of currencies. @@ -637,7 +637,7 @@ impl Price { // get the relevant fraction let frac = x_as_price.div(&y_as_price)?; - return Some(frac); + Some(frac) } } @@ -645,7 +645,6 @@ impl Price { mod test { use quickcheck::TestResult; use quickcheck_macros::quickcheck; - use std::convert::TryFrom; use crate::price::{ Price, @@ -2053,12 +2052,12 @@ mod test { } pub fn construct_quickcheck_affine_combination_price(price: i64) -> Price { - return Price { + Price { price: price, conf: 0, expo: -9, publish_time: 0, - }; + } } // quickcheck to confirm affine_combination introduces no error if normalization done @@ -2078,12 +2077,12 @@ mod test { ) -> TestResult { // generating xs and prices from i32 to limit the range to reasonable values and guard // against overflow/bespoke constraint setting for quickcheck - let y1 = construct_quickcheck_affine_combination_price(i64::try_from(p1).ok().unwrap()); - let y2 = construct_quickcheck_affine_combination_price(i64::try_from(p2).ok().unwrap()); + let y1 = construct_quickcheck_affine_combination_price(i64::from(p1)); + let y2 = construct_quickcheck_affine_combination_price(i64::from(p2)); - let x1 = i64::try_from(x1_inp).ok().unwrap(); - let x2 = i64::try_from(x2_inp).ok().unwrap(); - let x_query = i64::try_from(x_query_inp).ok().unwrap(); + let x1 = i64::from(x1_inp); + let x2 = i64::from(x2_inp); + let x_query = i64::from(x_query_inp); // stick with single expo for ease of testing and generation let pre_add_expo = -9; @@ -2124,12 +2123,12 @@ mod test { ) -> TestResult { // generating xs and prices from i32 to limit the range to reasonable values and guard // against overflow/bespoke constraint setting for quickcheck - let y1 = construct_quickcheck_affine_combination_price(i64::try_from(p1).ok().unwrap()); - let y2 = construct_quickcheck_affine_combination_price(i64::try_from(p2).ok().unwrap()); + let y1 = construct_quickcheck_affine_combination_price(i64::from(p1)); + let y2 = construct_quickcheck_affine_combination_price(i64::from(p2)); - let x1 = i64::try_from(x1_inp).ok().unwrap(); - let x2 = i64::try_from(x2_inp).ok().unwrap(); - let x_query = i64::try_from(x_query_inp).ok().unwrap(); + let x1 = i64::from(x1_inp); + let x2 = i64::from(x2_inp); + let x_query = i64::from(x_query_inp); // stick with single expo for ease of testing and generation let pre_add_expo = -9; @@ -2159,7 +2158,7 @@ mod test { x1_new = 0; xq_new = frac_q2.price; - x2_new = 100_000_000 as i64; + x2_new = 100_000_000; } // original result From 9a70bbdeea8464c77377755c75065954ef89fa35 Mon Sep 17 00:00:00 2001 From: Rodolfo Date: Wed, 5 Mar 2025 09:47:16 -0300 Subject: [PATCH 3/6] Fix formatting --- examples/sol-contract/Cargo.toml | 2 +- pyth-sdk-solana/src/state.rs | 18 +++++++++++------- pyth-sdk/Cargo.toml | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/sol-contract/Cargo.toml b/examples/sol-contract/Cargo.toml index 1572699..5f0e99e 100644 --- a/examples/sol-contract/Cargo.toml +++ b/examples/sol-contract/Cargo.toml @@ -12,4 +12,4 @@ crate-type = ["cdylib", "lib"] arrayref = "0.3.6" borsh.workspace = true solana-program.workspace = true -pyth-sdk-solana.workspace = true +pyth-sdk-solana.workspace = true diff --git a/pyth-sdk-solana/src/state.rs b/pyth-sdk-solana/src/state.rs index a853f02..8fcd787 100644 --- a/pyth-sdk-solana/src/state.rs +++ b/pyth-sdk-solana/src/state.rs @@ -128,13 +128,17 @@ pub enum PriceStatus { impl std::fmt::Display for PriceStatus { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", match self { - Self::Unknown =>"unknown", - Self::Trading =>"trading", - Self::Halted => "halted", - Self::Auction =>"auction", - Self::Ignored =>"ignored", - }) + write!( + f, + "{}", + match self { + Self::Unknown => "unknown", + Self::Trading => "trading", + Self::Halted => "halted", + Self::Auction => "auction", + Self::Ignored => "ignored", + } + ) } } diff --git a/pyth-sdk/Cargo.toml b/pyth-sdk/Cargo.toml index c779502..75dc47f 100644 --- a/pyth-sdk/Cargo.toml +++ b/pyth-sdk/Cargo.toml @@ -18,7 +18,7 @@ crate-type = ["cdylib", "lib"] hex = { version = "0.4.3", features = ["serde"] } borsh.workspace = true borsh-derive.workspace = true -serde = { worksapce = true, features = ["derive"] } +serde = { workspace = true, features = ["derive"] } schemars = "0.8.8" getrandom = { version = "0.2.2", features = ["custom"] } From 6e5629524c1e3d2204788b3708f6dbd3b2016588 Mon Sep 17 00:00:00 2001 From: Rodolfo Date: Wed, 12 Mar 2025 15:10:48 -0300 Subject: [PATCH 4/6] Code review --- Cargo.toml | 6 +++--- pyth-sdk-solana/Cargo.toml | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ceb6ac..4fd4283 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,10 @@ members = [ ] [workspace.dependencies] -pyth-sdk = { path = "./pyth-sdk",version = "0.8.0" } -pyth-sdk-solana = { path = "./pyth-sdk-solana", version = "0.10.2" } +pyth-sdk = { path = "./pyth-sdk", version = "0.8.0" } +pyth-sdk-solana = { path = "./pyth-sdk-solana", version = "0.10.3" } solana-program = ">= 1.10" borsh = "0.10.3" borsh-derive = "0.10.3" -serde = "1.0.136" +serde = "1.0.136" diff --git a/pyth-sdk-solana/Cargo.toml b/pyth-sdk-solana/Cargo.toml index 7be3c1b..1606b88 100644 --- a/pyth-sdk-solana/Cargo.toml +++ b/pyth-sdk-solana/Cargo.toml @@ -8,7 +8,7 @@ license = "Apache-2.0" homepage = "https://pyth.network" repository = "https://github.com/pyth-network/pyth-sdk-rs" description = "pyth price oracle data structures and example usage" -keywords = [ "pyth", "solana", "oracle" ] +keywords = ["pyth", "solana", "oracle"] readme = "README.md" [dependencies] @@ -17,11 +17,11 @@ pyth-sdk.workspace = true solana-program.workspace = true borsh.workspace = true borsh-derive.workspace = true -bytemuck = { version ="1.7.2", features = ["derive"] } +bytemuck = { version = "1.7.2", features = ["derive"] } num-derive = "0.3" num-traits = "0.2" thiserror = "1.0" -serde = { workspace=true, features = ["derive"] } +serde = { workspace = true, features = ["derive"] } [dev-dependencies] solana-client = ">= 1.9" From 0b685c52fed8309c1e7d60cf2a51397e6c6e9287 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 13 Mar 2025 12:44:54 +0100 Subject: [PATCH 5/6] fix: update rust version for tests to work --- rust-toolchain.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 7897a24..624eb0e 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "1.75.0" +channel = "1.76.0" From 73fd8e6d1493f4ec24fea8ddf2db2d9baa4fa7d1 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 13 Mar 2025 12:45:20 +0100 Subject: [PATCH 6/6] chore: bump solana sdk package --- Cargo.lock | 2 +- Cargo.toml | 4 ++-- pyth-sdk-solana/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81313e7..90479d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2754,7 +2754,7 @@ dependencies = [ [[package]] name = "pyth-sdk-solana" -version = "0.10.3" +version = "0.10.4" dependencies = [ "borsh 0.10.3", "borsh-derive 0.10.3", diff --git a/Cargo.toml b/Cargo.toml index 4fd4283..d14541d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,8 @@ members = [ ] [workspace.dependencies] -pyth-sdk = { path = "./pyth-sdk", version = "0.8.0" } -pyth-sdk-solana = { path = "./pyth-sdk-solana", version = "0.10.3" } +pyth-sdk = { path = "./pyth-sdk" } +pyth-sdk-solana = { path = "./pyth-sdk-solana" } solana-program = ">= 1.10" borsh = "0.10.3" diff --git a/pyth-sdk-solana/Cargo.toml b/pyth-sdk-solana/Cargo.toml index 1606b88..4489878 100644 --- a/pyth-sdk-solana/Cargo.toml +++ b/pyth-sdk-solana/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pyth-sdk-solana" -version = "0.10.3" +version = "0.10.4" authors = ["Pyth Data Foundation"] workspace = "../" edition = "2018"