accounts/abi: handle named ouputs prefixed with underscores #15766
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.
I've found this issue while trying to wrap the smart contracts from Akasha.
If a smart contract has named outputs (i.e. all of the outputs have names),
abigen
will group them into an on-the-fly generated struct, instead of simply returning a gazillion return value:For the above example,
abigen
can convert the return names (e.g.akashaId
) into output struct field names (e.g.AkashaId
) by capitalizing the first letter. Similarly, theabi
package can also unpack the fields using the same logic.Unfortunately, Akasha doesn't name its return values like that, rather it prepends an underscore in front of all the names, resulting in the following binding:
Of course trying to run the above generated code will fail at two locations:
_fieldname
fields, since they are private.abi
package cannot match the fields with the returns, since it only capitalizes.This PR solves the underlying issue by having a pre-processing step in both
abigen
and also inabi
, where we discard all prefixing underscores from variable names before trying to generate output structs, as well as before trying to match the deserialized outputs into said struct.The PR also adds a live test case (backed by a simulated backend) to verify that both binding underscores work (i.e. they compile and generate the correct code), as well as unmarshalling works (i.e. the test creates a live contract and retrieves data from it).