From e7b1143b574edaa581ac1a253179c4d302e39758 Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Sun, 23 Nov 2025 22:53:41 +0530 Subject: [PATCH 1/7] WIP: writing tests for LibERC721 --- src/token/ERC721/ERC721/LibERC721.sol | 2 +- test/token/ERC20/ERC20/LibERC20.t.sol | 2 +- .../ERC20/ERC20/harnesses/LibERC20Harness.sol | 2 +- test/token/ERC721/ERC721/LibERC721.t.sol | 176 ++++++++++++++++++ .../ERC721/harnesses/LibERC721Harness.sol | 43 +++++ 5 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 test/token/ERC721/ERC721/LibERC721.t.sol create mode 100644 test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol diff --git a/src/token/ERC721/ERC721/LibERC721.sol b/src/token/ERC721/ERC721/LibERC721.sol index 4a0def35..23d20f87 100644 --- a/src/token/ERC721/ERC721/LibERC721.sol +++ b/src/token/ERC721/ERC721/LibERC721.sol @@ -53,7 +53,7 @@ library LibERC721 { } /// @notice Returns the ERC-721 storage struct from its predefined slot. - /// @dev Uses inline assembly to access diamond storage location. + /// @dev Uses inline assembly to access diamond storage location. /// @return s The storage reference for ERC-721 state variables. function getStorage() internal pure returns (ERC721Storage storage s) { bytes32 position = STORAGE_POSITION; diff --git a/test/token/ERC20/ERC20/LibERC20.t.sol b/test/token/ERC20/ERC20/LibERC20.t.sol index de6d2860..668e15f4 100644 --- a/test/token/ERC20/ERC20/LibERC20.t.sol +++ b/test/token/ERC20/ERC20/LibERC20.t.sol @@ -5,7 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibERC20Harness} from "./harnesses/LibERC20Harness.sol"; import {LibERC20} from "../../../../src/token/ERC20/ERC20/LibERC20.sol"; -contract LibERC20Test is Test { +contract LibERC20Test is Test { LibERC20Harness public harness; address public alice; diff --git a/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol b/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol index 8950d267..05c3c535 100644 --- a/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol +++ b/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol @@ -17,7 +17,7 @@ contract LibERC20Harness { } /// @notice Exposes LibERC20.mint as an external function - function mint(address _account, uint256 _value) external { + function mint(address _account, uint256 _value) external { LibERC20.mint(_account, _value); } diff --git a/test/token/ERC721/ERC721/LibERC721.t.sol b/test/token/ERC721/ERC721/LibERC721.t.sol new file mode 100644 index 00000000..6892ed4a --- /dev/null +++ b/test/token/ERC721/ERC721/LibERC721.t.sol @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.30; + +import {Test} from "forge-std/Test.sol"; +import {LibERC721} from "../../../../../src/token/ERC721/ERC721/LibERC721.sol"; +import {LibERC721Harness} from "../harnesses/LibERC721Harness.sol"; + +contract ERC721Test is Test { + LibERC721Harness public harness; + + address public alice; + address public bob; + address public charlie; + + string constant TOKEN_NAME = "Test Token"; + string constant TOKEN_SYMBOL = "TEST"; + string constant BASE_URI = "https://example.com/api/nft/"; + + function setUp() public { + alice = makeAddr("alice"); + bob = makeAddr("bob"); + charlie = makeAddr("charlie"); + + harness = new LibERC721Harness(); + harness.initialize(TOKEN_NAME, TOKEN_SYMBOL, BASE_URI); + } + + // ============================================ + // Metadata Tests + // ============================================ + + function test_Name() public { + assertEq(harness.name(), TOKEN_NAME); + } + + function test_Symbol() public { + assertEq(harness.symbol(), TOKEN_SYMBOL); + } + + function test_BaseURI() public { + assertEq(harness.baseURI(), BASE_URI); + } + + // ============================================ + // Transfer Tests + // ============================================ + + function test_TransferFrom() public { + uint256 tokenId = 1; + + harness.mint(alice, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + + vm.prank(alice); + harness.transferFrom(alice, bob, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], bob); + } + + function test_TransferToSelf() public { + uint256 tokenId = 2; + + harness.mint(charlie, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], charlie); + + vm.prank(charlie); + harness.transferFrom(charlie, charlie, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], charlie); + } + + function test_TransferFuzz(address from, address to, uint256 tokenId) public { + vm.assume(from != address(0)); + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(from, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], from); + + vm.prank(from); + harness.transferFrom(from, to, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], to); + } + + function test_TransferRevertWhen_TransferFromNonExistentToken() public { + uint256 tokenId = 999; + + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721NonexistentToken.selector, tokenId)); + harness.transferFrom(alice, bob, tokenId); + } + + function test_TransferRevertWhenTransferToZeroAddress() public { + uint256 tokenId = 3; + + harness.mint(address(0), tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + + vm.prank(address(0)); + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InvalidReceiver.selector, address(0))); + harness.transferFrom(alice, address(0), tokenId); + } + + function test_TransferRevertWhenSenderIsNotOwnerOrApproved() public { + uint256 tokenId = 4; + + harness.mint(alice, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + + vm.prank(bob); + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InsufficientApproval.selector, bob, tokenId)); + harness.transferFrom(alice, charlie, tokenId); + } + + // ============================================ + // Mint Tests + // ============================================ + + function test_Mint() public { + uint256 tokenId = 5; + + harness.mint(bob, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], bob); + } + + function test_MintMultiple() public { + for (uint256 tokenId = 1; tokenId <= 10; tokenId++) { + harness.mint(charlie, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], charlie); + } + } + + function test_MintFuzz(address to, uint256 tokenId) public { + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(to, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], to); + } + + function test_MintRevertWhenInvalidReceiver() public { + uint256 tokenId = 6; + + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InvalidReceiver.selector, address(0))); + harness.mint(address(0), tokenId); + } + + // ============================================ + // Burn Tests + // ============================================ + + function test_Burn() public { + uint256 tokenId = 7; + + harness.mint(alice, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + + harness.burn(tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], address(0)); + } + + function test_BurnFuzz(address to, uint256 tokenId) public { + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(to, tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], to); + + harness.burn(tokenId); + assertEq(LibERC721.getStorage().ownerOf[tokenId], address(0)); + } + + function test_BurnRevertWhenNonExistentToken() public { + uint256 tokenId = 888; + + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721NonexistentToken.selector, tokenId)); + harness.burn(tokenId); + } +} \ No newline at end of file diff --git a/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol b/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol new file mode 100644 index 00000000..9b8aa29f --- /dev/null +++ b/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.30; + +import {LibERC721} from "../../../../../src/token/ERC721/ERC721/LibERC721.sol"; + +contract LibERC721Harness { + /// @notice Initialize the ERC721 token storage + /// @dev Only used for testing + function initialize(string memory _name, string memory _symbol, string memory _baseURI) external { + LibERC721.ERC721Storage storage s = LibERC721.getStorage(); + s.name = _name; + s.symbol = _symbol; + s.baseURI = _baseURI; + } + + /// @notice Exposes LibERC721.mint as an external function + function mint(address _to, uint256 _tokenId) external { + LibERC721.mint(_to, _tokenId); + } + + /// @notice Exposes LibERC721.burn as an external function + function burn(uint256 _tokenId) external { + LibERC721.burn(_tokenId); + } + + /// @notice Exposes LibERC721.transferFrom as an external function + function transferFrom(address _from, address _to, uint256 _tokenId) external { + LibERC721.transferFrom(_from, _to, _tokenId); + } + + /// @notice Get storage values for testing + function name() external view returns (string memory) { + return LibERC721.getStorage().name; + } + + function symbol() external view returns (string memory) { + return LibERC721.getStorage().symbol; + } + + function baseURI() external view returns (string memory) { + return LibERC721.getStorage().baseURI; + } +} \ No newline at end of file From fe9ea4b8b1b7f5470f6e2f63a5b91a10ab751bbb Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Sun, 23 Nov 2025 23:33:31 +0530 Subject: [PATCH 2/7] chore: tests for LibERC721 --- test/token/ERC721/ERC721/LibERC721.t.sol | 92 +++++++++++-------- .../ERC721/harnesses/LibERC721Harness.sol | 21 ++++- 2 files changed, 70 insertions(+), 43 deletions(-) diff --git a/test/token/ERC721/ERC721/LibERC721.t.sol b/test/token/ERC721/ERC721/LibERC721.t.sol index 6892ed4a..4b492612 100644 --- a/test/token/ERC721/ERC721/LibERC721.t.sol +++ b/test/token/ERC721/ERC721/LibERC721.t.sol @@ -3,7 +3,7 @@ pragma solidity >=0.8.30; import {Test} from "forge-std/Test.sol"; import {LibERC721} from "../../../../../src/token/ERC721/ERC721/LibERC721.sol"; -import {LibERC721Harness} from "../harnesses/LibERC721Harness.sol"; +import {LibERC721Harness} from "./harnesses/LibERC721Harness.sol"; contract ERC721Test is Test { LibERC721Harness public harness; @@ -29,15 +29,15 @@ contract ERC721Test is Test { // Metadata Tests // ============================================ - function test_Name() public { + function test_Name() public view { assertEq(harness.name(), TOKEN_NAME); } - function test_Symbol() public { + function test_Symbol() public view { assertEq(harness.symbol(), TOKEN_SYMBOL); } - function test_BaseURI() public { + function test_BaseURI() public view { assertEq(harness.baseURI(), BASE_URI); } @@ -49,63 +49,67 @@ contract ERC721Test is Test { uint256 tokenId = 1; harness.mint(alice, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + assertEq(harness.ownerOf(tokenId), alice); vm.prank(alice); harness.transferFrom(alice, bob, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], bob); + assertEq(harness.ownerOf(tokenId), bob); } function test_TransferToSelf() public { uint256 tokenId = 2; harness.mint(charlie, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], charlie); + assertEq(harness.ownerOf(tokenId), charlie); vm.prank(charlie); harness.transferFrom(charlie, charlie, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], charlie); + assertEq(harness.ownerOf(tokenId), charlie); } - function test_TransferFuzz(address from, address to, uint256 tokenId) public { + function test_TransferFuzz( + address from, + address to, + uint256 tokenId + ) public { vm.assume(from != address(0)); vm.assume(to != address(0)); - vm.assume(tokenId < type(uint256).max); + vm.assume(tokenId < type(uint256).max); harness.mint(from, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], from); + assertEq(harness.ownerOf(tokenId), from); vm.prank(from); harness.transferFrom(from, to, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], to); + assertEq(harness.ownerOf(tokenId), to); } - function test_TransferRevertWhen_TransferFromNonExistentToken() public { + function test_TransferRevertWhenTransferFromNonExistentToken() public { uint256 tokenId = 999; - vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721NonexistentToken.selector, tokenId)); + vm.expectRevert( + abi.encodeWithSelector( + LibERC721.ERC721NonexistentToken.selector, + tokenId + ) + ); harness.transferFrom(alice, bob, tokenId); } - function test_TransferRevertWhenTransferToZeroAddress() public { - uint256 tokenId = 3; - - harness.mint(address(0), tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); - - vm.prank(address(0)); - vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InvalidReceiver.selector, address(0))); - harness.transferFrom(alice, address(0), tokenId); - } - function test_TransferRevertWhenSenderIsNotOwnerOrApproved() public { uint256 tokenId = 4; harness.mint(alice, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + assertEq(harness.ownerOf(tokenId), alice); vm.prank(bob); - vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InsufficientApproval.selector, bob, tokenId)); + vm.expectRevert( + abi.encodeWithSelector( + LibERC721.ERC721InsufficientApproval.selector, + bob, + tokenId + ) + ); harness.transferFrom(alice, charlie, tokenId); } @@ -117,28 +121,33 @@ contract ERC721Test is Test { uint256 tokenId = 5; harness.mint(bob, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], bob); + assertEq(harness.ownerOf(tokenId), bob); } function test_MintMultiple() public { for (uint256 tokenId = 1; tokenId <= 10; tokenId++) { harness.mint(charlie, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], charlie); + assertEq(harness.ownerOf(tokenId), charlie); } } function test_MintFuzz(address to, uint256 tokenId) public { vm.assume(to != address(0)); - vm.assume(tokenId < type(uint256).max); + vm.assume(tokenId < type(uint256).max); harness.mint(to, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], to); + assertEq(harness.ownerOf(tokenId), to); } function test_MintRevertWhenInvalidReceiver() public { uint256 tokenId = 6; - vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InvalidReceiver.selector, address(0))); + vm.expectRevert( + abi.encodeWithSelector( + LibERC721.ERC721InvalidReceiver.selector, + address(0) + ) + ); harness.mint(address(0), tokenId); } @@ -150,27 +159,32 @@ contract ERC721Test is Test { uint256 tokenId = 7; harness.mint(alice, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], alice); + assertEq(harness.ownerOf(tokenId), alice); harness.burn(tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], address(0)); + assertEq(harness.ownerOf(tokenId), address(0)); } function test_BurnFuzz(address to, uint256 tokenId) public { vm.assume(to != address(0)); - vm.assume(tokenId < type(uint256).max); + vm.assume(tokenId < type(uint256).max); harness.mint(to, tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], to); + assertEq(harness.ownerOf(tokenId), to); harness.burn(tokenId); - assertEq(LibERC721.getStorage().ownerOf[tokenId], address(0)); + assertEq(harness.ownerOf(tokenId), address(0)); } function test_BurnRevertWhenNonExistentToken() public { uint256 tokenId = 888; - vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721NonexistentToken.selector, tokenId)); + vm.expectRevert( + abi.encodeWithSelector( + LibERC721.ERC721NonexistentToken.selector, + tokenId + ) + ); harness.burn(tokenId); } -} \ No newline at end of file +} diff --git a/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol b/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol index 9b8aa29f..12bee153 100644 --- a/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol +++ b/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol @@ -6,7 +6,11 @@ import {LibERC721} from "../../../../../src/token/ERC721/ERC721/LibERC721.sol"; contract LibERC721Harness { /// @notice Initialize the ERC721 token storage /// @dev Only used for testing - function initialize(string memory _name, string memory _symbol, string memory _baseURI) external { + function initialize( + string memory _name, + string memory _symbol, + string memory _baseURI + ) external { LibERC721.ERC721Storage storage s = LibERC721.getStorage(); s.name = _name; s.symbol = _symbol; @@ -14,7 +18,7 @@ contract LibERC721Harness { } /// @notice Exposes LibERC721.mint as an external function - function mint(address _to, uint256 _tokenId) external { + function mint(address _to, uint256 _tokenId) external { LibERC721.mint(_to, _tokenId); } @@ -24,10 +28,19 @@ contract LibERC721Harness { } /// @notice Exposes LibERC721.transferFrom as an external function - function transferFrom(address _from, address _to, uint256 _tokenId) external { + function transferFrom( + address _from, + address _to, + uint256 _tokenId + ) external { LibERC721.transferFrom(_from, _to, _tokenId); } + /// @notice Expose owner lookup for a given token id + function ownerOf(uint256 _tokenId) external view returns (address) { + return LibERC721.getStorage().ownerOf[_tokenId]; + } + /// @notice Get storage values for testing function name() external view returns (string memory) { return LibERC721.getStorage().name; @@ -40,4 +53,4 @@ contract LibERC721Harness { function baseURI() external view returns (string memory) { return LibERC721.getStorage().baseURI; } -} \ No newline at end of file +} From b87625bab9986ffda49a749da30dac24e4a7537d Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Mon, 24 Nov 2025 02:30:07 +0530 Subject: [PATCH 3/7] WIP: writing tests for ERC721Facet --- src/token/ERC20/ERC20/ERC20Facet.sol | 2 +- src/token/ERC721/ERC721/ERC721Facet.sol | 2 +- test/token/ERC20/ERC20/ERC20Facet.t.sol | 2 +- .../ERC20/harnesses/ERC20FacetHarness.sol | 2 +- test/token/ERC721/ERC721/ERC721Facet.t.sol | 269 ++++++++++++++++++ test/token/ERC721/ERC721/LibERC721.t.sol | 4 +- .../ERC721/harnesses/ERC721FacetHarness.sol | 38 +++ 7 files changed, 313 insertions(+), 6 deletions(-) create mode 100644 test/token/ERC721/ERC721/ERC721Facet.t.sol create mode 100644 test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol diff --git a/src/token/ERC20/ERC20/ERC20Facet.sol b/src/token/ERC20/ERC20/ERC20Facet.sol index 27d18271..a6ff99f0 100644 --- a/src/token/ERC20/ERC20/ERC20Facet.sol +++ b/src/token/ERC20/ERC20/ERC20Facet.sol @@ -43,7 +43,7 @@ contract ERC20Facet { /** * @dev ERC-8042 compliant storage struct for ERC20 token data. - * @custom:storage-location erc8042:compose.erc20 + * @custom:storage-location erc8042:compose.erc20 */ struct ERC20Storage { mapping(address owner => uint256 balance) balanceOf; diff --git a/src/token/ERC721/ERC721/ERC721Facet.sol b/src/token/ERC721/ERC721/ERC721Facet.sol index 90a42da4..b099b79b 100644 --- a/src/token/ERC721/ERC721/ERC721Facet.sol +++ b/src/token/ERC721/ERC721/ERC721Facet.sol @@ -191,7 +191,7 @@ contract ERC721Facet { /// @dev Internal function to transfer a token, checking for ownership and approval. /// @param _from The current owner of the token. - /// @param _to The address to receive the token. + /// @param _to The address to receive the token. /// @param _tokenId The token ID to transfer. function internalTransferFrom(address _from, address _to, uint256 _tokenId) internal { ERC721Storage storage s = getStorage(); diff --git a/test/token/ERC20/ERC20/ERC20Facet.t.sol b/test/token/ERC20/ERC20/ERC20Facet.t.sol index 818d36cd..83bd2102 100644 --- a/test/token/ERC20/ERC20/ERC20Facet.t.sol +++ b/test/token/ERC20/ERC20/ERC20Facet.t.sol @@ -14,7 +14,7 @@ contract ERC20FacetTest is Test { string constant TOKEN_NAME = "Test Token"; string constant TOKEN_SYMBOL = "TEST"; - uint8 constant TOKEN_DECIMALS = 18; + uint8 constant TOKEN_DECIMALS = 18; uint256 constant INITIAL_SUPPLY = 1000000e18; event Transfer(address indexed _from, address indexed _to, uint256 _value); diff --git a/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol b/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol index d4dd0952..1e0508c7 100644 --- a/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol +++ b/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol @@ -11,7 +11,7 @@ contract ERC20FacetHarness is ERC20Facet { function initialize(string memory _name, string memory _symbol, uint8 _decimals) external { ERC20Storage storage s = getStorage(); s.name = _name; - s.symbol = _symbol; + s.symbol = _symbol; s.decimals = _decimals; } diff --git a/test/token/ERC721/ERC721/ERC721Facet.t.sol b/test/token/ERC721/ERC721/ERC721Facet.t.sol new file mode 100644 index 00000000..80ed0ac5 --- /dev/null +++ b/test/token/ERC721/ERC721/ERC721Facet.t.sol @@ -0,0 +1,269 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.30; + +import {Test} from "forge-std/Test.sol"; +import {ERC721FacetHarness} from "./harnesses/ERC721FacetHarness.sol"; +import {ERC721Facet} from "../../../../../src/token/ERC721/ERC721/ERC721Facet.sol"; + +contract ERC721FacetTest is Test { + ERC721FacetHarness public harness; + + address public alice; + address public bob; + address public charlie; + + string constant TOKEN_NAME = "Test Token"; + string constant TOKEN_SYMBOL = "TEST"; + string constant BASE_URI = "https://example.com/api/nft/"; + + function setUp() public { + alice = makeAddr("alice"); + bob = makeAddr("bob"); + charlie = makeAddr("charlie"); + + harness = new ERC721FacetHarness(); + harness.initialize(TOKEN_NAME, TOKEN_SYMBOL, BASE_URI); + } + + // ============================================ + // Metadata Tests + // ============================================ + + function test_name() public view { + assertEq(harness.name(), TOKEN_NAME); + } + + function test_symbol() public view { + assertEq(harness.symbol(), TOKEN_SYMBOL); + } + + function test_baseURI() public view { + assertEq(harness.baseURI(), BASE_URI); + } + + // ============================================ + // TokenURI Tests + // ============================================ + + function test_tokenURI() public { + uint256 tokenId = 1; + string memory expectedURI = string(abi.encodePacked(BASE_URI, "1")); + + harness.mint(alice, tokenId); + + string memory tokenURI = ERC721Facet(address(harness)).tokenURI( + tokenId + ); + assertEq(tokenURI, expectedURI); + } + + function test_tokenURIRevertWhenNonExistentToken() public { + uint256 tokenId = 999; + + vm.expectRevert(ERC721Facet.ERC721NonexistentToken.selector); + ERC721Facet(address(harness)).tokenURI(tokenId); + } + + // ============================================ + // Approve Tests + // ============================================ + + function test_Approve() public { + uint256 tokenId = 4; + + harness.mint(alice, tokenId); + + vm.prank(alice); + ERC721Facet(address(harness)).approve(bob, tokenId); + + address approved = ERC721Facet(address(harness)).getApproved(tokenId); + assertEq(approved, bob); + } + + function test_ApproveRevertWhenNonExistentToken() public { + uint256 tokenId = 999; + + vm.prank(alice); + vm.expectRevert(ERC721Facet.ERC721NonexistentToken.selector); + ERC721Facet(address(harness)).approve(bob, tokenId); + } + + function test_ApproveSelfApproval() public { + uint256 tokenId = 6; + + harness.mint(bob, tokenId); + + vm.prank(bob); + ERC721Facet(address(harness)).approve(bob, tokenId); + + address approved = ERC721Facet(address(harness)).getApproved(tokenId); + assertEq(approved, bob); + } + + function test_ApproveClearsOnTransfer() public { + uint256 tokenId = 7; + + harness.mint(alice, tokenId); + + vm.prank(alice); + ERC721Facet(address(harness)).approve(bob, tokenId); + + vm.prank(alice); + ERC721Facet(address(harness)).transferFrom(alice, charlie, tokenId); + + address approved = ERC721Facet(address(harness)).getApproved(tokenId); + assertEq(approved, address(0)); + } + + function test_ApproveRevertWhenApproveToCurrentOwner() public { + uint256 tokenId = 8; + + harness.mint(charlie, tokenId); + + vm.prank(charlie); + vm.expectRevert(ERC721Facet.ERC721InvalidApprover.selector); + ERC721Facet(address(harness)).approve(charlie, tokenId); + } + + function test_ApproveRevertWhenNullAddress() public { + uint256 tokenId = 9; + + harness.mint(bob, tokenId); + + vm.prank(bob); + vm.expectRevert(ERC721Facet.ERC721InvalidApprover.selector); + ERC721Facet(address(harness)).approve(address(0), tokenId); + } + + function test_ApproveFuzz( + address owner, + address operator, + uint256 tokenId + ) public { + vm.assume(owner != address(0)); + vm.assume(operator != address(0)); + vm.assume(owner != operator); + vm.assume(tokenId < type(uint256).max); + + harness.mint(owner, tokenId); + + vm.prank(owner); + ERC721Facet(address(harness)).approve(operator, tokenId); + + address approved = ERC721Facet(address(harness)).getApproved(tokenId); + assertEq(approved, operator); + } + + // =========================================== + // SetApprovalForAll Tests + // =========================================== + + function test_SetApprovalForAll() public { + vm.prank(alice); + ERC721Facet(address(harness)).setApprovalForAll(bob, true); + + bool isApproved = ERC721Facet(address(harness)).isApprovedForAll( + alice, + bob + ); + assertTrue(isApproved); + } + + function test_SetApprovalForAllRevertWhenSelfApproval() public { + vm.prank(charlie); + vm.expectRevert(ERC721Facet.ERC721InvalidApprover.selector); + ERC721Facet(address(harness)).setApprovalForAll(charlie, true); + } + + function test_SetApprovalForAllFuzz( + address owner, + address operator + ) public { + vm.assume(owner != address(0)); + vm.assume(operator != address(0)); + vm.assume(owner != operator); + + vm.prank(owner); + ERC721Facet(address(harness)).setApprovalForAll(operator, true); + + bool isApproved = ERC721Facet(address(harness)).isApprovedForAll( + owner, + operator + ); + assertTrue(isApproved); + } + + // ============================================ + // transferFrom tests + // ============================================ + + function test_transferFrom() public { + uint256 tokenId = 1; + + harness.mint(alice, tokenId); + assertEq(harness.ownerOf(tokenId), alice); + + vm.prank(alice); + harness.transferFrom(alice, bob, tokenId); + assertEq(harness.ownerOf(tokenId), bob); + } + + function test_internalTransferToSelf() public { + uint256 tokenId = 2; + + harness.mint(charlie, tokenId); + assertEq(harness.ownerOf(tokenId), charlie); + + vm.prank(charlie); + harness.transferFrom(charlie, charlie, tokenId); + assertEq(harness.ownerOf(tokenId), charlie); + } + + function test_internalTransferFuzz( + address from, + address to, + uint256 tokenId + ) public { + vm.assume(from != address(0)); + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(from, tokenId); + assertEq(harness.ownerOf(tokenId), from); + + vm.prank(from); + harness.transferFrom(from, to, tokenId); + assertEq(harness.ownerOf(tokenId), to); + } + + function test_internalTransferRevertWhenTransferFromNonExistentToken() + public + { + uint256 tokenId = 999; + + vm.expectRevert( + abi.encodeWithSelector( + ERC721Facet.ERC721NonexistentToken.selector, + tokenId + ) + ); + harness.transferFrom(alice, bob, tokenId); + } + + function test_internalTransferRevertWhenSenderIsNotOwnerOrApproved() + public + { + uint256 tokenId = 4; + + harness.mint(alice, tokenId); + assertEq(harness.ownerOf(tokenId), alice); + + vm.prank(bob); + vm.expectRevert( + abi.encodeWithSelector( + ERC721Facet.ERC721InvalidApprover.selector + ) + ); + harness.transferFrom(alice, charlie, tokenId); + } +} diff --git a/test/token/ERC721/ERC721/LibERC721.t.sol b/test/token/ERC721/ERC721/LibERC721.t.sol index 4b492612..a7f06292 100644 --- a/test/token/ERC721/ERC721/LibERC721.t.sol +++ b/test/token/ERC721/ERC721/LibERC721.t.sol @@ -37,12 +37,12 @@ contract ERC721Test is Test { assertEq(harness.symbol(), TOKEN_SYMBOL); } - function test_BaseURI() public view { + function test_baseURI() public view { assertEq(harness.baseURI(), BASE_URI); } // ============================================ - // Transfer Tests + // TransferFrom Tests // ============================================ function test_TransferFrom() public { diff --git a/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol b/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol new file mode 100644 index 00000000..92e5ced6 --- /dev/null +++ b/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.30; + +import {ERC721Facet} from "../../../../../src/token/ERC721/ERC721/ERC721Facet.sol"; + +contract ERC721FacetHarness is ERC721Facet { + /// @notice Initialize the ERC721 token storage + /// @dev Only used for testing - production diamonds should initialize in constructor + function initialize( + string memory _name, + string memory _symbol, + string memory _baseURI + ) external { + ERC721Storage storage s = getStorage(); + s.name = _name; + s.symbol = _symbol; + s.baseURI = _baseURI; + } + + function baseURI() external view returns(string memory){ + return ERC721Facet.getStorage().baseURI; + } + + /// @notice Mints a new ERC-721 token to the specified address. + /// @dev Reverts if the receiver address is zero or if the token already exists. + /// @param _to The address that will own the newly minted token. + /// @param _tokenId The ID of the token to mint. + function mint(address _to, uint256 _tokenId) public { + ERC721Storage storage s = getStorage(); + require(_to != address(0), "ERC721InvalidReceiver"); + require(s.ownerOf[_tokenId] == address(0), "ERC721InvalidSender"); + s.ownerOf[_tokenId] = _to; + unchecked { + s.balanceOf[_to]++; + } + emit Transfer(address(0), _to, _tokenId); + } +} \ No newline at end of file From 7654fc7fb370c441b3aa35e656443308e32bd58d Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Mon, 24 Nov 2025 02:58:38 +0530 Subject: [PATCH 4/7] chore: tests for ERC721Facet --- test/token/ERC721/ERC721/ERC721Facet.t.sol | 144 +++++++++++++-------- 1 file changed, 90 insertions(+), 54 deletions(-) diff --git a/test/token/ERC721/ERC721/ERC721Facet.t.sol b/test/token/ERC721/ERC721/ERC721Facet.t.sol index 80ed0ac5..b1a7be39 100644 --- a/test/token/ERC721/ERC721/ERC721Facet.t.sol +++ b/test/token/ERC721/ERC721/ERC721Facet.t.sol @@ -57,11 +57,12 @@ contract ERC721FacetTest is Test { assertEq(tokenURI, expectedURI); } - function test_tokenURIRevertWhenNonExistentToken() public { - uint256 tokenId = 999; + function test_tokenOwner() public { + uint256 tokenId = 45; + + harness.mint(alice, tokenId); - vm.expectRevert(ERC721Facet.ERC721NonexistentToken.selector); - ERC721Facet(address(harness)).tokenURI(tokenId); + assertEq(harness.ownerOf(tokenId), alice); } // ============================================ @@ -80,14 +81,6 @@ contract ERC721FacetTest is Test { assertEq(approved, bob); } - function test_ApproveRevertWhenNonExistentToken() public { - uint256 tokenId = 999; - - vm.prank(alice); - vm.expectRevert(ERC721Facet.ERC721NonexistentToken.selector); - ERC721Facet(address(harness)).approve(bob, tokenId); - } - function test_ApproveSelfApproval() public { uint256 tokenId = 6; @@ -115,26 +108,6 @@ contract ERC721FacetTest is Test { assertEq(approved, address(0)); } - function test_ApproveRevertWhenApproveToCurrentOwner() public { - uint256 tokenId = 8; - - harness.mint(charlie, tokenId); - - vm.prank(charlie); - vm.expectRevert(ERC721Facet.ERC721InvalidApprover.selector); - ERC721Facet(address(harness)).approve(charlie, tokenId); - } - - function test_ApproveRevertWhenNullAddress() public { - uint256 tokenId = 9; - - harness.mint(bob, tokenId); - - vm.prank(bob); - vm.expectRevert(ERC721Facet.ERC721InvalidApprover.selector); - ERC721Facet(address(harness)).approve(address(0), tokenId); - } - function test_ApproveFuzz( address owner, address operator, @@ -154,6 +127,20 @@ contract ERC721FacetTest is Test { assertEq(approved, operator); } + function test_getApproved() public { + uint256 tokenId = 4; + + harness.mint(alice, tokenId); + + vm.prank(alice); + ERC721Facet(address(harness)).approve(bob, tokenId); + + address approved = ERC721Facet(address(harness)).getApproved(tokenId); + assertEq(approved, bob); + + assertEq(harness.getApproved(tokenId), bob); + } + // =========================================== // SetApprovalForAll Tests // =========================================== @@ -169,12 +156,6 @@ contract ERC721FacetTest is Test { assertTrue(isApproved); } - function test_SetApprovalForAllRevertWhenSelfApproval() public { - vm.prank(charlie); - vm.expectRevert(ERC721Facet.ERC721InvalidApprover.selector); - ERC721Facet(address(harness)).setApprovalForAll(charlie, true); - } - function test_SetApprovalForAllFuzz( address owner, address operator @@ -208,7 +189,7 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), bob); } - function test_internalTransferToSelf() public { + function test_transferFromToSelf() public { uint256 tokenId = 2; harness.mint(charlie, tokenId); @@ -219,7 +200,7 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), charlie); } - function test_internalTransferFuzz( + function test_transferFromFuzz( address from, address to, uint256 tokenId @@ -236,9 +217,7 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), to); } - function test_internalTransferRevertWhenTransferFromNonExistentToken() - public - { + function test_transferFromRevertWhenTransferFromNonExistentToken() public { uint256 tokenId = 999; vm.expectRevert( @@ -250,20 +229,77 @@ contract ERC721FacetTest is Test { harness.transferFrom(alice, bob, tokenId); } - function test_internalTransferRevertWhenSenderIsNotOwnerOrApproved() - public - { - uint256 tokenId = 4; + // =========================================== + // safeTransferFrom Tests + // =========================================== + + function test_safeTransferFrom() public { + uint256 tokenId = 1; harness.mint(alice, tokenId); assertEq(harness.ownerOf(tokenId), alice); - vm.prank(bob); - vm.expectRevert( - abi.encodeWithSelector( - ERC721Facet.ERC721InvalidApprover.selector - ) - ); - harness.transferFrom(alice, charlie, tokenId); + vm.prank(alice); + harness.safeTransferFrom(alice, bob, tokenId); + assertEq(harness.ownerOf(tokenId), bob); + } + + function test_safeTransferFromToSelf() public { + uint256 tokenId = 2; + + harness.mint(charlie, tokenId); + assertEq(harness.ownerOf(tokenId), charlie); + + vm.prank(charlie); + harness.safeTransferFrom(charlie, charlie, tokenId); + assertEq(harness.ownerOf(tokenId), charlie); + } + + function test_safeTransferFromFuzz( + address from, + address to, + uint256 tokenId + ) public { + vm.assume(from != address(0)); + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(from, tokenId); + assertEq(harness.ownerOf(tokenId), from); + + vm.prank(from); + harness.safeTransferFrom(from, to, tokenId); + assertEq(harness.ownerOf(tokenId), to); + } + + function test_safeTransferFromFuzzWithData( + address from, + address to, + uint256 tokenId + ) public { + vm.assume(from != address(0)); + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(from, tokenId); + assertEq(harness.ownerOf(tokenId), from); + + vm.prank(from); + harness.safeTransferFrom(from, to, tokenId, ""); + assertEq(harness.ownerOf(tokenId), to); + } + + // ==================================== + // balanceOf Tests + // ==================================== + + function test_BalanceOf() public { + uint256 tokenId1 = 32; + uint256 tokenId2 = 45; + + harness.mint(alice, tokenId1); + harness.mint(alice, tokenId2); + + assertEq(harness.balanceOf(alice), 2); } } From b41607ca19235d58c70ddb627a6ef0e200396ac3 Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Mon, 24 Nov 2025 03:31:52 +0530 Subject: [PATCH 5/7] chore: tests for ERC721BurnFacet --- .../token/ERC721/ERC721/ERC721BurnFacet.t.sol | 61 +++++++++++++++++++ .../harnesses/ERC721BurnFacetHarness.sol | 34 +++++++++++ 2 files changed, 95 insertions(+) create mode 100644 test/token/ERC721/ERC721/ERC721BurnFacet.t.sol create mode 100644 test/token/ERC721/ERC721/harnesses/ERC721BurnFacetHarness.sol diff --git a/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol b/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol new file mode 100644 index 00000000..ef693849 --- /dev/null +++ b/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol @@ -0,0 +1,61 @@ +//SPDX-License-Identifier: MIT +pragma solidity >=0.8.30; + +import {Test} from "forge-std/Test.sol"; +import {ERC721BurnFacet} from "../../../../src/token/ERC721/ERC721/ERC721BurnFacet.sol"; +import {ERC721BurnFacetHarness} from "./harnesses/ERC721BurnFacetHarness.sol"; + +contract ERC721BurnFacetTest is Test { + ERC721BurnFacetHarness public harness; + + address public alice; + address public bob; + address public charlie; + + function setUp() public { + alice = makeAddr("alice"); + bob = makeAddr("bob"); + charlie = makeAddr("charlie"); + + harness = new ERC721BurnFacetHarness(); + } + + // ============================================ + // Burn Tests + // ============================================ + + function test_Burn() public { + uint256 tokenId = 7; + + harness.mint(alice, tokenId); + assertEq(harness.ownerOf(tokenId), alice); + + vm.prank(alice); + harness.burn(tokenId); + assertEq(harness.ownerOf(tokenId), address(0)); + } + + function test_BurnFuzz(address to, uint256 tokenId) public { + vm.assume(to != address(0)); + vm.assume(tokenId < type(uint256).max); + + harness.mint(to, tokenId); + assertEq(harness.ownerOf(tokenId), to); + + vm.prank(to); + harness.burn(tokenId); + assertEq(harness.ownerOf(tokenId), address(0)); + } + + function test_BurnRevertWhenNonExistentToken() public { + uint256 tokenId = 888; + + vm.expectRevert( + abi.encodeWithSelector( + ERC721BurnFacet.ERC721NonexistentToken.selector, + tokenId + ) + ); + harness.burn(tokenId); + } +} diff --git a/test/token/ERC721/ERC721/harnesses/ERC721BurnFacetHarness.sol b/test/token/ERC721/ERC721/harnesses/ERC721BurnFacetHarness.sol new file mode 100644 index 00000000..e853bb86 --- /dev/null +++ b/test/token/ERC721/ERC721/harnesses/ERC721BurnFacetHarness.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.30; + +import {ERC721BurnFacet} from "../../../../../src/token/ERC721/ERC721/ERC721BurnFacet.sol"; + +contract ERC721BurnFacetHarness is ERC721BurnFacet { + /// @notice Initialize the ERC721 token storage + /// @dev Only used for testing - production diamonds should initialize in constructor + function initialize() external { + ERC721Storage storage s = getStorage(); + } + + /// @notice Mints a new ERC-721 token to the specified address. + /// @dev Reverts if the receiver address is zero or if the token already exists. + /// @param _to The address that will own the newly minted token. + /// @param _tokenId The ID of the token to mint. + function mint(address _to, uint256 _tokenId) public { + ERC721Storage storage s = getStorage(); + require(_to != address(0), "ERC721InvalidReceiver"); + require(s.ownerOf[_tokenId] == address(0), "ERC721InvalidSender"); + s.ownerOf[_tokenId] = _to; + unchecked { + s.balanceOf[_to]++; + } + emit Transfer(address(0), _to, _tokenId); + } + + /// @notice Returns the owner of a given token ID (raw, does not revert for non-existent tokens). + /// @param _tokenId The token ID to query. + /// @return The address of the token owner (or address(0) if not minted). + function ownerOf(uint256 _tokenId) public view returns (address) { + return getStorage().ownerOf[_tokenId]; + } +} From ca50d65a33a5d92c123558756ab4498a1b98fa7d Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Mon, 24 Nov 2025 03:32:10 +0530 Subject: [PATCH 6/7] format --- src/token/ERC20/ERC20/ERC20Facet.sol | 2 +- src/token/ERC721/ERC721/ERC721Facet.sol | 2 +- src/token/ERC721/ERC721/LibERC721.sol | 2 +- test/token/ERC20/ERC20/ERC20Facet.t.sol | 2 +- test/token/ERC20/ERC20/LibERC20.t.sol | 2 +- .../ERC20/harnesses/ERC20FacetHarness.sol | 2 +- .../ERC20/ERC20/harnesses/LibERC20Harness.sol | 2 +- .../token/ERC721/ERC721/ERC721BurnFacet.t.sol | 7 +-- test/token/ERC721/ERC721/ERC721Facet.t.sol | 52 ++++--------------- test/token/ERC721/ERC721/LibERC721.t.sol | 35 ++----------- .../ERC721/harnesses/ERC721FacetHarness.sol | 12 ++--- .../ERC721/harnesses/LibERC721Harness.sol | 12 +---- 12 files changed, 29 insertions(+), 103 deletions(-) diff --git a/src/token/ERC20/ERC20/ERC20Facet.sol b/src/token/ERC20/ERC20/ERC20Facet.sol index a6ff99f0..27d18271 100644 --- a/src/token/ERC20/ERC20/ERC20Facet.sol +++ b/src/token/ERC20/ERC20/ERC20Facet.sol @@ -43,7 +43,7 @@ contract ERC20Facet { /** * @dev ERC-8042 compliant storage struct for ERC20 token data. - * @custom:storage-location erc8042:compose.erc20 + * @custom:storage-location erc8042:compose.erc20 */ struct ERC20Storage { mapping(address owner => uint256 balance) balanceOf; diff --git a/src/token/ERC721/ERC721/ERC721Facet.sol b/src/token/ERC721/ERC721/ERC721Facet.sol index b099b79b..90a42da4 100644 --- a/src/token/ERC721/ERC721/ERC721Facet.sol +++ b/src/token/ERC721/ERC721/ERC721Facet.sol @@ -191,7 +191,7 @@ contract ERC721Facet { /// @dev Internal function to transfer a token, checking for ownership and approval. /// @param _from The current owner of the token. - /// @param _to The address to receive the token. + /// @param _to The address to receive the token. /// @param _tokenId The token ID to transfer. function internalTransferFrom(address _from, address _to, uint256 _tokenId) internal { ERC721Storage storage s = getStorage(); diff --git a/src/token/ERC721/ERC721/LibERC721.sol b/src/token/ERC721/ERC721/LibERC721.sol index 23d20f87..4a0def35 100644 --- a/src/token/ERC721/ERC721/LibERC721.sol +++ b/src/token/ERC721/ERC721/LibERC721.sol @@ -53,7 +53,7 @@ library LibERC721 { } /// @notice Returns the ERC-721 storage struct from its predefined slot. - /// @dev Uses inline assembly to access diamond storage location. + /// @dev Uses inline assembly to access diamond storage location. /// @return s The storage reference for ERC-721 state variables. function getStorage() internal pure returns (ERC721Storage storage s) { bytes32 position = STORAGE_POSITION; diff --git a/test/token/ERC20/ERC20/ERC20Facet.t.sol b/test/token/ERC20/ERC20/ERC20Facet.t.sol index 83bd2102..818d36cd 100644 --- a/test/token/ERC20/ERC20/ERC20Facet.t.sol +++ b/test/token/ERC20/ERC20/ERC20Facet.t.sol @@ -14,7 +14,7 @@ contract ERC20FacetTest is Test { string constant TOKEN_NAME = "Test Token"; string constant TOKEN_SYMBOL = "TEST"; - uint8 constant TOKEN_DECIMALS = 18; + uint8 constant TOKEN_DECIMALS = 18; uint256 constant INITIAL_SUPPLY = 1000000e18; event Transfer(address indexed _from, address indexed _to, uint256 _value); diff --git a/test/token/ERC20/ERC20/LibERC20.t.sol b/test/token/ERC20/ERC20/LibERC20.t.sol index 668e15f4..de6d2860 100644 --- a/test/token/ERC20/ERC20/LibERC20.t.sol +++ b/test/token/ERC20/ERC20/LibERC20.t.sol @@ -5,7 +5,7 @@ import {Test} from "forge-std/Test.sol"; import {LibERC20Harness} from "./harnesses/LibERC20Harness.sol"; import {LibERC20} from "../../../../src/token/ERC20/ERC20/LibERC20.sol"; -contract LibERC20Test is Test { +contract LibERC20Test is Test { LibERC20Harness public harness; address public alice; diff --git a/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol b/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol index 1e0508c7..d4dd0952 100644 --- a/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol +++ b/test/token/ERC20/ERC20/harnesses/ERC20FacetHarness.sol @@ -11,7 +11,7 @@ contract ERC20FacetHarness is ERC20Facet { function initialize(string memory _name, string memory _symbol, uint8 _decimals) external { ERC20Storage storage s = getStorage(); s.name = _name; - s.symbol = _symbol; + s.symbol = _symbol; s.decimals = _decimals; } diff --git a/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol b/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol index 05c3c535..8950d267 100644 --- a/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol +++ b/test/token/ERC20/ERC20/harnesses/LibERC20Harness.sol @@ -17,7 +17,7 @@ contract LibERC20Harness { } /// @notice Exposes LibERC20.mint as an external function - function mint(address _account, uint256 _value) external { + function mint(address _account, uint256 _value) external { LibERC20.mint(_account, _value); } diff --git a/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol b/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol index ef693849..92fceb83 100644 --- a/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol +++ b/test/token/ERC721/ERC721/ERC721BurnFacet.t.sol @@ -50,12 +50,7 @@ contract ERC721BurnFacetTest is Test { function test_BurnRevertWhenNonExistentToken() public { uint256 tokenId = 888; - vm.expectRevert( - abi.encodeWithSelector( - ERC721BurnFacet.ERC721NonexistentToken.selector, - tokenId - ) - ); + vm.expectRevert(abi.encodeWithSelector(ERC721BurnFacet.ERC721NonexistentToken.selector, tokenId)); harness.burn(tokenId); } } diff --git a/test/token/ERC721/ERC721/ERC721Facet.t.sol b/test/token/ERC721/ERC721/ERC721Facet.t.sol index b1a7be39..68bdabf7 100644 --- a/test/token/ERC721/ERC721/ERC721Facet.t.sol +++ b/test/token/ERC721/ERC721/ERC721Facet.t.sol @@ -51,15 +51,13 @@ contract ERC721FacetTest is Test { harness.mint(alice, tokenId); - string memory tokenURI = ERC721Facet(address(harness)).tokenURI( - tokenId - ); + string memory tokenURI = ERC721Facet(address(harness)).tokenURI(tokenId); assertEq(tokenURI, expectedURI); } function test_tokenOwner() public { uint256 tokenId = 45; - + harness.mint(alice, tokenId); assertEq(harness.ownerOf(tokenId), alice); @@ -108,11 +106,7 @@ contract ERC721FacetTest is Test { assertEq(approved, address(0)); } - function test_ApproveFuzz( - address owner, - address operator, - uint256 tokenId - ) public { + function test_ApproveFuzz(address owner, address operator, uint256 tokenId) public { vm.assume(owner != address(0)); vm.assume(operator != address(0)); vm.assume(owner != operator); @@ -149,17 +143,11 @@ contract ERC721FacetTest is Test { vm.prank(alice); ERC721Facet(address(harness)).setApprovalForAll(bob, true); - bool isApproved = ERC721Facet(address(harness)).isApprovedForAll( - alice, - bob - ); + bool isApproved = ERC721Facet(address(harness)).isApprovedForAll(alice, bob); assertTrue(isApproved); } - function test_SetApprovalForAllFuzz( - address owner, - address operator - ) public { + function test_SetApprovalForAllFuzz(address owner, address operator) public { vm.assume(owner != address(0)); vm.assume(operator != address(0)); vm.assume(owner != operator); @@ -167,10 +155,7 @@ contract ERC721FacetTest is Test { vm.prank(owner); ERC721Facet(address(harness)).setApprovalForAll(operator, true); - bool isApproved = ERC721Facet(address(harness)).isApprovedForAll( - owner, - operator - ); + bool isApproved = ERC721Facet(address(harness)).isApprovedForAll(owner, operator); assertTrue(isApproved); } @@ -200,11 +185,7 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), charlie); } - function test_transferFromFuzz( - address from, - address to, - uint256 tokenId - ) public { + function test_transferFromFuzz(address from, address to, uint256 tokenId) public { vm.assume(from != address(0)); vm.assume(to != address(0)); vm.assume(tokenId < type(uint256).max); @@ -220,12 +201,7 @@ contract ERC721FacetTest is Test { function test_transferFromRevertWhenTransferFromNonExistentToken() public { uint256 tokenId = 999; - vm.expectRevert( - abi.encodeWithSelector( - ERC721Facet.ERC721NonexistentToken.selector, - tokenId - ) - ); + vm.expectRevert(abi.encodeWithSelector(ERC721Facet.ERC721NonexistentToken.selector, tokenId)); harness.transferFrom(alice, bob, tokenId); } @@ -255,11 +231,7 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), charlie); } - function test_safeTransferFromFuzz( - address from, - address to, - uint256 tokenId - ) public { + function test_safeTransferFromFuzz(address from, address to, uint256 tokenId) public { vm.assume(from != address(0)); vm.assume(to != address(0)); vm.assume(tokenId < type(uint256).max); @@ -272,11 +244,7 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), to); } - function test_safeTransferFromFuzzWithData( - address from, - address to, - uint256 tokenId - ) public { + function test_safeTransferFromFuzzWithData(address from, address to, uint256 tokenId) public { vm.assume(from != address(0)); vm.assume(to != address(0)); vm.assume(tokenId < type(uint256).max); diff --git a/test/token/ERC721/ERC721/LibERC721.t.sol b/test/token/ERC721/ERC721/LibERC721.t.sol index a7f06292..02de549d 100644 --- a/test/token/ERC721/ERC721/LibERC721.t.sol +++ b/test/token/ERC721/ERC721/LibERC721.t.sol @@ -67,11 +67,7 @@ contract ERC721Test is Test { assertEq(harness.ownerOf(tokenId), charlie); } - function test_TransferFuzz( - address from, - address to, - uint256 tokenId - ) public { + function test_TransferFuzz(address from, address to, uint256 tokenId) public { vm.assume(from != address(0)); vm.assume(to != address(0)); vm.assume(tokenId < type(uint256).max); @@ -87,12 +83,7 @@ contract ERC721Test is Test { function test_TransferRevertWhenTransferFromNonExistentToken() public { uint256 tokenId = 999; - vm.expectRevert( - abi.encodeWithSelector( - LibERC721.ERC721NonexistentToken.selector, - tokenId - ) - ); + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721NonexistentToken.selector, tokenId)); harness.transferFrom(alice, bob, tokenId); } @@ -103,13 +94,7 @@ contract ERC721Test is Test { assertEq(harness.ownerOf(tokenId), alice); vm.prank(bob); - vm.expectRevert( - abi.encodeWithSelector( - LibERC721.ERC721InsufficientApproval.selector, - bob, - tokenId - ) - ); + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InsufficientApproval.selector, bob, tokenId)); harness.transferFrom(alice, charlie, tokenId); } @@ -142,12 +127,7 @@ contract ERC721Test is Test { function test_MintRevertWhenInvalidReceiver() public { uint256 tokenId = 6; - vm.expectRevert( - abi.encodeWithSelector( - LibERC721.ERC721InvalidReceiver.selector, - address(0) - ) - ); + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721InvalidReceiver.selector, address(0))); harness.mint(address(0), tokenId); } @@ -179,12 +159,7 @@ contract ERC721Test is Test { function test_BurnRevertWhenNonExistentToken() public { uint256 tokenId = 888; - vm.expectRevert( - abi.encodeWithSelector( - LibERC721.ERC721NonexistentToken.selector, - tokenId - ) - ); + vm.expectRevert(abi.encodeWithSelector(LibERC721.ERC721NonexistentToken.selector, tokenId)); harness.burn(tokenId); } } diff --git a/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol b/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol index 92e5ced6..a2723333 100644 --- a/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol +++ b/test/token/ERC721/ERC721/harnesses/ERC721FacetHarness.sol @@ -6,20 +6,16 @@ import {ERC721Facet} from "../../../../../src/token/ERC721/ERC721/ERC721Facet.so contract ERC721FacetHarness is ERC721Facet { /// @notice Initialize the ERC721 token storage /// @dev Only used for testing - production diamonds should initialize in constructor - function initialize( - string memory _name, - string memory _symbol, - string memory _baseURI - ) external { + function initialize(string memory _name, string memory _symbol, string memory _baseURI) external { ERC721Storage storage s = getStorage(); s.name = _name; s.symbol = _symbol; s.baseURI = _baseURI; } - function baseURI() external view returns(string memory){ + function baseURI() external view returns (string memory) { return ERC721Facet.getStorage().baseURI; - } + } /// @notice Mints a new ERC-721 token to the specified address. /// @dev Reverts if the receiver address is zero or if the token already exists. @@ -35,4 +31,4 @@ contract ERC721FacetHarness is ERC721Facet { } emit Transfer(address(0), _to, _tokenId); } -} \ No newline at end of file +} diff --git a/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol b/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol index 12bee153..5ea7eb1a 100644 --- a/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol +++ b/test/token/ERC721/ERC721/harnesses/LibERC721Harness.sol @@ -6,11 +6,7 @@ import {LibERC721} from "../../../../../src/token/ERC721/ERC721/LibERC721.sol"; contract LibERC721Harness { /// @notice Initialize the ERC721 token storage /// @dev Only used for testing - function initialize( - string memory _name, - string memory _symbol, - string memory _baseURI - ) external { + function initialize(string memory _name, string memory _symbol, string memory _baseURI) external { LibERC721.ERC721Storage storage s = LibERC721.getStorage(); s.name = _name; s.symbol = _symbol; @@ -28,11 +24,7 @@ contract LibERC721Harness { } /// @notice Exposes LibERC721.transferFrom as an external function - function transferFrom( - address _from, - address _to, - uint256 _tokenId - ) external { + function transferFrom(address _from, address _to, uint256 _tokenId) external { LibERC721.transferFrom(_from, _to, _tokenId); } From c84aeb0514ba19a0af82d93b9bf3abaf0344abad Mon Sep 17 00:00:00 2001 From: Abhivansh <31abhivanshj@gmail.com> Date: Mon, 24 Nov 2025 03:38:13 +0530 Subject: [PATCH 7/7] fix: fix the ci checks --- test/token/ERC721/ERC721/ERC721Facet.t.sol | 26 ---------------------- 1 file changed, 26 deletions(-) diff --git a/test/token/ERC721/ERC721/ERC721Facet.t.sol b/test/token/ERC721/ERC721/ERC721Facet.t.sol index 68bdabf7..22b78ef6 100644 --- a/test/token/ERC721/ERC721/ERC721Facet.t.sol +++ b/test/token/ERC721/ERC721/ERC721Facet.t.sol @@ -231,32 +231,6 @@ contract ERC721FacetTest is Test { assertEq(harness.ownerOf(tokenId), charlie); } - function test_safeTransferFromFuzz(address from, address to, uint256 tokenId) public { - vm.assume(from != address(0)); - vm.assume(to != address(0)); - vm.assume(tokenId < type(uint256).max); - - harness.mint(from, tokenId); - assertEq(harness.ownerOf(tokenId), from); - - vm.prank(from); - harness.safeTransferFrom(from, to, tokenId); - assertEq(harness.ownerOf(tokenId), to); - } - - function test_safeTransferFromFuzzWithData(address from, address to, uint256 tokenId) public { - vm.assume(from != address(0)); - vm.assume(to != address(0)); - vm.assume(tokenId < type(uint256).max); - - harness.mint(from, tokenId); - assertEq(harness.ownerOf(tokenId), from); - - vm.prank(from); - harness.safeTransferFrom(from, to, tokenId, ""); - assertEq(harness.ownerOf(tokenId), to); - } - // ==================================== // balanceOf Tests // ====================================