openzeppelin/contracts/utils/Counters.sol" not found

apeNote發表於2024-10-04

執行以下

// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

import {Counters} from "@openzeppelin/contracts/utils/Counters.sol";

contract MyToken is ERC721, Pausable, Ownable {
        using Counters for Counters.Counter;
        Counters.Counter private _tokenIdCounter;

        function safeMint(address receiver) public onlyOwner {
            uint256 tokenId = _tokenIdCounter.current();
            _safeMint(_receiver, tokenId);
            _tokenIdCounter.increment();
        }

報錯

ParserError: Source "project:/node_modules/openzeppelin/contracts/utils/Counters.sol" not found
 --> project:/contracts/tests.sol:4:1:
  |
4 | import "../node_modules/openzeppelin/contracts/utils/Counters.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

使用 unit 型別代替 counter

contract MyToken is ERC721, Pausable, Ownable {
        uint256 private _tokenIdCounter;

        function safeMint(address receiver) public onlyOwner {
            uint256 tokenId = _tokenIdCounter;
            _safeMint(_receiver, tokenId);
            _tokenIdCounter += 1;
        }
}

相關文章