Using Multiple Versions – How to use two solc versions in a Truffle project

solctruffle

I am creating an app that has various imports that require two different versions of solidity. AragonOS(0.4.x) and openzeppelin(0.5.x)

Of course, there are breaking changes in 0.5.x so my question is this. Is it possible to tell truffle to compile some contracts with a different compiler than others within the same project? If not how could I get around this problem

Best Answer

EDIT: I recommend using Buidler! The key feature their software provides is modularity, where compilation and testing of contracts can be done separately. Migration from Truffle is also seamless, as specified in their guide.

What you can do is to specify 2 separate configuration files for compilation of your v4 and v5 contracts, then use the --config flag.

For testing, use the --no-compile flag to skip contract compilation.

Example:

  • v4 contracts located in ./contracts/v4 folder
  • v5 contracts located in ./contracts/v5 folder
  • Test files located in ./test folder

1) Specify a configV4.js file with the following code:

module.exports = {
  defaultNetwork: "develop",

  networks: {
    develop: {
      gas: 6000000
    }
  },

  solc: {
    version: "0.4.18",
    optimizer: {
      enabled: true,
      runs: 200
    }
  },

  paths: {
    sources: "./contracts/v4",
  },
};

2) Specify a configV5.js file with the following code:

module.exports = {
  defaultNetwork: "develop",

  networks: {
    develop: {
      gas: 6000000
    }
  },

  solc: {
    version: "0.5.9",
    optimizer: {
      enabled: true,
      runs: 200
    }
  },

  paths: {
    sources: "./contracts/v5",
  },
};

3) Compile your v4 contracts: npx buidler compile --config configV4.js

4) Compile your v5 contracts: npx buidler compile --config configV5.js

5) For testing, you can specify yet another file configTest.js, or combine the settings in either config file mentioned above.

usePlugin("@nomiclabs/buidler-truffle5");

module.exports = {
  defaultNetwork: "develop",

  networks: {
    develop: {
      gas: 6000000
    }
  },

  paths: {
    tests: "./tests", 
    cache: "./cache",
    artifacts: "./artifacts"
  },

  mocha: {
    enableTimeouts: false
  }
};

6) Test your contracts: npx buidler test --no-compile --config configTest.js

Related Topic