Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions contracts/oracle/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ doctest = false
[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
pyth = ["pyth-sdk-cw"]

[dependencies]
cosmwasm-std = { workspace = true }
Expand All @@ -24,7 +23,7 @@ cw-storage-plus = { workspace = true }
mars-owner = { workspace = true }
mars-utils = { workspace = true }
mars-red-bank-types = { workspace = true }
pyth-sdk-cw = { workspace = true, optional = true }
pyth-sdk-cw = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
1 change: 0 additions & 1 deletion contracts/oracle/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod contract;
mod error;
mod traits;

#[cfg(feature = "pyth")]
pub mod pyth;

pub use contract::*;
Expand Down
33 changes: 32 additions & 1 deletion contracts/oracle/base/src/pyth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use pyth_sdk_cw::{query_price_feed, Price, PriceFeed, PriceFeedResponse, PriceId
use super::*;
use crate::error::ContractError::InvalidPrice;

// We don't support any denom with more than 18 decimals
const MAX_DENOM_DECIMALS: u8 = 18;

/// We want to discriminate which actions should trigger a circuit breaker check.
/// The objective is to allow liquidations to happen without requiring too many checks (always be open for liquidations)
/// while not allowing other actions to be taken in cases of extreme volatility (which could indicate price manipulation attacks).
Expand Down Expand Up @@ -121,7 +124,11 @@ fn query_pyth_price_for_liquidation(
}

/// Assert Pyth configuration
pub fn assert_pyth(max_confidence: Decimal, max_deviation: Decimal) -> ContractResult<()> {
pub fn assert_pyth(
max_confidence: Decimal,
max_deviation: Decimal,
denom_decimals: u8,
) -> ContractResult<()> {
if !max_confidence.le(&Decimal::percent(20u64)) {
return Err(ContractError::InvalidPriceSource {
reason: "max_confidence must be in the range of <0;0.2>".to_string(),
Expand All @@ -134,6 +141,12 @@ pub fn assert_pyth(max_confidence: Decimal, max_deviation: Decimal) -> ContractR
});
}

if denom_decimals > MAX_DENOM_DECIMALS {
return Err(ContractError::InvalidPriceSource {
reason: format!("denom_decimals must be <= {}", MAX_DENOM_DECIMALS),
});
}

Ok(())
}

Expand Down Expand Up @@ -255,6 +268,12 @@ pub fn scale_pyth_price(
// 26 decimals used (overflow) !!!
let price = usd_price.checked_mul(denom_scaled)?.checked_mul(pyth_price)?;

if price.is_zero() {
return Err(InvalidPrice {
reason: "price is zero".to_string(),
});
}

Ok(price)
}

Expand Down Expand Up @@ -327,4 +346,16 @@ mod tests {
.unwrap();
assert_eq!(ueth_price_in_uusd, Decimal::from_atomics(100000098000001u128, 20u32).unwrap());
}

#[test]
fn return_error_if_scaled_pyth_price_is_zero() {
let price_err =
scale_pyth_price(1u128, -18, 18u8, Decimal::from_str("1000000").unwrap()).unwrap_err();
assert_eq!(
price_err,
ContractError::InvalidPrice {
reason: "price is zero".to_string()
}
);
}
}
3 changes: 1 addition & 2 deletions contracts/oracle/osmosis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw2 = { workspace = true }
cw-storage-plus = { workspace = true }
mars-oracle-base = { workspace = true, features = ["pyth"] }
mars-oracle-base = { workspace = true }
mars-osmosis = { workspace = true }
mars-owner = { workspace = true }
mars-utils = { workspace = true }
Expand All @@ -38,4 +38,3 @@ cosmwasm-schema = { workspace = true }
mars-owner = { workspace = true }
mars-testing = { workspace = true }
mars-utils = { workspace = true }
pyth-sdk-cw = { workspace = true }
6 changes: 5 additions & 1 deletion contracts/oracle/osmosis/src/price_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ impl PriceSourceUnchecked<OsmosisPriceSourceChecked, Empty> for OsmosisPriceSour
max_deviation,
denom_decimals,
} => {
mars_oracle_base::pyth::assert_pyth(*max_confidence, *max_deviation)?;
mars_oracle_base::pyth::assert_pyth(
*max_confidence,
*max_deviation,
*denom_decimals,
)?;
mars_oracle_base::pyth::assert_usd_price_source(deps, price_sources)?;
Ok(OsmosisPriceSourceChecked::Pyth {
contract_addr: deps.api.addr_validate(contract_addr)?,
Expand Down
54 changes: 32 additions & 22 deletions contracts/oracle/osmosis/tests/test_set_price_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,30 +1021,31 @@ fn setting_price_source_xyk_lp() {
fn setting_price_source_pyth_with_invalid_params() {
let mut deps = helpers::setup_test();

let mut set_price_source_pyth = |max_confidence: Decimal, max_deviation: Decimal| {
execute(
deps.as_mut(),
mock_env(),
mock_info("owner"),
ExecuteMsg::SetPriceSource {
denom: "uatom".to_string(),
price_source: OsmosisPriceSourceUnchecked::Pyth {
contract_addr: "pyth_contract_addr".to_string(),
price_feed_id: PriceIdentifier::from_hex(
"61226d39beea19d334f17c2febce27e12646d84675924ebb02b9cdaea68727e3",
)
.unwrap(),
max_staleness: 30,
max_confidence,
max_deviation,
denom_decimals: 6u8,
let mut set_price_source_pyth =
|max_confidence: Decimal, max_deviation: Decimal, denom_decimals: u8| {
execute(
deps.as_mut(),
mock_env(),
mock_info("owner"),
ExecuteMsg::SetPriceSource {
denom: "uatom".to_string(),
price_source: OsmosisPriceSourceUnchecked::Pyth {
contract_addr: "pyth_contract_addr".to_string(),
price_feed_id: PriceIdentifier::from_hex(
"61226d39beea19d334f17c2febce27e12646d84675924ebb02b9cdaea68727e3",
)
.unwrap(),
max_staleness: 30,
max_confidence,
max_deviation,
denom_decimals,
},
},
},
)
};
)
};

// attempting to set max_confidence > 20%; should fail
let err = set_price_source_pyth(Decimal::percent(21), Decimal::percent(6)).unwrap_err();
let err = set_price_source_pyth(Decimal::percent(21), Decimal::percent(6), 6).unwrap_err();
assert_eq!(
err,
ContractError::InvalidPriceSource {
Expand All @@ -1053,13 +1054,22 @@ fn setting_price_source_pyth_with_invalid_params() {
);

// attempting to set max_deviation > 20%; should fail
let err = set_price_source_pyth(Decimal::percent(5), Decimal::percent(21)).unwrap_err();
let err = set_price_source_pyth(Decimal::percent(5), Decimal::percent(21), 18).unwrap_err();
assert_eq!(
err,
ContractError::InvalidPriceSource {
reason: "max_deviation must be in the range of <0;0.2>".to_string()
}
);

// attempting to set denom_decimals > 18; should fail
let err = set_price_source_pyth(Decimal::percent(5), Decimal::percent(20), 19).unwrap_err();
assert_eq!(
err,
ContractError::InvalidPriceSource {
reason: "denom_decimals must be <= 18".to_string()
}
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion contracts/oracle/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw2 = { workspace = true }
cw-storage-plus = { workspace = true }
mars-oracle-base = { workspace = true, features = ["pyth"] }
mars-oracle-base = { workspace = true }
mars-red-bank-types = { workspace = true }
pyth-sdk-cw = { workspace = true }

Expand Down
2 changes: 1 addition & 1 deletion contracts/oracle/wasm/src/price_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl PriceSourceUnchecked<WasmPriceSourceChecked, Empty> for WasmPriceSourceUnch
max_deviation,
denom_decimals,
} => {
mars_oracle_base::pyth::assert_pyth(max_confidence, max_deviation)?;
mars_oracle_base::pyth::assert_pyth(max_confidence, max_deviation, denom_decimals)?;
mars_oracle_base::pyth::assert_usd_price_source(deps, price_sources)?;
Ok(WasmPriceSourceChecked::Pyth {
contract_addr: deps.api.addr_validate(&contract_addr)?,
Expand Down
63 changes: 63 additions & 0 deletions contracts/oracle/wasm/tests/test_price_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,69 @@ fn setting_price_source_pyth_if_missing_usd() {
);
}

#[test]
fn setting_price_source_pyth_with_invalid_params() {
let runner = get_test_runner();
let robot = WasmOracleTestRobot::new(
&runner,
get_contracts(&get_test_runner()),
&get_test_runner().init_default_accounts().unwrap()[0],
None,
);

let mut deps = helpers::setup_test(&robot.astroport_contracts.factory.address);

let mut set_price_source_pyth =
|max_confidence: Decimal, max_deviation: Decimal, denom_decimals: u8| {
execute(
deps.as_mut(),
mock_env(),
mock_info("owner"),
ExecuteMsg::SetPriceSource {
denom: "uatom".to_string(),
price_source: WasmPriceSourceUnchecked::Pyth {
contract_addr: "pyth_contract_addr".to_string(),
price_feed_id: PriceIdentifier::from_hex(
"61226d39beea19d334f17c2febce27e12646d84675924ebb02b9cdaea68727e3",
)
.unwrap(),
max_staleness: 30,
max_confidence,
max_deviation,
denom_decimals,
},
},
)
};

// attempting to set max_confidence > 20%; should fail
let err = set_price_source_pyth(Decimal::percent(21), Decimal::percent(6), 6).unwrap_err();
assert_eq!(
err,
ContractError::InvalidPriceSource {
reason: "max_confidence must be in the range of <0;0.2>".to_string()
}
);

// attempting to set max_deviation > 20%; should fail
let err = set_price_source_pyth(Decimal::percent(5), Decimal::percent(21), 18).unwrap_err();
assert_eq!(
err,
ContractError::InvalidPriceSource {
reason: "max_deviation must be in the range of <0;0.2>".to_string()
}
);

// attempting to set denom_decimals > 18; should fail
let err = set_price_source_pyth(Decimal::percent(5), Decimal::percent(20), 19).unwrap_err();
assert_eq!(
err,
ContractError::InvalidPriceSource {
reason: "denom_decimals must be <= 18".to_string()
}
);
}

#[test]
fn twap_window_size_not_gt_tolerance() {
let runner = get_test_runner();
Expand Down