Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom solc versions #11

Merged
merged 3 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/solc-transpiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@eth-optimism/core-utils": "~0.0.1-alpha.7",
"@eth-optimism/rollup-dev-tools": "~0.0.1-alpha.7",
"require-from-string": "^2.0.2",
"solc": "^0.5.12"
},
"devDependencies": {
Expand Down
56 changes: 53 additions & 3 deletions packages/solc-transpiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
remove0x,
} from '@eth-optimism/core-utils'
import * as solc from 'solc'
import { execSync } from 'child_process'
import * as requireFromString from 'require-from-string'

const log: Logger = getLogger('solc-transpiler')

Expand All @@ -36,14 +38,15 @@ interface TranspilationOutput {
* @returns The Solc output as a string.
*/
export const compile = (configJsonString: string, callbacks?: any): string => {
const compiler = getCompiler(process.env.SOLC_VERSION)
log.debug(`Trying to transpile with config: ${configJsonString}`)
let json: any
try {
json = JSON.parse(configJsonString)
} catch (e) {
log.debug(`Cannot parse JSON: ${JSON.stringify(e)}`)
// todo: populate some errors
return solc.compile(configJsonString)
return compiler.compile(configJsonString)
}

const inputErrors: string = getInputErrors(json)
Expand All @@ -53,8 +56,8 @@ export const compile = (configJsonString: string, callbacks?: any): string => {

const solcConfig: string = getSolcConfig(json)
const resString = !!callbacks
? solc.compile(solcConfig, callbacks)
: solc.compile(solcConfig)
? compiler.compile(solcConfig, callbacks)
: compiler.compile(solcConfig)

const res = JSON.parse(resString)
if (
Expand Down Expand Up @@ -110,6 +113,53 @@ export const compile = (configJsonString: string, callbacks?: any): string => {
return formatOutput(res, json)
}

const getCompiler = (versionString: string): typeof solc => {
masonforest marked this conversation as resolved.
Show resolved Hide resolved
if (versionString) {
masonforest marked this conversation as resolved.
Show resolved Hide resolved
const getCompilerString = `
function httpsRequest(params, postData) {
return new Promise(function(resolve, reject) {
var req = https.request(params, function(res) {
var body = [];
res.on("data", function(chunk) {
body.push(chunk);
});
res.on("end", function() {
try {
body = Buffer.concat(body).toString();
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on("error", function(err) {
reject(err);
});
req.end();
});
}

async function getSolcVersion(versionString) {
const listUrl = "https://ethereum.github.io/solc-bin/bin/list.json";
const {releases} = JSON.parse(await httpsRequest(listUrl))
const solcUrl = "https://ethereum.github.io/solc-bin/bin/" + releases[versionString];
return await httpsRequest(solcUrl);
}

(async () => {
await process.stdout.write(await getSolcVersion("${versionString}"));
})();
`
Copy link
Contributor

@karlfloersch karlfloersch Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to use inline executed code btw? Just curious lol seems like a wild solution & I'm sure it has to do with truffle craziness

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's wackyness!

Since Truffle expects compile to be a synchronous function we can't do anything that's asynchronous in that function. The only way to make a web request "synchronously" in node is to fire up a whole other node process with execSync 🙃

https://stackoverflow.com/a/28394895

return solc.setupMethods(
requireFromString(
execSync(`${process.argv[0]} -e '${getCompilerString}'`).toString()
)
)
} else {
return solc
}
}

const getExecutionManagerAddress = (configObject: any): string => {
return (
configObject.settings.executionManagerAddress ||
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8906,7 +8906,7 @@ require-directory@^2.1.1:
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=

require-from-string@^2.0.0:
require-from-string@^2.0.0, require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==
Expand Down