Skip to content

Commit

Permalink
test: add missing E2E tests for Erc721 (#489)
Browse files Browse the repository at this point in the history
<!--
Thank you for your interest in contributing to OpenZeppelin!

Consider opening an issue for discussion prior to submitting a PR. New
features will be merged faster if they were first discussed and designed
with the team.

Describe the changes introduced in this pull request. Include any
context necessary for understanding the PR's purpose.
-->

<!-- Fill in with issue number -->
Resolves #483 

#### PR Checklist

<!--
Before merging the pull request all of the following must be completed.
Feel free to submit a PR or Draft PR even if some items are pending.
Some of the items may not apply.
-->

- [x] Tests
- [ ] Documentation
- [ ] Changelog

---------

Co-authored-by: Nenad <[email protected]>
Co-authored-by: Daniel Bigos <[email protected]>
Co-authored-by: Nenad <[email protected]>
Co-authored-by: Daniel Bigos <[email protected]>
  • Loading branch information
5 people authored Jan 14, 2025
1 parent 070b85d commit badee40
Show file tree
Hide file tree
Showing 3 changed files with 321 additions and 1 deletion.
21 changes: 21 additions & 0 deletions examples/erc721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ impl Erc721Example {
Ok(())
}

pub fn safe_mint(
&mut self,
to: Address,
token_id: U256,
data: Bytes,
) -> Result<(), Vec<u8>> {
self.pausable.when_not_paused()?;

self.erc721._safe_mint(to, token_id, &data)?;

// Update the extension's state.
self.enumerable._add_token_to_all_tokens_enumeration(token_id);
self.enumerable._add_token_to_owner_enumeration(
to,
token_id,
&self.erc721,
)?;

Ok(())
}

pub fn safe_transfer_from(
&mut self,
from: Address,
Expand Down
2 changes: 1 addition & 1 deletion examples/erc721/tests/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sol!(
function setApprovalForAll(address operator, bool approved) external;
function totalSupply() external view returns (uint256 totalSupply);
function transferFrom(address from, address to, uint256 tokenId) external;

function safeMint(address to, uint256 tokenId, bytes calldata data) external;
function mint(address to, uint256 tokenId) external;
function burn(uint256 tokenId) external;

Expand Down
299 changes: 299 additions & 0 deletions examples/erc721/tests/erc721.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,264 @@ async fn is_approved_for_all_invalid_operator(
Ok(())
}

#[e2e::test]
async fn safe_mint_to_eoa_without_data(alice: Account) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);
let alice_addr = alice.address();

let token_id = random_token_id();
let data = Bytes::new();

let initial_balance =
contract.balanceOf(alice.address()).call().await?.balance;

let receipt = receipt!(contract.safeMint(alice_addr, token_id, data))?;
assert!(receipt.emits(Erc721::Transfer {
from: Address::ZERO,
to: alice_addr,
tokenId: token_id,
}));

let owner_of = contract.ownerOf(token_id).call().await?.ownerOf;
assert_eq!(alice_addr, owner_of);

let balance = contract.balanceOf(alice.address()).call().await?.balance;
assert_eq!(balance, initial_balance + uint!(1_U256));

Ok(())
}

#[e2e::test]
async fn safe_mint_to_eoa_with_data(alice: Account) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);
let alice_addr = alice.address();

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let initial_balance =
contract.balanceOf(alice.address()).call().await?.balance;

let receipt = receipt!(contract.safeMint(alice_addr, token_id, data))?;
assert!(receipt.emits(Erc721::Transfer {
from: Address::ZERO,
to: alice_addr,
tokenId: token_id,
}));

let owner_of = contract.ownerOf(token_id).call().await?.ownerOf;
assert_eq!(alice_addr, owner_of);

let balance = contract.balanceOf(alice.address()).call().await?.balance;
assert_eq!(balance, initial_balance + uint!(1_U256));

Ok(())
}

#[e2e::test]
async fn safe_mint_to_receiver_contract_without_data(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);
let receiver_address =
receiver::deploy(&alice.wallet, ERC721ReceiverMock::RevertType::None)
.await?;

let token_id = random_token_id();
let data = Bytes::new();

let initial_balance =
contract.balanceOf(alice.address()).call().await?.balance;

let receipt =
receipt!(contract.safeMint(receiver_address, token_id, data.clone()))?;

assert!(receipt.emits(Erc721::Transfer {
from: Address::ZERO,
to: receiver_address,
tokenId: token_id,
}));

assert!(receipt.emits(ERC721ReceiverMock::Received {
operator: alice.address(),
from: Address::ZERO,
tokenId: token_id,
data,
}));

let owner_of = contract.ownerOf(token_id).call().await?.ownerOf;
assert_eq!(receiver_address, owner_of);

let balance = contract.balanceOf(receiver_address).call().await?.balance;
assert_eq!(balance, initial_balance + uint!(1_U256));

Ok(())
}

#[e2e::test]
async fn safe_mint_to_receiver_contract_with_data(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);
let receiver_address =
receiver::deploy(&alice.wallet, ERC721ReceiverMock::RevertType::None)
.await?;

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let initial_balance =
contract.balanceOf(alice.address()).call().await?.balance;

let receipt =
receipt!(contract.safeMint(receiver_address, token_id, data.clone()))?;

assert!(receipt.emits(Erc721::Transfer {
from: Address::ZERO,
to: receiver_address,
tokenId: token_id,
}));

assert!(receipt.emits(ERC721ReceiverMock::Received {
operator: alice.address(),
from: Address::ZERO,
tokenId: token_id,
data,
}));

let owner_of = contract.ownerOf(token_id).call().await?.ownerOf;
assert_eq!(receiver_address, owner_of);

let balance = contract.balanceOf(receiver_address).call().await?.balance;
assert_eq!(balance, initial_balance + uint!(1_U256));

Ok(())
}

#[e2e::test]
async fn error_when_safe_mint_to_invalid_receiver_contract(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err = send!(contract.safeMint(contract_addr, token_id, data))
.expect_err("should not safe mint the token to invalid receiver");

assert!(err.reverted_with(Erc721::ERC721InvalidReceiver {
receiver: contract_addr
}));

Ok(())
}

#[e2e::test]
async fn error_when_safe_mint_to_invalid_sender_with_data(
alice: Account,
bob: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

_ = watch!(contract.mint(alice.address(), token_id))?;

let err = send!(contract.safeMint(bob.address(), token_id, data))
.expect_err("should not safe mint an existing token");

assert!(err
.reverted_with(Erc721::ERC721InvalidSender { sender: Address::ZERO }));

Ok(())
}

#[e2e::test]
async fn error_when_receiver_reverts_with_reason_on_safe_mint_with_data(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

let receiver_address = receiver::deploy(
&alice.wallet,
ERC721ReceiverMock::RevertType::RevertWithMessage,
)
.await?;

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err = send!(contract.safeMint(receiver_address, token_id, data))
.expect_err("should not safe mint when receiver errors with reason");

assert!(err.reverted_with(Erc721::Error {
message: "ERC721ReceiverMock: reverting".to_string()
}));

Ok(())
}

#[e2e::test]
async fn error_when_receiver_reverts_without_reason_on_safe_mint_with_data(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

let receiver_address = receiver::deploy(
&alice.wallet,
ERC721ReceiverMock::RevertType::RevertWithoutMessage,
)
.await?;

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err = send!(contract.safeMint(receiver_address, token_id, data))
.expect_err(
"should not safe mint when receiver reverts without reason",
);

assert!(err.reverted_with(Erc721::ERC721InvalidReceiver {
receiver: receiver_address
}));

Ok(())
}

#[e2e::test]
async fn error_when_receiver_panics_on_safe_mint_with_data(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

let receiver_address =
receiver::deploy(&alice.wallet, ERC721ReceiverMock::RevertType::Panic)
.await?;

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err = send!(contract.safeMint(receiver_address, token_id, data))
.expect_err("should not safe mint when receiver panics");

assert!(err.reverted_with(Erc721::Panic {
code: U256::from(PanicCode::DivisionByZero as u8)
}));

Ok(())
}

// ============================================================================
// Integration Tests: ERC-721 Pausable Extension
// ============================================================================
Expand Down Expand Up @@ -1615,6 +1873,47 @@ async fn error_when_safe_transfer_with_data_in_paused_state(
Ok(())
}

#[e2e::test]
async fn error_when_safe_mint_in_paused_state(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

let alice_addr = alice.address();
let token_id = random_token_id();

let Erc721::balanceOfReturn { balance: initial_alice_balance } =
contract.balanceOf(alice_addr).call().await?;

let _ = watch!(contract.pause())?;

let err = send!(contract.safeMint(
alice_addr,
token_id,
fixed_bytes!("deadbeef").into()
))
.expect_err("should return `EnforcedPause`");
assert!(err.reverted_with(Erc721::EnforcedPause {}));

let err = contract
.ownerOf(token_id)
.call()
.await
.expect_err("should return `ERC721NonexistentToken`");

assert!(
err.reverted_with(Erc721::ERC721NonexistentToken { tokenId: token_id })
);

let Erc721::balanceOfReturn { balance: alice_balance } =
contract.balanceOf(alice_addr).call().await?;

assert_eq!(initial_alice_balance, alice_balance);

Ok(())
}

// ============================================================================
// Integration Tests: ERC-721 Burnable Extension
// ============================================================================
Expand Down

0 comments on commit badee40

Please sign in to comment.