Skip to content

Commit

Permalink
Merge pull request #400 from Phoenix-Protocol-Group/397-put-admin-und…
Browse files Browse the repository at this point in the history
…er-the-same-key-in-all-of-the-contract

All: adds a new const Symbol for the admin key
  • Loading branch information
ueco-jb authored Dec 19, 2024
2 parents 7c6c2d7 + e4bfe66 commit 00f406b
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 70 deletions.
11 changes: 10 additions & 1 deletion contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
storage::{
get_config, get_lp_vec, get_stable_wasm_hash, is_initialized, save_config, save_lp_vec,
save_lp_vec_with_tuple_as_key, save_stable_wasm_hash, set_initialized, Asset, Config,
LiquidityPoolInfo, LpPortfolio, PairTupleKey, StakePortfolio, UserPortfolio,
LiquidityPoolInfo, LpPortfolio, PairTupleKey, StakePortfolio, UserPortfolio, ADMIN,
},
utils::{deploy_and_initialize_multihop_contract, deploy_lp_contract},
ConvertVec,
Expand Down Expand Up @@ -83,6 +83,8 @@ pub trait FactoryTrait {
fn get_config(env: Env) -> Config;

fn query_user_portfolio(env: Env, sender: Address, staking: bool) -> UserPortfolio;

fn migrate_admin_key(env: Env) -> Result<(), ContractError>;
}

#[contractimpl]
Expand Down Expand Up @@ -489,6 +491,13 @@ impl FactoryTrait for Factory {
stake_portfolio,
}
}

fn migrate_admin_key(env: Env) -> Result<(), ContractError> {
let admin = get_config(&env).admin;
env.storage().instance().set(&ADMIN, &admin);

Ok(())
}
}

#[contractimpl]
Expand Down
1 change: 1 addition & 0 deletions contracts/factory/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pub enum ContractError {
TokenABiggerThanTokenB = 5,
MinStakeInvalid = 6,
MinRewardInvalid = 7,
AdminNotSet = 8,
}
33 changes: 31 additions & 2 deletions contracts/factory/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use phoenix::ttl::{PERSISTENT_BUMP_AMOUNT, PERSISTENT_LIFETIME_THRESHOLD};
use phoenix::ttl::{
INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD, PERSISTENT_BUMP_AMOUNT,
PERSISTENT_LIFETIME_THRESHOLD,
};
use soroban_sdk::{
contracttype, symbol_short, Address, BytesN, ConversionError, Env, Symbol, TryFromVal, Val, Vec,
contracttype, log, panic_with_error, symbol_short, Address, BytesN, ConversionError, Env,
Symbol, TryFromVal, Val, Vec,
};

use crate::error::ContractError;

pub const ADMIN: Symbol = symbol_short!("ADMIN");

#[derive(Clone, Copy)]
#[repr(u32)]
pub enum DataKey {
Expand Down Expand Up @@ -157,6 +165,27 @@ pub fn get_config(env: &Env) -> Config {
config
}

pub fn _save_admin(env: &Env, admin_addr: Address) {
env.storage().instance().set(&ADMIN, &admin_addr);

env.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);
}

pub fn _get_admin(env: &Env) -> Address {
let admin_addr = env.storage().instance().get(&ADMIN).unwrap_or_else(|| {
log!(env, "Factory: Admin not set");
panic_with_error!(&env, ContractError::AdminNotSet)
});

env.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);

admin_addr
}

pub fn get_lp_vec(env: &Env) -> Vec<Address> {
let lp_vec = env
.storage()
Expand Down
17 changes: 13 additions & 4 deletions contracts/multihop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::factory_contract::PoolType;
// FIXM: Disable Referral struct
// use crate::lp_contract::Referral;
use crate::storage::{
get_admin, get_factory, is_initialized, save_admin, save_factory, set_initialized,
SimulateReverseSwapResponse, SimulateSwapResponse, Swap,
get_admin_old, get_factory, is_initialized, save_admin_old, save_factory, set_initialized,
SimulateReverseSwapResponse, SimulateSwapResponse, Swap, ADMIN,
};
use crate::utils::{verify_reverse_swap, verify_swap};
use crate::{factory_contract, stable_pool, token_contract, xyk_pool};
Expand Down Expand Up @@ -54,6 +54,8 @@ pub trait MultihopTrait {
amount: i128,
pool_type: PoolType,
) -> SimulateReverseSwapResponse;

fn migrate_admin_key(env: Env) -> Result<(), ContractError>;
}

