esaulpaugh / headlong

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

how to encode address argument #19

Closed VictorECDSA closed 3 years ago

VictorECDSA commented 3 years ago
        Function function = new Function("foo(address)");
        ByteBuffer byteBuffer = function.encodeCallWithArgs("f85a7c4b088c07ec9e982f158b63385ff20d6757");

throw exception as follow:

Exception in thread "main" java.lang.IllegalArgumentException: tuple index 0: class mismatch: java.lang.String not assignable to java.math.BigInteger (String not instanceof BigInteger/address)

please help me, thank you

esaulpaugh commented 3 years ago

address is treated similarly to uint160, so if you have the address as a String, something like new BigInteger("f85a7c4b088c07ec9e982f158b63385ff20d6757", 16); should work. 16 to indicate hexadecimal encoding, base 16

VictorECDSA commented 3 years ago

address is treated similarly to uint160, so if you have the address as a String, something like new BigInteger("f85a7c4b088c07ec9e982f158b63385ff20d6757", 16); should work. 16 to indicate hexadecimal encoding, base 16

it is work. thank you!

esaulpaugh commented 2 years ago

Just added new utility methods:

BigInteger address = BigIntegerType.decodeAddress("0x0000000000000000000082095cafebabecafebab")

String address = BigIntegerType.formatAddress(bigInt)

esaulpaugh commented 2 years ago

There is a new datatype for addresses in v5.5.0. See Address.wrap https://github.com/esaulpaugh/headlong/blob/master/src/main/java/com/esaulpaugh/headlong/abi/Address.java