Skip to content

Commit e4e94f2

Browse files
test: add balances delay flow
1 parent e8b4e7d commit e4e94f2

File tree

3 files changed

+274
-6
lines changed

3 files changed

+274
-6
lines changed

src/flow_test/flow_ideas.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ more ideas:
7070
- staker enter in V1, advance epoch, update balance, upgrade to V3, attest, update balance, advance epoch, attest
7171
- staker enter in V0, advance epoch, update balance, advance epoch, update balance, upgrade to V3, attest
7272
- staker enter in V1, advance epoch, update balance, advance epoch, update balance, upgrade to V3, attest
73-
- staker in V2, update balance staker+update balance pool, upgrade, update balance staker+update balance pool, attest in current epoch, attest in next epoch, attest in next next epoch
7473
- staker in V2, advance epoch, update balance staker+update balance pool, advance epoch, update balance staker+update balance pool, upgrade, update balance staker+update balance pool, attest in current epoch, attest in next epoch, attest in next next epoch
7574

7675
## pool member balance at curr epoch migration

src/flow_test/flows.cairo

Lines changed: 257 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ use staking::test_utils::constants::{
3535
};
3636
use staking::test_utils::{
3737
calculate_pool_member_rewards, calculate_staker_btc_pool_rewards_v2,
38-
calculate_staker_strk_rewards_v2, calculate_strk_pool_rewards_v2,
39-
calculate_strk_pool_rewards_with_pool_balance_v2, compute_rewards_per_unit,
40-
declare_pool_contract, declare_pool_eic_contract, declare_staking_contract,
41-
load_from_simple_map, load_one_felt, strk_pool_update_rewards_v0, to_amount_18_decimals,
42-
upgrade_implementation,
38+
calculate_staker_strk_rewards_v2, calculate_staker_strk_rewards_with_balances_v2,
39+
calculate_strk_pool_rewards_v2, calculate_strk_pool_rewards_with_pool_balance_v2,
40+
compute_rewards_per_unit, declare_pool_contract, declare_pool_eic_contract,
41+
declare_staking_contract, load_from_simple_map, load_one_felt, strk_pool_update_rewards_v0,
42+
to_amount_18_decimals, upgrade_implementation,
4343
};
4444
use staking::types::{Amount, Commission};
4545
use starknet::{ClassHash, ContractAddress, Store};
@@ -7141,3 +7141,255 @@ pub(crate) impl ToggleTokensBeforeAfterUpgradeFlowImpl of FlowTrait<
71417141
assert!(tokens == expected_tokens);
71427142
}
71437143
}
7144+
7145+
/// Flow:
7146+
/// Epoch 0:
7147+
/// Staker stake
7148+
/// Epoch 1:
7149+
/// Staker increase stake
7150+
/// Delegators delegate STRK and BTC
7151+
/// Upgrade
7152+
/// Staker increase stake
7153+
/// Delegators increase delegation STRK and BTC
7154+
/// Test total staking power - according to Epoch 0
7155+
/// Test staker balance with attestation rewards - according to Epoch 0
7156+
/// Epoch 2:
7157+
/// Test delegator balances with attestation rewards - according to Epoch 0
7158+
/// Test total staking power - according to Epoch 1 (pre-upgrade)
7159+
/// Test staker balance with attestation rewards - according to Epoch 1 (pre-upgrade)
7160+
/// Epoch 3:
7161+
/// Test delegator balances with attestation rewards - according to Epoch 1 (pre-upgrade)
7162+
/// Test total staking power - according to Epoch 1 (post-upgrade)
7163+
/// Test staker balance with attestation rewards - according to Epoch 1 (post-upgrade)
7164+
/// Epoch 4:
7165+
/// Test delegator balances with attestation rewards - according to Epoch 1 (post-upgrade)
7166+
#[derive(Drop, Copy)]
7167+
pub(crate) struct BalancesDelayFlow {
7168+
pub(crate) staker: Option<Staker>,
7169+
pub(crate) stake_amount: Option<Amount>,
7170+
pub(crate) commission: Option<Commission>,
7171+
pub(crate) strk_delegated_amount: Option<Amount>,
7172+
pub(crate) btc_delegated_amount: Option<Amount>,
7173+
pub(crate) strk_delegator: Option<Delegator>,
7174+
pub(crate) btc_delegator: Option<Delegator>,
7175+
pub(crate) strk_pool: Option<ContractAddress>,
7176+
pub(crate) btc_pool: Option<ContractAddress>,
7177+
}
7178+
pub(crate) impl BalancesDelayFlowImpl of FlowTrait<BalancesDelayFlow> {
7179+
fn setup_v2(ref self: BalancesDelayFlow, ref system: SystemState) {
7180+
let stake_amount = system.staking.get_min_stake();
7181+
let strk_delegated_amount = STRK_CONFIG.min_for_rewards;
7182+
let btc_delegated_amount = TEST_MIN_BTC_FOR_REWARDS;
7183+
let staker = system.new_staker(amount: stake_amount * 3);
7184+
let commission = 200;
7185+
let strk_delegator = system.new_delegator(amount: strk_delegated_amount * 2);
7186+
let btc_delegator = system
7187+
.new_btc_delegator(amount: btc_delegated_amount * 2, token: system.btc_token);
7188+
7189+
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
7190+
let strk_pool = system.staking.get_pool(:staker);
7191+
let btc_pool = system
7192+
.set_open_for_delegation(:staker, token_address: system.btc_token.contract_address());
7193+
7194+
system.advance_epoch(); // Epoch 0 - > 1
7195+
system.increase_stake(:staker, amount: stake_amount);
7196+
system.delegate(delegator: strk_delegator, pool: strk_pool, amount: strk_delegated_amount);
7197+
system
7198+
.delegate_btc(
7199+
delegator: btc_delegator,
7200+
pool: btc_pool,
7201+
amount: btc_delegated_amount,
7202+
token: system.btc_token,
7203+
);
7204+
7205+
system.set_staker_for_migration(staker_address: staker.staker.address);
7206+
self.staker = Option::Some(staker);
7207+
self.stake_amount = Option::Some(stake_amount);
7208+
self.commission = Option::Some(commission);
7209+
self.strk_delegated_amount = Option::Some(strk_delegated_amount);
7210+
self.btc_delegated_amount = Option::Some(btc_delegated_amount);
7211+
self.strk_delegator = Option::Some(strk_delegator);
7212+
self.btc_delegator = Option::Some(btc_delegator);
7213+
self.strk_pool = Option::Some(strk_pool);
7214+
self.btc_pool = Option::Some(btc_pool);
7215+
}
7216+
7217+
#[feature("safe_dispatcher")]
7218+
fn test(self: BalancesDelayFlow, ref system: SystemState) {
7219+
let staker = self.staker.unwrap();
7220+
let stake_amount = self.stake_amount.unwrap();
7221+
let commission = self.commission.unwrap();
7222+
let strk_delegated_amount = self.strk_delegated_amount.unwrap();
7223+
let btc_delegated_amount = self.btc_delegated_amount.unwrap();
7224+
let strk_delegator = self.strk_delegator.unwrap();
7225+
let btc_delegator = self.btc_delegator.unwrap();
7226+
let strk_pool = self.strk_pool.unwrap();
7227+
let btc_pool = self.btc_pool.unwrap();
7228+
7229+
let staking_contract = system.staking.address;
7230+
let minting_curve_contract = system.minting_curve.address;
7231+
7232+
// Increase stake and delegations.
7233+
system.increase_stake(:staker, amount: stake_amount);
7234+
system
7235+
.increase_delegate(
7236+
delegator: strk_delegator, pool: strk_pool, amount: strk_delegated_amount,
7237+
);
7238+
system
7239+
.increase_delegate_btc(
7240+
delegator: btc_delegator,
7241+
pool: btc_pool,
7242+
amount: btc_delegated_amount,
7243+
token: system.btc_token,
7244+
);
7245+
7246+
// Test total staking power - according to Epoch 0.
7247+
let staker_stake = stake_amount;
7248+
let total_staking_power = system.staking.get_current_total_staking_power_v2();
7249+
let expected_total_staking_power = (
7250+
NormalizedAmountTrait::from_strk_native_amount(staker_stake), Zero::zero(),
7251+
);
7252+
assert!(
7253+
total_staking_power == expected_total_staking_power,
7254+
);
7255+
7256+
// Calculate expected rewards - according to Epoch 0.
7257+
let (expected_staker_rewards, _) =
7258+
calculate_staker_strk_rewards_with_balances_v2(
7259+
amount_own: staker_stake,
7260+
pool_amount: Zero::zero(),
7261+
:commission,
7262+
:staking_contract,
7263+
:minting_curve_contract,
7264+
);
7265+
assert!(expected_staker_rewards.is_non_zero());
7266+
7267+
// Test staker balance with attestation rewards - according to Epoch 0.
7268+
system
7269+
.advance_block_into_attestation_window_custom_stake(
7270+
staker_address: staker.staker.address, stake: staker_stake,
7271+
);
7272+
system.attest(:staker);
7273+
let staker_rewards = system.staker_claim_rewards(:staker);
7274+
assert!(staker_rewards == expected_staker_rewards);
7275+
7276+
// Advance epoch - test delegator balances with attestation rewards - according to Epoch 0.
7277+
system.advance_epoch(); // Epoch 1 - > 2
7278+
let strk_delegator_rewards = system
7279+
.delegator_claim_rewards(delegator: strk_delegator, pool: strk_pool);
7280+
let btc_delegator_rewards = system
7281+
.delegator_claim_rewards(delegator: btc_delegator, pool: btc_pool);
7282+
assert!(strk_delegator_rewards == Zero::zero());
7283+
assert!(btc_delegator_rewards == Zero::zero());
7284+
7285+
// Update variables for Epoch 1 (pre-upgrade).
7286+
let staker_stake = stake_amount * 2;
7287+
let strk_pool_balance = strk_delegated_amount;
7288+
let btc_pool_balance = btc_delegated_amount;
7289+
7290+
// Test total staking power - according to Epoch 1 (pre-upgrade).
7291+
let total_staking_power = system.staking.get_current_total_staking_power_v2();
7292+
let expected_total_staking_power = (
7293+
NormalizedAmountTrait::from_strk_native_amount(
7294+
staker_stake + strk_pool_balance,
7295+
),
7296+
NormalizedAmountTrait::from_native_amount(btc_pool_balance, TEST_BTC_DECIMALS),
7297+
);
7298+
assert!(total_staking_power == expected_total_staking_power);
7299+
7300+
// Calculate expected rewards - according to Epoch 1 (pre-upgrade).
7301+
let (expected_staker_rewards, expected_strk_pool_rewards) =
7302+
calculate_staker_strk_rewards_with_balances_v2(
7303+
amount_own: staker_stake,
7304+
pool_amount: strk_pool_balance,
7305+
:commission,
7306+
:staking_contract,
7307+
:minting_curve_contract,
7308+
);
7309+
assert!(expected_staker_rewards.is_non_zero());
7310+
assert!(expected_strk_pool_rewards.is_non_zero());
7311+
let (expected_btc_commission_rewards, expected_btc_pool_rewards) =
7312+
calculate_staker_btc_pool_rewards_v2(
7313+
pool_balance: btc_pool_balance,
7314+
:commission,
7315+
:staking_contract,
7316+
:minting_curve_contract,
7317+
token_address: system.btc_token.contract_address(),
7318+
);
7319+
assert!(expected_btc_commission_rewards.is_non_zero());
7320+
assert!(expected_btc_pool_rewards.is_non_zero());
7321+
7322+
// Test staker balance with attestation rewards - according to Epoch 1 (pre-upgrade).
7323+
system
7324+
.advance_block_into_attestation_window_custom_stake(
7325+
staker_address: staker.staker.address, stake: staker_stake + strk_pool_balance,
7326+
);
7327+
system.attest(:staker);
7328+
let staker_rewards = system.staker_claim_rewards(:staker);
7329+
assert!(staker_rewards == expected_staker_rewards + expected_btc_commission_rewards);
7330+
7331+
// Advance epoch - test delegator balances with attestation rewards - according to Epoch 1 (pre-upgrade).
7332+
system.advance_epoch(); // Epoch 2 - > 3
7333+
let strk_delegator_rewards = system
7334+
.delegator_claim_rewards(delegator: strk_delegator, pool: strk_pool);
7335+
let btc_delegator_rewards = system
7336+
.delegator_claim_rewards(delegator: btc_delegator, pool: btc_pool);
7337+
assert!(strk_delegator_rewards == expected_strk_pool_rewards);
7338+
assert!(btc_delegator_rewards == expected_btc_pool_rewards);
7339+
7340+
// Update variables for Epoch 1 (post-upgrade).
7341+
let staker_stake = stake_amount * 3;
7342+
let strk_pool_balance = strk_delegated_amount * 2;
7343+
let btc_pool_balance = btc_delegated_amount * 2;
7344+
7345+
// Test total staking power - according to Epoch 1 (post-upgrade).
7346+
let total_staking_power = system.staking.get_current_total_staking_power_v2();
7347+
let expected_total_staking_power = (
7348+
NormalizedAmountTrait::from_strk_native_amount(
7349+
staker_stake + strk_pool_balance,
7350+
),
7351+
NormalizedAmountTrait::from_native_amount(btc_pool_balance, TEST_BTC_DECIMALS),
7352+
);
7353+
assert!(total_staking_power == expected_total_staking_power);
7354+
7355+
// Calculate expected rewards - according to Epoch 1 (post-upgrade).
7356+
let (expected_staker_rewards, expected_strk_pool_rewards) =
7357+
calculate_staker_strk_rewards_with_balances_v2(
7358+
amount_own: staker_stake,
7359+
pool_amount: strk_pool_balance,
7360+
:commission,
7361+
:staking_contract,
7362+
:minting_curve_contract,
7363+
);
7364+
assert!(expected_staker_rewards.is_non_zero());
7365+
assert!(expected_strk_pool_rewards.is_non_zero());
7366+
let (expected_btc_commission_rewards, expected_btc_pool_rewards) =
7367+
calculate_staker_btc_pool_rewards_v2(
7368+
pool_balance: btc_pool_balance,
7369+
:commission,
7370+
:staking_contract,
7371+
:minting_curve_contract,
7372+
token_address: system.btc_token.contract_address(),
7373+
);
7374+
assert!(expected_btc_commission_rewards.is_non_zero());
7375+
assert!(expected_btc_pool_rewards.is_non_zero());
7376+
7377+
// Test staker balance with attestation rewards - according to Epoch 1 (post-upgrade).
7378+
system
7379+
.advance_block_into_attestation_window_custom_stake(
7380+
staker_address: staker.staker.address, stake: staker_stake + strk_pool_balance,
7381+
);
7382+
system.attest(:staker);
7383+
let staker_rewards = system.staker_claim_rewards(:staker);
7384+
assert!(staker_rewards == expected_staker_rewards + expected_btc_commission_rewards);
7385+
7386+
// Advance epoch - test delegator balances with attestation rewards - according to Epoch 1 (post-upgrade).
7387+
system.advance_epoch(); // Epoch 3 - > 4
7388+
let strk_delegator_rewards = system
7389+
.delegator_claim_rewards(delegator: strk_delegator, pool: strk_pool);
7390+
let btc_delegator_rewards = system
7391+
.delegator_claim_rewards(delegator: btc_delegator, pool: btc_pool);
7392+
assert!(strk_delegator_rewards == expected_strk_pool_rewards);
7393+
assert!(btc_delegator_rewards == expected_btc_pool_rewards);
7394+
}
7395+
}

src/flow_test/fork_test.cairo

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,20 @@ fn toggle_tokens_before_after_upgrade_flow_test() {
452452
};
453453
test_flow_mainnet(ref :flow);
454454
}
455+
456+
#[test]
457+
#[fork("MAINNET_LATEST")]
458+
fn balances_delay_flow_test() {
459+
let mut flow = flows::BalancesDelayFlow {
460+
staker: Option::None,
461+
stake_amount: Option::None,
462+
commission: Option::None,
463+
strk_delegated_amount: Option::None,
464+
btc_delegated_amount: Option::None,
465+
strk_delegator: Option::None,
466+
btc_delegator: Option::None,
467+
strk_pool: Option::None,
468+
btc_pool: Option::None,
469+
};
470+
test_flow_mainnet(ref :flow);
471+
}

0 commit comments

Comments
 (0)