I want to implement the _burn(address account, uint256 amount)
function provided by OpenZepplin's ERC20 library. The smart contract will call this function when some condition is true.
The Rust fungible token example has a on_tokens_burned()
function, but it looks like a logger.
fn on_tokens_burned(&mut self, account_id: AccountId, amount: Balance) {
log!("Account @{} burned {}", account_id, amount);
}
How to burn tokens of a particular user?
Here is some implementation of burn:
pub fn burn(&mut self, amount: U128String){
let balance = self.internal_unwrap_balance_of(env::predecessor_account_id());
assert!(balance>=amount);
self.internal_update_account(&env::predecessor_account_id(), balance - amount);
assert!(self.total_supply>=amount);
self.total_supply -= amount;
}
/// Inner method to save the given account for a given account ID.
/// If the account balance is 0, the account is deleted instead to release storage.
pub fn internal_update_account(&mut self, account_id: &AccountId, balance: u128) {
if balance == 0 {
self.accounts.remove(account_id);
} else {
self.accounts.insert(account_id, &balance); //insert_or_update
}
}
pub fn internal_unwrap_balance_of(&self, account_id: &AccountId) -> Balance {
match self.accounts.get(&account_id) {
Some(balance) => balance,
None => 0,
}
}
we're using something similar here: https://github.com/alpha-fi/cheddar/blob/master/cheddar/src/internal.rs