Skip to content

Commit d25df14

Browse files
test: delegator_claim_rewards_after_change_balance_flow_test (#75)
1 parent bd39f30 commit d25df14

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

src/flow_test/flow_ideas.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@
1212
- update rewards for 2 different blocks in the same epoch - should be same rewards.
1313
- Change epoch len in blocks - rewards should be changed.
1414
- disable_rewards = true, advance block, disable_rewards = false, advance block, disable_rewards = true, test rewards.
15-
- update rewards for some different blocks in the same epoch, test rewards of pool member.
1615
- staker change balance, attest, change balance, attest, set_v3, change balance, update_rewards, change_balance, update_rewards, test rewards.
1716
- with member from previous versions.
1817

19-
## k=1 -> k=2 balances
20-
- delegator claim rewards when last change is in epoch + 2, then advance epochs and claim again to see no missing rewards
21-
- same as above, also when there is change in epoch + 1
22-
2318
## k=1 -> k=2 Migration Member
2419
- find sigma: Enter V0, change in V1, catch all ifs.
2520
more ideas:

src/flow_test/test.cairo

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use staking::test_utils::constants::{
2424
use staking::test_utils::{
2525
StakingInitConfig, calculate_staker_btc_pool_rewards_v2,
2626
calculate_staker_strk_rewards_with_balances_v2, calculate_staker_strk_rewards_with_balances_v3,
27-
calculate_strk_pool_rewards_with_pool_balance_v2, compute_rewards_per_unit,
27+
calculate_strk_pool_rewards_with_pool_balance_v2,
28+
calculate_strk_pool_rewards_with_pool_balance_v3, compute_rewards_per_unit,
2829
custom_decimals_token, deploy_mock_erc20_decimals_contract,
2930
};
3031
use starkware_utils::errors::{Describable, ErrorDisplay};
@@ -3571,3 +3572,119 @@ fn delegate_claim_after_claim_with_rewards_flow_test() {
35713572
system.advance_epoch();
35723573
assert!(system.delegator_claim_rewards(:delegator, :pool).is_zero());
35733574
}
3575+
3576+
3577+
/// Flow:
3578+
/// Stake + delegate.
3579+
/// Advance K epochs.
3580+
/// Attest.
3581+
/// Advance epoch.
3582+
/// Increase delegate.
3583+
/// Attest.
3584+
/// Delegator claim rewards.
3585+
/// Advance epoch and attest.
3586+
/// Delegator claim rewards.
3587+
/// Advance epoch and attest.
3588+
/// Delegator claim rewards.
3589+
/// Advance epoch and attest.
3590+
/// Delegator claim rewards.
3591+
/// Start consensus rewards.
3592+
/// Increase delegate.
3593+
/// update_rewards.
3594+
/// Advance block and update_rewards.
3595+
/// Delegator claim rewards.
3596+
/// Advance epoch.
3597+
/// Delegator claim rewards.
3598+
/// update_rewards.
3599+
/// Advance block and update_rewards.
3600+
/// Advance epoch.
3601+
/// update_rewards.
3602+
/// Delegator claim rewards.
3603+
/// Advance block and update_rewards.
3604+
/// Advance epoch.
3605+
/// Delegator claim rewards.
3606+
#[test]
3607+
fn delegator_claim_rewards_after_change_balance_flow_test() {
3608+
let cfg: StakingInitConfig = Default::default();
3609+
let mut system = SystemConfigTrait::basic_stake_flow_cfg(:cfg).deploy();
3610+
let min_stake = system.staking.get_min_stake();
3611+
let stake_amount = min_stake * 2;
3612+
let delegation_amount = stake_amount / 2;
3613+
let staker = system.new_staker(amount: stake_amount);
3614+
let commission = 200;
3615+
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
3616+
let pool = system.staking.get_pool(:staker);
3617+
let delegator = system.new_delegator(amount: 3 * delegation_amount);
3618+
system.delegate(:delegator, :pool, amount: delegation_amount);
3619+
system.advance_k_epochs();
3620+
3621+
let mut pool_balance = delegation_amount;
3622+
system.advance_block_custom_and_attest(:staker, stake: stake_amount + pool_balance);
3623+
let expected_rewards = calculate_strk_pool_rewards_with_pool_balance_v2(
3624+
staker_address: staker.staker.address,
3625+
staking_contract: system.staking.address,
3626+
minting_curve_contract: system.minting_curve.address,
3627+
:pool_balance,
3628+
);
3629+
system.advance_epoch();
3630+
system.add_to_delegation_pool(:delegator, :pool, amount: delegation_amount);
3631+
system.advance_block_custom_and_attest(:staker, stake: stake_amount + pool_balance);
3632+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3633+
assert!(rewards == expected_rewards);
3634+
system.advance_epoch();
3635+
system.advance_block_custom_and_attest(:staker, stake: stake_amount + pool_balance);
3636+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3637+
assert!(rewards == expected_rewards);
3638+
system.advance_epoch();
3639+
pool_balance += delegation_amount;
3640+
system.advance_block_custom_and_attest(:staker, stake: stake_amount + pool_balance);
3641+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3642+
assert!(rewards == expected_rewards);
3643+
system.advance_epoch();
3644+
let expected_rewards = calculate_strk_pool_rewards_with_pool_balance_v2(
3645+
staker_address: staker.staker.address,
3646+
staking_contract: system.staking.address,
3647+
minting_curve_contract: system.minting_curve.address,
3648+
:pool_balance,
3649+
);
3650+
system.advance_block_custom_and_attest(:staker, stake: stake_amount + pool_balance);
3651+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3652+
assert!(rewards == expected_rewards);
3653+
3654+
system.start_consensus_rewards();
3655+
system.add_to_delegation_pool(:delegator, :pool, amount: delegation_amount);
3656+
system.update_rewards(:staker, disable_rewards: false);
3657+
let expected_rewards_v3 = calculate_strk_pool_rewards_with_pool_balance_v3(
3658+
staker_address: staker.staker.address,
3659+
staking_contract: system.staking.address,
3660+
minting_curve_contract: system.minting_curve.address,
3661+
:pool_balance,
3662+
);
3663+
advance_block_number_global(blocks: 1);
3664+
system.update_rewards(:staker, disable_rewards: false);
3665+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3666+
assert!(rewards == expected_rewards);
3667+
system.advance_epoch();
3668+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3669+
assert!(rewards == expected_rewards_v3 * 2);
3670+
system.update_rewards(:staker, disable_rewards: false);
3671+
advance_block_number_global(blocks: 1);
3672+
system.update_rewards(:staker, disable_rewards: false);
3673+
system.advance_epoch();
3674+
pool_balance += delegation_amount;
3675+
system.update_rewards(:staker, disable_rewards: false);
3676+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3677+
assert!(rewards == expected_rewards_v3 * 2);
3678+
advance_block_number_global(blocks: 1);
3679+
system.update_rewards(:staker, disable_rewards: false);
3680+
system.advance_epoch();
3681+
let expected_rewards_v3 = calculate_strk_pool_rewards_with_pool_balance_v3(
3682+
staker_address: staker.staker.address,
3683+
staking_contract: system.staking.address,
3684+
minting_curve_contract: system.minting_curve.address,
3685+
:pool_balance,
3686+
);
3687+
let rewards = system.delegator_claim_rewards(:delegator, :pool);
3688+
// Allow small rounding errors.
3689+
assert!(wide_abs_diff(rewards, expected_rewards_v3 * 2) <= 1);
3690+
}

src/test_utils.cairo

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,25 @@ pub(crate) fn calculate_strk_pool_rewards_v3(
11721172
pool_rewards
11731173
}
11741174

1175+
/// Calculate strk pool rewards for one epoch for the given pool balance.
1176+
pub(crate) fn calculate_strk_pool_rewards_with_pool_balance_v3(
1177+
staker_address: ContractAddress,
1178+
staking_contract: ContractAddress,
1179+
minting_curve_contract: ContractAddress,
1180+
pool_balance: Amount,
1181+
) -> Amount {
1182+
let staking_dispatcher = IStakingDispatcher { contract_address: staking_contract };
1183+
let staker_info = staking_dispatcher.staker_info_v1(:staker_address);
1184+
let (_, pool_rewards) = calculate_staker_strk_rewards_with_balances_v3(
1185+
amount_own: staker_info.amount_own,
1186+
pool_amount: pool_balance,
1187+
commission: staker_info.pool_info.unwrap().commission,
1188+
:staking_contract,
1189+
:minting_curve_contract,
1190+
);
1191+
pool_rewards
1192+
}
1193+
11751194
/// Returns (staker_commission_rewards, BTC_pool_rewards) for the specified
11761195
/// `normalized_pool_balance`, `normalized_staker_total_btc_balance` and `commission`.
11771196
///

0 commit comments

Comments
 (0)