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
|
|
|
|
2020-12-21 19:47:00 +00:00
|
|
|
const compiler = require('./solc-js/wrapper.js')(require('./solc-js/soljson.js'))
|
2020-12-12 23:40:48 +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)
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:46:31 +00:00
|
|
|
|
|
|
|
let stripSMTPragmas = false
|
|
|
|
let firstFileArgumentIndex = 2
|
|
|
|
|
|
|
|
if (process.argv.length >= 3 && process.argv[2] === '--strip-smt-pragmas')
|
|
|
|
{
|
|
|
|
stripSMTPragmas = true
|
|
|
|
firstFileArgumentIndex = 3
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:47:00 +00:00
|
|
|
for (const optimize of [false, true])
|
2020-12-12 23:40:48 +00:00
|
|
|
{
|
2020-12-22 06:46:31 +00:00
|
|
|
for (const filename of process.argv.slice(firstFileArgumentIndex))
|
2020-12-12 23:40:48 +00:00
|
|
|
{
|
|
|
|
if (filename !== undefined)
|
|
|
|
{
|
2020-12-22 06:46:31 +00:00
|
|
|
let input = {
|
2020-12-12 23:40:48 +00:00
|
|
|
language: 'Solidity',
|
2020-12-21 19:47:00 +00:00
|
|
|
sources: {
|
2020-12-22 06:46:31 +00:00
|
|
|
[filename]: {content: loadSource(filename, stripSMTPragmas)}
|
2020-12-21 19:47:00 +00:00
|
|
|
},
|
2020-12-12 23:40:48 +00:00
|
|
|
settings: {
|
2020-12-21 19:47:00 +00:00
|
|
|
optimizer: {enabled: optimize},
|
2020-12-22 06:46:31 +00:00
|
|
|
outputSelection: {'*': {'*': ['evm.bytecode.object', 'metadata']}}
|
2020-12-12 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 06:46:31 +00:00
|
|
|
if (!stripSMTPragmas)
|
|
|
|
input['settings']['modelChecker'] = {engine: 'none'}
|
2020-12-21 19:47:00 +00:00
|
|
|
|
2021-01-23 14:46:54 +00:00
|
|
|
let serializedOutput
|
|
|
|
let result
|
|
|
|
const serializedInput = JSON.stringify(input)
|
2020-12-22 01:08:40 +00:00
|
|
|
|
2020-12-21 20:16:35 +00:00
|
|
|
let internalCompilerError = false
|
2021-01-23 14:46:54 +00:00
|
|
|
try
|
2020-12-21 20:16:35 +00:00
|
|
|
{
|
2021-01-23 14:46:54 +00:00
|
|
|
serializedOutput = compiler.compile(serializedInput)
|
|
|
|
}
|
|
|
|
catch (exception)
|
|
|
|
{
|
|
|
|
internalCompilerError = true
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (['UnimplementedFeatureError', 'CompilerError', 'CodeGenerationError'].includes(error['type']))
|
|
|
|
{
|
|
|
|
internalCompilerError = true
|
|
|
|
break
|
|
|
|
}
|
2020-12-21 20:16:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 23:40:48 +00:00
|
|
|
if (
|
2021-01-23 14:46:54 +00:00
|
|
|
internalCompilerError ||
|
2020-12-12 23:40:48 +00:00
|
|
|
!('contracts' in result) ||
|
|
|
|
Object.keys(result['contracts']).length === 0 ||
|
2021-01-23 14:46:54 +00:00
|
|
|
Object.keys(result['contracts']).every(file => Object.keys(result['contracts'][file]).length === 0)
|
2020-12-12 23:40:48 +00:00
|
|
|
)
|
|
|
|
// NOTE: do not exit here because this may be run on source which cannot be compiled
|
2020-12-22 01:10:10 +00:00
|
|
|
console.log(filename + ': <ERROR>')
|
2020-12-12 23:40:48 +00:00
|
|
|
else
|
2020-12-22 01:08:40 +00:00
|
|
|
for (const contractFile in result['contracts'])
|
|
|
|
for (const contractName in result['contracts'][contractFile])
|
|
|
|
{
|
|
|
|
const contractResults = result['contracts'][contractFile][contractName]
|
|
|
|
|
2020-12-22 01:10:10 +00:00
|
|
|
let bytecode = '<NO BYTECODE>'
|
|
|
|
let metadata = '<NO METADATA>'
|
2020-12-22 01:09:12 +00:00
|
|
|
|
2020-12-21 23:55:52 +00:00
|
|
|
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)
|
2020-12-22 01:08:40 +00:00
|
|
|
|
2020-12-21 23:55:52 +00:00
|
|
|
if ('metadata' in contractResults && cleanString(contractResults.metadata) !== undefined)
|
2020-12-22 01:09:12 +00:00
|
|
|
metadata = contractResults.metadata
|
|
|
|
|
2020-12-22 01:08:40 +00:00
|
|
|
console.log(filename + ':' + contractName + ' ' + bytecode)
|
2020-12-22 01:09:12 +00:00
|
|
|
console.log(filename + ':' + contractName + ' ' + metadata)
|
2020-12-22 01:08:40 +00:00
|
|
|
}
|
2020-12-12 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|