Skip to content

Commit

Permalink
feat(erc721): add ERC-721 Pausable extension (#66) (#79)
Browse files Browse the repository at this point in the history
Resolves #66 

#### PR Checklist

- [x] Tests
- [x] Documentation
  • Loading branch information
bidzyyys authored May 23, 2024
1 parent caf6e71 commit b0fa37d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
1 change: 1 addition & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ erc20_pausable = ["erc20"]
erc721 = []
erc721_burnable = ["erc721"]
erc721_metadata = ["erc721"]
erc721_pausable = ["erc721"]
erc721_uri_storage = ["erc721"]

access = []
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cfg_if::cfg_if! {
}

cfg_if::cfg_if! {
if #[cfg(any(test, feature = "erc20_pausable"))] {
if #[cfg(any(test, feature = "erc20_pausable", feature = "erc721_pausable"))] {
pub mod pausable;
pub use pausable::Pausable;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/erc721/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
version = "0.0.0"

[dependencies]
contracts = { path = "../../contracts", features = ["erc721_burnable", "erc721_metadata", "erc721_uri_storage"] }
contracts = { path = "../../contracts", features = ["erc721_burnable", "erc721_metadata", "erc721_pausable", "erc721_uri_storage"] }
alloy-primitives.workspace = true
stylus-sdk.workspace = true
stylus-proc.workspace = true
Expand Down
55 changes: 49 additions & 6 deletions examples/erc721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ use alloc::{
vec::Vec,
};

use alloy_primitives::U256;
use contracts::erc721::{
extensions::{ERC721Metadata, ERC721UriStorage, IERC721Burnable},
ERC721,
use alloy_primitives::{Address, U256};
use contracts::{
erc721::{
extensions::{ERC721Metadata, ERC721UriStorage, IERC721Burnable},
ERC721,
},
utils::Pausable,
};
use stylus_sdk::{
abi::Bytes,
prelude::{entrypoint, external, sol_storage},
};
use stylus_sdk::prelude::{entrypoint, external, sol_storage};

sol_storage! {
#[entrypoint]
Expand All @@ -22,11 +28,13 @@ sol_storage! {
ERC721Metadata metadata;
#[borrow]
ERC721UriStorage uri_storage;
#[borrow]
Pausable pausable;
}
}

#[external]
#[inherit(ERC721, ERC721Metadata, ERC721UriStorage)]
#[inherit(ERC721, ERC721Metadata, ERC721UriStorage, Pausable)]
impl Token {
// We need to properly initialize all of Token's attributes.
// For that, we need to call each attribute's constructor if it exists.
Expand All @@ -42,6 +50,7 @@ impl Token {
}

pub fn burn(&mut self, token_id: U256) -> Result<(), Vec<u8>> {
self.pausable.when_not_paused()?;
self.erc721.burn(token_id).map_err(|e| e.into())
}

Expand All @@ -67,4 +76,38 @@ impl Token {

uri
}

pub fn safe_transfer_from(
&mut self,
from: Address,
to: Address,
token_id: U256,
) -> Result<(), Vec<u8>> {
self.pausable.when_not_paused()?;
self.erc721.safe_transfer_from(from, to, token_id).map_err(|e| e.into())
}

#[selector(name = "safeTransferFrom")]
pub fn safe_transfer_from_with_data(
&mut self,
from: Address,
to: Address,
token_id: U256,
data: Bytes,
) -> Result<(), Vec<u8>> {
self.pausable.when_not_paused()?;
self.erc721
.safe_transfer_from_with_data(from, to, token_id, data)
.map_err(|e| e.into())
}

pub fn transfer_from(
&mut self,
from: Address,
to: Address,
token_id: U256,
) -> Result<(), Vec<u8>> {
self.pausable.when_not_paused()?;
self.erc721.transfer_from(from, to, token_id).map_err(|e| e.into())
}
}

0 comments on commit b0fa37d

Please sign in to comment.