let say we have the following function in solidity:

function tryAbiEncode (address  _address, uint8 _amount) public pure returns (bytes memory)
    return abi.encode("\x19Ethereum Signed Message:\n",_address, _amount);

If I executed the above function with the following parameters:

_address = 0xbfF89Fe7598f162ACC86CfC3267Eb132F69B7e2B
_amount = 10

I will get the below result:


My question is, how I can get the same results but in Java using web3j library ? I mean how I can execute abi.encode but in Java ?



4 Answers 4


Web3j does not provide such methods. However, you can tinker with the internal types to get what you're looking for.

A good place to start is the ABI tests.

You should define a Function with the parameters you specified. Then, you can encode it the same way we are doing in most tests.


*** TypeEncoder.encode([the generated type]) *** If you are using a web3j client for the generation of the java code, your types will extend DynamicStruct which you you can use in TypeEncoder.encode to get the correctly encoded Type.

Lastly you need to append ethereum signed message and it should be fine


You can get the same result with web3j using TypeEncoder and DynamicStruct.

Note that the JavaScript control character \x19 is represented as \u0019 in Java, so that needs to be replaced accordingly.

import org.junit.jupiter.api.Test;
import org.web3j.abi.TypeEncoder;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.DynamicStruct;
import org.web3j.abi.datatypes.Uint;
import org.web3j.abi.datatypes.Utf8String;

import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AbiEncodingTest {

    public void testAbiEncoding() {
        String result = "0x" +
                TypeEncoder.encode(new DynamicStruct(
                        new Utf8String("\u0019Ethereum Signed Message:\n"),
                        new Address("0xbfF89Fe7598f162ACC86CfC3267Eb132F69B7e2B"),
                        new Uint(BigInteger.valueOf(10))));
        assertEquals("0x" +
                "0000000000000000000000000000000000000000000000000000000000000060" +
                "000000000000000000000000bff89fe7598f162acc86cfc3267eb132f69b7e2b" +
                "000000000000000000000000000000000000000000000000000000000000000a" +
                "000000000000000000000000000000000000000000000000000000000000001a" +
                "19457468657265756d205369676e6564204d6573736167653a0a000000000000", result);


Does FunctionEncoder is what you look for?

public void testFunctionSimpleEncode() {
    Function function =
            new Function(
                    Arrays.asList(new Uint32(BigInteger.valueOf(69)), new Bool(true)),
                + "0000000000000000000000000000000000000000000000000000000000000045"
                + "0000000000000000000000000000000000000000000000000000000000000001",

Web3j source code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.