日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

web3j批量转账

發(fā)布時(shí)間:2024/1/1 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 web3j批量转账 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用web3j來連接geth并轉(zhuǎn)賬,基本轉(zhuǎn)賬函數(shù)可以這樣寫:

//以太坊轉(zhuǎn)賬//from:轉(zhuǎn)出方賬戶//password:轉(zhuǎn)出方密碼//addrTo:收款賬戶//value:轉(zhuǎn)賬額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;}

上面函數(shù)中,當(dāng)轉(zhuǎn)賬失敗,會(huì)將失敗結(jié)果寫入F:/testErr.txt中。

批量轉(zhuǎn)賬就是在for循環(huán)中連續(xù)調(diào)用上面這個(gè)函數(shù)進(jìn)行轉(zhuǎn)賬,現(xiàn)在設(shè)置從addr0向addr1連續(xù)轉(zhuǎn)賬10次:

for(int i=0;i<10;i++) {transferEth(addr0,"123",addr1,Convert.toWei("1", Convert.Unit.ETHER).toBigInteger()); }

先查詢addr1的余額,有102.9110385個(gè)ether:


運(yùn)行批量轉(zhuǎn)賬函數(shù),會(huì)發(fā)現(xiàn)控制臺(tái)報(bào)錯(cuò):


查詢余額,發(fā)現(xiàn)只轉(zhuǎn)成功了一筆:


查看打印的錯(cuò)誤信息textErr.txt:


可見失敗了9筆交易,只有第一筆交易成功了。失敗的信息是因?yàn)橛薪灰字貜?fù)。初步判斷是nonce設(shè)置出問題了。對于單筆轉(zhuǎn)賬,nonce可以從區(qū)塊鏈查詢到,在for循環(huán)里,需要自己去遞增nonce。

修改transferEth函數(shù)如下:

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;}

查看geth的log信息,可以看到提交了多筆交易:


查詢addr1的賬戶余額,從103變成113了,轉(zhuǎn)賬成功:


總結(jié)

以上是生活随笔為你收集整理的web3j批量转账的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。