Skip to content

Commit d5fca4e

Browse files
rodoufuali-bahjati
andauthored
Adding workspace dependencies and implementing Display for PriceStatus (#128)
* Using workspace dependencies * Implementing Display for PriceStatus * Fix formatting * Code review * fix: update rust version for tests to work * chore: bump solana sdk package --------- Co-authored-by: Ali Behjati <[email protected]>
1 parent 2b5c4ee commit d5fca4e

File tree

10 files changed

+72
-63
lines changed

10 files changed

+72
-63
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ members = [
66
"pyth-sdk-solana/test-contract",
77
"examples/sol-contract",
88
]
9+
10+
[workspace.dependencies]
11+
pyth-sdk = { path = "./pyth-sdk" }
12+
pyth-sdk-solana = { path = "./pyth-sdk-solana" }
13+
14+
solana-program = ">= 1.10"
15+
borsh = "0.10.3"
16+
borsh-derive = "0.10.3"
17+
serde = "1.0.136"

examples/sol-contract/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2021"
99
crate-type = ["cdylib", "lib"]
1010

1111
[dependencies]
12-
borsh = "0.10.3"
1312
arrayref = "0.3.6"
14-
solana-program = ">= 1.10"
15-
pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.10.2" }
13+
borsh.workspace = true
14+
solana-program.workspace = true
15+
pyth-sdk-solana.workspace = true

examples/sol-contract/src/processor.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ pub fn process_instruction(
128128
// If the loan and collateral prices use different exponent,
129129
// normalize the value.
130130
if result1.expo > result2.expo {
131-
let normalize = (10 as i64)
131+
let normalize = 10_i64
132132
.checked_pow((result1.expo - result2.expo) as u32)
133133
.ok_or(ProgramError::Custom(4))?;
134134
collateral_min_value = collateral_min_value
135135
.checked_mul(normalize)
136136
.ok_or(ProgramError::Custom(4))?;
137137
} else if result1.expo < result2.expo {
138-
let normalize = (10 as i64)
138+
let normalize = 10_i64
139139
.checked_pow((result2.expo - result1.expo) as u32)
140140
.ok_or(ProgramError::Custom(4))?;
141141
loan_max_value = loan_max_value
@@ -146,10 +146,10 @@ pub fn process_instruction(
146146
// Check whether the value of the collateral is higher.
147147
if collateral_min_value > loan_max_value {
148148
msg!("The value of the collateral is higher.");
149-
return Ok(());
149+
Ok(())
150150
} else {
151151
msg!("The value of the loan is higher!");
152-
return Err(ProgramError::Custom(5));
152+
Err(ProgramError::Custom(5))
153153
}
154154
}
155155
}

pyth-sdk-solana/Cargo.toml

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
[package]
22
name = "pyth-sdk-solana"
3-
version = "0.10.3"
3+
version = "0.10.4"
44
authors = ["Pyth Data Foundation"]
55
workspace = "../"
66
edition = "2018"
77
license = "Apache-2.0"
88
homepage = "https://pyth.network"
99
repository = "https://github.com/pyth-network/pyth-sdk-rs"
1010
description = "pyth price oracle data structures and example usage"
11-
keywords = [ "pyth", "solana", "oracle" ]
11+
keywords = ["pyth", "solana", "oracle"]
1212
readme = "README.md"
1313

1414
[dependencies]
15-
solana-program = ">= 1.9"
16-
borsh = "0.10.3"
17-
borsh-derive = "0.10.3"
18-
bytemuck = {version ="1.7.2", features = ["derive"]}
15+
pyth-sdk.workspace = true
16+
17+
solana-program.workspace = true
18+
borsh.workspace = true
19+
borsh-derive.workspace = true
20+
bytemuck = { version = "1.7.2", features = ["derive"] }
1921
num-derive = "0.3"
2022
num-traits = "0.2"
2123
thiserror = "1.0"
22-
serde = { version = "1.0.136", features = ["derive"] }
23-
pyth-sdk = { path = "../pyth-sdk", version = "0.8.0" }
24+
serde = { workspace = true, features = ["derive"] }
2425

2526
[dev-dependencies]
2627
solana-client = ">= 1.9"

pyth-sdk-solana/src/state.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,17 @@ pub const PROD_ATTR_SIZE: usize = PROD_ACCT_SIZE - PROD_HDR_SIZE;
4646
BorshDeserialize,
4747
serde::Serialize,
4848
serde::Deserialize,
49+
Default,
4950
)]
5051
#[repr(u8)]
5152
pub enum AccountType {
53+
#[default]
5254
Unknown,
5355
Mapping,
5456
Product,
5557
Price,
5658
}
5759

