2020-12-12 23:40:48 +00:00
|
|
|
#!/usr/bin/env node
|
2020-12-21 19:47:00 +00:00
|
|
|
const process = require('process')
|
|
|
|
const fs = require('fs')
|
2020-12-12 23:40:48 +00:00
|
|
|
|
2022-01-25 14:29:08 +00:00
|
|
|
const compiler = require('solc')
|
2020-12-12 23:40:48 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
SETTINGS_PRESETS = {
|
2023-06-19 09:29:00 +00:00
|
|
|
'legacy-optimize': {optimize: true, viaIR: false},
|
|
|
|
'legacy-no-optimize': {optimize: false, viaIR: false},
|
|
|
|
'via-ir-optimize': {optimize: true, viaIR: true},
|
|
|
|
'via-ir-no-optimize': {optimize: false, viaIR: true},
|
2023-06-19 09:28:40 +00:00
|
|
|
}
|
2020-12-22 06:46:31 +00:00
|
|
|
|
|
|
|
function loadSource(sourceFileName, stripSMTPragmas)
|
|
|
|
{
|
|
|
|
source = fs.readFileSync(sourceFileName).toString()
|
|
|
|
|
|
|
|
if (stripSMTPragmas)
|
|
|
|
// NOTE: replace() with string parameter replaces only the first occurrence.
|
|
|
|
return source.replace('pragma experimental SMTChecker;', '');
|
|
|
|
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
2020-12-21 23:55:52 +00:00
|
|
|
function cleanString(string)
|
|
|
|
{
|
|
|
|
if (string !== undefined)
|
|
|
|
string = string.trim()
|
|
|
|
return (string !== '' ? string : undefined)
|
|
|
|
}
|
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
let inputFiles = []
|
2020-12-22 06:46:31 +00:00
|
|
|
let stripSMTPragmas = false
|
2023-06-19 09:28:40 +00:00
|
|
|
let presets = []
|
2020-12-22 06:46:31 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
for (let i = 2; i < process.argv.length; ++i)
|
2020-12-22 06:46:31 +00:00
|
|
|
{
|
2023-06-19 09:28:40 +00:00
|
|
|
if (process.argv[i] === '--strip-smt-pragmas')
|
|
|
|
stripSMTPragmas = true
|
|
|
|
else if (process.argv[i] === '--preset')
|
2020-12-12 23:40:48 +00:00
|
|
|
{
|
2023-06-19 09:28:40 +00:00
|
|
|
if (i + 1 === process.argv.length)
|
|
|
|
throw Error("Option --preset was used, but no preset name given.")
|
2020-12-21 19:47:00 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
presets.push(process.argv[i + 1])
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
inputFiles.push(process.argv[i])
|
|
|
|
}
|
2020-12-22 01:08:40 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
if (presets.length === 0)
|
|
|
|
presets = ['legacy-no-optimize', 'legacy-optimize']
|
2021-01-23 14:46:54 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
for (const preset of presets)
|
|
|
|
if (!(preset in SETTINGS_PRESETS))
|
|
|
|
throw Error(`Invalid preset name: ${preset}.`)
|
2020-12-21 20:16:35 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
for (const preset of presets)
|
|
|
|
{
|
|
|
|
settings = SETTINGS_PRESETS[preset]
|
2020-12-22 01:08:40 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
for (const filename of inputFiles)
|
|
|
|
{
|
|
|
|
let input = {
|
|
|
|
language: 'Solidity',
|
|
|
|
sources: {
|
|
|
|
[filename]: {content: loadSource(filename, stripSMTPragmas)}
|
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
optimizer: {enabled: settings.optimize},
|
2023-06-19 09:29:00 +00:00
|
|
|
// NOTE: We omit viaIR rather than set it to false to handle older versions that don't have it.
|
|
|
|
viaIR: settings.viaIR ? true : undefined,
|
2023-06-19 09:28:40 +00:00
|
|
|
outputSelection: {'*': {'*': ['evm.bytecode.object', 'metadata']}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!stripSMTPragmas)
|
|
|
|
input['settings']['modelChecker'] = {engine: 'none'}
|
2020-12-22 01:09:12 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
let serializedOutput
|
|
|
|
let result
|
|
|
|
const serializedInput = JSON.stringify(input)
|
2020-12-22 01:08:40 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
let internalCompilerError = false
|
|
|
|
try
|
|
|
|
{
|
|
|
|
serializedOutput = compiler.compile(serializedInput)
|
|
|
|
}
|
|
|
|
catch (exception)
|
|
|
|
{
|
|
|
|
internalCompilerError = true
|
|
|
|
}
|
2020-12-22 01:09:12 +00:00
|
|
|
|
2023-06-19 09:28:40 +00:00
|
|
|
if (!internalCompilerError)
|
|
|
|
{
|
|
|
|
result = JSON.parse(serializedOutput)
|
|
|
|
|
|
|
|
if ('errors' in result)
|
|
|
|
for (const error of result['errors'])
|
|
|
|
// JSON interface still returns contract metadata in case of an internal compiler error while
|
|
|
|
// CLI interface does not. To make reports comparable we must force this case to be detected as
|
|
|
|
// an error in both cases.
|
2023-06-28 15:28:41 +00:00
|
|
|
if (['UnimplementedFeatureError', 'CompilerError', 'CodeGenerationError', 'YulException'].includes(error['type']))
|
2023-06-19 09:28:40 +00:00
|
|
|
{
|
|
|
|
internalCompilerError = true
|
|
|
|
break
|
2020-12-22 01:08:40 +00:00
|
|
|
}
|
2020-12-12 23:40:48 +00:00
|
|
|
}
|
2023-06-19 09:28:40 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
internalCompilerError ||
|
|
|
|
!('contracts' in result) ||
|
|
|
|
Object.keys(result['contracts']).length === 0 ||
|
|
|
|
Object.keys(result['contracts']).every(file => Object.keys(result['contracts'][file]).length === 0)
|
|
|
|
)
|
|
|
|
// NOTE: do not exit here because this may be run on source which cannot be compiled
|
|
|
|
console.log(filename + ': <ERROR>')
|
|
|
|
else
|
|
|
|
for (const contractFile in result['contracts'])
|
|
|
|
for (const contractName in result['contracts'][contractFile])
|
|
|
|
{
|
|
|
|
const contractResults = result['contracts'][contractFile][contractName]
|
|
|
|
|
|
|
|
let bytecode = '<NO BYTECODE>'
|
|
|
|
let metadata = '<NO METADATA>'
|
|
|
|
|
|
|
|
if (
|
|
|
|
'evm' in contractResults &&
|
|
|
|
'bytecode' in contractResults['evm'] &&
|
|
|
|
'object' in contractResults['evm']['bytecode'] &&
|
|
|
|
cleanString(contractResults.evm.bytecode.object) !== undefined
|
|
|
|
)
|
|
|
|
bytecode = cleanString(contractResults.evm.bytecode.object)
|
|
|
|
|
|
|
|
if ('metadata' in contractResults && cleanString(contractResults.metadata) !== undefined)
|
|
|
|
metadata = contractResults.metadata
|
|
|
|
|
|
|
|
console.log(filename + ':' + contractName + ' ' + bytecode)
|
|
|
|
console.log(filename + ':' + contractName + ' ' + metadata)
|
|
|
|
}
|
2020-12-12 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|