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: add test to check if unbonding brakes reward distribution #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions contracts/stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ pub fn execute_mass_bond(
.map(|((asset_info, mut distribution), old_reward_power)| {
let new_reward_power =
distribution.calc_rewards_power(deps.storage, &cfg, &sender)?;
dbg!("BONDING", old_reward_power, new_reward_power);
update_rewards(
deps.storage,
&asset_info,
Expand Down
16 changes: 14 additions & 2 deletions contracts/stake/src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,19 @@ pub fn execute_distribute_rewards(

let leftover: u128 = distribution.shares_leftover.into();
let points = (amount << SHARES_SHIFT) + leftover;
dbg!(points, total_rewards.u128(), points / total_rewards.u128());
let points_per_share = points / total_rewards.u128();
distribution.shares_leftover = (points % total_rewards.u128()) as u64;

// Everything goes back to 128-bits/16-bytes
// Full amount is added here to total withdrawable, as it should not be considered on its own
// on future distributions - even if because of calculation offsets it is not fully
// distributed, the error is handled by leftover.
// poi 1
distribution.shares_per_point += Uint128::new(points_per_share);
distribution.distributed_total += Uint128::new(amount);
distribution.withdrawable_total += Uint128::new(amount);
dbg!(distribution.clone());

DISTRIBUTION.save(deps.storage, &asset_info, &distribution)?;

Expand Down Expand Up @@ -146,6 +149,7 @@ pub fn execute_withdraw_rewards(
let mut adjustment = WITHDRAW_ADJUSTMENT
.may_load(deps.storage, (&owner, &asset_info))?
.unwrap_or_default();
dbg!(adjustment.clone());

let reward = withdrawable_rewards(deps.as_ref(), &cfg, &owner, &distribution, &adjustment)?;

Expand Down Expand Up @@ -290,8 +294,15 @@ pub fn apply_points_correction(
diff: i128,
) -> StdResult<()> {
WITHDRAW_ADJUSTMENT.update(storage, (addr, asset_info), |old| -> StdResult<_> {
let mut old = old.unwrap_or_default();
let mut old = old.unwrap_or_default(); //eq to withdraw_adjustment in phoenix
let shares_correction: i128 = old.shares_correction;
dbg!(old.clone());
dbg!(
shares_correction,
shares_per_point,
diff,
shares_correction - shares_per_point as i128 * diff
);
old.shares_correction = shares_correction - shares_per_point as i128 * diff;
Ok(old)
})?;
Expand All @@ -312,11 +323,12 @@ pub fn withdrawable_rewards(
.calc_rewards_power(deps.storage, cfg, owner)?
.u128();

dbg!(ppw, points, adjustment.clone());
let correction = adjustment.shares_correction;
let points = (ppw * points) as i128;
dbg!(ppw * points as u128);
let points = points + correction;
let amount = points as u128 >> SHARES_SHIFT;
let amount = amount - adjustment.withdrawn_rewards.u128();

Ok(amount.into())
}
112 changes: 112 additions & 0 deletions contracts/stake/src/multitest/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2527,3 +2527,115 @@ fn withdraw_adjustment_handled_lazily() {
// member should get rewards
assert_eq!(suite.query_balance(member, "juno").unwrap(), 500);
}

#[test]
fn test_unbond_brakes_reward_distribution() {
let members = vec!["member0".to_owned(), "member1".to_owned()];
let executor = "executor";

let unbonding_period = 10_000;

let mut suite = SuiteBuilder::new()
.with_unbonding_periods(vec![unbonding_period])
.with_min_bond(1000)
.with_initial_balances(vec![(&members[0], 1_000), (&members[1], 1_000)])
.with_admin("admin")
.with_native_balances("juno", vec![(executor, 100_000)])
.build();

suite
.create_distribution_flow(
"admin",
executor,
AssetInfo::Native("juno".to_string()),
vec![(unbonding_period, Decimal::one())],
)
.unwrap();

// delegate
suite
.delegate(&members[0], 1_000u128, unbonding_period)
.unwrap();
suite
.delegate(&members[1], 1_000u128, unbonding_period)
.unwrap();

suite.update_time(2_000);

// distribute 20% of total supply
suite
.distribute_funds(executor, executor, Some(juno(20_000)))
.unwrap();

// withdraw the rewards
suite
.withdraw_funds(&members[0], members[0].as_str(), None)
.unwrap();
suite
.withdraw_funds(&members[1], members[1].as_str(), None)
.unwrap();

assert_eq!(suite.query_balance(&members[0], "juno").unwrap(), 10_000);
assert_eq!(suite.query_balance(&members[1], "juno").unwrap(), 10_000);

suite.update_time(5_000);

// unbond member1
suite.unbond(&members[0], 1000, unbonding_period).unwrap();

suite.update_time(10_000);

// distribute the rest of the 100k
suite
.distribute_funds(executor, executor, Some(juno(80_000)))
.unwrap();

suite
.withdraw_funds(&members[1], members[1].as_str(), None)
.unwrap();

assert_eq!(suite.query_balance(&members[0], "juno").unwrap(), 10_000);
assert_eq!(suite.query_balance(&members[1], "juno").unwrap(), 90_000);
}

#[test]
fn test_bond_withdraw_unbond() {
let user = "member";
let executor = "executor";
let unbonding_period = 10_000;

let mut suite = SuiteBuilder::new()
.with_unbonding_periods(vec![unbonding_period])
.with_min_bond(1000)
.with_initial_balances(vec![(user, 1_000)])
.with_admin("admin")
.with_native_balances("juno", vec![(executor, 100_000)])
.build();

// create distribution flow
suite
.create_distribution_flow(
"admin",
executor,
AssetInfo::Native("juno".to_string()),
vec![(unbonding_period, Decimal::one())],
)
.unwrap();

// Bond
suite.delegate(user, 1_000, unbonding_period).unwrap();

// Distribute rewards
suite
.distribute_funds(executor, None, Some(juno(500)))
.unwrap();

// Withdraw rewards
suite.withdraw_funds(user, user, None).unwrap();

// Unbond
suite.unbond(user, 1_000, unbonding_period).unwrap();

// Assert balances
assert_eq!(suite.query_balance(user, "juno").unwrap(), 500);
}
7 changes: 7 additions & 0 deletions contracts/stake/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ pub fn create_undelegate_msg(
}

pub fn calc_power(cfg: &Config, stake: Uint128, multiplier: Decimal) -> Uint128 {
dbg!("CALCULATING POWER");
if stake < cfg.min_bond {
Uint128::zero()
} else {
dbg!(
stake,
multiplier,
cfg.tokens_per_power,
stake * multiplier / cfg.tokens_per_power
);
stake * multiplier / cfg.tokens_per_power
}
}
Expand Down