Skip to content

Commit 6f53073

Browse files
authored
Port MAX_CI_DIVISOR and MAX_NUM_DECIMALS (#281)
1 parent 9ef18a2 commit 6f53073

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

program/c/src/oracle/oracle.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ const uint64_t EXTRA_PUBLISHER_SPACE = 1000ULL;
2828
#define PC_PUBKEY_SIZE_64 (PC_PUBKEY_SIZE/sizeof(uint64_t))
2929
#define PC_MAP_TABLE_SIZE 640
3030
#define PC_COMP_SIZE 32
31-
// Bound on the range of the exponent in price accounts. This number is set such that the
32-
// PD-based EMA computation does not lose too much precision.
33-
#define PC_MAX_NUM_DECIMALS 8
31+
3432
#define PC_PROD_ACC_SIZE 512
3533
#define PC_EXP_DECAY -9
36-
// If ci > price / PC_MAX_CI_DIVISOR, set publisher status to unknown.
37-
// (e.g., 20 means ci must be < 5% of price)
38-
#define PC_MAX_CI_DIVISOR 20
3934

4035
#ifndef PC_HEAP_START
4136
#define PC_HEAP_START (0x300000000)

program/rust/src/c_oracle_header.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ use std::mem::size_of;
1313
//things defined in bindings.h
1414
include!("../bindings.rs");
1515

16+
17+
/// If ci > price / PC_MAX_CI_DIVISOR, set publisher status to unknown.
18+
/// (e.g., 20 means ci must be < 5% of price)
19+
pub const MAX_CI_DIVISOR: i64 = 20;
20+
/// Bound on the range of the exponent in price accounts. This number is set such that the
21+
/// PD-based EMA computation does not lose too much precision.
22+
pub const MAX_NUM_DECIMALS: i32 = 8;
23+
1624
/// The PythAccount trait's purpose is to attach constants to the 3 types of accounts that Pyth has
1725
/// (mapping, price, product). This allows less duplicated code, because now we can create generic
1826
/// functions to perform common checks on the accounts and to load and initialize the accounts.

program/rust/src/rust_oracle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::c_oracle_header::{
1010
PriceEma,
1111
PriceInfo,
1212
ProductAccount,
13+
MAX_CI_DIVISOR,
1314
PC_COMP_SIZE,
1415
PC_MAP_TABLE_SIZE,
15-
PC_MAX_CI_DIVISOR,
1616
PC_PROD_ACC_SIZE,
1717
PC_PTYPE_UNKNOWN,
1818
PC_STATUS_UNKNOWN,
@@ -273,7 +273,7 @@ pub fn upd_price(
273273
// Try to update the publisher's price
274274
if is_component_update(cmd_args)? {
275275
let mut status: u32 = cmd_args.status;
276-
let mut threshold_conf = cmd_args.price / PC_MAX_CI_DIVISOR as i64;
276+
let mut threshold_conf = cmd_args.price / MAX_CI_DIVISOR;
277277

278278
if threshold_conf < 0 {
279279
threshold_conf = -threshold_conf;

program/rust/src/tests/test_init_price.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use solana_program::pubkey::Pubkey;
44

55
use crate::c_oracle_header::{
66
PriceAccount,
7-
PC_MAX_NUM_DECIMALS,
7+
MAX_NUM_DECIMALS,
88
PC_VERSION,
99
};
1010
use crate::deserialize::{
@@ -143,7 +143,7 @@ fn test_init_price() {
143143
price_account.is_signer = true;
144144
let cmd: InitPriceArgs = InitPriceArgs {
145145
header: OracleCommand::InitPrice.into(),
146-
exponent: -(PC_MAX_NUM_DECIMALS as i32) - 1,
146+
exponent: -MAX_NUM_DECIMALS - 1,
147147
price_type: ptype,
148148
};
149149
let instruction_data = bytes_of::<InitPriceArgs>(&cmd);

program/rust/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::c_oracle_header::{
22
AccountHeader,
3-
PC_MAX_NUM_DECIMALS,
3+
MAX_NUM_DECIMALS,
44
};
55
use crate::deserialize::load_account_as;
66
use crate::instruction::{
@@ -83,7 +83,7 @@ pub fn check_valid_fresh_account(account: &AccountInfo) -> Result<(), ProgramErr
8383
// Check that an exponent is within the range of permitted exponents for price accounts.
8484
pub fn check_exponent_range(expo: i32) -> Result<(), ProgramError> {
8585
pyth_assert(
86-
expo >= -(PC_MAX_NUM_DECIMALS as i32) && expo <= PC_MAX_NUM_DECIMALS as i32,
86+
(-MAX_NUM_DECIMALS..=MAX_NUM_DECIMALS).contains(&expo),
8787
ProgramError::InvalidArgument,
8888
)
8989
}

0 commit comments

Comments
 (0)