Skip to content

Commit

Permalink
Merge pull request #385 from Phoenix-Protocol-Group/stake-every-day-d…
Browse files Browse the repository at this point in the history
…istribution

Stake every day distribution - broken merge
  • Loading branch information
ueco-jb authored Oct 8, 2024
2 parents c1ef57f + bc01344 commit 4a0b6e6
Show file tree
Hide file tree
Showing 22 changed files with 1,329 additions and 1,530 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

19 changes: 8 additions & 11 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
error::ContractError,
storage::{
get_config, get_lp_vec, is_initialized, save_config, save_lp_vec,
save_lp_vec_with_tuple_as_key, set_initialized, Asset, Config, LiquidityPoolInfo,
LpPortfolio, PairTupleKey, StakePortfolio, StakedResponse, UserPortfolio,
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, StakedResponse,
UserPortfolio,
},
utils::{deploy_and_initialize_multihop_contract, deploy_lp_contract},
};
Expand Down Expand Up @@ -33,7 +34,6 @@ pub trait FactoryTrait {
lp_wasm_hash: BytesN<32>,
stable_wasm_hash: BytesN<32>,
stake_wasm_hash: BytesN<32>,
stake_rewards_wasm_hash: BytesN<32>,
token_wasm_hash: BytesN<32>,
whitelisted_accounts: Vec<Address>,
lp_token_decimals: u32,
Expand Down Expand Up @@ -91,7 +91,6 @@ impl FactoryTrait for Factory {
lp_wasm_hash: BytesN<32>,
stable_wasm_hash: BytesN<32>,
stake_wasm_hash: BytesN<32>,
stake_rewards_wasm_hash: BytesN<32>,
token_wasm_hash: BytesN<32>,
whitelisted_accounts: Vec<Address>,
lp_token_decimals: u32,
Expand Down Expand Up @@ -120,14 +119,13 @@ impl FactoryTrait for Factory {
admin: admin.clone(),
multihop_address,
lp_wasm_hash,
stable_wasm_hash,
stake_wasm_hash,
stake_rewards_wasm_hash,
token_wasm_hash,
whitelisted_accounts,
lp_token_decimals,
},
);
save_stable_wasm_hash(&env, stable_wasm_hash);

save_lp_vec(&env, Vec::new(&env));

Expand Down Expand Up @@ -167,11 +165,10 @@ impl FactoryTrait for Factory {
let config = get_config(&env);
let stake_wasm_hash = config.stake_wasm_hash;
let token_wasm_hash = config.token_wasm_hash;
let stake_rewards_wasm_hash = config.stake_rewards_wasm_hash;

let pool_hash = match pool_type {
PoolType::Xyk => config.lp_wasm_hash,
PoolType::Stable => config.stable_wasm_hash,
PoolType::Stable => get_stable_wasm_hash(&env),
};

let lp_contract_address = deploy_lp_contract(
Expand All @@ -195,7 +192,6 @@ impl FactoryTrait for Factory {
let mut init_fn_args: Vec<Val> = (
stake_wasm_hash,
token_wasm_hash,
stake_rewards_wasm_hash,
lp_init_info.clone(),
factory_addr,
config.lp_token_decimals,
Expand Down Expand Up @@ -464,11 +460,12 @@ impl FactoryTrait for Factory {
#[contractimpl]
impl Factory {
#[allow(dead_code)]
pub fn update(env: Env, new_wasm_hash: BytesN<32>) {
pub fn update(env: Env, new_wasm_hash: BytesN<32>, new_stable_pool_hash: BytesN<32>) {
let admin = get_config(&env).admin;
admin.require_auth();

env.deployer().update_current_contract_wasm(new_wasm_hash);
save_stable_wasm_hash(&env, new_stable_pool_hash);
}
}

Expand Down
33 changes: 30 additions & 3 deletions contracts/factory/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use phoenix::ttl::{PERSISTENT_BUMP_AMOUNT, PERSISTENT_LIFETIME_THRESHOLD};
use soroban_sdk::{contracttype, Address, BytesN, ConversionError, Env, TryFromVal, Val, Vec};
use soroban_sdk::{
contracttype, symbol_short, Address, BytesN, ConversionError, Env, Symbol, TryFromVal, Val, Vec,
};

#[derive(Clone, Copy)]
#[repr(u32)]
Expand Down Expand Up @@ -30,14 +32,39 @@ pub struct Config {
pub admin: Address,
pub multihop_address: Address,
pub lp_wasm_hash: BytesN<32>,
pub stable_wasm_hash: BytesN<32>,
pub stake_wasm_hash: BytesN<32>,
pub token_wasm_hash: BytesN<32>,
pub stake_rewards_wasm_hash: BytesN<32>,
pub whitelisted_accounts: Vec<Address>,
pub lp_token_decimals: u32,
}

const STABLE_WASM_HASH: Symbol = symbol_short!("stabwasm");

pub fn save_stable_wasm_hash(env: &Env, hash: BytesN<32>) {
env.storage().persistent().set(&STABLE_WASM_HASH, &hash);
env.storage().persistent().extend_ttl(
&STABLE_WASM_HASH,
PERSISTENT_LIFETIME_THRESHOLD,
PERSISTENT_BUMP_AMOUNT,
)
}

pub fn get_stable_wasm_hash(env: &Env) -> BytesN<32> {
let hash = env
.storage()
.persistent()
.get(&STABLE_WASM_HASH)
.expect("Stable wasm hash not set");

env.storage().persistent().extend_ttl(
&STABLE_WASM_HASH,
PERSISTENT_LIFETIME_THRESHOLD,
PERSISTENT_BUMP_AMOUNT,
);

hash
}

#[contracttype]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UserPortfolio {
Expand Down
4 changes: 0 additions & 4 deletions contracts/factory/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use setup::install_stake_rewards_wasm;
use soroban_sdk::{testutils::Address as _, vec, Address, Env};

use self::setup::{
Expand All @@ -24,7 +23,6 @@ fn test_deploy_factory_twice_should_fail() {
let lp_wasm_hash = install_lp_contract(&env);
let stable_wasm_hash = install_stable_lp(&env);
let stake_wasm_hash = install_stake_wasm(&env);
let stake_rewards_wasm_hash = install_stake_rewards_wasm(&env);
let token_wasm_hash = install_token_wasm(&env);

let factory = deploy_factory_contract(&env, admin.clone());
Expand All @@ -35,7 +33,6 @@ fn test_deploy_factory_twice_should_fail() {
&lp_wasm_hash,
&stable_wasm_hash,
&stake_wasm_hash,
&stake_rewards_wasm_hash,
&token_wasm_hash,
&vec![&env, auth_user.clone()],
&10u32,
Expand All @@ -46,7 +43,6 @@ fn test_deploy_factory_twice_should_fail() {
&lp_wasm_hash,
&stable_wasm_hash,
&stake_wasm_hash,
&stake_rewards_wasm_hash,
&token_wasm_hash,
&vec![&env, auth_user.clone()],
&10u32,
Expand Down
6 changes: 1 addition & 5 deletions contracts/factory/src/tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::setup::{
deploy_factory_contract, install_lp_contract, install_multihop_wasm, install_stable_lp,
install_stake_rewards_wasm, install_stake_wasm, install_token_wasm, lp_contract,
install_stake_wasm, install_token_wasm, lp_contract,
};
use crate::{
contract::{Factory, FactoryClient},
Expand Down Expand Up @@ -98,7 +98,6 @@ fn factory_successfully_inits_lp() {
token_a: token1,
token_b: token2,
total_fee_bps: 0,
default_slippage_bps: 100,
}
);
}
Expand Down Expand Up @@ -242,7 +241,6 @@ fn factory_fails_to_init_lp_when_no_whitelisted_accounts() {
let lp_wasm_hash = install_lp_contract(&env);
let stable_wasm_hash = install_stable_lp(&env);
let stake_wasm_hash = install_stake_wasm(&env);
let stake_rewards_wasm_hash = install_stake_rewards_wasm(&env);
let token_wasm_hash = install_token_wasm(&env);

factory.initialize(
Expand All @@ -251,7 +249,6 @@ fn factory_fails_to_init_lp_when_no_whitelisted_accounts() {
&lp_wasm_hash,
&stable_wasm_hash,
&stake_wasm_hash,
&stake_rewards_wasm_hash,
&token_wasm_hash,
&whitelisted_accounts,
&10u32,
Expand Down Expand Up @@ -494,7 +491,6 @@ fn factory_create_xyk_pool_with_amp_parameter_should_still_succeed() {
token_a: token1.address,
token_b: token2.address,
total_fee_bps: 0,
default_slippage_bps: 100,
}
);
}
1 change: 0 additions & 1 deletion contracts/factory/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub struct LiquidityPoolConfig {
/// The maximum amount of spread (in bps) that is tolerated during swap
pub max_allowed_spread_bps: i64,
pub max_referral_bps: i64,
pub default_slippage_bps: i64,
}

#[test]
Expand Down
9 changes: 0 additions & 9 deletions contracts/factory/src/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ pub fn install_stake_wasm(env: &Env) -> BytesN<32> {
env.deployer().upload_contract_wasm(WASM)
}

pub fn install_stake_rewards_wasm(env: &Env) -> BytesN<32> {
soroban_sdk::contractimport!(
file = "../../target/wasm32-unknown-unknown/release/phoenix_stake_rewards.wasm"
);
env.deployer().upload_contract_wasm(WASM)
}

pub fn deploy_factory_contract<'a>(
env: &Env,
admin: impl Into<Option<Address>>,
Expand All @@ -74,7 +67,6 @@ pub fn deploy_factory_contract<'a>(
let lp_wasm_hash = install_lp_contract(env);
let stable_wasm_hash = install_stable_lp(env);
let stake_wasm_hash = install_stake_wasm(env);
let stake_rewards_wasm_hash = install_stake_rewards_wasm(env);
let token_wasm_hash = install_token_wasm(env);

factory.initialize(
Expand All @@ -83,7 +75,6 @@ pub fn deploy_factory_contract<'a>(
&lp_wasm_hash,
&stable_wasm_hash,
&stake_wasm_hash,
&stake_rewards_wasm_hash,
&token_wasm_hash,
&whitelisted_accounts,
&10u32,
Expand Down
9 changes: 0 additions & 9 deletions contracts/multihop/src/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ pub fn install_stake_wasm(env: &Env) -> BytesN<32> {
env.deployer().upload_contract_wasm(WASM)
}

pub fn install_stake_rewards_wasm(env: &Env) -> BytesN<32> {
soroban_sdk::contractimport!(
file = "../../target/wasm32-unknown-unknown/release/phoenix_stake_rewards.wasm"
);
env.deployer().upload_contract_wasm(WASM)
}

#[allow(clippy::too_many_arguments)]
pub fn install_multihop_wasm(env: &Env) -> BytesN<32> {
soroban_sdk::contractimport!(
Expand Down Expand Up @@ -105,7 +98,6 @@ pub fn deploy_and_initialize_factory(env: &Env, admin: Address) -> factory_contr
let lp_wasm_hash = install_lp_contract(env);
let stable_wasm_hash = install_stable_lp_contract(env);
let stake_wasm_hash = install_stake_wasm(env);
let stake_rewards_wasm_hash = install_stake_rewards_wasm(env);
let token_wasm_hash = install_token_wasm(env);

factory_client.initialize(
Expand All @@ -114,7 +106,6 @@ pub fn deploy_and_initialize_factory(env: &Env, admin: Address) -> factory_contr
&lp_wasm_hash,
&stable_wasm_hash,
&stake_wasm_hash,
&stake_rewards_wasm_hash,
&token_wasm_hash,
&whitelisted_accounts,
&10u32,
Expand Down
15 changes: 6 additions & 9 deletions contracts/pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
error::ContractError,
stake_contract,
storage::{
get_config, save_config,
get_config, get_default_slippage_bps, save_config, save_default_slippage_bps,
utils::{self, get_admin, is_initialized, set_initialized},
Asset, ComputeSwap, Config, LiquidityPoolInfo, PairType, PoolResponse,
SimulateReverseSwapResponse, SimulateSwapResponse,
Expand Down Expand Up @@ -43,7 +43,6 @@ pub trait LiquidityPoolTrait {
env: Env,
stake_wasm_hash: BytesN<32>,
token_wasm_hash: BytesN<32>,
stake_rewards_wasm_hash: BytesN<32>,
lp_init_info: LiquidityPoolInitInfo,
factory_addr: Address,
share_token_decimals: u32,
Expand Down Expand Up @@ -113,7 +112,7 @@ pub trait LiquidityPoolTrait {
);

// Migration entrypoint
fn upgrade(e: Env, new_wasm_hash: BytesN<32>);
fn upgrade(e: Env, new_wasm_hash: BytesN<32>, new_default_slippage_bps: i64);

// QUERIES

Expand Down Expand Up @@ -153,7 +152,6 @@ impl LiquidityPoolTrait for LiquidityPool {
env: Env,
stake_wasm_hash: BytesN<32>,
token_wasm_hash: BytesN<32>,
stake_rewards_wasm_hash: BytesN<32>,
lp_init_info: LiquidityPoolInitInfo,
factory_addr: Address,
share_token_decimals: u32,
Expand Down Expand Up @@ -234,7 +232,6 @@ impl LiquidityPoolTrait for LiquidityPool {
stake_contract::Client::new(&env, &stake_contract_address).initialize(
&admin,
&share_token_address,
&stake_rewards_wasm_hash,
&min_bond,
&min_reward,
&manager,
Expand All @@ -253,10 +250,10 @@ impl LiquidityPoolTrait for LiquidityPool {
max_allowed_slippage_bps,
max_allowed_spread_bps,
max_referral_bps,
default_slippage_bps,
};

save_config(&env, config);
save_default_slippage_bps(&env, default_slippage_bps);

utils::save_admin(&env, admin);
utils::save_total_shares(&env, 0);
Expand Down Expand Up @@ -322,7 +319,7 @@ impl LiquidityPoolTrait for LiquidityPool {
min_b,
pool_balance_a,
pool_balance_b,
Decimal::bps(custom_slippage_bps.unwrap_or(config.default_slippage_bps)),
Decimal::bps(custom_slippage_bps.unwrap_or(get_default_slippage_bps(&env))),
)
}
// None or invalid amounts are provided
Expand Down Expand Up @@ -571,11 +568,12 @@ impl LiquidityPoolTrait for LiquidityPool {
save_config(&env, config);
}

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

env.deployer().update_current_contract_wasm(new_wasm_hash);
save_default_slippage_bps(&env, new_default_slippage_bps);
}

// Queries
Expand Down Expand Up @@ -1365,7 +1363,6 @@ mod tests {
max_allowed_slippage_bps: 100i64,
max_allowed_spread_bps: 100i64,
max_referral_bps: 1_000i64,
default_slippage_bps: 100i64,
};
split_deposit_based_on_pool_ratio(&env, config, 100, 100, 100, &Address::generate(&env));
}
Expand Down
Loading

0 comments on commit 4a0b6e6

Please sign in to comment.