Last active
May 24, 2020 18:08
-
-
Save pyggie/2e59f4b9f711b0742e1eaf010b7ef5c8 to your computer and use it in GitHub Desktop.
Buidler / Use native solc compiler, instead of default solcjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { TASK_COMPILE_RUN_COMPILER } = require('@nomiclabs/buidler/builtin-tasks/task-names') | |
internalTask(TASK_COMPILE_RUN_COMPILER) | |
.setAction(async (taskArgs, bre, runSuper) => { | |
const { input } = taskArgs | |
const { execSync } = require('child_process') | |
const fetch = require('node-fetch') | |
const version = config.solc && config.solc.version || '0.5.16' | |
const solcPath = `cache/compilers/solc-v${version}` | |
if (!fs.existsSync(solcPath)) { | |
console.log(`Downloading native solidity compiler version ${version}`) | |
const url = `https://github.com/ethereum/solidity/releases/download/v${version}/solc-static-linux` | |
try { | |
const resp = await fetch(url) | |
if (!resp.ok || resp.status !== 200) { | |
throw new Error(`Download failed: ${resp.status} ${resp.statusText}: ${url}`) | |
} | |
const data = await resp.buffer() | |
fs.mkdirSync(`cache`) | |
fs.mkdirSync(`cache/compilers`) | |
fs.writeFileSync(solcPath, data, { mode: 0o755 }) | |
} catch (err) { | |
console.log(`Unable to install native solidity compiler version ${version}:`, err.message) | |
} | |
} | |
if (fs.existsSync(solcPath)) { | |
console.log(`Using native solidity compiler, version ${version}`) | |
const output = execSync(`${solcPath} --standard-json`, { | |
input: JSON.stringify(input, undefined, 2), | |
}) | |
return JSON.parse(output.toString('utf8')) | |
} else { | |
return runSuper() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment