Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stake: Hotfix/stake new query #388

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
556ef56
Stake: Implement way smaller staking approach
ueco-jb Aug 20, 2024
97c2399
Stake: WIP tests
ueco-jb Aug 21, 2024
dbef917
Factory: Make storage changes backward compatible
ueco-jb Aug 21, 2024
6f98cbb
Factory: Add stable pool to the upgrade path
ueco-jb Aug 21, 2024
2a7e97d
Remove stake_rewards from arguments in factory, pool and stake
ueco-jb Aug 21, 2024
6cfdaf2
Pool: Refactor storage changes to be backward compatible
ueco-jb Aug 21, 2024
293db68
Pool: Include default slippage bps in the upgrade call
ueco-jb Aug 21, 2024
3e24366
Pool: Adjust tests for the removed parameter
ueco-jb Aug 21, 2024
2b05e7b
Factory: Update the factory tests to the removed config parameter
ueco-jb Aug 21, 2024
2bc9d11
Stake: Change back the storage types to persistent
ueco-jb Aug 21, 2024
4c31836
wip
gangov Aug 22, 2024
2c26a0d
comments a lot of tests, working upgrade test
gangov Aug 22, 2024
0658390
test update
gangov Aug 22, 2024
f3d9daf
fixes the failing test
gangov Aug 22, 2024
d9edeed
Stake: Remove comments from the entire test files
ueco-jb Aug 28, 2024
cbd5c38
Merge pull request #387 from Phoenix-Protocol-Group/stake-every-day-d…
ueco-jb Aug 28, 2024
79e1ead
Stake: Fix storage issues; add tests for pending stake rewards
ueco-jb Aug 28, 2024
97edccd
Stake: Fix calculate_pending_rewards
ueco-jb Sep 2, 2024
0fd33da
Stake: Adjust first distribution test
ueco-jb Sep 2, 2024
8d54e22
Stake: Adjusting more distribution tests
ueco-jb Sep 2, 2024
bdd63e5
Stake: Add tests for a consecutive reward distribution
ueco-jb Sep 2, 2024
cff3cc4
Stake: Fix clippy lints
ueco-jb Sep 2, 2024
1909836
Stake: Use user's actual stake instead of the total staked amount
ueco-jb Sep 3, 2024
1dcbd15
Stake: Add missing imports to the upgrade test
ueco-jb Sep 3, 2024
89bc61d
Stake: WIP
ueco-jb Sep 3, 2024
53f266b
WIP
ueco-jb Sep 3, 2024
722f60e
Stake: Fix migration issue
ueco-jb Sep 3, 2024
53ffd1a
wip
ueco-jb Sep 3, 2024
0d0e810
wip
ueco-jb Sep 3, 2024
57b8441
Stake: Latest changes
ueco-jb Sep 12, 2024
bc01344
Stake: Remove automatic rewards withdrawal during an unbond
ueco-jb Sep 20, 2024
e7dd285
wip
gangov Sep 20, 2024
619936a
migration query
gangov Sep 20, 2024
87f4b46
Stake: Actual migration
ueco-jb Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading