ERC-20 Pausable
ERC20 token with pausable token transfers, minting, and burning.
Useful for scenarios such as preventing trades until the end of an evaluation period, or having an emergency switch for freezing all token transfers in the event of a large bug.
Usage
In order to make your ERC20 token pausable
, you need to use the Pausable
contract and apply its mechanisms to ERC20 token functions as follows:
use openzeppelin_stylus::{
token::erc20::{self, extensions::IErc20Burnable, Erc20, IErc20},
utils::{pausable, Pausable},
};
#[derive(SolidityError, Debug)]
enum Error {
Erc20(erc20::Error),
Pausable(pausable::Error),
}
#[entrypoint]
#[storage]
struct Erc20Example {
#[borrow]
erc20: Erc20,
#[borrow]
pausable: Pausable,
}
#[public]
#[inherit(Erc20, Pausable)]
impl Erc20Example {
fn burn(&mut self, value: U256) -> Result<(), Error> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc20.burn(value).map_err(|e| e.into())
}
fn burn_from(
&mut self,
account: Address,
value: U256,
) -> Result<(), Error> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc20.burn_from(account, value).map_err(|e| e.into())
}
fn mint(&mut self, account: Address, value: U256) -> Result<(), Error> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc20._mint(account, value)?;
Ok(())
}
fn transfer(&mut self, to: Address, value: U256) -> Result<bool, Error> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc20.transfer(to, value).map_err(|e| e.into())
}
fn transfer_from(
&mut self,
from: Address,
to: Address,
value: U256,
) -> Result<bool, Error> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc20.transfer_from(from, to, value).map_err(|e| e.into())
}
}