Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Use specified semver, or detect it form source.
if(requiredSemver) console.log(`Version required by the user:`, requiredSemver);
const targetSemver = requiredSemver || sourceSemver;
// Translate semver to an actual version.
// E.g. '0.5.8' to 'soljson-v0.5.8+commit.23d335f2.js'.
const version = findVersionFromSemver(targetSemver, availableVersions);
// Retrieve compiler source.
let compilerSource;
if(fs.existsSync(`${SOLJSON_PATH}${version}`)) compilerSource = fs.readFileSync(`${SOLJSON_PATH}${version}`, 'utf8');
else compilerSource = await downloadAndCacheCompilerSource(version);
// "Build" compiler source.
solc = solc.setupMethods(requireFromString(compilerSource, version));
return solc;
}
};
return new Promise(async (resolve, reject) => {
// Find version in available versions.
const versions = await availableCompilerVersions();
let match;
if(useLatestPatch) match = findMostRecentPatchVersion(version, versions);
else match = findExactPatchVersion(version, versions);
// Retrieve version source.
let compilerSource;
if(fs.existsSync(`${SOLJSON_PATH}${match}`)) compilerSource = fs.readFileSync(`${SOLJSON_PATH}${match}`, 'utf8');
else compilerSource = await downloadAndCacheCompilerSource(match);
// Wrap compiler source.
solc = solc.setupMethods(requireFromString(compilerSource, match));
resolve(solc);
});
}
if (this.initialisedAlready(localInstallationPath, remoteInstallationVersion, enableNodeCompiler)) {
resolve();
}
let solidityfile = '';
this.enableNodeCompilerSetting = enableNodeCompiler;
if (enableNodeCompiler && this.isInstalledSolcUsingNode(this.rootPath)) {
solidityfile = require(this.getLocalSolcNodeInstallation());
this.localSolc = solc.setupMethods(solidityfile);
this.currentCompilerType = compilerType.localNode;
this.currentCompilerSetting = null;
resolve();
} else {
// local file
if (typeof localInstallationPath !== 'undefined' && localInstallationPath !== null) {
solidityfile = require(localInstallationPath);
this.localSolc = solc.setupMethods(solidityfile);
this.currentCompilerType = compilerType.localFile;
this.currentCompilerSetting = localInstallationPath;
resolve();
} else {
// remote
if (typeof remoteInstallationVersion !== 'undefined' && remoteInstallationVersion !== null) {
const solcService = this;
solc.loadRemoteVersion(remoteInstallationVersion, function(err, solcSnapshot) {
if (err) {
reject('There was an error loading the remote version: ' + remoteInstallationVersion);
} else {
solcService.currentCompilerType = compilerType.Remote;
solcService.currentCompilerSetting = remoteInstallationVersion;
solcService.localSolc = solcSnapshot;
resolve();
}
function compileContract(content) {
var outputFilePath = outputFolder + "/" + outputContractName + "_flat.sol";//.replace(pathLib.basename(inputFilePath), pathLib.basename(inputFilePath));
fs.writeFileSync(outputFilePath, content);
var solcV011 = solc.setupMethods(require("./bin/soljson-v0.4.11+commit.68ef5810.js"));
//var solcV011 = solc.useVersion('v0.4.11+commit.68ef5810');
var output = solcV011.compile(content, 1);
//console.log(output);
for (let contractName in output.contracts) {
if (targetContractName.toLowerCase() === contractName.substr(contractName.indexOf(":") + 1).toLowerCase()) {
//output.contracts[contractName].bytecode = solc.linkBytecode(output.contracts[contractName].bytecode, { ':SafeMathLib': '0x072ba774dfd4e827c539ecfc9c6e2fae8d012534' });
//0xbf90948c40197c1c22b5fdd72a212efda3994d68 - safemathlib with divides
//output.contracts[contractName].bytecode = solc.linkBytecode(output.contracts[contractName].bytecode, { ':SafeMathLib': '0xbf90948c40197c1c22b5fdd72a212efda3994d68' });
//0xe9ae538ffea453eae179e45a787ca76db619d40d - safemathlibext
//output.contracts[contractName].bytecode = solc.linkBytecode(output.contracts[contractName].bytecode, { ":SafeMathLibExt": "0xe9ae538ffea453eae179e45a787ca76db619d40d" });
fs.writeFileSync(outputFolder + "/" + outputContractName + "_flat.bin", output.contracts[contractName].bytecode);
fs.writeFileSync(outputFolder + "/" + outputContractName + "_flat.abi", output.contracts[contractName].interface);
}
}
}
module.exports = () => {
solc = solc.setupMethods(require('./solc'));
const files = getFiles();
const input = {
sources: files.reduce(reducer, {}),
settings: {
evmVersion: "spuriousDragon"
}
};
const output = solc.compile(input, 1);
if (output.errors && output.errors.length > 0) {
return console.log('ERROR: \n', output.errors);
}
const keys = Object.keys(output.contracts);
return new Promise((resolve, reject) => {
stream.on(
'finish',
() => resolve(fs.readFileSync(filePath).toString())
).on(
'error',
err => reject(err)
);
});
});
}
/**
* NOTE: `solcSnapshot` has the same interface as the `solc`.
*/
const solcSnapshot = solc.setupMethods(
requireFromString(solcString),
'soljson-' + releases[version] + '.js'
);
return { solcSnapshot, fromCache };
};
export function initialiseLocalSolc(compileUsingLocalVersion: string, rootPath: string) {
let solidityfile = '';
if (isInstalledSolcLocally(rootPath)) {
solidityfile = require(getLocalSolcInstallation(rootPath));
solc.setupMethods(solidityfile);
return true;
}else {
if ( compileUsingLocalVersion !== 'undefined' || compileUsingLocalVersion !== null) {
solidityfile = require(compileUsingLocalVersion);
solc.setupMethods(solidityfile);
return true;
}
}
}
solcjs = (await fsWrapper.readFileAsync(compilerBinFilename)).toString();
} else {
logUtils.warn(`Downloading ${fullSolcVersion}...`);
const url = `${constants.BASE_COMPILER_URL}${fullSolcVersion}`;
const response = await fetchAsync(url);
const SUCCESS_STATUS = 200;
if (response.status !== SUCCESS_STATUS) {
throw new Error(`Failed to load ${fullSolcVersion}`);
}
solcjs = await response.text();
await fsWrapper.writeFileAsync(compilerBinFilename, solcjs);
}
if (solcjs.length === 0) {
throw new Error('No compiler available');
}
const solcInstance = solc.setupMethods(requireFromString(solcjs, compilerBinFilename));
return solcInstance;
}