web3j – How to Check Balance and Transfer ERC20 Custom Token

web3j

I'm studying to create wallets using infura, web3j, and android studio.

I am using ropsten testnet.
I have been able to check the amount of ethereum in my wallet and send ethereum to another account.

But I do not know how to check the balance of the custom token I issued and how to transfer it to another account.

Best Answer

  1. ` ERC20 javaToken = ERC20.load(contractAddress, web3, creds, new DefaultGasProvider());//load contract

    EthBlockNumber blockNumber = web3.ethBlockNumber().send();
    
    System.out.println("erc20 class loaded");
    
    //to check balance use below code
    System.out.println("Balance of Account:'0xF01413046858033fbDC816B81DCef1E055ee8E42' :"
            + javaToken.balanceOf("0xF01413046858033fbDC816B81DCef1E055ee8E42").send() + "");
    BigInteger totalSuppy = javaToken.totalSupply().send();
    System.out.println("Total suppy of token is:\t" + totalSuppy);
    
    String symbol = javaToken.symbol().send();
    System.out.println("Information of token");
    String name = javaToken.name().send();
    BigInteger decimal = javaToken.decimals().send();
    

    //to transfer tokens use below code TransactionReceipt receipt = javaToken.transfer("0xdC99e1F3a42553C183B85FC760B3943eAe8539Ef", value).send(); BigInteger blockNumber54 = receipt.getBlockNumber();

    BigInteger GasUsed = receipt.getGasUsed();
    List<Log> logs = receipt.getLogs();
    
      BigInteger balance = javaToken.balanceOf("0xdC99e1F3a42553C183B85FC760B3943eAe8539Ef").send();
    System.out.println("Balace of :'0xdC99e1F3a42553C183B85FC760B3943eAe8539Ef': " + balance.toString()
            + "\nTransaction Receipt:\t" + receipt + "\nInfo stored in 
    

    Block Number: " + blockNumber54 + "\nGas Usage:" + GasUsed); `

Here's an small snippet from my code, mine is also an custom token