Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions examples/sol-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions examples/sol-contract/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions pyth-sdk-solana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 21 additions & 21 deletions pyth-sdk-solana/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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",
}
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions pyth-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
4 changes: 2 additions & 2 deletions pyth-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl PriceFeed {
) -> Option<Price> {
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;
Expand All @@ -193,7 +193,7 @@ impl PriceFeed {
) -> Option<Price> {
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;
Expand Down
39 changes: 19 additions & 20 deletions pyth-sdk/src/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -637,15 +637,14 @@ impl Price {
// get the relevant fraction
let frac = x_as_price.div(&y_as_price)?;

return Some(frac);
Some(frac)
}
}

#[cfg(test)]
mod test {
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
use std::convert::TryFrom;

use crate::price::{
Price,
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading