0

I am trying to use Web3j to query the balanceOf() function of a standard ERC20 token as shown in the code below:

try {
        String encodedFunction = FunctionEncoder.encode(function);
        String contractAddress = "0x4330e0c338b48128a4534d90fd83579ef5539998";
        Address[] addressInput = new Address[]{new Address(fromAddress)};
        Function function = new Function(
                "balanceOf",
                Arrays.<Type>asList(addressInput),
                Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {
                }));

        String fromAddress = "0x266d3299d8ede74e9dce770366bbe59179f676ef";
        EthCall response = web3j.ethCall(
                Transaction.createEthCallTransaction(fromAddress, contractAddress, encodedFunction),
                DefaultBlockParameterName.LATEST)
                .sendAsync().get();

        if (response == null) {
            System.out.println("Nothing was returned !!!");
        } else {
            if (response.getValue() == null) {
                System.out.println("Nothing was returned !!!");
            } else {
                System.out.println("Detected response ...");
                List<cryptocurrency.proto.eth.abi.datatypes.Type> someTypes = FunctionReturnDecoder.decode(
                        response.getValue(), function.getOutputParameters());

                System.out.println("Return Value(s)");
                System.out.println("===============");
                for (int i = 0; i < someTypes.size(); i++) {
                    System.out.println("" + someTypes.get(i).getValue());
                }
            }
        }
} catch (InterruptedException | ExecutionException ex) {
    Logger.getLogger(TestERC20.class.getName()).log(Level.SEVERE, null, ex);
}

The response is no response as shown below:

Detected response ...
Return Value(s)
===============

I am trying to querying this contract (https://ropsten.etherscan.io/token/0x4330e0c338b48128a4534d90fd83579ef5539998) using a token owner address of (https://ropsten.etherscan.io/token/0x4330e0c338b48128a4534d90fd83579ef5539998?a=0x266d3299d8ede74e9dce770366bbe59179f676ef).

What am I missing to get the above code to work on the balanceOf() method and return me the answer of the current holding amount of 999,981,060.605788211684519359 ZEN tokens belonging to the above token owner address ?

0

I have found out some missing variables that are causing my querying of balance to not work.

The reason is I did not fill in the parameter of the address I wanted to query.

The above codes have been edited and are now fully working to query an address's balance from a supplied contract address.

The output is now:

Detected response ...
Return Value(s)
===============
999981049099940051684519338

Which is the correct amount of tokens at the moment the query was executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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