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: pay rewards during unbond #170

Merged
merged 10 commits into from
Mar 11, 2024
8 changes: 8 additions & 0 deletions contracts/stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ impl StakingTrait for Staking {

let config = get_config(&env);

// check for rewards and withdraw them
let found_rewards: WithdrawableRewardsResponse =
Self::query_withdrawable_rewards(env.clone(), sender.clone());

if !found_rewards.rewards.is_empty() {
Self::withdraw_rewards(env.clone(), sender.clone());
}

let mut stakes = get_stakes(&env, &sender);
remove_stake(&mut stakes.stakes, stake_amount, stake_timestamp);
stakes.total_stake -= stake_amount as u128;
Expand Down
54 changes: 54 additions & 0 deletions contracts/stake/src/tests/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,57 @@ fn unbond_wrong_user_stake_not_found() {

staking.unbond(&user2, &10_000, &2_000);
}

#[test]
fn pay_rewards_during_unbond() {
const AMOUNT: i128 = 100_000;
const WITHDRAW_TIMESTAMP: u64 = 2_000;

let env = Env::default();
env.mock_all_auths();

let admin = Address::generate(&env);
let user = Address::generate(&env);
let manager = Address::generate(&env);
let owner = Address::generate(&env);

let lp_token = deploy_token_contract(&env, &admin);
let staking = deploy_staking_contract(&env, admin.clone(), &lp_token.address, &manager, &owner);

lp_token.mint(&user, &AMOUNT);
lp_token.mint(&owner, &AMOUNT);

staking.create_distribution_flow(&owner, &lp_token.address);
staking.fund_distribution(&owner, &0u64, &10_000u64, &lp_token.address, &AMOUNT);

env.ledger().with_mut(|li| {
li.timestamp = WITHDRAW_TIMESTAMP;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You created a distribution ranging from timestamp 0 to timestamp 10_000, and you immediately jump 2000 seconds ahead. Why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose numbers based on other places in our code where we change the ledger().timestamp. Do the numbers we use matter?

staking.bond(&user, &10_000);

env.ledger().with_mut(|li| {
li.timestamp = 4_000;
});

// user hasn't unbonded yet, no rewards to withdraw
assert_eq!(
staking
.query_withdrawable_rewards(&user)
.rewards
.iter()
.map(|reward| reward.reward_amount)
.sum::<u128>(),
0
);
assert_eq!(
staking.query_undistributed_rewards(&lp_token.address),
110_000
);
gangov marked this conversation as resolved.
Show resolved Hide resolved
staking.unbond(&user, &10_000, &WITHDRAW_TIMESTAMP);

// user unbonded we automatically distribute rewards
assert_eq!(
staking.query_undistributed_rewards(&lp_token.address),
100_000
);
}
Loading