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 a988e2e..d14541d 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" } +pyth-sdk-solana = { path = "./pyth-sdk-solana" } + +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..5f0e99e 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/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/Cargo.toml b/pyth-sdk-solana/Cargo.toml index a2989c3..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" @@ -8,19 +8,20 @@ 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] -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-solana/src/state.rs b/pyth-sdk-solana/src/state.rs index c4fab5d..8fcd787 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,19 @@ 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/Cargo.toml b/pyth-sdk/Cargo.toml index 3814f50..75dc47f 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 = { workspace = true, features = ["derive"] } schemars = "0.8.8" getrandom = { version = "0.2.2", features = ["custom"] } 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 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"