はじめに
.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;
}
}
参考記事