ethereum - 使用 web3j 发送 ERC20 代币始终处于挂起状态

标签 ethereum erc20 web3-java

我尝试使用 web3j 发送 erc20 token ,它总是抛出协议(protocol)异常并且交易始终处于挂起状态。我的代码有什么问题吗?

代码:


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)

最佳答案

这就是我解决问题的方法。

下面使用 web3j 传输 erc20 代币的代码工作正常。

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();

关于ethereum - 使用 web3j 发送 ERC20 代币始终处于挂起状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71332019/

相关文章:

ethereum - 交易完成,但NFT未转移

blockchain - 如何让 dapp 在常规浏览器上运行?

token - 如何创建一个 ‘feeless’ ERC20 代币?

javascript - 如何使用Web3购买ERC20代币

ethereum - web3j - 带有字符串参数的事件的 TypeReference

ethereum - 如何解决此 Hardhat 编译器错误? (编译内联汇编时堆栈太深)

javascript - 为什么 web3.eth.getAccounts().then(console.log) 返回空数组?

token - 获取以太坊 ERC-20 代币信息的正确方法

java - web3J 与 infura 的兼容性

blockchain - web3j 中凭证的钱包路径是什么?