Created
February 7, 2019 20:50
-
-
Save BlinkyStitt/b0c0d71e6cdfb66a1189b9eae7a39972 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
const fs = require('fs'); | |
const semver = require('semver'); | |
// node sol-compiler-json-to-truffle.js inputFilename | |
const inputFilename = process.argv[2]; | |
console.error('Converting ' + inputFilename + ' to a truffle artifact...'); | |
const inputObj = JSON.parse(fs.readFileSync(inputFilename, 'utf8')); | |
// console.error(inputObj); | |
// console.error(inputObj['compilerOutput']['abi']); | |
// console.error(inputObj['compilerOutput']['evm']); | |
// console.error(inputObj['sources']); | |
if (!semver.satisfies(inputObj["schemaVersion"], '^2.0.0')) { | |
throw "incompatible schemaVersion"; | |
} | |
var truffleObject = { | |
"contractName": inputObj["contractName"], | |
"abi": inputObj["compilerOutput"]["abi"], // TODO: why isn't this working? | |
"compiler": { | |
"name": inputObj["compiler"]["name"], | |
"version": inputObj["compiler"]["version"], | |
}, | |
"networks": {}, | |
"schemaVersion": "3.0.1", | |
"updatedAt": "2019-01-27T03:25:52.161Z", // TODO: dynamic | |
"devdoc": inputObj["compilerOutput"]["devdoc"], | |
"userdoc": inputObj["compilerOutput"]["userdoc"], | |
}; | |
if (inputObj["compilerOutput"]["evm"]["bytecode"]["object"] != "0x") { | |
truffleObject["bytecode"] = inputObj["compilerOutput"]["evm"]["bytecode"]["object"]; | |
} | |
if (inputObj["compilerOutput"]["evm"]["deployedBytecode"]["object"] != "0x") { | |
truffleObject["deployedBytecode"] = inputObj["compilerOutput"]["evm"]["deployedBytecode"]["object"]; | |
} | |
if (inputObj["compilerOutput"]["evm"]["bytecode"]["sourceMap"] != "") { | |
truffleObject["sourceMap"] = inputObj["compilerOutput"]["evm"]["bytecode"]["sourceMap"]; | |
} | |
if (inputObj["compilerOutput"]["evm"]["deployedBytecode"]["sourceMap"] != "") { | |
truffleObject["deployedSourceMap"] = inputObj["compilerOutput"]["evm"]["deployedBytecode"]["sourceMap"]; | |
} | |
var sourcePath; | |
Object.keys(inputObj['sources']).forEach(function(key) { | |
var val = inputObj['sources'][key]; | |
if ( typeof sourcePath === 'undefined' ) { | |
// if we don't have a sourcePath yet, assume its the first one we see | |
// TODO: maybe set if we see "ast" or "lagacyAST", but I think this will work. (willynilly JSON ordering be with us) | |
sourcePath = key; | |
} | |
if ( typeof val['ast'] !== 'undefined' && val['ast'] ) { | |
truffleObject["ast"] = val['ast']; | |
} | |
if ( typeof val['legacyAST'] !== 'undefined' && val['legacyAST'] ) { | |
truffleObject["legacyAST"] = val['legacyAST']; | |
} | |
}); | |
if ( typeof sourcePath === 'undefined' ) { | |
throw "sourcePath not detected!"; | |
} | |
if ( typeof truffleObject["ast"] === 'undefined' ) { | |
throw "AST is missing for " + inputFilename; | |
} | |
if ( typeof truffleObject["legacyAST"] === 'undefined' ) { | |
throw "legacyAST is missing for " + inputFilename; | |
} | |
truffleObject['sourcePath'] = sourcePath; | |
truffleObject['source'] = inputObj["sourceCodes"][sourcePath]; | |
// TODO: do we need the flattened source? | |
// TODO: check somewhere for mainnet contract addresses and add them to networks | |
// console.error(truffleObject); | |
// no need to do this here. truffle does this for us | |
// require("truffle-contract-schema").validate(truffleObject); | |
// give truffle the contract artifact formatted like it expects | |
console.log(JSON.stringify(truffleObject)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment