Skip to content

Commit

Permalink
Fix: Rewards distribution amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Lacy committed Dec 18, 2024
1 parent 1b7ac7d commit 19531cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion contracts/btc-finality/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn instantiate(
let params = msg.params.unwrap_or_default();
PARAMS.save(deps.storage, &params)?;
// initialize storage, so no issue when reading for the first time
TOTAL_REWARDS.save(deps.storage, &Uint128::zero())?;

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::new().add_attribute("action", "instantiate"))
Expand Down Expand Up @@ -261,7 +262,7 @@ fn handle_end_block(
// On an epoch boundary, send rewards to Babylon through the babylon contract
let params = PARAMS.load(deps.storage)?;
if env.block.height > 0 && env.block.height % params.epoch_length == 0 {
let rewards = TOTAL_REWARDS.may_load(deps.storage)?.unwrap_or_default();
let rewards = TOTAL_REWARDS.load(deps.storage)?;
if rewards.u128() > 0 {
let wasm_msg = send_rewards_msg(deps, rewards.u128(), &cfg)?;
res = res.add_message(wasm_msg);
Expand Down
18 changes: 13 additions & 5 deletions contracts/btc-finality/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,20 @@ pub fn distribute_rewards(deps: &mut DepsMut, env: &Env) -> Result<(), ContractE
};
// Get the voting power of the active FPS
let total_voting_power = active_fps.iter().map(|fp| fp.power as u128).sum::<u128>();
// Get the rewards to distribute (bank balance of the finality contract)
// Get the rewards to distribute (bank balance of the finality contract minus already distributed rewards)
let distributed_rewards = TOTAL_REWARDS.load(deps.storage)?;
let cfg = CONFIG.load(deps.storage)?;
let rewards_amount = deps
.querier
.query_balance(env.contract.address.clone(), cfg.denom)?
.amount;
.amount
.saturating_sub(distributed_rewards);
// Short-circuit if there are no rewards to distribute
if rewards_amount.is_zero() {
return Ok(());
}
// Compute the rewards for each active FP
let mut total_rewards = Uint128::zero();
let mut accumulated_rewards = Uint128::zero();
for fp in active_fps {
let reward = (Decimal::from_ratio(fp.power as u128, total_voting_power)
* Decimal::from_ratio(rewards_amount, 1u128))
Expand All @@ -649,9 +655,11 @@ pub fn distribute_rewards(deps: &mut DepsMut, env: &Env) -> Result<(), ContractE
Ok::<Uint128, ContractError>(r.unwrap_or_default() + reward)
})?;
// Compute the total rewards
total_rewards += reward;
accumulated_rewards += reward;
}
// Update the total rewards
TOTAL_REWARDS.save(deps.storage, &total_rewards)?;
TOTAL_REWARDS.update(deps.storage, |r| {
Ok::<Uint128, ContractError>(r + accumulated_rewards)
})?;
Ok(())
}

0 comments on commit 19531cd

Please sign in to comment.