Skip to content

Commit

Permalink
[GMS-1172] Add metadata fields to ERC1155 preset contract (#133)
Browse files Browse the repository at this point in the history
* add contractURI

* rename file

* add tests for contractURI

* add failing tests

* add baseURI

---------

Co-authored-by: Allan Almeida <[email protected]>
  • Loading branch information
allan-almeida and allan-almeida-imtbl authored Nov 15, 2023
1 parent f3408ff commit a14110b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
17 changes: 16 additions & 1 deletion contracts/token/erc1155/abstract/ImmutableERC1155Base.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -44,6 +51,8 @@ abstract contract ImmutableERC1155Base is
_grantRole(DEFAULT_ADMIN_ROLE, owner);
_setDefaultRoyalty(_receiver, _feeNumerator);
_setOperatorAllowlistRegistry(_operatorAllowlist);
contractURI = contractURI_;
baseURI = baseURI_;
}

/**
Expand Down Expand Up @@ -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_;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion contracts/token/erc1155/preset/draft-ImmutableERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 =====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ contract ImmutableERC1155Test is Test {
immutableERC1155 = new ImmutableERC1155(
owner,
"test",
"test",
"test-base-uri",
"test-contract-uri",
address(operatorAllowlist),
feeReceiver,
0
Expand All @@ -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);

Expand Down

0 comments on commit a14110b

Please sign in to comment.