Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate rust structs from solidity JSON ABI #378

Merged
merged 8 commits into from Aug 16, 2021

Conversation

mattsse
Copy link
Collaborator

@mattsse mattsse commented Aug 14, 2021

Motivation

Currently, parameters of solidity functions that are solidity structures are generated to rust tuples, which then requires some extra boilerplate code if the user wants to use rust structs within the ethers program.

Solution

Since solidity 0.5.11 the solidity compiler adds an extra fields internalType to the inputs/outputs objects in the JSON ABI, which also contains structs with their qualified path (struct SomeLibrary.StructName). We can use this to essentially turn the tuple into a rust struct.

However this is not trivial because ethabi strips the internalType field entirely (rust-ethereum/ethabi#230), therefor we need to parse the abi string again but keep the internalType. I made a minimal representation auf the abi (RawAbi) which supports that.

Next challenge are nested structs, again since the ethabi totally removes any information about the internaltype, we need to do additional work. Essentially we start with the structs that are used as inputs and outputs and check their fields recursively for structs. Since all structs boil down to elementary type, we will eventually end up with structs that only use elementary types.
This tedious step is necessary, so that we don't end up with lots of struct duplicates.

  • turn JSON ABI tuples into rust structs
  • use them as input parameters for the corresponding contract calls
  • use them as return types for the corresponding contract calls
  • test some edge cases, like name collisions LibraryA.Point LibraryB.Point

@gakonst
Copy link
Owner

gakonst commented Aug 16, 2021

As discussed, this seems to work well so far so merging as-is.

@gakonst gakonst merged commit ba5f650 into gakonst:master Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants