2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

記事投稿キャンペーン 「2024年!初アウトプットをしよう」

@openzeppelin/contracts/utils/Counters.sol, imported from [filename].sol, not found エラーへの対処法

Last updated at Posted at 2024-01-23

はじめに

.solファイルを実行したら、このようなエラーが出ました。

Error HH404: File @openzeppelin/contracts/utils/Counters.sol, imported from contracts/MyEpicGame.sol, not found.

For more info go to https://hardhat.org/HH404 or run Hardhat with --show-stack-traces
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: /Users/username/.nvm/versions/node/v20.11.0/bin/node
Arguments: /opt/homebrew/Cellar/yarn/1.22.19/libexec/lib/cli.js run:script
Directory: /Users/username/github/ETH-NFT-Game/packages/contract
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

結論

原因

OpenZeppelin V5.0では Counters が削除されたため。

他の機能やコントラクトも削除されています。

  • Address.isContract
  • Counters
  • SafeMath
  • SignedSafeMath
  • GovernorCompatibilityBravo
  • GovernorVotesComp
  • TokenTimelock (in favor of VestingWallet)
  • ERC777
  • All cross-chain contracts (including AccessControlCrossChain)
  • All presets in favor of OpenZeppelin Contracts Wizard

対処法

この記事を参考に書き換え、エラーが解消されました。

import

v4
import {Counters} from "@openzeppelin/contracts/utils/Counters.sol";
v5
// 削除

using Counters for Counters.Counter

v4
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
v5
uint256 private _tokenIdCounter;

.current()

v4
uint256 tokenId = _tokenIdCounter.current();
v5
uint256 tokenId = _tokenIdCounter;

.increment()

v4
_tokenIdCounter.increment();
v5
_tokenIdCounter += 1;

全体

v4
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();
        }
}
v5
contract MyToken is ERC721, Pausable, Ownable {
        uint256 private _tokenIdCounter;

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

参考記事

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?