-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(erc20): implement Burnable extension (#27)
- Loading branch information
Showing
5 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use stylus_sdk::{ | ||
alloy_primitives::{Address, U256}, | ||
msg, | ||
prelude::*, | ||
}; | ||
|
||
use crate::erc20::{Error, ERC20}; | ||
|
||
sol_storage! { | ||
pub struct ERC20Burnable { | ||
ERC20 erc20; | ||
} | ||
} | ||
|
||
#[external] | ||
#[inherit(ERC20)] | ||
impl ERC20Burnable { | ||
/// Destroys a `value` amount of tokens from the caller. | ||
/// lowering the total supply. | ||
/// | ||
/// Relies on the `update` mechanism. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `value` - Amount to be burnt. | ||
/// | ||
/// If the `from` address doesn't have enough tokens, then the error | ||
/// [`Error::InsufficientBalance`] is returned. | ||
pub fn burn(&mut self, value: U256) -> Result<(), Error> { | ||
self.erc20._burn(msg::sender(), value) | ||
} | ||
|
||
/// Destroys a `value` amount of tokens from `account`, | ||
/// lowering the total supply. | ||
/// | ||
/// Relies on the `update` mechanism. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `account` - Owner's address. | ||
/// * `value` - Amount to be burnt. | ||
/// | ||
/// If not enough allowance is available, then the error | ||
/// [`Error::InsufficientAllowance`] is returned. | ||
/// * If the `from` address is `Address::ZERO`, then the error | ||
/// [`Error::InvalidSender`] is returned. | ||
/// If the `from` address doesn't have enough tokens, then the error | ||
/// [`Error::InsufficientBalance`] is returned. | ||
pub fn burn_from( | ||
&mut self, | ||
account: Address, | ||
value: U256, | ||
) -> Result<(), Error> { | ||
self.erc20._spend_allowance(account, msg::sender(), value)?; | ||
self.erc20._burn(account, value) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#[cfg(any(test, erc20_metadata))] | ||
pub mod metadata; | ||
|
||
#[cfg(any(test, erc20_burnable))] | ||
pub mod burnable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
* xref:index.adoc[Overview] | ||
* xref:crypto.adoc[Cryptography] | ||