Skip to content

Commit

Permalink
Fix for HAL-03 issue - follow up.
Browse files Browse the repository at this point in the history
To coply with existing tests, burning above balance will not cause revert, but the amount is checked.
  • Loading branch information
kubaplas committed May 6, 2024
1 parent 65cc8a6 commit c14a005
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions dao/src/core_contracts/reputation/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ impl BalanceStorage {
/// [`NotWhitelisted`](crate::utils::Error::NotWhitelisted) if called by a not whitelisted account.
pub fn burn(&mut self, owner: Address, amount: Balance) {
self.access_control.ensure_whitelisted();
self.dec_balance(&owner, amount);
self.total_supply -= amount;

let decreased = self.dec_balance(&owner, amount);

self.total_supply -= decreased;

Burn {
address: owner,
Expand Down Expand Up @@ -99,8 +101,7 @@ impl BalanceStorage {
total_supply += amount;
}
for (address, amount) in burns {
self.dec_balance(&address, amount);
total_supply -= amount;
total_supply -= self.dec_balance(&address, amount);
}

self.total_supply.set(total_supply);
Expand Down Expand Up @@ -153,12 +154,14 @@ impl BalanceStorage {
self.set_balance(owner, new_balance);
}

fn dec_balance(&mut self, owner: &Address, amount: Balance) {
/// Returns the amount actually decreased.
fn dec_balance(&mut self, owner: &Address, amount: Balance) -> Balance {
let balance = self.balances.get(owner).unwrap_or_default();
let new_balance = balance
.checked_sub(amount)
.unwrap_or_revert_with(Error::ArithmeticOverflow);
self.set_balance(owner, new_balance);
let amount = if amount > balance { balance } else { amount };

self.set_balance(owner, balance - amount);

amount
}
}

Expand Down

0 comments on commit c14a005

Please sign in to comment.