Ethers.js: Tool for generating human-readable ABI

Created on 3 Feb 2019  Â·  14Comments  Â·  Source: ethers-io/ethers.js

Is there a tool in the _ethers.js_ ecosystem that takes a smart contract interface and returns a human-readable ABI? If not, it'd be cool if there was...

As an aside, I think there's a very small error with the docs explaining the human readable ABI:

let ABI = [
    "event Transfer(address from, address to, uint amount)",
    "function transfer(address to, uint amount)",
    "function symbol() returns (string)"
]

I _think_ that symbol() function should have this signature:

"function symbol() view returns (string)"

....otherwise, _ethers.js_ thinks that's a transaction and will try and charge you ether. _I think_ - forgive me if I'm wrong. However, if I'm not, that might be a bug, too ;)

discussion enhancement

Most helpful comment

I'm working on the tool right now. Hopefully I'll get beta.2 ready soon; which will still be on top of ethers v4, but I'll be adding the cli to v5 after this and porting it over, so it will be renamed to @ethersproject/cli.

I believe it will handle the case you are talking about, but it would be great to get your feedback. Let me get to it and update this ticket in a few hours.

Also, you should check out: https://www.npmjs.com/package/solidity-parser-antlr . It will allow you to do away with regex; I had so many regex in my first version, to the point where it was nearly a full lexer, and this package saved me a lot of time. :)

All 14 comments

You are absolutely right! I will fix that typo today.

The ethers-ts tool does that, and has a library to help too, but that’s still on my desktop. I’ll try to get it released tomorrow.

You are absolutely right! I will fix that typo today.

Awesome!

The ethers-ts tool does that, and has a library to help too, but that’s still on my desktop. I’ll try to get it released tomorrow.

You are an absolute star :)

The documentation has been updated: https://docs.ethers.io/ethers.js/html/api-contract.html

ps. I get _most of the way there_ using this quick bash script, which writes, in a format which I use in my frontend, the human readable ABI to STDOUT :

for FILE in $(ls [AO]*.sol)
do

  echo "$FILE\n"

  # print out struct tuple first
  awk '!/0$/{printf $0}/0$/' $FILE | sed 's/^.*\(struct[^}]*}\).*$/\1/g' | sed 's/struct[^{]*{ /tuple(/' | sed 's/ }/)/' | sed 's/;/,/g' | sed 's/, )/)/' | tr -s " " | sed 's/e( /e(/'

  echo "\n"

  # now turn events and functions into ethers.js human readable ABI form
  NAME=`echo $FILE | sed 's/\.sol//' | sed 's/^./\L&/'`
  echo "static ${NAME}ABI = ["
  egrep "event|function" $FILE | sed 's/^[ ]*\([e|f]\)/\1/' | sed s'/^\(.*\);$/\t"\1",/'
  echo "]\n"

done

I'm aware I could do better using other tools, but that was fun and fast to figure out ;)

@ricmoo Does the tool you mentioned generate human-readable ABIs from .sol files or from "regular" ABIs? Thanks!

I had one that could get "part" done by just the ABI, but the missing part is the contract name, which makes things more robust, and in V5 I will be pulling in structs too, which requires the solidity files.

It seems far more reliable to base it off the Solidity file, the other thing it does is provider a static factory method too, which includes the byte code, so the contract can be used to deploy contracts in a OO way...

I finished off some backlog of tasks I had today, so tomorrow I'll focus on getting the updated CLI tools out (as a beta). They will be a lot more powerful in V5, which I'm also hoping to get out for beta testing soon, since there are libraries to convert back and forth between human-readable and the JSON ABI formats.

@ricmoo the dApp I'm currently working on uses a dozen or so smart contracts, and there are more coming. The dApp takes advantage of pragma experimental ABIEncoderV2 and its ability to _set_ and _get_ structs; handcrafting the necessary human-readable ABIs was getting a proper pain, so that little script above saves me hours!

Even so, the script's limited - it only outputs the last _struct_ it finds (I've tried different regex's for fixing that, but have failed to find anything that works, so far), so if a struct embeds another struct, I still have to handcraft that (i.e. tuple(byte32 _structVar, tuple(byte32 _otherStructVar) _otherstruct) _struct). It also fails to put the tuple stuff in its place in the function parameter, but that's a whole other level of complexity that I'm hoping you've solved ;)

I'm working on the tool right now. Hopefully I'll get beta.2 ready soon; which will still be on top of ethers v4, but I'll be adding the cli to v5 after this and porting it over, so it will be renamed to @ethersproject/cli.

I believe it will handle the case you are talking about, but it would be great to get your feedback. Let me get to it and update this ticket in a few hours.

Also, you should check out: https://www.npmjs.com/package/solidity-parser-antlr . It will allow you to do away with regex; I had so many regex in my first version, to the point where it was nearly a full lexer, and this package saved me a lot of time. :)

After diving into it again (it's been a while), I realize this version doesn't support structs... That was a reason I was waiting to move to v5 first, which has more libs for handling ABI. I've published it anyways, sans-abiv2, at npm install -g ethers-cli@next and then you can use ethers-ts generate contract.sol.

I'll start working on the v5 version right now with full struct support.

How's this going?

The new version should work, no? Sorry this fell of my radar, but if it doesn’t work let me know and I’ll take a look into it. :)

@ricmoo are there any updates to generating Human Readable ABI from Solidity source files or is there a better way now todo this?

The package given in your guidance is deprecated and the recommended package @ethersproject/cli which doesn't appear to include this functionality.

Running generate with the ethers-cli@next package it doesn't respect the solidity compiler version specified in the .sol.

ParserError: Source file requires different compiler version (current compiler is 0.5.17+commit.d19bba13.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version\n' +
      'pragma solidity ^0.7.3;\n' +

Thanks

In testing, a direct copy/paste of function worked fine, easily manageable by hand for a small interface at least.

But that's the point - not-so-easily fashioned/managed by hand when your
app' gets beyond trivial...

I have a script that gets most of the way there...but a tool that did it
all would be awesome.

Steve

On Mon, 14 Dec 2020 at 20:24, Martin Walsh notifications@github.com wrote:

In testing, a direct copy/paste of function worked fine, easily manageable
by hand for a small interface at least.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/ethers-io/ethers.js/issues/413#issuecomment-744688750,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ACGV6INVP5TZVEZP6OJS7RTSUZX7TANCNFSM4GUBJX4A
.

Was this page helpful?
0 / 5 - 0 ratings