Openzeppelin

OpenZeppelin 上的 ERC1155 提供擴展不導入函式

  • September 4, 2022

我正在對 OpenZeppelin 的 ERC1155 合約進行一些實驗,而 ERC1155Supply 擴展似乎沒有按應有的方式工作。即使我將它導入到契約中,在部署公共函式**totalSupply(id)exists(id)**後,這裡描述:(https://docs.openzeppelin.com/contracts/4.x/api/token/erc1155 #ERC1155Supply)不可用。

這是我的契約程式碼,我錯過了什麼?

// 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, "");
   }
}

您的契約不繼承自,ERC1155Supply而僅繼承自ERC1155. 導入文件並不重要,除非它們繼承,否則您的合約不會知道它。

// 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") {
       ...
   }
}

引用自:https://ethereum.stackexchange.com/questions/134957