6

I have written one simple smart contract in solidity and trying to migrate it with truffle.

$ truffle migrate
Compiling .\contracts\Election.sol...
Compiling .\contracts\Migrations.sol...

    /D/ethereum/electiondemo/contracts/Migrations.sol:1:1: SyntaxError: Source file requires different compiler version (current compiler is 0.5.0+commit.1d4f565a.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
    pragma solidity ^0.4.24;
    ^----------------------^

Compilation failed. See above.`enter code here`
Truffle v5.0.0 (core: 5.0.0)
Node v8.11.1

Solidity version is 0.5.0. Please find below the code for the smart contract:

pragma solidity ^0.5.0;

contract Election {
    // Read/write candidate
    string public candidate;

    // Constructor
    constructor ( ) public {
        candidate = "Candidate 1";
    }
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Vikas Banage
  • 494
  • 1
  • 8
  • 25

5 Answers5

17

Got the solution : In truffle.js. You need specify solidity version

module.exports = {
   // See <http://truffleframework.com/docs/advanced/configuration>
   // for more about customizing your Truffle configuration!
   networks: {
       development: {
           host: "127.0.0.1",
           port: 7545,
           network_id: "*" // Match any network id
       }
   },
   compilers: {
       solc: {
           **version: "0.4.24"** // ex:  "0.4.20". (Default: Truffle's installed solc)
       }
   }
};

Same need to given in your smart contract

John
  • 321
  • 2
  • 12
Vikas Banage
  • 494
  • 1
  • 8
  • 25
11

Add the below line to truffle-config.js

{
  compilers: {
    solc: {
      version: "0.4.24" // ex:  "0.4.20". (Default: Truffle's installed solc)
    }
  }
}
Adam Azad
  • 11,171
  • 5
  • 29
  • 70
Prashant Kajale
  • 473
  • 4
  • 8
3

As of now, truffle uses '0.5.16' as default. So if your code is using newer solidity versions, it would throw an error. you do not need to put a specific value for the solc version.

this is what I use on the contract

 pragma solidity >=0.7.0 <0.9.0;

in the config file

compilers: {
    solc: {
      // default is 0.5.16
      version: ">=0.7.0 <0.9.0",    // Fetch exact version from solc-bin (default: truffle's version)
    
      }
    }
  },
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
0

Your migration contract (Migrations.sol) needs 0.4.24.

Go to your migration contract and change your dependency to 0.5 or change your main contracts dependency to 0.4.*

Ferit
  • 8,692
  • 8
  • 34
  • 59
0

to your truffle.js / truffle-config.js add this

module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// for more about customizing your Truffle configuration!
networks: {
  development: {
    host: "127.0.0.1",
    port: 7545,
    network_id: "*" // Match any network id
  }
},
compilers: {
  solc: {
    version: "0.4.24" //(Default: Truffle's installed    solc)
  }
 }
};

Then use npx to run your packages. npx is a native npm package, so it comes with your installation of nodejs & npm. It allows you to run local node package binaries. This way, you can get rid of a lot of your global package installations, and use the local binaries that are defined in your package.json.

npx truffle compile

npx truffle test (optional)

npx truffle migrate

Sifundo Moyo
  • 175
  • 1
  • 7