Skip to content

Instantly share code, notes, and snippets.

@chriseth
Created September 13, 2016 23:42
Show Gist options
  • Save chriseth/8f533d133fa0c15b0d6eaf3ec502c82b to your computer and use it in GitHub Desktop.
Save chriseth/8f533d133fa0c15b0d6eaf3ec502c82b to your computer and use it in GitHub Desktop.
ABI to solidity interface converter
#!/usr/bin/env node
// Credits: @rangelife
var fs=require('fs');
try {
var abi=JSON.parse(fs.readFileSync(process.argv[3]));
} catch (e) {
dieUsage();
}
var contractName=process.argv[2];
if(!contractName) dieUsage();
var toDump="";
function dump(x) {
toDump += x;
}
dump("contract "+contractName);
dump("{");
var i=0;
for(i=0;i<abi.length;i++) {
var entity=abi[i];
if (!Array.isArray(entity.inputs)) {
die("entity inputs isn't an array in "+JSON.stringify(entity));
}
switch (entity.type) {
case 'constructor':
dumpConstructor(entity);
break;
case 'function':
dumpFunction(entity);
break;
case 'event':
// these don't go in the interface file
break;
default:
die("Unknown entity type "+entity.type+" in ABI.");
}
}
dump("}");
console.log(toDump);
///////////
function die(x) {
console.error(x);
process.exit(1);
}
function dieUsage() {
die("Usage: "+process.argv[1]+" [contractName] [ABI file]");
}
function dumpConstructor(entity) {
dump("function "+contractName+"(");
dumpArgs(entity.inputs);
dump(");");
}
function dumpFunction(entity) {
dump("function "+entity.name+"(");
dumpArgs(entity.inputs);
dump(") ");
if (entity.constant) {
dump("constant ");
}
if (entity.outputs.length) {
dump("returns (");
dumpArgs(entity.outputs);
dump(")")
}
dump(";");
}
function dumpArgs(args) {
var i;
for (i=0;i<args.length;i++) {
var arg = args[i];
if(i) dump(",");
dump(arg.type+" "+arg.name);
}
}
@Rancid1616
Copy link

Rancid1616 commented Oct 5, 2022 via email

@Shuebird-svg
Copy link

Marykassman can you help me gain access to this contract 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2. I've already got verified I on etherscan and was able to move and somewhat interact with tokens. Just no withdrawl. Yet.......

@Rancid1616
Copy link

Rancid1616 commented Nov 26, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment