Skip to content

Commit 3023c5c

Browse files
test: add another balances delay flow
1 parent dbfa1a6 commit 3023c5c

File tree

3 files changed

+194
-5
lines changed

3 files changed

+194
-5
lines changed

src/flow_test/flow_ideas.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ more ideas:
3434
- staker enter in V2, advance epoch, advance epoch, upgrade to V3, attest, update balance,advance epoch, attest, advance epoch, attest, test rewards
3535
- staker enter in V2, advance epoch, update balance, upgrade to V3, attest, advance epoch, attest, advance epoch, attest, test rewards
3636
- staker enter in V2, advance epoch, upgrade to V3, advance epoch, attest,
37-
- staker in V2, update balance staker+update balance pool, upgrade, attest in current epoch, attest in next epoch, attest in next next epoch
3837
- 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
3938

4039
## pool member balance at curr epoch migration

src/flow_test/flows.cairo

Lines changed: 177 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8716,6 +8716,179 @@ pub(crate) impl MemberChangeBalanceClaimRewardsOneRewardsFlowImpl of MultiVersio
87168716
}
87178717
}
87188718

8719+
/// Flow:
8720+
/// Epoch 0:
8721+
/// Staker stake
8722+
/// Epoch 1:
8723+
/// Staker increase stake
8724+
/// Delegators delegate STRK and BTC
8725+
/// Upgrade
8726+
/// Test total staking power - according to Epoch 0
8727+
/// Test staker balance with attestation rewards - according to Epoch 0
8728+
/// Epoch 2:
8729+
/// Test delegator balances with attestation rewards - according to Epoch 0
8730+
/// Test total staking power - according to Epoch 1
8731+
/// Test staker balance with attestation rewards - according to Epoch 1
8732+
/// Epoch 3:
8733+
/// Test delegator balances with attestation rewards - according to Epoch 1
8734+
#[derive(Drop, Copy)]
8735+
pub(crate) struct BalancesDelayFlow {
8736+
pub(crate) staker: Option<Staker>,
8737+
pub(crate) stake_amount: Option<Amount>,
8738+
pub(crate) commission: Option<Commission>,
8739+
pub(crate) strk_delegated_amount: Option<Amount>,
8740+
pub(crate) btc_delegated_amount: Option<Amount>,
8741+
pub(crate) strk_delegator: Option<Delegator>,
8742+
pub(crate) btc_delegator: Option<Delegator>,
8743+
pub(crate) strk_pool: Option<ContractAddress>,
8744+
pub(crate) btc_pool: Option<ContractAddress>,
8745+
}
8746+
pub(crate) impl BalancesDelayFlowImpl of FlowTrait<BalancesDelayFlow> {
8747+
fn setup_v2(ref self: BalancesDelayFlow, ref system: SystemState) {
8748+
let stake_amount = system.staking.get_min_stake();
8749+
let strk_delegated_amount = STRK_CONFIG.min_for_rewards;
8750+
let btc_delegated_amount = TEST_MIN_BTC_FOR_REWARDS;
8751+
let staker = system.new_staker(amount: stake_amount * 3);
8752+
let commission = 200;
8753+
let strk_delegator = system.new_delegator(amount: strk_delegated_amount * 2);
8754+
let btc_delegator = system
8755+
.new_btc_delegator(amount: btc_delegated_amount * 2, token: system.btc_token);
8756+
8757+
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
8758+
let strk_pool = system.staking.get_pool(:staker);
8759+
let btc_pool = system
8760+
.set_open_for_delegation(:staker, token_address: system.btc_token.contract_address());
8761+
8762+
system.advance_epoch(); // Epoch 0 - > 1
8763+
system.increase_stake(:staker, amount: stake_amount);
8764+
system.delegate(delegator: strk_delegator, pool: strk_pool, amount: strk_delegated_amount);
8765+
system
8766+
.delegate_btc(
8767+
delegator: btc_delegator,
8768+
pool: btc_pool,
8769+
amount: btc_delegated_amount,
8770+
token: system.btc_token,
8771+
);
8772+
8773+
system.set_staker_for_migration(staker_address: staker.staker.address);
8774+
self.staker = Option::Some(staker);
8775+
self.stake_amount = Option::Some(stake_amount);
8776+
self.commission = Option::Some(commission);
8777+
self.strk_delegated_amount = Option::Some(strk_delegated_amount);
8778+
self.btc_delegated_amount = Option::Some(btc_delegated_amount);
8779+
self.strk_delegator = Option::Some(strk_delegator);
8780+
self.btc_delegator = Option::Some(btc_delegator);
8781+
self.strk_pool = Option::Some(strk_pool);
8782+
self.btc_pool = Option::Some(btc_pool);
8783+
}
8784+
8785+
#[feature("safe_dispatcher")]
8786+
fn test(self: BalancesDelayFlow, ref system: SystemState) {
8787+
let staker = self.staker.unwrap();
8788+
let stake_amount = self.stake_amount.unwrap();
8789+
let commission = self.commission.unwrap();
8790+
let strk_delegated_amount = self.strk_delegated_amount.unwrap();
8791+
let btc_delegated_amount = self.btc_delegated_amount.unwrap();
8792+
let strk_delegator = self.strk_delegator.unwrap();
8793+
let btc_delegator = self.btc_delegator.unwrap();
8794+
let strk_pool = self.strk_pool.unwrap();
8795+
let btc_pool = self.btc_pool.unwrap();
8796+
8797+
let staking_contract = system.staking.address;
8798+
let minting_curve_contract = system.minting_curve.address;
8799+
8800+
// Test total staking power - according to Epoch 0.
8801+
let mut staker_stake = stake_amount;
8802+
let total_staking_power = system.staking.get_current_total_staking_power_v2();
8803+
let expected_total_staking_power = (
8804+
NormalizedAmountTrait::from_strk_native_amount(staker_stake), Zero::zero(),
8805+
);
8806+
assert!(total_staking_power == expected_total_staking_power);
8807+
8808+
// Calculate expected rewards - according to Epoch 0.
8809+
let (expected_staker_rewards, _) = calculate_staker_strk_rewards_with_balances_v2(
8810+
amount_own: staker_stake,
8811+
pool_amount: Zero::zero(),
8812+
:commission,
8813+
:staking_contract,
8814+
:minting_curve_contract,
8815+
);
8816+
assert!(expected_staker_rewards.is_non_zero());
8817+
8818+
// Test staker balance with attestation rewards - according to Epoch 0.
8819+
system
8820+
.advance_block_into_attestation_window_custom_stake(
8821+
staker_address: staker.staker.address, stake: staker_stake,
8822+
);
8823+
system.attest(:staker);
8824+
let staker_rewards = system.staker_claim_rewards(:staker);
8825+
assert!(staker_rewards == expected_staker_rewards);
8826+
8827+
// Advance epoch - test delegator balances with attestation rewards - according to Epoch 0.
8828+
system.advance_epoch(); // Epoch 1 - > 2
8829+
let strk_delegator_rewards = system
8830+
.delegator_claim_rewards(delegator: strk_delegator, pool: strk_pool);
8831+
let btc_delegator_rewards = system
8832+
.delegator_claim_rewards(delegator: btc_delegator, pool: btc_pool);
8833+
assert!(strk_delegator_rewards == Zero::zero());
8834+
assert!(btc_delegator_rewards == Zero::zero());
8835+
8836+
// Update variables for Epoch 1.
8837+
staker_stake += stake_amount;
8838+
let mut strk_pool_balance = strk_delegated_amount;
8839+
let mut btc_pool_balance = btc_delegated_amount;
8840+
8841+
// Test total staking power - according to Epoch 1.
8842+
let total_staking_power = system.staking.get_current_total_staking_power_v2();
8843+
let expected_total_staking_power = (
8844+
NormalizedAmountTrait::from_strk_native_amount(staker_stake + strk_pool_balance),
8845+
NormalizedAmountTrait::from_native_amount(btc_pool_balance, TEST_BTC_DECIMALS),
8846+
);
8847+
assert!(total_staking_power == expected_total_staking_power);
8848+
8849+
// Calculate expected rewards - according to Epoch 1.
8850+
let prev_staker_rewards = expected_staker_rewards;
8851+
let (expected_staker_rewards, expected_strk_pool_rewards) =
8852+
calculate_staker_strk_rewards_with_balances_v2(
8853+
amount_own: staker_stake,
8854+
pool_amount: strk_pool_balance,
8855+
:commission,
8856+
:staking_contract,
8857+
:minting_curve_contract,
8858+
);
8859+
assert!(expected_staker_rewards != prev_staker_rewards);
8860+
assert!(expected_strk_pool_rewards.is_non_zero());
8861+
let (expected_btc_commission_rewards, expected_btc_pool_rewards) =
8862+
calculate_staker_btc_pool_rewards_v2(
8863+
pool_balance: btc_pool_balance,
8864+
:commission,
8865+
:staking_contract,
8866+
:minting_curve_contract,
8867+
token_address: system.btc_token.contract_address(),
8868+
);
8869+
assert!(expected_btc_commission_rewards.is_non_zero());
8870+
assert!(expected_btc_pool_rewards.is_non_zero());
8871+
8872+
// Test staker balance with attestation rewards - according to Epoch 1.
8873+
system
8874+
.advance_block_into_attestation_window_custom_stake(
8875+
staker_address: staker.staker.address, stake: staker_stake + strk_pool_balance,
8876+
);
8877+
system.attest(:staker);
8878+
let staker_rewards = system.staker_claim_rewards(:staker);
8879+
assert!(staker_rewards == expected_staker_rewards + expected_btc_commission_rewards);
8880+
8881+
// Advance epoch - test delegator balances with attestation rewards - according to Epoch 1.
8882+
system.advance_epoch(); // Epoch 2 - > 3
8883+
let strk_delegator_rewards = system
8884+
.delegator_claim_rewards(delegator: strk_delegator, pool: strk_pool);
8885+
let btc_delegator_rewards = system
8886+
.delegator_claim_rewards(delegator: btc_delegator, pool: btc_pool);
8887+
assert!(strk_delegator_rewards == expected_strk_pool_rewards);
8888+
assert!(btc_delegator_rewards == expected_btc_pool_rewards);
8889+
}
8890+
}
8891+
87198892
/// Flow:
87208893
/// Epoch 0:
87218894
/// Staker stake
@@ -8738,7 +8911,7 @@ pub(crate) impl MemberChangeBalanceClaimRewardsOneRewardsFlowImpl of MultiVersio
87388911
/// Epoch 4:
87398912
/// Test delegator balances with attestation rewards - according to Epoch 1 (post-upgrade)
87408913
#[derive(Drop, Copy)]
8741-
pub(crate) struct BalancesDelayFlow {
8914+
pub(crate) struct BalancesDelayBalanceChangeFlow {
87428915
pub(crate) staker: Option<Staker>,
87438916
pub(crate) stake_amount: Option<Amount>,
87448917
pub(crate) commission: Option<Commission>,
@@ -8749,8 +8922,8 @@ pub(crate) struct BalancesDelayFlow {
87498922
pub(crate) strk_pool: Option<ContractAddress>,
87508923
pub(crate) btc_pool: Option<ContractAddress>,
87518924
}
8752-
pub(crate) impl BalancesDelayFlowImpl of FlowTrait<BalancesDelayFlow> {
8753-
fn setup_v2(ref self: BalancesDelayFlow, ref system: SystemState) {
8925+
pub(crate) impl BalancesDelayBalanceChangeFlowImpl of FlowTrait<BalancesDelayBalanceChangeFlow> {
8926+
fn setup_v2(ref self: BalancesDelayBalanceChangeFlow, ref system: SystemState) {
87548927
let stake_amount = system.staking.get_min_stake();
87558928
let strk_delegated_amount = STRK_CONFIG.min_for_rewards;
87568929
let btc_delegated_amount = TEST_MIN_BTC_FOR_REWARDS;
@@ -8789,7 +8962,7 @@ pub(crate) impl BalancesDelayFlowImpl of FlowTrait<BalancesDelayFlow> {
87898962
}
87908963

87918964
#[feature("safe_dispatcher")]
8792-
fn test(self: BalancesDelayFlow, ref system: SystemState) {
8965+
fn test(self: BalancesDelayBalanceChangeFlow, ref system: SystemState) {
87938966
let staker = self.staker.unwrap();
87948967
let stake_amount = self.stake_amount.unwrap();
87958968
let commission = self.commission.unwrap();

src/flow_test/fork_test.cairo

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,23 @@ fn delegator_rewards_v0_find_sigma_flow_test() {
612612
test_flow_mainnet(ref :flow);
613613
}
614614

615+
#[test]
616+
#[fork("MAINNET_LATEST")]
617+
fn balances_delay_balance_change_flow_test() {
618+
let mut flow = flows::BalancesDelayBalanceChangeFlow {
619+
staker: Option::None,
620+
stake_amount: Option::None,
621+
commission: Option::None,
622+
strk_delegated_amount: Option::None,
623+
btc_delegated_amount: Option::None,
624+
strk_delegator: Option::None,
625+
btc_delegator: Option::None,
626+
strk_pool: Option::None,
627+
btc_pool: Option::None,
628+
};
629+
test_flow_mainnet(ref :flow);
630+
}
631+
615632
#[test]
616633
#[fork("MAINNET_LATEST")]
617634
fn enable_disable_token_before_after_upgrade_flow_test() {

0 commit comments

Comments
 (0)