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 6 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
1 change: 0 additions & 1 deletion examples/erc721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ impl Erc721Example {
Ok(())
}

#[selector(name = "safeMint")]
pub fn safe_mint(
&mut self,
to: 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 safeMint(address to, uint256 tokenId, bytes calldata data) external;
function mint(address to, uint256 tokenId) external;
function burn(uint256 tokenId) external;

Expand Down
175 changes: 76 additions & 99 deletions examples/erc721/tests/erc721.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,113 +1353,92 @@ async fn is_approved_for_all_invalid_operator(
}

#[e2e::test]
async fn safe_mint_to_valid_address_with_data(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);
async fn safe_mint_to_eoa(alice: Account) -> eyre::Result<()> {
let test_cases = vec![
("blank data", Bytes::new()),
("custom data", fixed_bytes!("deadbeef").into()),
];

let alice_addr = alice.address();
let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();
for (_, data) in test_cases {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
let alice_addr = alice.address();
let token_id = random_token_id();

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

assert!(receipt.emits(Erc721::Transfer {
from: Address::ZERO,
to: alice_addr,
tokenId: token_id,
}));
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::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);
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(
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 } =
contract.ownerOf(token_id).call().await?;
assert_eq!(receiver_address, ownerOf);
async fn safe_mint_to_receiver_contract(alice: Account) -> eyre::Result<()> {
let test_cases = vec![
("blank data", Bytes::new()),
("custom data", fixed_bytes!("deadbeef").into()),
];
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
for (_, data) in test_cases {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract = Erc721::new(contract_addr, &alice.wallet);

#[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 receiver_address =
receiver::deploy(&alice.wallet, ERC721ReceiverMock::RevertType::None)
.await?;
let token_id = random_token_id();

let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();
let receipt = receipt!(contract.safeMint(
receiver_address,
token_id,
data.clone()
))?;

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(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,
}));

assert!(receipt.emits(ERC721ReceiverMock::Received {
operator: alice.address(),
from: Address::ZERO,
tokenId: token_id,
data,
}));
let Erc721::ownerOfReturn { ownerOf } =
contract.ownerOf(token_id).call().await?;
assert_eq!(receiver_address, ownerOf);

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

Ok(())
}

#[e2e::test]
async fn error_when_safe_mint_to_contract_without_receiver_interface_with_data(
async fn error_when_safe_mint_to_invalid_receiver_contract(
alice: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
Expand All @@ -1468,7 +1447,7 @@ async fn error_when_safe_mint_to_contract_without_receiver_interface_with_data(
let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err = send!(contract.safeMint(contract_addr, token_id, data.clone()))
let err = send!(contract.safeMint(contract_addr, token_id, data))
.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 {
Expand All @@ -1486,17 +1465,16 @@ async fn error_when_safe_mint_to_invalid_sender_with_data(
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();

_ = 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");
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: invalid_sender }));
.reverted_with(Erc721::ERC721InvalidSender { sender: Address::ZERO }));

Ok(())
}
Expand All @@ -1517,9 +1495,8 @@ async fn error_when_receiver_reverts_with_reason_on_safe_mint_with_data(
let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err =
send!(contract.safeMint(receiver_address, token_id, data.clone()))
.expect_err("should not mint when receiver errors with reason");
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()
Expand All @@ -1544,9 +1521,10 @@ async fn error_when_receiver_reverts_without_reason_on_safe_mint_with_data(
let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err =
send!(contract.safeMint(receiver_address, token_id, data.clone()))
.expect_err("should not mint when receiver reverts without reason");
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
Expand All @@ -1569,9 +1547,8 @@ async fn error_when_receiver_panics_on_safe_mint_with_data(
let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();

let err =
send!(contract.safeMint(receiver_address, token_id, data.clone()))
.expect_err("should not mint when receiver panics");
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)
Expand Down
Loading