esaulpaugh / headlong

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

How to parse struct as input argument? #47

Closed ShoaibKakal closed 2 years ago

ShoaibKakal commented 2 years ago

I've a function that takes a struct as an argument.

    function addUser(User memory newUser) public payable{
        users.push(newUser);
    }

How can i parse this in headlong?

If i do the following, it does not work val headLongFun = com.esaulpaugh.headlong.abi.Function("addUser(string)")

esaulpaugh commented 2 years ago

a Function that takes a struct as an argument would be something like Function("addUser(())") where () is the struct (i.e. tuple).

If that struct contains a string, the representation of the argument would be (string) i.e. Function("addUser((string))")

ShoaibKakal commented 2 years ago

Great! would love to contribute by adding documentation of this library once i got some grip Thanks @esaulpaugh but after doing this i got an error on encodeCallWithArgs() that tuple index 0: class mismatch: java.lang.String != com.esaulpaugh.headlong.abi.Tuple ((string) requires Tuple but found String) My current code looks like this

val headLongFun = com.esaulpaugh.headlong.abi.Function("addUser((string))")
                val headLongEncode =
                    headLongFun.encodeCallWithArgs("esaulpaugh")
ShoaibKakal commented 2 years ago

I tried using .encode() but same error:

 val args = Tuple.of("esaulpaugh")
                val headLongFun = com.esaulpaugh.headlong.abi.Function("addUser((string))")
                val headLongEncode =
                    headLongFun.encodeCall(args)
ShoaibKakal commented 2 years ago

Solved it buy passing: val args = Tuple.of(Tuple.of("esaulpaugh")), Tuple inside tuple.