I am trying to send erc20 tokens using web3j , it always throw the protocol exception and transaction is always pending. Is there anything wrong i did in my code?
Code:
Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/v3/xxxxxxxxxxxx"));
Credentials creds = Credentials.create(privateKey);
String tokenAddress = "0x............."
ERC20 contract = ERC20.load(tokenAddress, web3, creds, new StaticGasProvider(new BigInteger('25000000000'), new BigInteger('5500000')));
String tokenName = contract.name().send()
String tokenSymbol = contract.symbol().send()
def balanceToken = contract.balanceOf(creds.address).send()
resp = contract.transfer("0xXXXX", new BigInteger('100')).send()
Exception:
org.web3j.protocol.exceptions.TransactionException: Transaction receipt was not generated after 600 seconds for transaction: 0x4e730fab8621396ef520d976b777db62ea79e30b7b402d6206dba47d59ccc2fc
at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:61)
at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:38)
at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:181)
at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:81)
at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:128)
at org.web3j.tx.Contract.executeTransaction(Contract.java:367)
at org.web3j.tx.Contract.executeTransaction(Contract.java:350)
at org.web3j.tx.Contract.executeTransaction(Contract.java:344)
at org.web3j.tx.Contract.executeTransaction(Contract.java:339)
at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:410)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:42)
at org.web3j.protocol.core.RemoteCall$send.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at com.elintegro.coinface.cft.BuyCftService.$tt__buyCFT(BuyCftService.groovy:246)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
This is how I fixed the problem.
The below code for transferring erc20 tokens using web3j works fine.
Web3j web3 = Web3j.build(new HttpService(https://ropsten.infura.io/)) // for ropsten test network
Credentials credentials = Credentials.create(privateKey);
EthGetTransactionCount ethGetTransactionCount = web3.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
BigInteger amountToSend = new BigInteger(100); // you can provide yourself how much you want to send
Function function = new Function(
"transfer",
Arrays.asList(new Address(toAddress), new Uint256(amountToSend)),
Collections.singletonList(new TypeReference<Bool>() {
}));;
String encodedFunction = FunctionEncoder.encode(function);
BigInteger gasLimit = BigInteger.valueOf(71000); // you can customize these
BigInteger gasPrice = new BigInteger(100000); //
RawTransaction rawTransaction =
RawTransaction.createTransaction(
nonce, gasPrice, gasLimit, <contract_address_of_erc20_token>, encodedFunction);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(hexValue).sendAsync().get();
String transactionHash = ethSendTransaction.getTransactionHash();