I would like to ask how to correctly send ERC20 Token from a wallet to another wallet , followings are my codes :

public static void main(String[] args) throws Exception {
    Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/
    Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
    String clientVersion = web3ClientVersion.getWeb3ClientVersion();
    Credentials creds = WalletUtils.loadCredentials("password",
    EthGetBalance ethGetBalance = web3.ethGetBalance(creds.getAddress(), DefaultBlockParameterName.LATEST)

    BigInteger wei = ethGetBalance.getBalance();

    FixedSupplyToken_sol_FixedSupplyToken contract = FixedSupplyToken_sol_FixedSupplyToken
            .load("0x<CONTRACT_ADDRESS>", web3, creds, gasPrice, gasLimit);

    BigInteger tokenAmount = contract.balanceOf(creds.getAddress()).send();

    contract.transfer("0x<TO_ADDRESS>", new BigInteger("10000")).send();

When I tried to run this code, its working,the transaction is being submitted to my private network, but it return these errors.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.web3j.protocol.exceptions.TransactionException: Error processing request: unknown transaction
    at org.web3j.tx.response.TransactionReceiptProcessor.sendTransactionReceiptRequest(TransactionReceiptProcessor.java:32)
    at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:37)
    at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:29)
    at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:72)
    at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51)
    at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:70)
    at org.web3j.tx.Contract.executeTransaction(Contract.java:223)
    at org.web3j.tx.Contract.executeTransaction(Contract.java:207)
    at org.web3j.tx.Contract.executeTransaction(Contract.java:201)
    at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:240)
    at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
    at com.gio.myapp.App.main(App.java:50)

Is there anything that I missed here ?

  • can you provide my your whole code for creating erc20 transaction and explain how it works? i am trying to make erc20 wallet for few months but i dont know how to do it.. any help? Apr 11 '18 at 15:21
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
    – ivicaa
    Apr 11 '18 at 19:59

You can manually encode data with the function:

public static String encodeTransferData(String toAddress, BigInteger sum) {
    Function function = new Function(
            "transfer",  // function we're calling
            Arrays.asList(new Address(toAddress), new Uint256(sum)),  // Parameters to pass as Solidity Types
            Arrays.asList(new org.web3j.abi.TypeReference<Bool>() {}));
    return FunctionEncoder.encode(function);

and call the contract this way:

public static void main(String[] args) throws Exception {
    Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/    
    Credentials creds = WalletUtils.loadCredentials("password", "path/to/wallet");
    RawTransactionManager manager = new RawTransactionManager(web3, creds);
    String toAddress = "0x..."; // destination address
    String contractAddress = "0x...";
    BigInteger sum = BigInteger.valueOf(10000); // amount you want to send
    String data = encodeTransferData(toAddress, sum);
    BigInteger gasPrice = web3.ethGasPrice().send().getGasPrice();
    BigInteger gasLimit = BigInteger.valueOf(120000); // set gas limit here
    EthSendTransaction transaction = manager.sendTransaction(gasPrice, gasLimit, contractAddress, data, null);

This has to do with the transaction hash not being found in the blockchain.

Try for example to output the transactionhash as a String after the transaction.

Also try replacing the line

contract.transfer("0x<TO_ADDRESS>", new BigInteger("10000")).send();


contract.transfer("0x<TO_ADDRESS>", new BigInteger("10000")).sendAsync().get();

may help you locate the problem more explicitly or even resolve it.

  • TransactionReceipt tr = contract.transfer("0x80daA7E15FE9D96CC5dfdC1d61009d3108eb6ED4", new BigInteger("10000")).sendAsync().get(); String hash = tr.getTransactionHash(); I tried changing the code become the code above, no luck, same error occurs Mar 1 '18 at 16:39
  • So ? What happen ?
    – AvgJoe54
    Mar 1 '18 at 16:40
  • nothing happen, same error still occurs Mar 2 '18 at 2:29

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.