Skip to content

Commit

Permalink
Merge pull request #69 from pyth-network/solana-pubkey
Browse files Browse the repository at this point in the history
Replace pyth_sdk_solana::AccKey with solana_program::pubkey::Pubkey
  • Loading branch information
tompntn authored Sep 1, 2022
2 parents 248b2f7 + 3db056d commit 0225ed4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 60 deletions.
2 changes: 1 addition & 1 deletion pyth-sdk-solana/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyth-sdk-solana"
version = "0.5.0"
version = "0.6.0"
authors = ["Pyth Data Foundation"]
edition = "2018"
license = "Apache-2.0"
Expand Down
15 changes: 7 additions & 8 deletions pyth-sdk-solana/examples/get_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ fn main() {

// iget and print each Product in Mapping directory
let mut i = 0;
for prod_akey in &map_acct.products {
let prod_pkey = Pubkey::new(&prod_akey.val);
for prod_pkey in &map_acct.products {
let prod_data = clnt.get_account_data(&prod_pkey).unwrap();
let prod_acct = load_product_account(&prod_data).unwrap();

Expand All @@ -65,8 +64,8 @@ fn main() {
}

// print all Prices that correspond to this Product
if prod_acct.px_acc.is_valid() {
let mut px_pkey = Pubkey::new(&prod_acct.px_acc.val);
if prod_acct.px_acc != Pubkey::default() {
let mut px_pkey = prod_acct.px_acc;
loop {
let price_data = clnt.get_account_data(&px_pkey).unwrap();
let price_account = load_price_account(&price_data).unwrap();
Expand Down Expand Up @@ -120,8 +119,8 @@ fn main() {
}

// go to next price account in list
if price_account.next.is_valid() {
px_pkey = Pubkey::new(&price_account.next.val);
if price_account.next != Pubkey::default() {
px_pkey = price_account.next;
} else {
break;
}
Expand All @@ -135,9 +134,9 @@ fn main() {
}

// go to next Mapping account in list
if !map_acct.next.is_valid() {
if map_acct.next == Pubkey::default() {
break;
}
akey = Pubkey::new(&map_acct.next.val);
akey = map_acct.next;
}
}
57 changes: 7 additions & 50 deletions pyth-sdk-solana/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,6 @@ impl Default for PriceType {
}
}

/// Public key of a Solana account
#[derive(
Copy,
Clone,
Debug,
Default,
PartialEq,
Eq,
Hash,
Ord,
PartialOrd,
BorshSerialize,
BorshDeserialize,
serde::Serialize,
serde::Deserialize,
)]
#[repr(C)]
pub struct AccKey {
pub val: [u8; 32],
}

/// Mapping accounts form a linked-list containing the listing of all products on Pyth.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(C)]
Expand All @@ -152,8 +131,8 @@ pub struct MappingAccount {
pub num: u32,
pub unused: u32,
/// next mapping account (if any)
pub next: AccKey,
pub products: [AccKey; MAP_TABLE_SIZE],
pub next: Pubkey,
pub products: [Pubkey; MAP_TABLE_SIZE],
}

#[cfg(target_endian = "little")]
Expand All @@ -179,7 +158,7 @@ pub struct ProductAccount {
/// price account size
pub size: u32,
/// first price account in list
pub px_acc: AccKey,
pub px_acc: Pubkey,
/// key/value pairs of reference attr.
pub attr: [u8; PROD_ATTR_SIZE],
}
Expand Down Expand Up @@ -247,7 +226,7 @@ pub struct PriceInfo {
#[repr(C)]
pub struct PriceComp {
/// key of contributing publisher
pub publisher: AccKey,
pub publisher: Pubkey,
/// the price used to compute the current aggregate price
pub agg: PriceInfo,
/// The publisher's latest price. This price will be incorporated into the aggregate price
Expand Down Expand Up @@ -317,9 +296,9 @@ pub struct PriceAccount {
/// space for future derived values
pub drv4: u32,
/// product account key
pub prod: AccKey,
pub prod: Pubkey,
/// next Price account in linked list
pub next: AccKey,
pub next: Pubkey,
/// valid slot of previous update
pub prev_slot: u64,
/// aggregate price of previous update with TRADING status
Expand Down Expand Up @@ -367,7 +346,7 @@ impl PriceAccount {
self.expo,
self.num,
self.num_qt,
ProductIdentifier::new(self.prod.val),
ProductIdentifier::new(self.prod.to_bytes()),
self.agg.price,
self.agg.conf,
self.ema_price.val,
Expand All @@ -379,28 +358,6 @@ impl PriceAccount {
}
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct AccKeyU64 {
pub val: [u64; 4],
}

#[cfg(target_endian = "little")]
unsafe impl Zeroable for AccKeyU64 {
}

#[cfg(target_endian = "little")]
unsafe impl Pod for AccKeyU64 {
}

impl AccKey {
pub fn is_valid(&self) -> bool {
match load::<AccKeyU64>(&self.val) {
Ok(k8) => k8.val[0] != 0 || k8.val[1] != 0 || k8.val[2] != 0 || k8.val[3] != 0,
Err(_) => false,
}
}
}

fn load<T: Pod>(data: &[u8]) -> Result<&T, PodCastError> {
let size = size_of::<T>();
if data.len() >= size {
Expand Down
2 changes: 1 addition & 1 deletion pyth-sdk-solana/test-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test-bpf = []
no-entrypoint = []

[dependencies]
pyth-sdk-solana = { path = "../", version = "0.5.0" }
pyth-sdk-solana = { path = "../", version = "0.6.0" }
solana-program = "1.8.1, < 1.11" # Currently latest Solana 1.11 crate can't build bpf: https://github.com/solana-labs/solana/issues/26188
bytemuck = "1.7.2"
borsh = "0.9"
Expand Down

0 comments on commit 0225ed4

Please sign in to comment.