Skip to content

Commit 3c3c53b

Browse files
committed
Create rust structs and replace bindgen structs
1 parent d2646c5 commit 3c3c53b

22 files changed

+408
-309
lines changed

program/rust/src/c_oracle_header.rs

Lines changed: 116 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -34,94 +34,137 @@ pub trait PythAccount: Pod {
3434
}
3535
}
3636

37-
impl PythAccount for pc_map_table_t {
37+
impl PythAccount for MappingAccount {
3838
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_MAPPING;
3939
const INITIAL_SIZE: u32 = PC_MAP_TABLE_T_PROD_OFFSET as u32;
4040
}
4141

42-
impl PythAccount for pc_prod_t {
42+
impl PythAccount for ProductAccount {
4343
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_PRODUCT;
44-
const INITIAL_SIZE: u32 = size_of::<pc_prod_t>() as u32;
44+
const INITIAL_SIZE: u32 = size_of::<ProductAccount>() as u32;
4545
fn minimum_size() -> usize {
4646
PC_PROD_ACC_SIZE as usize
4747
}
4848
}
4949

50-
impl PythAccount for pc_price_t {
50+
impl PythAccount for PriceAccount {
5151
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_PRICE;
5252
const INITIAL_SIZE: u32 = PC_PRICE_T_COMP_OFFSET as u32;
5353
}
5454

55-
#[cfg(target_endian = "little")]
56-
unsafe impl Zeroable for pc_acc {
57-
}
58-
59-
#[cfg(target_endian = "little")]
60-
unsafe impl Pod for pc_acc {
61-
}
62-
63-
#[cfg(target_endian = "little")]
64-
unsafe impl Zeroable for pc_map_table {
65-
}
66-
67-
#[cfg(target_endian = "little")]
68-
unsafe impl Pod for pc_map_table {
69-
}
70-
71-
#[cfg(target_endian = "little")]
72-
unsafe impl Zeroable for pc_prod {
73-
}
74-
75-
#[cfg(target_endian = "little")]
76-
unsafe impl Pod for pc_prod {
77-
}
78-
79-
#[cfg(target_endian = "little")]
80-
unsafe impl Zeroable for pc_price {
81-
}
82-
83-
#[cfg(target_endian = "little")]
84-
unsafe impl Pod for pc_price {
85-
}
86-
87-
#[cfg(target_endian = "little")]
88-
unsafe impl Zeroable for pc_price_info {
89-
}
90-
91-
#[cfg(target_endian = "little")]
92-
unsafe impl Pod for pc_price_info {
93-
}
94-
95-
#[cfg(target_endian = "little")]
96-
unsafe impl Zeroable for pc_ema {
97-
}
98-
99-
#[cfg(target_endian = "little")]
100-
unsafe impl Pod for pc_ema {
101-
}
102-
103-
104-
#[cfg(target_endian = "little")]
105-
unsafe impl Zeroable for pc_pub_key_t {
106-
}
107-
108-
#[cfg(target_endian = "little")]
109-
unsafe impl Pod for pc_pub_key_t {
110-
}
111-
112-
113-
#[cfg(target_endian = "little")]
114-
unsafe impl Zeroable for pc_price_comp_t {
115-
}
116-
117-
#[cfg(target_endian = "little")]
118-
unsafe impl Pod for pc_price_comp_t {
119-
}
120-
121-
impl pc_pub_key_t {
122-
pub fn new_unique() -> pc_pub_key_t {
55+
#[repr(C)]
56+
#[derive(Copy, Clone, Pod, Zeroable)]
57+
pub struct PriceAccount {
58+
pub magic_: u32,
59+
pub ver_: u32,
60+
pub type_: u32,
61+
pub size_: u32,
62+
pub ptype_: u32, // Type of the price account
63+
pub expo_: i32, // Exponent for the published prices
64+
pub num_: u32, // Current number of authorized publishers
65+
pub num_qt_: u32, // Number of valid quotes for the last aggregation
66+
pub last_slot_: u64, // Last slot with a succesful aggregation (status : TRADING)
67+
pub valid_slot_: u64, // Second to last slot where aggregation was attempted
68+
pub twap_: PriceEma, // Ema for price
69+
pub twac_: PriceEma, // Ema for confidence
70+
pub timestamp_: i64, // Last time aggregation was attempted
71+
pub min_pub_: u8, // Minimum valid publisher quotes for a succesful aggregation
72+
pub unused_1_: i8,
73+
pub unused_2_: i16,
74+
pub unused_3_: i32,
75+
pub prod_: CPubkey, // Corresponding mapping account
76+
pub next_: CPubkey, // Next price account in the list
77+
pub prev_slot_: u64, /* Second to last slot where aggregation was succesful
78+
* (status : TRADING) */
79+
pub prev_price_: i64, // Aggregate price at prev_slot_
80+
pub prev_conf_: u64, // Confidence interval at prev_slot_
81+
pub prev_timestamp_: i64, // Timestamp of prev_slot_
82+
pub agg_: PriceInfo, // Last attempted aggregate results
83+
pub comp_: [PriceComponent; 32usize], // Publisher's price components
84+
}
85+
86+
#[repr(C)]
87+
#[derive(Copy, Clone, Pod, Zeroable)]
88+
pub struct PriceComponent {
89+
pub pub_: CPubkey,
90+
pub agg_: PriceInfo,
91+
pub latest_: PriceInfo,
92+
}
93+
94+
#[repr(C)]
95+
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
96+
pub struct PriceInfo {
97+
pub price_: i64,
98+
pub conf_: u64,
99+
pub status_: u32,
100+
pub corp_act_status_: u32,
101+
pub pub_slot_: u64,
102+
}
103+
104+
#[repr(C)]
105+
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
106+
pub struct PriceEma {
107+
pub val_: i64,
108+
pub numer_: i64,
109+
pub denom_: i64,
110+
}
111+
112+
#[repr(C)]
113+
#[derive(Copy, Clone, Zeroable, Pod)]
114+
pub struct AccountHeader {
115+
pub magic_: u32,
116+
pub ver_: u32,
117+
pub type_: u32,
118+
pub size_: u32,
119+
}
120+
121+
#[repr(C)]
122+
#[derive(Copy, Clone, Pod, Zeroable)]
123+
pub struct ProductAccount {
124+
pub magic_: u32,
125+
pub ver_: u32,
126+
pub type_: u32,
127+
pub size_: u32,
128+
pub px_acc_: CPubkey,
129+
}
130+
131+
#[repr(C)]
132+
#[derive(Copy, Clone)]
133+
pub struct MappingAccount {
134+
pub magic_: u32,
135+
pub ver_: u32,
136+
pub type_: u32,
137+
pub size_: u32,
138+
pub num_: u32,
139+
pub unused_: u32,
140+
pub next_: CPubkey,
141+
pub prod_: [CPubkey; 640usize],
142+
}
143+
144+
// Unsafe impl because CPubkey is a union
145+
unsafe impl Pod for CPubkey {
146+
}
147+
unsafe impl Zeroable for CPubkey {
148+
}
149+
150+
151+
// Unsafe impl because product_list is of size 640 and there's no derived trait for this size
152+
unsafe impl Pod for MappingAccount {
153+
}
154+
unsafe impl Zeroable for MappingAccount {
155+
}
156+
157+
#[repr(C)]
158+
#[derive(Copy, Clone)]
159+
pub union CPubkey {
160+
pub k1_: [u8; 32usize],
161+
pub k8_: [u64; 4usize],
162+
}
163+
164+
impl CPubkey {
165+
pub fn new_unique() -> CPubkey {
123166
let solana_unique = Pubkey::new_unique();
124-
pc_pub_key_t {
167+
CPubkey {
125168
k1_: solana_unique.to_bytes(),
126169
}
127170
}

program/rust/src/deserialize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bytemuck::{
77
};
88

99
use crate::c_oracle_header::{
10-
pc_acc,
10+
AccountHeader,
1111
PythAccount,
1212
PC_MAGIC,
1313
};
@@ -71,7 +71,7 @@ pub fn load_checked<'a, T: PythAccount>(
7171
version: u32,
7272
) -> Result<RefMut<'a, T>, ProgramError> {
7373
{
74-
let account_header = load_account_as::<pc_acc>(account)?;
74+
let account_header = load_account_as::<AccountHeader>(account)?;
7575
pyth_assert(
7676
account_header.magic_ == PC_MAGIC
7777
&& account_header.ver_ == version
@@ -90,7 +90,7 @@ pub fn initialize_pyth_account_checked<'a, T: PythAccount>(
9090
clear_account(account)?;
9191

9292
{
93-
let mut account_header = load_account_as_mut::<pc_acc>(account)?;
93+
let mut account_header = load_account_as_mut::<AccountHeader>(account)?;
9494
account_header.magic_ = PC_MAGIC;
9595
account_header.ver_ = version;
9696
account_header.type_ = T::ACCOUNT_TYPE;

program/rust/src/instruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::c_oracle_header::{
2-
pc_pub_key_t,
2+
CPubkey,
33
PC_VERSION,
44
};
55
use crate::deserialize::load;
@@ -122,7 +122,7 @@ pub type InitPriceArgs = AddPriceArgs;
122122
#[derive(Zeroable, Pod, Copy, Clone)]
123123
pub struct AddPublisherArgs {
124124
pub header: CommandHeader,
125-
pub publisher: pc_pub_key_t,
125+
pub publisher: CPubkey,
126126
}
127127

128128
pub type DelPublisherArgs = AddPublisherArgs;

0 commit comments

Comments
 (0)