-
-
Save fducom/459fde95479565c82910ec1ed8c5c323 to your computer and use it in GitHub Desktop.
Convert ABI to solidity file
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
/** | |
* Convert ABI json file to solidity interface contract | |
*/ | |
const fs = require('fs'); | |
const args = process.argv.slice(2); | |
function convert(inputFilename) { | |
const abi = JSON.parse(fs.readFileSync(inputFilename, 'utf8')); | |
const name = inputFilename.match(/(\w+)/)[1] | |
const w = process.stdout.write.bind(process.stdout); | |
w(`pragma solidity >=0.4.21 <0.6.0;\n`) | |
w(`// converted from ${inputFilename}\n`); | |
w(`contract ${name} {\n\n`) | |
abi.forEach(f => { | |
if (f.type === "constructor") return; | |
w(` ${f.type}${f.name?" "+f.name:""}`); | |
if (f.inputs && f.inputs.length) { | |
w("(\n"); | |
w(f.inputs.map(i => ` ${i.type} ${["string"].indexOf(i.type)>=0?" calldata ":""}${i.name}`).join(",\n")); | |
w("\n )"); | |
} else { | |
w("()"); | |
} | |
if (["function"].indexOf(f.type) >= 0) { | |
w(" external"); | |
} | |
w(`${f.view ? " view" : ""}${f.payable?" payable":""}`) | |
if (f.outputs && f.outputs.length){ | |
w(" returns (\n"); | |
w(f.outputs.map(o => ` ${o.type}${["string"].indexOf(o.type)>=0?" memory":""}${o.name ? " "+o.name:""}`).join(",\n")); | |
w("\n )"); | |
} | |
w(";\n\n"); | |
}) | |
w("}\n") | |
} | |
args.forEach(convert) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment