From a14110b966e4b5a301f9d8f6a9e44de678993c5e Mon Sep 17 00:00:00 2001 From: Allan Almeida Date: Wed, 15 Nov 2023 14:49:31 +1100 Subject: [PATCH] [GMS-1172] Add metadata fields to ERC1155 preset contract (#133) * add contractURI * rename file * add tests for contractURI * add failing tests * add baseURI --------- Co-authored-by: Allan Almeida --- .../erc1155/abstract/ImmutableERC1155Base.sol | 17 ++++++- .../erc1155/preset/draft-ImmutableERC1155.sol | 3 +- ...ERC1155Test.sol => ImmutableERC1155.t.sol} | 49 ++++++++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) rename test/token/erc1155/{ImmutableERC1155Test.sol => ImmutableERC1155.t.sol} (63%) diff --git a/contracts/token/erc1155/abstract/ImmutableERC1155Base.sol b/contracts/token/erc1155/abstract/ImmutableERC1155Base.sol index 49274887..ac29dccb 100644 --- a/contracts/token/erc1155/abstract/ImmutableERC1155Base.sol +++ b/contracts/token/erc1155/abstract/ImmutableERC1155Base.sol @@ -1,7 +1,8 @@ //SPDX-License-Identifier: Apache 2.0 pragma solidity 0.8.19; -import "contracts/token/erc1155/abstract/ERC1155Permit.Sol"; +import "../../../token/erc1155/abstract/ERC1155Permit.Sol"; + // Allowlist import "@openzeppelin/contracts/token/common/ERC2981.sol"; import "../../../allowlist/OperatorAllowlistEnforced.sol"; @@ -14,6 +15,11 @@ abstract contract ImmutableERC1155Base is ERC1155Permit, ERC2981 { + /// @dev Contract level metadata + string public contractURI; + + /// @dev Common URIs for individual token URIs + string public baseURI; // Optional mapping for token URIs mapping(uint256 => string) private _tokenURIs; @@ -36,6 +42,7 @@ abstract contract ImmutableERC1155Base is address owner, string memory name_, string memory baseURI_, + string memory contractURI_, address _operatorAllowlist, address _receiver, uint96 _feeNumerator @@ -44,6 +51,8 @@ abstract contract ImmutableERC1155Base is _grantRole(DEFAULT_ADMIN_ROLE, owner); _setDefaultRoyalty(_receiver, _feeNumerator); _setOperatorAllowlistRegistry(_operatorAllowlist); + contractURI = contractURI_; + baseURI = baseURI_; } /** @@ -133,6 +142,12 @@ abstract contract ImmutableERC1155Base is */ function setBaseURI(string memory baseURI_) public onlyRole(DEFAULT_ADMIN_ROLE) { _setURI(baseURI_); + baseURI = baseURI_; + } + + /// @dev Allows admin to set the contract URI + function setContractURI(string memory contractURI_) public onlyRole(DEFAULT_ADMIN_ROLE) { + contractURI = contractURI_; } /** diff --git a/contracts/token/erc1155/preset/draft-ImmutableERC1155.sol b/contracts/token/erc1155/preset/draft-ImmutableERC1155.sol index 1bed7fc5..2de1509a 100644 --- a/contracts/token/erc1155/preset/draft-ImmutableERC1155.sol +++ b/contracts/token/erc1155/preset/draft-ImmutableERC1155.sol @@ -25,11 +25,12 @@ contract ImmutableERC1155 is ImmutableERC1155Base { address owner, string memory name_, string memory baseURI_, + string memory contractURI_, address _operatorAllowlist, address _receiver, uint96 _feeNumerator ) - ImmutableERC1155Base(owner, name_, baseURI_, _operatorAllowlist, _receiver, _feeNumerator) + ImmutableERC1155Base(owner, name_, baseURI_, contractURI_, _operatorAllowlist, _receiver, _feeNumerator) {} /// ===== External functions ===== diff --git a/test/token/erc1155/ImmutableERC1155Test.sol b/test/token/erc1155/ImmutableERC1155.t.sol similarity index 63% rename from test/token/erc1155/ImmutableERC1155Test.sol rename to test/token/erc1155/ImmutableERC1155.t.sol index 381ad402..d6bbc683 100644 --- a/test/token/erc1155/ImmutableERC1155Test.sol +++ b/test/token/erc1155/ImmutableERC1155.t.sol @@ -30,7 +30,8 @@ contract ImmutableERC1155Test is Test { immutableERC1155 = new ImmutableERC1155( owner, "test", - "test", + "test-base-uri", + "test-contract-uri", address(operatorAllowlist), feeReceiver, 0 @@ -53,6 +54,52 @@ contract ImmutableERC1155Test is Test { sig = abi.encodePacked(r, s, v); } + /* + * Contract deployment + */ + function test_DeploymentShouldSetAdminRoleToOwner() public { + bytes32 adminRole = immutableERC1155.DEFAULT_ADMIN_ROLE(); + assertTrue(immutableERC1155.hasRole(adminRole, owner)); + } + + function test_DeploymentShouldSetContractURI() public { + assertEq(immutableERC1155.contractURI(), "test-contract-uri"); + } + + function test_DeploymentShouldSetBaseURI() public { + assertEq(immutableERC1155.baseURI(), "test-base-uri"); + } + + /* + * Metadata + */ + function test_AdminRoleCanSetContractURI() public { + vm.prank(owner); + immutableERC1155.setContractURI("new-contract-uri"); + assertEq(immutableERC1155.contractURI(), "new-contract-uri"); + } + + function test_RevertIfNonAdminAttemptsToSetContractURI() public { + vm.prank(vm.addr(anotherPrivateKey)); + vm.expectRevert("AccessControl: account 0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718 is missing role 0x0000000000000000000000000000000000000000000000000000000000000000"); + immutableERC1155.setContractURI("new-contract-uri"); + } + + function test_AdminRoleCanSetBaseURI() public { + vm.prank(owner); + immutableERC1155.setBaseURI("new-base-uri"); + assertEq(immutableERC1155.baseURI(), "new-base-uri"); + } + + function test_RevertIfNonAdminAttemptsToSetBaseURI() public { + vm.prank(vm.addr(anotherPrivateKey)); + vm.expectRevert("AccessControl: account 0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718 is missing role 0x0000000000000000000000000000000000000000000000000000000000000000"); + immutableERC1155.setBaseURI("new-base-uri"); + } + + /* + * Permits + */ function test_PermitSuccess() public { bytes memory sig = _sign(ownerPrivateKey, owner, spender, true, 0, 1 days);