use truffle to compile contract with openzepplin contracted imported, the following error appears
truffle compile --compile-all
> Warning: possible unsupported (undocumented in help) command line option(s): --compile-all
Compiling your contracts...
===========================
> Compiling ./contracts/ApePunks.sol
> Compiling ./contracts/Migrations.sol
> Compiling @openzeppelin/contracts/access/Ownable.sol
> Compiling @openzeppelin/contracts/token/ERC721/ERC721.sol
> Compiling @openzeppelin/contracts/token/ERC721/IERC721.sol
> Compiling @openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol
> Compiling @openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol
> Compiling @openzeppelin/contracts/utils/math/SafeMath.sol
ParserError: Source file requires different compiler version (current compiler is 0.8.7+commit.e28d00a7.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
--> @openzeppelin/contracts/access/Ownable.sol:3:1:
|
3 | pragma solidity ^0.6.0;
| ^^^^^^^^^^^^^^^^^^^^^^^
,ParserError: Source file requires different compiler version (current compiler is 0.8.7+commit.e28d00a7.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
--> @openzeppelin/contracts/token/ERC721/ERC721.sol:3:1:
|
3 | pragma solidity ^0.6.0;
| ^^^^^^^^^^^^^^^^^^^^^^^
,ParserError: Source file requires different compiler version (current compiler is 0.8.7+commit.e28d00a7.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
--> @openzeppelin/contracts/token/ERC721/IERC721.sol:3:1:
|
3 | pragma solidity ^0.6.2;
| ^^^^^^^^^^^^^^^^^^^^^^^
Error: Truffle is currently using solc 0.8.7, but one or more of your contracts specify "pragma solidity ^0.6.0".
Please update your truffle config or pragma statement(s).
(See https://trufflesuite.com/docs/truffle/reference/configuration#compiler-configuration for information on
configuring Truffle to use a specific solc compiler version.)
Compilation failed. See above.
Truffle v5.4.14 (core: 5.4.14)
Node v16.10.0
in my case, I have already "npm install @openzeppelin/contracts", in node_modules/@openzeppelin/contracts, all contracts are
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
Code to reproduce
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract ApePunks is ERC721Enumerable, Ownable{
using SafeMath for uint256;
using Strings for uint256;
string public baseTokenURI;
event CreatePunk(uint256 indexed id);
constructor(string memory baseURI) ERC721("ApePunks", "APEK") {
setBaseURI(baseURI);
}
function setBaseURI(string memory _baseURI) public onlyOwner {
baseTokenURI = _baseURI;
}
function batchMint(address _to, uint256 _number) public onlyOwner{
uint256 id = totalSupply() + 1; //nft is numbered from 1.
for (uint256 i=0; i<_number; i++){
_safeMint(_to, id);
emit CreatePunk(id);
id ++;
}
}
function mint(address _to) public onlyOwner{
uint256 id = totalSupply() + 1; //nft is numbered from 1.
_safeMint(_to, id);
emit CreatePunk(id);
}
function tokenURI(uint256 tokenId) public view override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
return bytes(baseTokenURI).length > 0 ? string(abi.encodePacked(baseTokenURI, tokenId.toString())) : "";
}
}
Environment
Truffle