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 13 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
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
273 changes: 273 additions & 0 deletions examples/erc721/tests/erc721.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,238 @@
Ok(())
}

#[e2e::test]
async fn safe_mint_to_eoa(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();

// Test case 1: Blank data

Check warning on line 1361 in examples/erc721/tests/erc721.rs

View workflow job for this annotation

GitHub Actions / nightly / fmt

Diff in /home/runner/work/rust-contracts-stylus/rust-contracts-stylus/examples/erc721/tests/erc721.rs
let token_id = random_token_id();
let data = Bytes::new();
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 Erc721::ownerOfReturn { ownerOf } =
contract.ownerOf(token_id).call().await?;
assert_eq!(alice_addr, ownerOf);
let Erc721::balanceOfReturn { balance } =
contract.balanceOf(alice.address()).call().await?;
let one = uint!(1_U256);
assert_eq!(balance, one);

0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
// Test case 2: Custom data

Check warning on line 1379 in examples/erc721/tests/erc721.rs

View workflow job for this annotation

GitHub Actions / nightly / fmt

Diff in /home/runner/work/rust-contracts-stylus/rust-contracts-stylus/examples/erc721/tests/erc721.rs
bidzyyys marked this conversation as resolved.
Show resolved Hide resolved
let token_id = random_token_id();
let data: Bytes = fixed_bytes!("deadbeef").into();
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 Erc721::ownerOfReturn { ownerOf } =
contract.ownerOf(token_id).call().await?;
assert_eq!(alice_addr, ownerOf);

let Erc721::balanceOfReturn { balance } =
contract.balanceOf(alice.address()).call().await?;
let two = uint!(2_U256);
assert_eq!(balance, two);

Ok(())
}

#[e2e::test]
async fn safe_mint_to_receiver_contract(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?;

// Test case 1: Blank data

Check warning on line 1409 in examples/erc721/tests/erc721.rs

View workflow job for this annotation

GitHub Actions / nightly / fmt

Diff in /home/runner/work/rust-contracts-stylus/rust-contracts-stylus/examples/erc721/tests/erc721.rs
let token_id = random_token_id();
let data = 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(),

Check warning on line 1423 in examples/erc721/tests/erc721.rs

View workflow job for this annotation

GitHub Actions / nightly / fmt

Diff in /home/runner/work/rust-contracts-stylus/rust-contracts-stylus/examples/erc721/tests/erc721.rs
from: Address::ZERO,
tokenId: token_id,
data: 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);
let Erc721::balanceOfReturn { balance } =
contract.balanceOf(receiver_address).call().await?;
let one = uint!(1_U256);
assert_eq!(balance, one);

0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
// Test case 2: Custom data

Check warning on line 1436 in examples/erc721/tests/erc721.rs

View workflow job for this annotation

GitHub Actions / nightly / fmt

Diff in /home/runner/work/rust-contracts-stylus/rust-contracts-stylus/examples/erc721/tests/erc721.rs
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(),

Check warning on line 1450 in examples/erc721/tests/erc721.rs

View workflow job for this annotation

GitHub Actions / nightly / fmt

Diff in /home/runner/work/rust-contracts-stylus/rust-contracts-stylus/examples/erc721/tests/erc721.rs
from: Address::ZERO,
tokenId: token_id,
data: 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);

let Erc721::balanceOfReturn { balance } =
contract.balanceOf(receiver_address).call().await?;
let two = uint!(2_U256);
assert_eq!(balance, two);

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 +1847,47 @@
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
Loading