Skip to content

Commit

Permalink
Add validation and unit test for oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Sep 1, 2023
1 parent 045219f commit 0b52ffc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
40 changes: 35 additions & 5 deletions steward/src/cellars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::result::Result;
use abscissa_core::tracing::log::info;
use ethers::prelude::*;

use crate::{error::Error, utils::sp_call_error};
use crate::{
error::Error,
utils::{sp_call_error, string_to_u256},
};

pub(crate) mod aave_v2_stablecoin;
pub(crate) mod adaptors;
Expand All @@ -18,15 +21,15 @@ pub(crate) mod cellar_v2_5;

// oracles

pub const ORACLE1: (U256, &'static str) = (
pub const ORACLE1: (U256, &str) = (
U256([3, 0, 0, 0]),
"72249f0199eacf6230def33a31e80cf76de78f67",
);
pub const ORACLE2: (U256, &'static str) = (
pub const ORACLE2: (U256, &str) = (
U256([5, 0, 0, 0]),
"c47278b65443ce71cf47e8455bb343f2db11b70e",
);
pub const ALLOWED_PRICE_ORACLES: [(U256, &'static str); 2] = [ORACLE1, ORACLE2];
pub const ALLOWED_PRICE_ORACLES: [(U256, &str); 2] = [ORACLE1, ORACLE2];

// permissions

Expand Down Expand Up @@ -184,6 +187,23 @@ pub fn validate_new_position(
Ok(())
}

pub fn validate_oracle(
cellar_id: &str,
registry_id_in: &str,
oracle_in: &str,
) -> Result<(), Error> {
let cellar_id_normalized = normalize_address(cellar_id.to_string());
let oracle_in = normalize_address(oracle_in.to_string());
let registry_id_in = string_to_u256(registry_id_in.to_string())?;
if !cellar_id_normalized.eq(CELLAR_TURBO_SWETH)
|| !ALLOWED_PRICE_ORACLES.contains(&(registry_id_in, oracle_in.as_str()))
{
return Err(sp_call_error("unauthorized oracle update".to_string()));
}

Ok(())
}

pub fn check_blocked_adaptor(adaptor_id: &str) -> Result<(), Error> {
let adaptor_id = normalize_address(adaptor_id.to_string());
if BLOCKED_ADAPTORS.contains(&adaptor_id.as_str()) {
Expand Down Expand Up @@ -373,7 +393,17 @@ mod tests {
}

#[test]
fn test_U256_sanity() {
fn test_u256_sanity() {
assert_eq!(U256([5, 0, 0, 0]), U256::from(5));
}

#[test]
fn test_validate_oracle() {
assert!(validate_oracle(CELLAR_RYBTC, &ORACLE1.0.to_string(), ORACLE1.1).is_err());
assert!(
validate_oracle(CELLAR_TURBO_SWETH, &U256::from(6).to_string(), ORACLE2.1).is_err()
);
assert!(validate_oracle(CELLAR_TURBO_SWETH, &ORACLE1.0.to_string(), ORACLE1.1).is_ok());
assert!(validate_oracle(CELLAR_TURBO_SWETH, &ORACLE2.0.to_string(), ORACLE2.1).is_ok());
}
}
17 changes: 3 additions & 14 deletions steward/src/cellars/cellar_v2_5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ use crate::{
};

use super::{
check_blocked_adaptor, check_blocked_position, log_cellar_call, normalize_address,
validate_new_adaptor, validate_new_position, ALLOWED_PRICE_ORACLES, CELLAR_TURBO_SWETH,
V2_5_PERMISSIONS,
check_blocked_adaptor, check_blocked_position, log_cellar_call, validate_new_adaptor,
validate_new_position, validate_oracle, V2_5_PERMISSIONS,
};

const CELLAR_NAME: &str = "CellarV2.5";
Expand Down Expand Up @@ -262,17 +261,7 @@ pub fn get_encoded_function(call: FunctionCall, cellar_id: String) -> Result<Vec
Ok(CellarV2_5Calls::DecreaseShareSupplyCap(call).encode())
}
Function::SetSharePriceOracle(params) => {
let cellar_id_normalized = normalize_address(cellar_id.clone());
let oracle_in = normalize_address(params.oracle.clone());
let registry_id_in = string_to_u256(params.registry_id.clone())?;
if !cellar_id_normalized.eq(CELLAR_TURBO_SWETH)
|| !ALLOWED_PRICE_ORACLES.contains(&(registry_id_in, &oracle_in))
{
return Err(ErrorKind::SPCallError
.context("unauthorized oracle update".to_string())
.into());
}

validate_oracle(&cellar_id, &params.oracle, &params.registry_id)?;
log_cellar_call(
CELLAR_NAME,
&SetSharePriceOracleCall::function_name(),
Expand Down

0 comments on commit 0b52ffc

Please sign in to comment.