Skip to content

Instantly share code, notes, and snippets.

@hellwolf
Last active June 9, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hellwolf/71779776003a99b82dd641f76e420e63 to your computer and use it in GitHub Desktop.
Save hellwolf/71779776003a99b82dd641f76e420e63 to your computer and use it in GitHub Desktop.
Convert ABI to solidity file
/**
* 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