From 0d73284faecd1f88199ea3427b57d47c7e143134 Mon Sep 17 00:00:00 2001 From: Stanley Date: Wed, 4 Sep 2024 09:42:01 -0400 Subject: [PATCH] created tests for tokenIdERC1155 --- src/core/token/ERC1155Core.sol | 2 +- .../{tokenId1155.sol => tokenIdERC1155.sol} | 6 +- test/module/minting/ClaimableERC1155.t.sol | 9 +- test/module/minting/MintableERC1155.t.sol | 9 +- test/module/royalty/RoyaltyERC1155.t.sol | 4 + test/module/tokenId/tokenIdERC1155.t.sol | 115 ++++++++++++++++++ .../transferable/TransferableERC1155.t.sol | 6 +- 7 files changed, 142 insertions(+), 9 deletions(-) rename src/module/token/tokenId/{tokenId1155.sol => tokenIdERC1155.sol} (94%) create mode 100644 test/module/tokenId/tokenIdERC1155.t.sol diff --git a/src/core/token/ERC1155Core.sol b/src/core/token/ERC1155Core.sol index b06ba911..826b62fa 100644 --- a/src/core/token/ERC1155Core.sol +++ b/src/core/token/ERC1155Core.sol @@ -143,7 +143,7 @@ contract ERC1155Core is ERC1155, Core, Multicallable, EIP712 { override returns (SupportedCallbackFunction[] memory supportedCallbackFunctions) { - supportedCallbackFunctions = new SupportedCallbackFunction[](8); + supportedCallbackFunctions = new SupportedCallbackFunction[](9); supportedCallbackFunctions[0] = SupportedCallbackFunction({ selector: BeforeMintCallbackERC1155.beforeMintERC1155.selector, mode: CallbackMode.REQUIRED diff --git a/src/module/token/tokenId/tokenId1155.sol b/src/module/token/tokenId/tokenIdERC1155.sol similarity index 94% rename from src/module/token/tokenId/tokenId1155.sol rename to src/module/token/tokenId/tokenIdERC1155.sol index e058772c..217a3ee7 100644 --- a/src/module/token/tokenId/tokenId1155.sol +++ b/src/module/token/tokenId/tokenIdERC1155.sol @@ -45,7 +45,7 @@ contract TokenIdERC1155 is Module, UpdateTokenIdCallbackERC1155 { config.callbackFunctions = new CallbackFunction[](1); config.fallbackFunctions = new FallbackFunction[](1); - config.callbackFunctions[0] = CallbackFunction(this.updateTokenId.selector); + config.callbackFunctions[0] = CallbackFunction(this.updateTokenIdERC1155.selector); config.fallbackFunctions[0] = FallbackFunction({selector: this.getNextTokenId.selector, permissionBits: 0}); @@ -57,7 +57,7 @@ contract TokenIdERC1155 is Module, UpdateTokenIdCallbackERC1155 { CALLBACK FUNCTION //////////////////////////////////////////////////////////////*/ - function updateTokenId(uint256 _tokenId, uint256 _amount) external returns (uint256) { + function updateTokenIdERC1155(uint256 _tokenId, uint256 _amount) external payable override returns (uint256) { uint256 _nextTokenId = _tokenIdStorage().nextTokenId; if (_tokenId == type(uint256).max) { @@ -66,7 +66,7 @@ contract TokenIdERC1155 is Module, UpdateTokenIdCallbackERC1155 { return _nextTokenId; } - if (_tokenId >= _nextTokenId) { + if (_tokenId > _nextTokenId) { revert TokenIdInvalidTokenId(); } diff --git a/test/module/minting/ClaimableERC1155.t.sol b/test/module/minting/ClaimableERC1155.t.sol index aca34a30..d07af21f 100644 --- a/test/module/minting/ClaimableERC1155.t.sol +++ b/test/module/minting/ClaimableERC1155.t.sol @@ -21,6 +21,7 @@ import {IModuleConfig} from "src/interface/IModuleConfig.sol"; import {BatchMetadataERC1155} from "src/module/token/metadata/BatchMetadataERC1155.sol"; import {BatchMetadataERC721} from "src/module/token/metadata/BatchMetadataERC721.sol"; import {ClaimableERC1155} from "src/module/token/minting/ClaimableERC1155.sol"; +import {TokenIdERC1155} from "src/module/token/tokenId/tokenIdERC1155.sol"; contract MockCurrency is ERC20 { @@ -46,6 +47,7 @@ contract ClaimableERC1155Test is Test { ClaimableERC1155 public claimableModule; BatchMetadataERC1155 public batchMetadataModule; + TokenIdERC1155 public tokenIdModule; uint256 ownerPrivateKey = 1; address public owner; @@ -108,6 +110,7 @@ contract ClaimableERC1155Test is Test { core = new ERC1155Core("test", "TEST", "", owner, modules, moduleData); claimableModule = new ClaimableERC1155(); batchMetadataModule = new BatchMetadataERC1155(); + tokenIdModule = new TokenIdERC1155(); // install module bytes memory encodedInstallParams = abi.encode(owner); @@ -115,9 +118,11 @@ contract ClaimableERC1155Test is Test { core.installModule(address(claimableModule), encodedInstallParams); // install module - bytes memory encodedBatchMetadataInstallParams = ""; vm.prank(owner); - core.installModule(address(batchMetadataModule), encodedBatchMetadataInstallParams); + core.installModule(address(batchMetadataModule), ""); + + vm.prank(owner); + core.installModule(address(tokenIdModule), ""); // Setup signature vars typehashClaimSignatureParams = diff --git a/test/module/minting/MintableERC1155.t.sol b/test/module/minting/MintableERC1155.t.sol index 04db1f62..bf3ba951 100644 --- a/test/module/minting/MintableERC1155.t.sol +++ b/test/module/minting/MintableERC1155.t.sol @@ -21,6 +21,7 @@ import {IModuleConfig} from "src/interface/IModuleConfig.sol"; import {BatchMetadataERC1155} from "src/module/token/metadata/BatchMetadataERC1155.sol"; import {BatchMetadataERC721} from "src/module/token/metadata/BatchMetadataERC721.sol"; import {MintableERC1155} from "src/module/token/minting/MintableERC1155.sol"; +import {TokenIdERC1155} from "src/module/token/tokenId/tokenIdERC1155.sol"; contract MockCurrency is ERC20 { @@ -46,6 +47,7 @@ contract MintableERC1155Test is Test { MintableERC1155 public mintableModule; BatchMetadataERC1155 public batchMetadataModule; + TokenIdERC1155 public tokenIdModule; uint256 ownerPrivateKey = 1; address public owner; @@ -107,15 +109,18 @@ contract MintableERC1155Test is Test { core = new ERC1155Core("test", "TEST", "", owner, modules, moduleData); mintableModule = new MintableERC1155(); batchMetadataModule = new BatchMetadataERC1155(); + tokenIdModule = new TokenIdERC1155(); // install module bytes memory encodedInstallParams = abi.encode(owner); vm.prank(owner); core.installModule(address(mintableModule), encodedInstallParams); - bytes memory encodedBatchMetadataInstallParams = ""; vm.prank(owner); - core.installModule(address(batchMetadataModule), encodedBatchMetadataInstallParams); + core.installModule(address(batchMetadataModule), ""); + + vm.prank(owner); + core.installModule(address(tokenIdModule), ""); // Setup signature vars typehashMintSignatureParams = diff --git a/test/module/royalty/RoyaltyERC1155.t.sol b/test/module/royalty/RoyaltyERC1155.t.sol index 5a82df00..567da9c3 100644 --- a/test/module/royalty/RoyaltyERC1155.t.sol +++ b/test/module/royalty/RoyaltyERC1155.t.sol @@ -18,6 +18,7 @@ import {IModuleConfig} from "src/interface/IModuleConfig.sol"; import {MintableERC1155} from "src/module/token/minting/MintableERC1155.sol"; import {RoyaltyERC1155} from "src/module/token/royalty/RoyaltyERC1155.sol"; +import {TokenIdERC1155} from "src/module/token/tokenId/tokenIdERC1155.sol"; contract RoyaltyExt is RoyaltyERC1155 {} @@ -49,6 +50,7 @@ contract RoyaltyERC1155Test is Test { MintableERC1155 public mintableModuleImplementation; TransferToken public transferTokenContract; + TokenIdERC1155 public tokenIdModule; ITransferValidator public mockTransferValidator; uint256 ownerPrivateKey = 1; @@ -110,6 +112,7 @@ contract RoyaltyERC1155Test is Test { core = new ERC1155Core("test", "TEST", "", owner, modules, moduleData); moduleImplementation = new RoyaltyExt(); mintableModuleImplementation = new MintableERC1155(); + tokenIdModule = new TokenIdERC1155(); transferTokenContract = new TransferToken(); @@ -120,6 +123,7 @@ contract RoyaltyERC1155Test is Test { vm.startPrank(owner); core.installModule(address(moduleImplementation), moduleInitializeData); core.installModule(address(mintableModuleImplementation), mintableModuleInitializeData); + core.installModule(address(tokenIdModule), ""); vm.stopPrank(); typehashMintSignatureParams = diff --git a/test/module/tokenId/tokenIdERC1155.t.sol b/test/module/tokenId/tokenIdERC1155.t.sol new file mode 100644 index 00000000..ffa36ea8 --- /dev/null +++ b/test/module/tokenId/tokenIdERC1155.t.sol @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +import "lib/forge-std/src/console.sol"; + +import {OwnableRoles} from "@solady/auth/OwnableRoles.sol"; +import {ERC20} from "@solady/tokens/ERC20.sol"; +import {Test} from "forge-std/Test.sol"; + +// Target contract + +import {Core} from "src/Core.sol"; +import {Module} from "src/Module.sol"; + +import {Role} from "src/Role.sol"; +import {ERC1155Core} from "src/core/token/ERC1155Core.sol"; + +import {ICore} from "src/interface/ICore.sol"; +import {IModuleConfig} from "src/interface/IModuleConfig.sol"; + +import {BatchMetadataERC1155} from "src/module/token/metadata/BatchMetadataERC1155.sol"; +import {BatchMetadataERC721} from "src/module/token/metadata/BatchMetadataERC721.sol"; +import {MintableERC1155} from "src/module/token/minting/MintableERC1155.sol"; +import {TokenIdERC1155} from "src/module/token/tokenId/tokenIdERC1155.sol"; + +contract MockCurrency is ERC20 { + + function mintTo(address _recipient, uint256 _amount) public { + _mint(_recipient, _amount); + } + + /// @dev Returns the name of the token. + function name() public view virtual override returns (string memory) { + return "MockCurrency"; + } + + /// @dev Returns the symbol of the token. + function symbol() public view virtual override returns (string memory) { + return "MOCK"; + } + +} + +contract MintableERC1155Test is Test { + + ERC1155Core public core; + + MintableERC1155 public mintableModule; + BatchMetadataERC1155 public batchMetadataModule; + TokenIdERC1155 public tokenIdModule; + + uint256 ownerPrivateKey = 1; + address public owner; + + address tokenRecipient = address(0x123); + + MintableERC1155.MintSignatureParamsERC1155 public mintRequest; + + // Constants + address private constant NATIVE_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + function setUp() public { + owner = vm.addr(ownerPrivateKey); + + address[] memory modules; + bytes[] memory moduleData; + + core = new ERC1155Core("test", "TEST", "", owner, modules, moduleData); + mintableModule = new MintableERC1155(); + batchMetadataModule = new BatchMetadataERC1155(); + tokenIdModule = new TokenIdERC1155(); + + // install module + bytes memory encodedInstallParams = abi.encode(owner); + vm.prank(owner); + core.installModule(address(mintableModule), encodedInstallParams); + + vm.prank(owner); + core.installModule(address(batchMetadataModule), ""); + + vm.prank(owner); + core.installModule(address(tokenIdModule), ""); + + // Give permissioned actor minter role + vm.prank(owner); + core.grantRoles(owner, Role._MINTER_ROLE); + } + + /*////////////////////////////////////////////////////////////// + Tests: beforeMintERC1155 + //////////////////////////////////////////////////////////////*/ + + function test_state_updateTokenId() public { + assertEq(TokenIdERC1155(address(core)).getNextTokenId(), 0); + + // increments the tokenId + vm.prank(owner); + core.mint(owner, type(uint256).max, 1, "", ""); + + assertEq(TokenIdERC1155(address(core)).getNextTokenId(), 1); + + // does not increment the tokenId + vm.prank(owner); + core.mint(owner, 1, 1, "", ""); + + assertEq(TokenIdERC1155(address(core)).getNextTokenId(), 1); + } + + function test_revert_updateTokenId() public { + vm.expectRevert(TokenIdERC1155.TokenIdInvalidTokenId.selector); + vm.prank(owner); + core.mint(owner, 2, 1, "", ""); + } + +} diff --git a/test/module/transferable/TransferableERC1155.t.sol b/test/module/transferable/TransferableERC1155.t.sol index 9d8e4cfb..98af1dad 100644 --- a/test/module/transferable/TransferableERC1155.t.sol +++ b/test/module/transferable/TransferableERC1155.t.sol @@ -27,10 +27,14 @@ contract Core is ERC1155Core { bytes[] memory moduleInstallData ) ERC1155Core(name, symbol, contractURI, owner, modules, moduleInstallData) {} - // disable mint and approve callbacks for these tests + // disable mint, approve and tokenId callbacks for these tests function _beforeMint(address to, uint256 tokenId, uint256 value, bytes memory data) internal override {} function _beforeApproveForAll(address from, address to, bool approved) internal override {} + function _updateTokenId(uint256 tokenId, uint256 amount) internal override returns (uint256) { + return tokenId; + } + } contract TransferableERC1155Test is Test {