Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Encoding a pure uint256[] function parameter #48

Closed
p00temkin opened this issue Nov 4, 2022 · 2 comments
Closed

Encoding a pure uint256[] function parameter #48

p00temkin opened this issue Nov 4, 2022 · 2 comments

Comments

@p00temkin
Copy link

Hi, I have a question on how to encode a uint256[] parameter. Creating a tuple for the following works just fine:

Function func = new Function("test(uint256[],bool)");
BigInteger[] bigints = new BigInteger[] { BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9) };
Tuple args = Tuple.of(bigints, true);
ByteBuffer bb = func.encodeCall(args);
System.out.println(Function.formatCall(bb.array())); 

but if the function argument is a pure uint256[] such as

Function func = new Function("test(uint256[])");
BigInteger[] bigints = new BigInteger[] { BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9) };
Tuple args = Tuple.of(bigints);
ByteBuffer bb = func.encodeCall(args);
System.out.println(Function.formatCall(bb.array())); 

the encoding fails with the following trace:

java.lang.IllegalArgumentException: tuple length mismatch: actual != expected: 3 != 1
	at com.esaulpaugh.headlong.abi.TupleType.validate(TupleType.java:146)
	at com.esaulpaugh.headlong.abi.Function.validatedCallLength(Function.java:193)
	at com.esaulpaugh.headlong.abi.Function.encodeCall(Function.java:205)

Any suggestions on how to pass the pure uint256[] correctly in this case?

Thank you

@p00temkin
Copy link
Author

Solved by using Tuple.singleton() in this case .. realized just after posting :)

@esaulpaugh esaulpaugh pinned this issue Nov 6, 2022
@esaulpaugh
Copy link
Owner

Alternatively you can also do an explicit cast to Object:

Tuple.of((Object) bigints)

so that bigints doesn't accidentally match Object[] and get unpacked:

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants