Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add missing E2E tests for Erc721 #489

Merged
merged 15 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/erc721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ impl Erc721Example {
Ok(())
}

#[selector(name = "safeMint")]
bidzyyys marked this conversation as resolved.
Show resolved Hide resolved
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;
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
function mint(address to, uint256 tokenId) external;
function burn(uint256 tokenId) external;

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

#[e2e::test]
async fn safe_mint_to_valid_address_with_data(
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
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 receipt =
receipt!(contract.safeMint(alice_addr, token_id, data.clone()))?;
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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

let Erc721::ownerOfReturn { ownerOf } =
contract.ownerOf(token_id).call().await?;
assert_eq!(alice_addr, ownerOf);

let Erc721::balanceOfReturn { balance } =
contract.balanceOf(alice_addr).call().await?;
let one = uint!(1_U256);
assert_eq!(balance, one);

Ok(())
}

#[e2e::test]
async fn safe_mint_to_receiver_contract_with_blank_data(
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
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 = Bytes::new();

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 Erc721::ownerOfReturn { ownerOf } =
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
contract.ownerOf(token_id).call().await?;
assert_eq!(receiver_address, ownerOf);

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 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 Erc721::ownerOfReturn { ownerOf } =
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
contract.ownerOf(token_id).call().await?;
assert_eq!(receiver_address, ownerOf);

Ok(())
}

#[e2e::test]
async fn error_when_safe_mint_to_contract_without_receiver_interface_with_data(
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
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.clone()))
.expect_err("should not transfer the token to invalid receiver");
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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 invalid_sender = Address::ZERO;
let token_id = random_token_id();

let data: Bytes = fixed_bytes!("deadbeef").into();
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
_ = watch!(contract.mint(alice.address(), token_id))?;

let err = send!(contract.safeMint(bob.address(), token_id, data.clone()))
.expect_err("should not mint with an invalid sender");
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

assert!(err
.reverted_with(Erc721::ERC721InvalidSender { sender: invalid_sender }));
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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.clone()))
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
.expect_err("should not mint when receiver errors with reason");
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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.clone()))
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
.expect_err("should not mint when receiver reverts without reason");
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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.clone()))
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
.expect_err("should not mint when receiver panics");
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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 +1843,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());
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

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