How to specify the version of imported OpenZeppelin Contracts?

#support

I faced an error like below
Error: Truffle is currently using solc 0.6.6, but one or more of your contracts specify "pragma solidity ^0.5.0".

I’ve already changed the version of all contracts in my project to > 0.6.6 but still, this error doesn’t disappear.

:computer: Environment

Truffle v5.1.37 (core: 5.1.37)
Node v10.16.0
compilers version: “0.6.6”,

:memo:Details

currently, I’m trying to compile my solidity codes but facing several errors with imported openzeppelin codes like below.

$ truffle compile

Compiling your contracts...

===========================

.
.
> Compiling @openzeppelin/contracts/math/SafeMath.sol
> Compiling @openzeppelin/contracts/token/ERC20/ERC20.sol
> Compiling @openzeppelin/contracts/token/ERC20/IERC20.sol
> Compiling @openzeppelin/contracts/token/ERC20/SafeERC20.sol

@openzeppelin/contracts/math/SafeMath.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.6.6+commit.6c089d02.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.5.0;
^---------------------^

,@openzeppelin/contracts/token/ERC20/ERC20.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.6.6+commit.6c089d02.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.5.0;
^---------------------^

,@openzeppelin/contracts/token/ERC20/IERC20.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.6.6+commit.6c089d02.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.5.0;
^---------------------^

,@openzeppelin/contracts/token/ERC20/SafeERC20.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.6.6+commit.6c089d02.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version

pragma solidity ^0.5.0;
^---------------------^
Error: Truffle is currently using solc 0.6.6, but one or more of your contracts specify "pragma solidity ^0.5.0".

Please update your truffle config or pragma statement(s).
(See https://truffleframework.com/docs/truffle/reference/configuration#compiler-configuration for information on
configuring Truffle to use a specific solc compiler version.)

what makes me confused is these imported oppenzellin contract’s versions are not automatically updated to the same(^0.6.0) as contracts that import them.

How can I specify the version of these imported contracts that seems I’m not able to change manually to 0.6.6? or is there no way to do that?

Actually, I’ve already tried to change the versions of all my contracts to 0.5.0 in order to avoid this error but ( as expected…) it threw other errors. So I’d like to find out a better way.

*just in case, the source code that i forked and tried to compile is https://github.com/Goyemon/aave-flash-loans/tree/main/contracts. Some of the contracts of this project import above openzeppelin contracts.

thanks!

:1234: Code to reproduce

1 Like

Hi @wat,

The current version of OpenZeppelin Contracts v3.3 uses Solidity 0.6. (also supports Solidity 0.7).

OpenZeppelin Contracts v2.x uses Solidity 0.5.

Can you check what version of OpenZeppelin Contracts you are using in your package.json? I suspect you have OpenZeppelin Contracts v2.5 installed.

1 Like

Hi @abcoathup. thanks for your support.

I could successfully compile my contracts by replacing the openzeppelin contract codes in node_modues which remained actually 0.5.0 version.

1 Like

Hi @wat,

I recommend not modifying the contracts in node_modules.

From: https://docs.openzeppelin.com/contracts/3.x/#usage

To keep your system secure, you should always use the installed code as-is, and neither copy-paste it from online sources, nor modify it yourself.

If you want to use a different version of Solidity, then you can install the appropriate version of OpenZeppelin Contracts.

Solidity 0.5

npm install @openzeppelin/contracts@2.5.1

Solidity 0.6

npm install @openzeppelin/contracts

Solidity 0.7

npm install @openzeppelin/contracts@solc-0.7

Also see: OpenZeppelin Contracts 3.3

Hi @wat,

I wanted to check that you were able to install and use the appropriate version of OpenZeppelin Contracts?

Hi @abcoathup. sorry for the late reply.

if I remember correctly, the way of replacing was copy&paste for the contracts which caused errors. So I will reinstall the source codes instead. thanks.

btw, there haven’t been any errors due to this modification so far though.

1 Like

A post was split to a new topic: Yard add OpenZeppelin Contracts 2.5.1

Hi, I am trying to do this but it doesn’t work as described on my Macbook:

  • npm install @openzeppelin/contracts → installs pragma solidity ^0.5.0
  • npm install @openzeppelin/contracts@solc-0.7 → doesn’t work, error ‘No matching version found for @openzeppelin/contracts@solc-0.7’

simply trying to follow: https://docs.openzeppelin.com/contracts/4.x/erc20 :frowning:

anyone else with this problem?

if you want the v3 version using solc 0.7 you can do :slight_smile:
yarn add @openzeppelin/contracts@3.4.1-solc-0.7-2

1 Like