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

发布于 2025-01-11 15:44:52 字数 2766 浏览 4 评论 0原文

我尝试使用 web3j 发送 erc20 令牌,它总是抛出协议异常并且事务始终处于待处理状态。我的代码有什么问题吗?

代码:


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

异常:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2025-01-18 15:44:52

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

下面使用 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文