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

abi.decode to accept types as parameter and output as struct #7134

Closed
thegostep opened this issue Jul 20, 2019 · 4 comments
Closed

abi.decode to accept types as parameter and output as struct #7134

thegostep opened this issue Jul 20, 2019 · 4 comments
Labels
language design :rage4: Any changes to the language, e.g. new features

Comments

@thegostep
Copy link

Abstract

With AbiEncoderV2 around the corner, it would be great to be able to pass in a template of types as a parameter to the abi.decode() function and have it output a struct with the desired shape.

function sample(bytes input) public returns (address outputA, uint256 outputB) {
    (outputA, outputB) = abi.decode(input, (address, uint256);
}

Becomes

template = '(address outputA, uint256 outputB)'

function sample(bytes input, string template) public returns (struct output) {
    output = abi.decode(input, template);
}

output => {
    address outputA;
    uint256 outputB;
}

Motivation

This allows for using abi.encode and abi.decode for encoding arbitrary data without specifying hardcoded types.

Specification

unknown

Backwards Compatibility

unknown

@leonardoalt
Copy link
Member

I like the general idea, but why not simply ask to decode into a struct?
Example:

    struct S { uint x; address a; }
    function f(bytes memory data) public {
        (S memory s) = abi.decode(data, (S));
    }

@leonardoalt leonardoalt added the language design :rage4: Any changes to the language, e.g. new features label Jul 25, 2019
@leonardoalt
Copy link
Member

Answering my own question:
I guess what you're also interested in is passing this template at runtime instead of compile time?
In that case, wouldn't that break/make the ABI ambiguous?

@thegostep
Copy link
Author

My use case was for creating a registry which serves as a generic factory for arbitrary contracts. I ended up moving away from the generic factory and instead resolved to adding custom factories which can have ABI decoding template in the code.

The ability to decode into a struct would be great. Perhaps providing a template at runtime may be used in other use cases.

@chriseth
Copy link
Contributor

chriseth commented Aug 5, 2019

If I understand you correctly, this is either about templates or about dynamic types. There is another issue that proposes to implement templates ( #869 ) and dynamic types is against the core design of Solidity, so I'm closing this. Please reopen if I misunderstood you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language design :rage4: Any changes to the language, e.g. new features
Projects
None yet
Development

No branches or pull requests

3 participants