This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
feat: generate rust structs from solidity JSON ABI #378
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
LibraryA.Point
LibraryB.Point