Skip to content

Commit 0dc9e31

Browse files
committed
feat!: allow loading network-specific PriceAccount
1 parent 1acfc3d commit 0dc9e31

File tree

14 files changed

+298
-45
lines changed

14 files changed

+298
-45
lines changed

.github/workflows/pyth-sdk-example-anchor-contract.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Install solana binaries
2323
run: |
2424
# Installing 1.16.x cli tools to have sbf instead of bpf. bpf does not work anymore.
25-
sh -c "$(curl -sSfL https://release.solana.com/v1.17.0/install)"
25+
sh -c "$(curl -sSfL https://release.solana.com/v1.18.1/install)"
2626
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
2727
- name: Install anchor binaries
2828
run: |

.github/workflows/pyth-sdk-example-solana-contract.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Install solana binaries
2323
run: |
2424
# Installing 1.16.x cli tools to have sbf instead of bpf. bpf does not work anymore.
25-
sh -c "$(curl -sSfL https://release.solana.com/v1.17.0/install)"
25+
sh -c "$(curl -sSfL https://release.solana.com/v1.18.1/install)"
2626
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
2727
- name: Build
2828
run: scripts/build.sh

.github/workflows/pyth-sdk-solana.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- name: Install Solana Binaries
3636
run: |
3737
# Installing 1.17.x cli tools to have sbf instead of bpf. bpf does not work anymore.
38-
sh -c "$(curl -sSfL https://release.solana.com/v1.17.0/install)"
38+
sh -c "$(curl -sSfL https://release.solana.com/v1.18.1/install)"
3939
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
4040
- name: Build
4141
run: cargo build --verbose

examples/sol-anchor-contract/programs/sol-anchor-contract/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ default = []
1919
[dependencies]
2020
anchor-lang = "0.28.0"
2121
pyth-sdk = { path = "../../../../pyth-sdk", version = "0.8.0" }
22-
pyth-sdk-solana = { path = "../../../../pyth-sdk-solana", version = "0.9.0" }
22+
pyth-sdk-solana = { path = "../../../../pyth-sdk-solana", version = "0.10.0" }

examples/sol-anchor-contract/programs/sol-anchor-contract/src/state.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anchor_lang::prelude::*;
22
use pyth_sdk_solana::state::load_price_account;
3+
use pyth_sdk_solana::state::SolanaPriceAccount;
34
use std::ops::Deref;
45
use std::str::FromStr;
56

@@ -25,7 +26,8 @@ impl anchor_lang::Owner for PriceFeed {
2526

2627
impl anchor_lang::AccountDeserialize for PriceFeed {
2728
fn try_deserialize_unchecked(data: &mut &[u8]) -> Result<Self> {
28-
let account = load_price_account(data).map_err(|_x| error!(ErrorCode::PythError))?;
29+
let account: &SolanaPriceAccount =
30+
load_price_account(data).map_err(|_x| error!(ErrorCode::PythError))?;
2931

3032
// Use a dummy key since the key field will be removed from the SDK
3133
let zeros: [u8; 32] = [0; 32];

examples/sol-contract/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ crate-type = ["cdylib", "lib"]
1111
borsh = "0.10.3"
1212
arrayref = "0.3.6"
1313
solana-program = ">= 1.10"
14-
pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.9.0" }
14+
pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.10.0" }

examples/sol-contract/src/processor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use borsh::{
1818
BorshDeserialize,
1919
BorshSerialize,
2020
};
21-
use pyth_sdk_solana::load_price_feed_from_account_info;
21+
use pyth_sdk_solana::state::SolanaPriceAccount;
2222

2323
use crate::instruction::ExampleInstructions;
2424
use crate::state::AdminConfig;
@@ -53,8 +53,8 @@ pub fn process_instruction(
5353
config.collateral_price_feed_id = *pyth_collateral_account.key;
5454

5555
// Make sure these Pyth price accounts can be loaded
56-
load_price_feed_from_account_info(pyth_loan_account)?;
57-
load_price_feed_from_account_info(pyth_collateral_account)?;
56+
SolanaPriceAccount::account_info_to_feed(pyth_loan_account)?;
57+
SolanaPriceAccount::account_info_to_feed(pyth_collateral_account)?;
5858

5959
let config_data = config.try_to_vec()?;
6060
let config_dst = &mut admin_config_account.try_borrow_mut_data()?;
@@ -85,7 +85,7 @@ pub fn process_instruction(
8585
// (price + conf) * loan_qty * 10 ^ (expo).
8686
// Here is more explanation on confidence interval in Pyth:
8787
// https://docs.pyth.network/consume-data/best-practices
88-
let feed1 = load_price_feed_from_account_info(pyth_loan_account)?;
88+
let feed1 = SolanaPriceAccount::account_info_to_feed(pyth_loan_account)?;
8989
let current_timestamp1 = Clock::get()?.unix_timestamp;
9090
let result1 = feed1
9191
.get_price_no_older_than(current_timestamp1, 60)
@@ -107,7 +107,7 @@ pub fn process_instruction(
107107
// (price - conf) * collateral_qty * 10 ^ (expo).
108108
// Here is more explanation on confidence interval in Pyth:
109109
// https://docs.pyth.network/consume-data/best-practices
110-
let feed2 = load_price_feed_from_account_info(pyth_collateral_account)?;
110+
let feed2 = SolanaPriceAccount::account_info_to_feed(pyth_collateral_account)?;
111111
let current_timestamp2 = Clock::get()?.unix_timestamp;
112112
let result2 = feed2
113113
.get_price_no_older_than(current_timestamp2, 60)

pyth-sdk-solana/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-sdk-solana"
3-
version = "0.9.0"
3+
version = "0.10.0"
44
authors = ["Pyth Data Foundation"]
55
edition = "2018"
66
license = "Apache-2.0"

pyth-sdk-solana/examples/eth_price.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// example usage of reading pyth price from solana/pythnet price account
22

3-
use pyth_sdk_solana::load_price_feed_from_account;
3+
use pyth_sdk_solana::state::SolanaPriceAccount;
44
use solana_client::rpc_client::RpcClient;
55
use solana_program::pubkey::Pubkey;
66
use std::str::FromStr;
@@ -25,7 +25,7 @@ fn main() {
2525
// get price data from key
2626
let mut eth_price_account = clnt.get_account(&eth_price_key).unwrap();
2727
let eth_price_feed =
28-
load_price_feed_from_account(&eth_price_key, &mut eth_price_account).unwrap();
28+
SolanaPriceAccount::account_to_feed(&eth_price_key, &mut eth_price_account).unwrap();
2929

3030
println!(".....ETH/USD.....");
3131

pyth-sdk-solana/examples/get_accounts.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use pyth_sdk_solana::state::{
99
load_product_account,
1010
CorpAction,
1111
PriceType,
12+
SolanaPriceAccount,
1213
};
1314
use solana_client::rpc_client::RpcClient;
1415
use solana_program::pubkey::Pubkey;
@@ -62,7 +63,8 @@ fn main() {
6263
let mut px_pkey = prod_acct.px_acc;
6364
loop {
6465
let price_data = clnt.get_account_data(&px_pkey).unwrap();
65-
let price_account = load_price_account(&price_data).unwrap();
66+
let price_account: &SolanaPriceAccount =
67+
load_price_account(&price_data).unwrap();
6668
let price_feed = price_account.to_price_feed(&px_pkey);
6769

6870
println!(" price_account .. {:?}", px_pkey);

pyth-sdk-solana/src/lib.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ use solana_program::account_info::{
1414
};
1515
use solana_program::pubkey::Pubkey;
1616

17-
use state::load_price_account;
17+
use state::{
18+
load_price_account,
19+
GenericPriceAccount,
20+
SolanaPriceAccount,
21+
};
1822

1923
pub use pyth_sdk::{
2024
Price,
@@ -27,24 +31,43 @@ pub use pyth_sdk::{
2731
pub const VALID_SLOT_PERIOD: u64 = 25;
2832

2933
/// Loads Pyth Feed Price from Price Account Info.
34+
#[deprecated(note = "solana-specific, use SolanaPriceAccount::from_account_info instead.")]
3035
pub fn load_price_feed_from_account_info(
3136
price_account_info: &AccountInfo,
3237
) -> Result<PriceFeed, PythError> {
33-
let data = price_account_info
34-
.try_borrow_data()
35-
.map_err(|_| PythError::InvalidAccountData)?;
36-
let price_account = load_price_account(*data)?;
37-
38-
Ok(price_account.to_price_feed(price_account_info.key))
38+
SolanaPriceAccount::account_info_to_feed(price_account_info)
3939
}
4040

4141
/// Loads Pyth Price Feed from Account when using Solana Client.
4242
///
4343
/// It is a helper function which constructs Account Info when reading Account in clients.
44+
#[deprecated(note = "solana-specific, use SolanaPriceAccount::from_account instead.")]
4445
pub fn load_price_feed_from_account(
4546
price_key: &Pubkey,
4647
price_account: &mut impl Account,
4748
) -> Result<PriceFeed, PythError> {
48-
let price_account_info = (price_key, price_account).into_account_info();
49-
load_price_feed_from_account_info(&price_account_info)
49+
SolanaPriceAccount::account_to_feed(price_key, price_account)
50+
}
51+
52+
impl<const N: usize, T: 'static> GenericPriceAccount<N, T>
53+
where
54+
T: Default,
55+
T: Copy,
56+
{
57+
pub fn account_info_to_feed(price_account_info: &AccountInfo) -> Result<PriceFeed, PythError> {
58+
load_price_account::<N, T>(
59+
*price_account_info
60+
.try_borrow_data()
61+
.map_err(|_| PythError::InvalidAccountData)?,
62+
)
63+
.map(|acc| acc.to_price_feed(price_account_info.key))
64+
}
65+
66+
pub fn account_to_feed(
67+
price_key: &Pubkey,
68+
price_account: &mut impl Account,
69+
) -> Result<PriceFeed, PythError> {
70+
let price_account_info = (price_key, price_account).into_account_info();
71+
Self::account_info_to_feed(&price_account_info)
72+
}
5073
}

0 commit comments

Comments
 (0)