In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve web3j batch transfer". In daily operation, I believe many people have doubts about how to achieve web3j batch transfer. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve web3j batch transfer". Next, please follow the editor to study!
Use web3j to connect to geth and transfer money. The basic transfer function can be written as follows:
/ / from: transferor account / / password: transferor password / / addrTo: payee account / / value: transfer amount public String transferEth (String from,String password,String to,BigInteger value) throws Exception {EthGetTransactionCount ethGetTransactionCount = ethClient.ethGetTransactionCount (from, DefaultBlockParameterName.LATEST). SendAsync (). Get (); BigInteger nonce = ethGetTransactionCount.getTransactionCount () PersonalUnlockAccount personalUnlockAccount = ethClient.personalUnlockAccount (from,password). Send (); if (personalUnlockAccount.accountUnlocked ()) {BigInteger gasPrice = Contract.GAS_PRICE; BigInteger gasLimit = Contract.GAS_LIMIT.divide (new BigInteger ("2")); synchronized (TestLocal.class) {Transaction transaction = Transaction.createEtherTransaction (from,nonce,gasPrice,gasLimit,to,value) EthSendTransaction transactionResponse = ethClient.ethSendTransaction (transaction). SendAsync (). Get (); if (transactionResponse.hasError ()) {String message=transactionResponse.getError () .getMessage (); System.out.println ("transaction failed,info:" + message); Utils.writeFile ("F:/testErr.txt", "transaction failed,info:" + message) Return message;} else {String hash=transactionResponse.getTransactionHash (); System.out.println ("transaction from" + from+ "to" + to+ "amount:" + value); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") / / writeFile ("transaction from" + from+ "to" + to+ "amount:" + value+ "time:" + df.format (new Date (); return hash;} return null;}
In the above function, when the transfer fails, the failed result will be written to F:/testErr.txt.
Batch transfer is to continuously call the above function in the for cycle to transfer money. Now set 10 consecutive transfers from addr0 to addr1:
For (int ionometeri web3.formwei (eth.getBalance (eth.statements [1])) 102.9110385
If you run the batch transfer function, you will find that the console reported an error:
Check the balance and find that there is only one successful transfer:
> web3.formwei (eth.getBalance (eth.statements [1]) 103.9110385
View the printed error message textErr.txt:
It can be seen that nine deals failed and only the first one was successful. The failed message is due to a duplicate transaction. Preliminary judgment is that there is something wrong with the nonce setting. For a single transfer, nonce can be queried from the blockchain. In the for loop, you need to increment the nonce yourself.
Modify the transferEth function as follows:
Public String transferEthWith (String from,String password,String to,BigInteger value) throws Exception {EthGetTransactionCount ethGetTransactionCount = ethClient.ethGetTransactionCount (from, DefaultBlockParameterName.LATEST). SendAsync (). Get (); BigInteger nonce = ethGetTransactionCount.getTransactionCount (); if (gNoce = = null) gNoce = nonce; PersonalUnlockAccount personalUnlockAccount = ethClient.personalUnlockAccount (from,password). Send () If (personalUnlockAccount.accountUnlocked ()) {BigInteger gasPrice = Contract.GAS_PRICE; BigInteger gasLimit = Contract.GAS_LIMIT.divide (new BigInteger ("2")); synchronized (TestLocal.class) {Transaction transaction = Transaction.createEtherTransaction (from,gNoce,gasPrice,gasLimit,to,value); gNoce = gNoce.add (new BigInteger ("1")) EthSendTransaction transactionResponse = ethClient.ethSendTransaction (transaction). SendAsync (). Get (); if (transactionResponse.hasError ()) {String message=transactionResponse.getError () .getMessage (); System.out.println ("transaction failed,info:" + message); Utils.writeFile ("F:/testErr.txt", "transaction failed,info:" + message) Return message;} else {String hash=transactionResponse.getTransactionHash (); System.out.println ("transaction from" + from+ "to" + to+ "amount:" + value); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") / / writeFile ("transaction from" + from+ "to" + to+ "amount:" + value+ "time:" + df.format (new Date (); return hash;} return null;}
Looking at geth's log information, you can see that multiple deals have been submitted:
Query the account balance of addr1, from 103 to 113. the transfer was successful:
> web3.formwei (eth.getBalance (eth.transfers [1]) 113.9110385 at this point, the study on "how to achieve batch transfer of web3j" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.