Skip to content

Commit

Permalink
Merge pull request #20 from axieinfinity/feature/ERC1155Common
Browse files Browse the repository at this point in the history
chore(ERC1155Common): add interface and minor linter
  • Loading branch information
huyhuynh3103 authored Nov 7, 2024
2 parents 1281b17 + 345c4b9 commit f665e9e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
27 changes: 9 additions & 18 deletions src/ERC1155Common.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import {ERC1155Supply} from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import {AccessControlEnumerable} from "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import { ERC1155 } from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import { ERC1155Supply } from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import { AccessControlEnumerable } from "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { IERC1155Common } from "./interfaces/IERC1155Common.sol";

contract ERC1155Common is AccessControlEnumerable, ERC1155, ERC1155Supply {
contract ERC1155Common is AccessControlEnumerable, ERC1155, ERC1155Supply, IERC1155Common {
using Strings for uint256;

bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
Expand Down Expand Up @@ -52,28 +53,18 @@ contract ERC1155Common is AccessControlEnumerable, ERC1155, ERC1155Supply {
_setURI(uri_);
}

/**
* @dev Mints a single ERC1155 token and assigns it to the specified address.
* @param to The address to which the minted token will be assigned.
* @param id The ID of the token to mint.
* @param amount The amount of tokens to mint.
*/
/// @inheritdoc IERC1155Common
function mint(address to, uint256 id, uint256 amount) external onlyRole(MINTER_ROLE) {
_mint(to, id, amount, "");
}

/**
* @dev Mints multiple ERC1155 tokens and assigns them to the specified address.
* @param to The address to which the minted tokens will be assigned.
* @param ids The IDs of the tokens to mint.
* @param amounts The amounts of tokens to mint.
*/
/// @inheritdoc IERC1155Common
function batchMint(address to, uint256[] calldata ids, uint256[] calldata amounts) external onlyRole(MINTER_ROLE) {
_mintBatch(to, ids, amounts, "");
}

function supportsInterface(bytes4 interfaceId) public view override(ERC1155, AccessControlEnumerable) returns (bool) {
return super.supportsInterface(interfaceId);
return interfaceId == type(IERC1155Common).interfaceId || super.supportsInterface(interfaceId);
}

function _beforeTokenTransfer(
Expand Down
20 changes: 20 additions & 0 deletions src/interfaces/IERC1155Common.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IERC1155Common {
/**
* @dev Mints a single ERC1155 token and assigns it to the specified address.
* @param to The address to which the minted token will be assigned.
* @param id The ID of the token to mint.
* @param amount The amount of tokens to mint.
*/
function mint(address to, uint256 id, uint256 amount) external;

/**
* @dev Mints multiple ERC1155 tokens and assigns them to the specified address.
* @param to The address to which the minted tokens will be assigned.
* @param ids The IDs of the tokens to mint.
* @param amounts The amounts of tokens to mint.
*/
function batchMint(address to, uint256[] calldata ids, uint256[] calldata amounts) external;
}
13 changes: 11 additions & 2 deletions test/foundry/SampleERC1155.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {SampleERC1155, ERC1155Common} from "../../src/mock/SampleERC1155.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { SampleERC1155, ERC1155Common } from "../../src/mock/SampleERC1155.sol";
import { IERC1155 } from "@openzeppelin/contracts/interfaces/IERC1155.sol";
import { IAccessControlEnumerable } from "@openzeppelin/contracts/access/IAccessControlEnumerable.sol";
import { IERC1155Common } from "src/interfaces/IERC1155Common.sol";

contract SampleERC1155Test is Test {
using Strings for uint256;
Expand Down Expand Up @@ -44,6 +47,12 @@ contract SampleERC1155Test is Test {
vm.stopPrank();
}

function testSupportsInterface() public virtual {
assertEq(_token().supportsInterface(type(IERC1155).interfaceId), true);
assertEq(_token().supportsInterface(type(IAccessControlEnumerable).interfaceId), true);
assertEq(_token().supportsInterface(type(IERC1155Common).interfaceId), true);
}

function _token() internal view virtual returns (ERC1155Common) {
return _t;
}
Expand Down

0 comments on commit f665e9e

Please sign in to comment.