One relatively easy solution is to install node.js and use the solc npm package.
I used it myself when I initially started playing with ethereum contracts and it was a snap to set up.
Here are the steps -
- install the latest version of node.js
- create a folder for the project
add solc package to your package.json file, or use the following package file -
{
"name": "eth_compiler",
"version": "0.0.1",
"engines": {
"node": ">=7.0.0",
"npm": "^3.0.0"
},
"dependencies": {
"solc": "^0.4.7"
}
}
run npm install in the folder
- add a small js script to the folder (compile.js) - it will receive the contract file name and output the abi/code files you need.
Here's a example -
var solc = require('solc');
var fs = require('fs');
var inputFilePath = process.argv[2];
var outputPath = process.argv[3];
// note - using the synchronous version of the file system functions for simplicity and because the async version isn't really needed in this case
var contractSolidity = fs.readFileSync(inputFilePath , 'utf-8');
if (!contractSolidity)
return console.error('unable to read file: ' + inputFilePath );
var output = solc.compile(contractSolidity, 1);
for (var contractName in output.contracts) {
var abi = output.contracts[contractName].interface;
var code = output.contracts[contractName].bytecode;
fs.writeFileSync(outputPath + '/' + contractName + '.abi', abi, 'utf-8');
fs.writeFileSync(outputPath + '/' + contractName + '.code', code, 'utf-8');
}
- once you have that file, you can simply execute the following command in the new folder -
node compile [inputFilePath] [outputPath]
and it should generate the compiled files.
Note: I did test it locally and managed to compile a contract so I believe it should work but it's worth mentioning that I have both party & cpp-ethereum installed on my machine (parity wasn't running at the time of this script's execution).
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
Best Answer
You may be interested in the new truffle feature, the ability to specify your solidity compiler, https://github.com/trufflesuite/truffle/releases/tag/v5.0.0#user-content-what-s-new-in-truffle-v5-truffle-compile-solidity-specify-your-compiler-version.
As for manually switching between the 2, I think docker would be your best bet.