ERC-1155 Pausable
ERC-1155 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 ERC-1155 token pausable
, you need to use the Pausable
contract and apply its mechanisms to ERC1155 token functions as follows:
use openzeppelin_stylus::{
token::erc1155::{Erc1155, IErc1155},
utils::Pausable,
};
#[entrypoint]
#[storage]
struct Erc1155Example {
#[borrow]
pub erc1155: Erc1155,
#[borrow]
pub pausable: Pausable,
}
#[public]
#[inherit(Erc1155, Pausable)]
impl Erc1155Example {
fn mint(
&mut self,
to: Address,
token_id: U256,
amount: U256,
data: Bytes,
) -> Result<(), Vec<u8>> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc1155._mint(to, token_id, amount, &data)?;
Ok(())
}
fn mint_batch(
&mut self,
to: Address,
token_ids: Vec<U256>,
amounts: Vec<U256>,
data: Bytes,
) -> Result<(), Vec<u8>> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc1155._mint_batch(to, token_ids, amounts, &data)?;
Ok(())
}
fn burn(
&mut self,
account: Address,
token_id: U256,
value: U256,
) -> Result<(), Vec<u8>> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc1155.burn(account, token_id, value)?;
Ok(())
}
fn burn_batch(
&mut self,
account: Address,
token_ids: Vec<U256>,
values: Vec<U256>,
) -> Result<(), Vec<u8>> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc1155.burn_batch(account, token_ids, values)?;
Ok(())
}
fn safe_transfer_from(
&mut self,
from: Address,
to: Address,
id: U256,
value: U256,
data: Bytes,
) -> Result<(), Vec<u8>> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc1155.safe_transfer_from(from, to, id, value, data)?;
Ok(())
}
fn safe_batch_transfer_from(
&mut self,
from: Address,
to: Address,
ids: Vec<U256>,
values: Vec<U256>,
data: Bytes,
) -> Result<(), Vec<u8>> {
// ...
self.pausable.when_not_paused()?;
// ...
self.erc1155.safe_batch_transfer_from(from, to, ids, values, data)?;
Ok(())
}
}
Additionally, you need to ensure proper initialization during contract deployment. Make sure to include the following code in your Solidity Constructor:
contract Erc1155Example {
bool private _paused;
constructor() {
_paused = false;
}
}