Skip to content

Commit

Permalink
admin arg removed from administration functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hawthorne-abendsen authored and orbitlens committed Feb 25, 2024
1 parent 0c65c4f commit 22571f4
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 146 deletions.
18 changes: 5 additions & 13 deletions src/extensions/env_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const DECIMALS: &str = "decimals";
const RESOLUTION: &str = "resolution";

pub trait EnvExtensions {
fn is_authorized(&self, invoker: &Address) -> bool;

fn get_admin(&self) -> Option<Address>;

fn set_admin(&self, admin: &Address);
Expand Down Expand Up @@ -54,22 +52,14 @@ pub trait EnvExtensions {

fn get_asset_index(&self, asset: &Asset) -> Option<u8>;

fn panic_if_not_admin(&self, invoker: &Address);
fn panic_if_not_admin(&self);

fn is_initialized(&self) -> bool;

fn bump(&self, ledgers_to_live: u32);
}

impl EnvExtensions for Env {
fn is_authorized(&self, invoker: &Address) -> bool {
invoker.require_auth();

//invoke get_admin to check if the admin is set
let admin = self.get_admin();
!admin.is_none() && invoker == &admin.unwrap()
}

fn is_initialized(&self) -> bool {
get_instance_storage(&self).has(&ADMIN_KEY)
}
Expand Down Expand Up @@ -181,10 +171,12 @@ impl EnvExtensions for Env {
return Some(index.unwrap() as u8);
}

fn panic_if_not_admin(&self, invoker: &Address) {
if !self.is_authorized(invoker) {
fn panic_if_not_admin(&self) {
let admin = self.get_admin();
if admin.is_none() {
panic_with_error!(self, Error::Unauthorized);
}
admin.unwrap().require_auth()
}

fn bump(&self, ledgers_to_live: u32) {
Expand Down
23 changes: 10 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,11 @@ impl PriceOracleContract {
// # Panics
//
// Panics if the contract is already initialized, or if the version is invalid
pub fn config(e: Env, admin: Address, config: ConfigData) {
admin.require_auth();
pub fn config(e: Env, config: ConfigData) {
config.admin.require_auth();
if e.is_initialized() {
e.panic_with_error(Error::AlreadyInitialized);
}
if admin != config.admin {
e.panic_with_error(Error::Unauthorized);
}
e.set_admin(&config.admin);
e.set_base_asset(&config.base_asset);
e.set_decimals(config.decimals);
Expand Down Expand Up @@ -323,8 +320,8 @@ impl PriceOracleContract {
// # Panics
//
// Panics if the caller doesn't match admin address, or if the assets are already added
pub fn add_assets(e: Env, admin: Address, assets: Vec<Asset>) {
e.panic_if_not_admin(&admin);
pub fn add_assets(e: Env, assets: Vec<Asset>) {
e.panic_if_not_admin();
Self::__add_assets(&e, assets);
}

Expand All @@ -339,8 +336,8 @@ impl PriceOracleContract {
// # Panics
//
// Panics if the caller doesn't match admin address, or if the period/version is invalid
pub fn set_period(e: Env, admin: Address, period: u64) {
e.panic_if_not_admin(&admin);
pub fn set_period(e: Env, period: u64) {
e.panic_if_not_admin();
e.set_retention_period(period);
}

Expand All @@ -355,8 +352,8 @@ impl PriceOracleContract {
// # Panics
//
// Panics if the caller doesn't match admin address, or if the price snapshot record is invalid
pub fn set_price(e: Env, admin: Address, updates: Vec<i128>, timestamp: u64) {
e.panic_if_not_admin(&admin);
pub fn set_price(e: Env, updates: Vec<i128>, timestamp: u64) {
e.panic_if_not_admin();
let updates_len = updates.len();
if updates_len == 0 || updates_len >= 256 {
panic_with_error!(&e, Error::InvalidUpdateLength);
Expand Down Expand Up @@ -399,8 +396,8 @@ impl PriceOracleContract {
// # Panics
//
// Panics if the caller doesn't match admin address
pub fn update_contract(env: Env, admin: Address, wasm_hash: BytesN<32>) {
env.panic_if_not_admin(&admin);
pub fn update_contract(env: Env, wasm_hash: BytesN<32>) {
env.panic_if_not_admin();
env.deployer().update_current_contract_wasm(wasm_hash)
}

Expand Down
Loading

0 comments on commit 22571f4

Please sign in to comment.