58-
impl Default for AccountType {
59-
fn default() -> Self {
60-
AccountType::Unknown
61-
}
62-
}
63-
6460
/// Status of any ongoing corporate actions.
6561
/// (still undergoing dev)
6662
#[derive(
@@ -73,18 +69,14 @@ impl Default for AccountType {
7369
BorshDeserialize,
7470
serde::Serialize,
7571
serde::Deserialize,
72+
Default,
7673
)]
7774
#[repr(u8)]
7875
pub enum CorpAction {
76+
#[default]
7977
NoCorpAct,
8078
}
8179

82-
impl Default for CorpAction {
83-
fn default() -> Self {
84-
CorpAction::NoCorpAct
85-
}
86-
}
87-
8880
/// The type of prices associated with a product -- each product may have multiple price feeds of
8981
/// different types.
9082
#[derive(
@@ -97,19 +89,15 @@ impl Default for CorpAction {
9789
BorshDeserialize,
9890
serde::Serialize,
9991
serde::Deserialize,
92+
Default,
10093
)]
10194
#[repr(u8)]
10295
pub enum PriceType {
96+
#[default]
10397
Unknown,
10498
Price,
10599
}
106100

107-
impl Default for PriceType {
108-
fn default() -> Self {
109-
PriceType::Unknown
110-
}
111-
}
112-
113101
/// Represents availability status of a price feed.
114102
#[derive(
115103
Copy,
@@ -121,10 +109,12 @@ impl Default for PriceType {
121109
BorshDeserialize,
122110
serde::Serialize,
123111
serde::Deserialize,
112+
Default,
124113
)]
125114
#[repr(u8)]
126115
pub enum PriceStatus {
127116
/// The price feed is not currently updating for an unknown reason.
117+
#[default]
128118
Unknown,
129119
/// The price feed is updating as expected.
130120
Trading,
@@ -136,9 +126,19 @@ pub enum PriceStatus {
136126
Ignored,
137127
}
138128

139-
impl Default for PriceStatus {
140-
fn default() -> Self {
141-
PriceStatus::Unknown
129+
impl std::fmt::Display for PriceStatus {
130+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131+
write!(
132+
f,
133+
"{}",
134+
match self {
135+
Self::Unknown => "unknown",
136+
Self::Trading => "trading",
137+
Self::Halted => "halted",
138+
Self::Auction => "auction",
139+
Self::Ignored => "ignored",
140+
}
141+
)
142142
}
143143
}
144144

pyth-sdk/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ crate-type = ["cdylib", "lib"]
1616

1717
[dependencies]
1818
hex = { version = "0.4.3", features = ["serde"] }
19-
borsh = "0.10.3"
20-
borsh-derive = "0.10.3"
21-
serde = { version = "1.0.136", features = ["derive"] }
19+
borsh.workspace = true
20+
borsh-derive.workspace = true
21+
serde = { workspace = true, features = ["derive"] }
2222
schemars = "0.8.8"
2323
getrandom = { version = "0.2.2", features = ["custom"] }
2424

pyth-sdk/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl PriceFeed {
167167
) -> Option<Price> {
168168
let price = self.get_price_unchecked();
169169

170-
let time_diff_abs = (price.publish_time - current_time).abs() as u64;
170+
let time_diff_abs = (price.publish_time - current_time).unsigned_abs();
171171

172172
if time_diff_abs > age {
173173
return None;
@@ -193,7 +193,7 @@ impl PriceFeed {
193193
) -> Option<Price> {
194194
let price = self.get_ema_price_unchecked();
195195

196-
let time_diff_abs = (price.publish_time - current_time).abs() as u64;
196+
let time_diff_abs = (price.publish_time - current_time).unsigned_abs();
197197

198198
if time_diff_abs > age {
199199
return None;

pyth-sdk/src/price.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ impl Price {
163163
.mul(&discount_interpolated)?
164164
.scale_to_exponent(expo_orig)?;
165165

166-
return Some(Price {
166+
Some(Price {
167167
price: price_discounted.price,
168168
conf: conf_orig,
169169
expo: price_discounted.expo,
170170
publish_time: self.publish_time,
171-
});
171+
})
172172
}
173173

174174
/// Get the valuation of a borrow position according to:
@@ -243,12 +243,12 @@ impl Price {
243243
.mul(&premium_interpolated)?
244244
.scale_to_exponent(expo_orig)?;
245245

246-
return Some(Price {
246+
Some(Price {
247247
price: price_premium.price,
248248
conf: conf_orig,
249249
expo: price_premium.expo,
250250
publish_time: self.publish_time,
251-
});
251+
})
252252
}
253253

254254
/// affine_combination performs an affine combination of two prices located at x coordinates x1
@@ -345,7 +345,7 @@ impl Price {
345345
right = right.scale_to_exponent(pre_add_expo)?;
346346

347347
// 8. compute H = F + G, Err(H) ~= 4x + 2*10^pre_add_expo
348-
return left.add(&right);
348+
left.add(&right)
349349
}
350350

351351
/// Get the price of a basket of currencies.
@@ -637,15 +637,14 @@ impl Price {
637637
// get the relevant fraction
638638
let frac = x_as_price.div(&y_as_price)?;
639639

640-
return Some(frac);
640+
Some(frac)
641641
}
642642
}
643643

644644
#[cfg(test)]
645645
mod test {
646646
use quickcheck::TestResult;
647647
use quickcheck_macros::quickcheck;
648-
use std::convert::TryFrom;
649648

650649
use crate::price::{
651650
Price,
@@ -2053,12 +2052,12 @@ mod test {
20532052
}
20542053

20552054
pub fn construct_quickcheck_affine_combination_price(price: i64) -> Price {
2056-
return Price {
2055+
Price {
20572056
price: price,
20582057
conf: 0,
20592058
expo: -9,
20602059
publish_time: 0,
2061-
};
2060+
}
20622061
}
20632062

20642063
// quickcheck to confirm affine_combination introduces no error if normalization done
@@ -2078,12 +2077,12 @@ mod test {
20782077
) -> TestResult {
20792078
// generating xs and prices from i32 to limit the range to reasonable values and guard
20802079
// against overflow/bespoke constraint setting for quickcheck
2081-
let y1 = construct_quickcheck_affine_combination_price(i64::try_from(p1).ok().unwrap());
2082-
let y2 = construct_quickcheck_affine_combination_price(i64::try_from(p2).ok().unwrap());
2080+
let y1 = construct_quickcheck_affine_combination_price(i64::from(p1));
2081+
let y2 = construct_quickcheck_affine_combination_price(i64::from(p2));
20832082

2084-
let x1 = i64::try_from(x1_inp).ok().unwrap();
2085-
let x2 = i64::try_from(x2_inp).ok().unwrap();
2086-
let x_query = i64::try_from(x_query_inp).ok().unwrap();
2083+
let x1 = i64::from(x1_inp);
2084+
let x2 = i64::from(x2_inp);
2085+
let x_query = i64::from(x_query_inp);
20872086

20882087
// stick with single expo for ease of testing and generation
20892088
let pre_add_expo = -9;
@@ -2124,12 +2123,12 @@ mod test {
21242123
) -> TestResult {
21252124
// generating xs and prices from i32 to limit the range to reasonable values and guard
21262125
// against overflow/bespoke constraint setting for quickcheck
2127-
let y1 = construct_quickcheck_affine_combination_price(i64::try_from(p1).ok().unwrap());
2128-
let y2 = construct_quickcheck_affine_combination_price(i64::try_from(p2).ok().unwrap());
2126+
let y1 = construct_quickcheck_affine_combination_price(i64::from(p1));
2127+
let y2 = construct_quickcheck_affine_combination_price(i64::from(p2));
21292128

2130-
let x1 = i64::try_from(x1_inp).ok().unwrap();
2131-
let x2 = i64::try_from(x2_inp).ok().unwrap();
2132-
let x_query = i64::try_from(x_query_inp).ok().unwrap();
2129+
let x1 = i64::from(x1_inp);
2130+
let x2 = i64::from(x2_inp);
2131+
let x_query = i64::from(x_query_inp);
21332132

21342133
// stick with single expo for ease of testing and generation
21352134
let pre_add_expo = -9;
@@ -2159,7 +2158,7 @@ mod test {
21592158

21602159
x1_new = 0;
21612160
xq_new = frac_q2.price;
2162-
x2_new = 100_000_000 as i64;
2161+
x2_new = 100_000_000;
21632162
}
21642163

21652164
// original result

rust-toolchain.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[toolchain]
2-
channel = "1.75.0"
2+
channel = "1.76.0"

0 commit comments

Comments
 (0)