Skip to content

Commit

Permalink
Macro for checking if program is a localnet build or not
Browse files Browse the repository at this point in the history
  • Loading branch information
jgur-psyops committed Nov 7, 2024
1 parent 259fd83 commit 3a36bb8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
11 changes: 11 additions & 0 deletions programs/marginfi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ macro_rules! assert_struct_align {
static_assertions::const_assert_eq!(std::mem::align_of::<$struct>(), $align);
};
}

#[macro_export]
macro_rules! live {
() => {
cfg!(any(
feature = "mainnet-beta",
feature = "staging",
feature = "devnet"
))
};
}
26 changes: 5 additions & 21 deletions programs/marginfi/src/state/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
MIN_PYTH_PUSH_VERIFICATION_LEVEL, NATIVE_STAKE_ID, PYTH_ID, SPL_SINGLE_POOL_ID,
STD_DEV_MULTIPLE, SWITCHBOARD_PULL_ID,
},
debug, math_error,
debug, live, math_error,
prelude::*,
};

Expand Down Expand Up @@ -298,11 +298,7 @@ impl OraclePriceFeedAdapter {
check!(oracle_ais.len() == 3, MarginfiError::InvalidOracleAccount);

// Note: mainnet/staging/devnet use "push" oracles, localnet uses legacy
if cfg!(any(
feature = "mainnet-beta",
feature = "staging",
feature = "devnet"
)) {
if live!() {
PythPushOraclePriceFeed::check_ai_and_feed_id(
&oracle_ais[0],
bank_config.get_pyth_push_oracle_feed_id().unwrap(),
Expand Down Expand Up @@ -356,11 +352,7 @@ impl OraclePriceFeedAdapter {
// light validation (after initial setup, only the Pyth oracle needs to be validated)
check!(oracle_ais.len() == 1, MarginfiError::InvalidOracleAccount);
// Note: mainnet/staging/devnet use push oracles, localnet uses legacy push
if cfg!(any(
feature = "mainnet-beta",
feature = "staging",
feature = "devnet"
)) {
if live!() {
PythPushOraclePriceFeed::check_ai_and_feed_id(
&oracle_ais[0],
bank_config.get_pyth_push_oracle_feed_id().unwrap(),
Expand Down Expand Up @@ -388,23 +380,15 @@ impl PythLegacyPriceFeed {
let price_feed = load_pyth_price_feed(ai)?;

// Note: mainnet/staging/devnet use oracle age, localnet ignores oracle age
let ema_price = if cfg!(any(
feature = "mainnet-beta",
feature = "staging",
feature = "devnet"
)) {
let ema_price = if live!() {
price_feed
.get_ema_price_no_older_than(current_time, max_age)
.ok_or(MarginfiError::StaleOracle)?
} else {
price_feed.get_ema_price_unchecked()
};

let price = if cfg!(any(
feature = "mainnet-beta",
feature = "staging",
feature = "devnet"
)) {
let price = if live!() {
price_feed
.get_price_no_older_than(current_time, max_age)
.ok_or(MarginfiError::StaleOracle)?
Expand Down

0 comments on commit 3a36bb8

Please sign in to comment.