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

Staking rewards fix #34

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Changes from all commits
Commits
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
57 changes: 51 additions & 6 deletions src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,12 @@ impl StakeKeeper {
validator_info.stake,
);

// update validator info and delegators
if !new_rewards.is_zero() {
validator_info.last_rewards_calculation = block.time;

// save updated validator
VALIDATOR_INFO.save(staking_storage, validator, &validator_info)?;
// update validator info
validator_info.last_rewards_calculation = block.time;
VALIDATOR_INFO.save(staking_storage, validator, &validator_info)?;

// update delegators
if !new_rewards.is_zero() {
let validator_addr = api.addr_validate(&validator_obj.address)?;
// update all delegators
for staker in validator_info.stakers.iter() {
Expand Down Expand Up @@ -2451,5 +2450,51 @@ mod test {
.unwrap();
assert_eq!(balance.amount.u128(), 55);
}

#[test]
fn rewards_initial_wait() {
let (mut test_env, validator) =
TestEnv::wrap(setup_test_env(Decimal::percent(10), Decimal::zero()));
let delegator = Addr::unchecked("delegator");

// init balance
test_env
.router
.bank
.init_balance(&mut test_env.store, &delegator, vec![coin(100, "TOKEN")])
.unwrap();

// wait a year before staking
const YEAR: u64 = 60 * 60 * 24 * 365;
test_env.block.time = test_env.block.time.plus_seconds(YEAR);

// delegate some tokens
execute_stake(
&mut test_env,
delegator.clone(),
StakingMsg::Delegate {
validator: validator.to_string(),
amount: coin(100, "TOKEN"),
},
)
.unwrap();

// wait another year
test_env.block.time = test_env.block.time.plus_seconds(YEAR);

// query rewards
let response: DelegationResponse = query_stake(
&test_env,
StakingQuery::Delegation {
delegator: delegator.to_string(),
validator: validator.to_string(),
},
)
.unwrap();
assert_eq!(
response.delegation.unwrap().accumulated_rewards,
vec![coin(10, "TOKEN")] // 10% of 100
);
}
}
}