[Ethereum] Solidity version mismatch

soliditytruffle

My smart contract has pragma solidity 0.4.18;

According to Solidity: Best practices – Which compiler version should I use? Advantages / Disadvantages, this pragma will work on any compiler from 0.4.18 and upwards.

$ truffle version
Truffle v5.1.65 (core: 5.1.65)
Solidity v0.5.16 (solc-js)
Node v14.15.4
Web3.js v1.2.9
$ truffle compile

Compiling your contracts...
===========================
> Compiling .\contracts\Migrations.sol
> Compiling .\contracts\Wrestling.sol

CompileError: /F/hackernoon/contracts/Wrestling.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.16+commit.9c3226ce.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity 0.4.18;
^---------------------^

Error: Truffle is currently using solc 0.5.16, but one or more of your contracts specify "pragma solidity 0.4.18".
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.)

My compiler version is 0.5.16, which I think is greater than 0.4.18. But truffle fails to compile my file.

I checked https://github.com/ethereum/solc-bin/blob/gh-pages/bin/soljson-v0.5.16%2Bcommit.9c3226ce.js, it doesn't look like a nightly build.

What caused the error?

Best Answer

If you want to compile from one version and upwards, you need to include symbol ^:

pragma solidity ^0.4.18;

However, this will only take up to version 0.4.26, which is the latest from 0.4, but not versions above (0.5, 0.6...). As described in the official documentation:

pragma solidity ^0.5.2;

A source file with the line above does not compile with a compiler earlier than version 0.5.2, and it also does not work on a compiler starting from version 0.6.0 (this second condition is added by using ^). This is because there will be no breaking changes until version 0.6.0, so you can always be sure that your code compiles the way you intended. The exact version of the compiler is not fixed, so that bugfix releases are still possible.

You have two options:

  1. Update the pragma from your smart contract to ^0.5.16 (and apply the necessary changes in the contract to the new pragma version)
  2. Change the compiler version in truffle to 0.4.18 (in file truffle-config.js)
Related Topic