ERC1155 Supply Extension – How to Resolve Function Import Issues in OpenZeppelin

erc-1155importopenzeppelin

I'm doing some experiments with OpenZeppelin's ERC1155 contracts and the ERC1155Supply extension does not seem to be working as it should. Even though I'm importing it to the contract, after deployment the public functions totalSupply(id) and exists(id) described here: (https://docs.openzeppelin.com/contracts/4.x/api/token/erc1155#ERC1155Supply) are unavailable.

Here's my contract code, what am I missing?

// contracts/GameItems.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";

contract GameItems is ERC1155 {
    uint256 public constant GOLD = 0;
    uint256 public constant SILVER = 1;
    uint256 public constant THORS_HAMMER = 2;
    uint256 public constant SWORD = 3;
    uint256 public constant SHIELD = 4;

    constructor() public ERC1155("https://game.example/api/item/{id}.json") {
        _mint(msg.sender, GOLD, 10**18, "");
        _mint(msg.sender, SILVER, 10**27, "");
        _mint(msg.sender, THORS_HAMMER, 1, "");
        _mint(msg.sender, SWORD, 10**9, "");
        _mint(msg.sender, SHIELD, 10**9, "");
    }
}

Best Answer

Your contract doesn't inherit from ERC1155Supply but only ERC1155. It doesn't matter that you imported the file, your contract won't know about it unless they inherit.

// contracts/GameItems.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";

contract GameItems is ERC1155Supply {
    ...

    constructor() public ERC1155Supply("https://game.example/api/item/{id}.json") {
        ...
    }
}
Related Topic