34

I've got a contract where I want to use <address>.transfer(), which was released in the latest Solidity version 0.4.10.

I have as the first line of my contract: pragma solidity 0.4.10; but when I compile I get Error: Source file requires different compiler version which pretty obviously means truffle is trying to compile my contract with not that version of Solidity. It still uses 0.4.8.

I installed 0.4.10 last night and solc --version shows 0.4.10.

So my question is, how do I change the version of Solidity in a truffle project? I've looked in the config file and the truffle docs and could not find this. I also cannot even find where 0.4.8 is installed (was assuming in truffle itself but it is not listed in truffle's package.json).

JohnAllen
  • 1,318
  • 1
  • 13
  • 29

6 Answers6

19

In Truffle version 5.0.0 (currently in Beta) you can specify a Solidity version in the truffle.js config file eg

module.exports = {
  networks: {
    ... etc ...
  },
  compilers: {
     solc: {
       version: <string>  // ex:  "0.4.20". (Default: Truffle's installed solc)
     }
  }
};

This is copied from the release details here

willjgriff
  • 1,658
  • 8
  • 13
  • Somehow, a long time after that, and despite having
    Truffle v5.1.15 (core: 5.1.15)
    Solidity - 0.6.4 (solc-js)
    

    and telling just like in your file that my solc version should be 0.6.4, my IDE still believes I'm working with a 0.4.17 compiler... Do you have an idea why?

    – Thanh-Quy Nguyen Mar 18 '20 at 17:11
8

Truffle unfortunately doesn't allow you to select a different compiler version, apparently this is a result of the solc API not yet being stable, thus making it difficult to change version.

If you run truffle version it will output both the truffle, and compiler version.

$ truffle version
Truffle v3.4.9 (core: 3.4.8)
Solidity v0.4.15 (solc-js)

Updating truffle will get you the most up to date version it supports. It tends to lag behind solc itself somewhat, since it takes time to implement the latest version, though they've been quicker recently.

maurelian
  • 3,211
  • 2
  • 16
  • 34
7

As of truffle v5.2.0, you can let Truffle compile based on the pragma expressions of each solidity file. The only thing to do is to write the following in the truffle-config.js file:

module.exports = {
  compilers: {
    solc: {
      version: “pragma”
    }
  },
  // … the rest of your config goes here
};

Truffle will then check each solidity file in the project for the pragma expression and compile based on the expression mentioned in each.

Source: https://www.trufflesuite.com/blog/take-a-dive-into-truffle-5#pragma-compilation

Narcis
  • 95
  • 1
  • 6
1

All answers are moot above.

Add a 2nd line to the truffle-config.

Where it says version: "X.X.X"

Add a comma, then copy paste without the comma to the next line. Now your config should look like this:

version: "X.X.X",

version: "Y.Y.Y"

Do not add a comma to the 2nd one. Save the changes.

Now try compiling. It will use BOTH versions. Neat trick huh? :D

*** This only works in truffle 5 and up but you can specify lower versions such as 4.

1

in 2022.6, truffle 5.x + version, you can speicify the compiler version. e.g.

Bby default , solc version is 0.4.x , you can make it 0.8.x


$ cat truffle-config.js

module.exports = { networks: { development: { host: "127.0.0.1", port: 8545, network_id: "*" }, }, // here you can speicify the compiler version // by default , solc version is 0.4.x compilers: { solc: { version: "^0.8.0", } } };

and at the first time you run truffle migrate, you may see truffle downloaded the 0.8.x compiler, e.g.

$ truffle migrate --network development --verbose-rpc --interactive

Compiling your contracts...

✓ Fetching solc version list from solc-bin. Attempt #1 ✓ Downloading compiler. Attempt #1. ✓ Fetching solc version list from solc-bin. Attempt #1 > Compiling ./contracts/MyTestNft.sol > Artifacts written to /mnt/d/workspace/test_erc_721/build/contracts > Compiled successfully using:

  • solc: 0.8.15+commit.e14f2714.Emscripten.clang

refer to: https://trufflesuite.com/docs/truffle/reference/configuration/

a compile example from official doc:

module.exports = {
  compilers: {
    solc: {
      version: <string>, // A version or constraint - Ex. "^0.5.0"
                         // Can be set to "native" to use a native solc or
                         // "pragma" which attempts to autodetect compiler versions
      docker: <boolean>, // Use a version obtained through docker
      parser: "solcjs",  // Leverages solc-js purely for speedy parsing
      settings: {
        optimizer: {
          enabled: <boolean>,
          runs: <number>   // Optimize for how many times you intend to run the code
        },
        evmVersion: <string> // Default: "istanbul"
      },
      modelCheckerSettings: {
        // contains options for SMTChecker
      }
    }
  }
}
Siwei
  • 322
  • 2
  • 10
1

You will have to update your truffle to do this. I've answered how to update your truffle here.