Skip to content

Commit

Permalink
fix: token::burn storage api
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Fraccaroli committed Jan 16, 2024
1 parent b951476 commit b1adb46
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions core/src/ledger/storage_api/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,28 @@ pub fn burn<S>(
where
S: StorageRead + StorageWrite,
{
let key = token::balance_key(token, source);
let balance = read_balance(storage, token, source)?;
let source_balance_key = token::balance_key(token, source);
let source_balance = read_balance(storage, token, source)?;

let amount_to_burn = match balance.checked_sub(amount) {
Some(new_balance) => {
storage.write(&key, new_balance)?;
let amount_to_burn =
if let Some(amount) = source_balance.checked_sub(amount) {
storage.write(&source_balance_key, amount)?;
amount
}
None => {
storage.write(&key, token::Amount::zero())?;
balance
}
};
} else {
storage.write(&source_balance_key, token::Amount::zero())?;
source_balance
};

let total_supply = read_total_supply(&*storage, source)?;
let new_total_supply =
total_supply.checked_sub(amount_to_burn).unwrap_or_default();
let old_total_supply = read_total_supply(storage, token)?;
let new_total_supply = old_total_supply
.checked_sub(amount_to_burn)
.ok_or_else(|| {
tracing::error!(
"Burning more token than the total supply of {}",
token
);
storage_api::Error::new_const("Token total supply underflowed")
})?;

let total_supply_key = token::minted_balance_key(token);
storage.write(&total_supply_key, new_total_supply)
Expand Down

0 comments on commit b1adb46

Please sign in to comment.