#[contractimpl]
Expand All @@ -69,7 +71,7 @@ impl MultihopTrait for Multihop {

set_initialized(&env);

save_admin(&env, &admin);
save_admin_old(&env, &admin);

save_factory(&env, factory);

Expand Down Expand Up @@ -285,13 +287,20 @@ impl MultihopTrait for Multihop {

simulate_swap_response
}

fn migrate_admin_key(env: Env) -> Result<(), ContractError> {
let admin = get_admin_old(&env);
env.storage().instance().set(&ADMIN, &admin);

Ok(())
}
}

#[contractimpl]
impl Multihop {
#[allow(dead_code)]
pub fn update(env: Env, new_wasm_hash: BytesN<32>) {
let admin = get_admin(&env);
let admin = get_admin_old(&env);
admin.require_auth();

env.deployer().update_current_contract_wasm(new_wasm_hash);
Expand Down
36 changes: 32 additions & 4 deletions contracts/multihop/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use phoenix::ttl::{PERSISTENT_BUMP_AMOUNT, PERSISTENT_LIFETIME_THRESHOLD};
use soroban_sdk::{contracttype, log, panic_with_error, Address, Env, String, Vec};
use phoenix::ttl::{
INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD, PERSISTENT_BUMP_AMOUNT,
PERSISTENT_LIFETIME_THRESHOLD,
};
use soroban_sdk::{
contracttype, log, panic_with_error, symbol_short, Address, Env, String, Symbol, Vec,
};

use crate::error::ContractError;

pub const ADMIN: Symbol = symbol_short!("ADMIN");

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Swap {
Expand Down Expand Up @@ -87,14 +94,21 @@ pub fn get_factory(env: &Env) -> Address {
address
}

pub fn save_admin(env: &Env, admin: &Address) {
pub fn save_admin_old(env: &Env, admin: &Address) {
env.storage().instance().set(&DataKey::Admin, admin);
env.storage()
.instance()
.extend_ttl(PERSISTENT_LIFETIME_THRESHOLD, PERSISTENT_BUMP_AMOUNT);
}

pub fn get_admin(env: &Env) -> Address {
pub fn _save_admin(env: &Env, admin: &Address) {
env.storage().instance().set(&ADMIN, admin);
env.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);
}

pub fn get_admin_old(env: &Env) -> Address {
let address = env
.storage()
.instance()
Expand All @@ -110,6 +124,20 @@ pub fn get_admin(env: &Env) -> Address {

address
}

pub fn _get_admin(env: &Env) -> Address {
let admin = env.storage().instance().get(&ADMIN).unwrap_or_else(|| {
log!(env, "Multihop: Admin not set");
panic_with_error!(&env, ContractError::AdminNotSet)
});

env.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);

admin
}

pub fn is_initialized(e: &Env) -> bool {
e.storage()
.persistent()
Expand Down
22 changes: 15 additions & 7 deletions contracts/pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::{
stake_contract,
storage::{
get_config, get_default_slippage_bps, save_config, save_default_slippage_bps,
utils::{self, get_admin, is_initialized, set_initialized},
utils::{self, get_admin_old, is_initialized, set_initialized},
Asset, ComputeSwap, Config, LiquidityPoolInfo, PairType, PoolResponse,
SimulateReverseSwapResponse, SimulateSwapResponse,
SimulateReverseSwapResponse, SimulateSwapResponse, ADMIN,
},
token_contract,
};
Expand Down Expand Up @@ -143,6 +143,8 @@ pub trait LiquidityPoolTrait {
fn query_share(env: Env, amount: i128) -> (Asset, Asset);

fn query_total_issued_lp(env: Env) -> i128;

fn migrate_admin_key(env: Env) -> Result<(), ContractError>;
}

#[contractimpl]
Expand Down Expand Up @@ -253,7 +255,7 @@ impl LiquidityPoolTrait for LiquidityPool {
save_config(&env, config);
save_default_slippage_bps(&env, default_slippage_bps);

utils::save_admin(&env, admin);
utils::save_admin_old(&env, admin);
utils::save_total_shares(&env, 0);
utils::save_pool_balance_a(&env, 0);
utils::save_pool_balance_b(&env, 0);
Expand Down Expand Up @@ -546,7 +548,7 @@ impl LiquidityPoolTrait for LiquidityPool {
max_allowed_spread_bps: Option<i64>,
max_referral_bps: Option<i64>,
) {
let admin: Address = utils::get_admin(&env);
let admin: Address = utils::get_admin_old(&env);
admin.require_auth();
env.storage()
.instance()
Expand All @@ -555,7 +557,7 @@ impl LiquidityPoolTrait for LiquidityPool {
let mut config = get_config(&env);

if let Some(new_admin) = new_admin {
utils::save_admin(&env, new_admin);
utils::save_admin_old(&env, new_admin);
}
if let Some(total_fee_bps) = total_fee_bps {
validate_bps!(total_fee_bps);
Expand All @@ -581,7 +583,7 @@ impl LiquidityPoolTrait for LiquidityPool {
}

fn upgrade(env: Env, new_wasm_hash: BytesN<32>, new_default_slippage_bps: i64) {
let admin: Address = utils::get_admin(&env);
let admin: Address = utils::get_admin_old(&env);
admin.require_auth();

env.deployer().update_current_contract_wasm(new_wasm_hash);
Expand Down Expand Up @@ -772,13 +774,19 @@ impl LiquidityPoolTrait for LiquidityPool {
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);
utils::get_total_shares(&env)
}

fn migrate_admin_key(env: Env) -> Result<(), ContractError> {
let admin = get_admin_old(&env);
env.storage().instance().set(&ADMIN, &admin);
Ok(())
}
}

#[contractimpl]
impl LiquidityPool {
#[allow(dead_code)]
pub fn update(env: Env, new_wasm_hash: BytesN<32>) {
let admin = get_admin(&env);
let admin = get_admin_old(&env);
admin.require_auth();

env.deployer().update_current_contract_wasm(new_wasm_hash);
Expand Down
1 change: 1 addition & 0 deletions contracts/pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ pub enum ContractError {
SwapFeeBpsOverLimit = 25,
NotEnoughSharesToBeMinted = 26,
NotEnoughLiquidityProvided = 27,
AdminNotSet = 28,
}
27 changes: 24 additions & 3 deletions contracts/pool/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use soroban_sdk::{
use crate::{error::ContractError, token_contract};
use soroban_decimal::Decimal;

pub const ADMIN: Symbol = symbol_short!("ADMIN");

#[derive(Clone, Copy)]
#[repr(u32)]
pub enum DataKey {
Expand Down Expand Up @@ -180,6 +182,7 @@ pub struct SimulateReverseSwapResponse {
}

pub mod utils {
use phoenix::ttl::{INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD};
use soroban_sdk::String;

use super::*;
Expand Down Expand Up @@ -213,7 +216,7 @@ pub mod utils {
.deploy_v2(stake_wasm_hash, ())
}

pub fn save_admin(e: &Env, address: Address) {
pub fn save_admin_old(e: &Env, address: Address) {
e.storage().persistent().set(&DataKey::Admin, &address);
e.storage().persistent().extend_ttl(
&DataKey::Admin,
Expand All @@ -222,6 +225,13 @@ pub mod utils {
);
}

pub fn _save_admin(e: &Env, address: Address) {
e.storage().instance().set(&ADMIN, &address);
e.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);
}

pub fn save_total_shares(e: &Env, amount: i128) {
e.storage().persistent().set(&DataKey::TotalShares, &amount);
e.storage().persistent().extend_ttl(
Expand Down Expand Up @@ -266,7 +276,7 @@ pub mod utils {
}

// queries
pub fn get_admin(e: &Env) -> Address {
pub fn get_admin_old(e: &Env) -> Address {
let admin = e.storage().persistent().get(&DataKey::Admin).unwrap();
e.storage().persistent().extend_ttl(
&DataKey::Admin,
Expand All @@ -277,6 +287,17 @@ pub mod utils {
admin
}

pub fn _get_admin(e: &Env) -> Address {
e.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);

e.storage().instance().get(&ADMIN).unwrap_or_else(|| {
log!(e, "XYZ Pool: Admin not set");
panic_with_error!(&e, ContractError::AdminNotSet)
})
}

pub fn get_total_shares(e: &Env) -> i128 {
let total_shares = e.storage().persistent().get(&DataKey::TotalShares).unwrap();
e.storage().persistent().extend_ttl(
Expand Down Expand Up @@ -463,7 +484,7 @@ mod tests {
#[should_panic]
fn test_get_admin_failure() {
let env = Env::default();
let _ = utils::get_admin(&env);
let _ = utils::get_admin_old(&env);
}

#[test]
Expand Down
Loading

0 comments on commit 00f406b

Please sign in to comment.