esaulpaugh / headlong

High-performance Contract ABI and RLP for Ethereum
Apache License 2.0
76 stars 20 forks source link

How to create function that returns array? #37

Closed tomasz90 closed 2 years ago

tomasz90 commented 2 years ago

I've got problem with creation function that returns struct array. My function looks like: getPoolsInfo(address[]) then the output is array of structs. Struct has fields: address, string, string I tried to create function like this:

val f = Function("getPoolsInfo(address[])", "(address,string,string)[]")

But ide is screaming that it cannot cast arraytype to tupletype. Can you tell me how to construct this function properly?

tomasz90 commented 2 years ago

FYI and anyone that might look for this: ((address,string,string))

esaulpaugh commented 2 years ago

Inputs and outputs must be specified by a TupleType, as all Functions accept a Tuple of arguments and return a Tuple of return values. What you're looking for is a singleton return type, a tuple with one element. Try:

Function("getPoolsInfo(address[])", "((address,string,string)[])")

For singleton return types, there is a shortcut method as well which unwraps the return Tuple for you and infers the type of the one element:

Tuple[] zeroth = f.decodeSingletonReturn(bytes);