Skip to content

Commit

Permalink
SetSharePriceOracle for turbo sweth (#220)
Browse files Browse the repository at this point in the history
* Add temporary SetSharePriceOracle function call

* Move allow values to consts

* Add validation and unit test for oracle

* Fix oracle validation arg order

* Fix test and ordering
  • Loading branch information
cbrit authored Sep 1, 2023
1 parent 3a601a0 commit b1f9894
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 284 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sommelier_steward"
version = "3.4.1"
version = "3.4.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
14 changes: 14 additions & 0 deletions proto/cellar_v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ message CellarV2_5 {
IncreaseShareSupplyCap increase_share_supply_cap = 16;
// Represents function `decreaseShareSupplyCap(uint192)
DecreaseShareSupplyCap decrease_share_supply_cap = 17;
// Represents function `setSharePriceOracle(uint256, address)
SetSharePriceOracle set_share_price_oracle = 18;
}
}

Expand Down Expand Up @@ -612,6 +614,18 @@ message CellarV2_5 {
message DecreaseShareSupplyCap {
string new_cap = 1;
}

/*
* Allows strategist to set the share price oracle contract
*
* Represents function `setSharePriceOracle(uint256, address)`
*/
message SetSharePriceOracle {
// The oracle registry ID
string registry_id = 1;
// The oracle contract address
string oracle = 2;
}
}

// Represents a call to adaptor an. The cellar must be authorized to call the target adaptor.
Expand Down
2 changes: 1 addition & 1 deletion steward/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "steward"
authors = []
version = "3.4.1"
version = "3.4.2"
edition = "2018"

[dependencies]
Expand Down
50 changes: 49 additions & 1 deletion 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 @@ -16,6 +19,18 @@ pub(crate) mod cellar_v2_5;
// constants
// addresses are normalized by removing the 0x prefix and converting to lowercase for reliable comparison

// oracles

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

// permissions

pub const ALLOWED_V2_0_SETUP_ADAPTORS: [(&str, &str); 1] = [(CELLAR_RYUSD, ADAPTOR_CELLAR_V2)];
Expand Down Expand Up @@ -91,6 +106,7 @@ pub const CELLAR_RYSNX: &str = "cbf2250f33c4161e18d4a2fa47464520af5216b5";
pub const CELLAR_RYUNI: &str = "6a6af5393dc23d7e3db28d28ef422db7c40932b6";
pub const CELLAR_RYUSD: &str = "97e6e0a40a3d02f12d1cec30ebfbae04e37c119e";
pub const CELLAR_RYBTC: &str = "0274a704a6d9129f90a62ddc6f6024b33ecdad36";
pub const CELLAR_TURBO_SWETH: &str = "d33dad974b938744dac81fe00ac67cb5aa13958e";

// deprecated adaptors

Expand Down Expand Up @@ -171,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 @@ -358,4 +391,19 @@ mod tests {
assert!(res.is_err());
assert_eq!(expected_err, res.unwrap_err().to_string());
}

#[test]
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());
}
}
16 changes: 15 additions & 1 deletion steward/src/cellars/cellar_v2_5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{

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

const CELLAR_NAME: &str = "CellarV2.5";
Expand Down Expand Up @@ -260,6 +260,20 @@ pub fn get_encoded_function(call: FunctionCall, cellar_id: String) -> Result<Vec

Ok(CellarV2_5Calls::DecreaseShareSupplyCap(call).encode())
}
Function::SetSharePriceOracle(params) => {
validate_oracle(&cellar_id, &params.registry_id, &params.oracle)?;
log_cellar_call(
CELLAR_NAME,
&SetSharePriceOracleCall::function_name(),
&cellar_id,
);
let call = SetSharePriceOracleCall {
registry_id: string_to_u256(params.registry_id)?,
share_price_oracle: sp_call_parse_address(params.oracle)?,
};

Ok(CellarV2_5Calls::SetSharePriceOracle(call).encode())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion steward_abi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "steward_abi"
version = "3.4.1"
version = "3.4.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Loading

0 comments on commit b1f9894

Please sign in to comment.