销毁 NEP-141 代币

Burn NEP-141 tokens

我想实现OpenZepplin's ERC20 library提供的_burn(address account, uint256 amount)功能。当某些条件为真时,智能合约将调用此函数。

Rust fungible token example 有一个 on_tokens_burned() 功能,但它看起来像一个记录器。

    fn on_tokens_burned(&mut self, account_id: AccountId, amount: Balance) {
        log!("Account @{} burned {}", account_id, amount);
    }

如何销毁特定用户的代币?

下面是刻录的一些实现:


    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,
        }
    }

我们在这里使用类似的东西:https://github.com/alpha-fi/cheddar/blob/master/cheddar/src/internal.rs