0

I'm looking for the java version of the solution of this question.

How is the address of an Ethereum contract computed?

Actually I want have a solution of the problem below.

I have a master contract which creates many sub contracts. And I need to get the addresses of the sub contracts when it's created.

master contract creates sub contract with a function like this

function createForwarder() public returns (address) {
    return new Forwarder();
}

But I can't get the address from this function. I'm using web3j and it has not internal transaction concept. So I need to get the address with another way.

What will be the best solution for me?

1 Answer 1

1

Here is my solution in Java using web3j utilities. We have to convert the address to a byte[], then encode, then Hash, then transform the output. I've included my steps below. Hope this helps!

    private String calculateContractAddress(String address, long nonce){
    byte[] addressAsBytes = Numeric.hexStringToByteArray(address);

    byte[] calculatedAddressAsBytes =
            Hash.sha3(RlpEncoder.encode(
                    new RlpList(
                            RlpString.create(addressAsBytes),
                            RlpString.create((nonce)))));

    calculatedAddressAsBytes = Arrays.copyOfRange(calculatedAddressAsBytes,
            12, calculatedAddressAsBytes.length);
    String calculatedAddressAsHex = Numeric.toHexString(calculatedAddressAsBytes);
    return calculatedAddressAsHex;
}
0

Your Answer

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

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