您好,登錄后才能下訂單哦!
這篇文章主要介紹“web3j批量轉賬怎么實現”,在日常操作中,相信很多人在web3j批量轉賬怎么實現問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”web3j批量轉賬怎么實現”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
使用web3j來連接geth并轉賬,基本轉賬函數可以這樣寫:
//以太坊轉賬 //from:轉出方賬戶 //password:轉出方密碼 //addrTo:收款賬戶 //value:轉賬額 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; }
上面函數中,當轉賬失敗,會將失敗結果寫入F:/testErr.txt中。
批量轉賬就是在for循環中連續調用上面這個函數進行轉賬,現在設置從addr0向addr1連續轉賬10次:
for(int i=0;i<10;i++) { transferEth(addr0,"123",addr1,Convert.toWei("1", Convert.Unit.ETHER).toBigInteger()); }
先查詢addr1的余額,有102.9110385個ether:
> web3.formwei(eth.getBalance(eth.accounts[1])) 102.9110385
運行批量轉賬函數,會發現控制臺報錯:
查詢余額,發現只轉成功了一筆:
> web3.formwei(eth.getBalance(eth.accounts[1])) 103.9110385
查看打印的錯誤信息textErr.txt:
可見失敗了9筆交易,只有第一筆交易成功了。失敗的信息是因為有交易重復。初步判斷是nonce設置出問題了。對于單筆轉賬,nonce可以從區塊鏈查詢到,在for循環里,需要自己去遞增nonce。
修改transferEth函數如下:
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了,轉賬成功:
> web3.formwei(eth.getBalance(eth.accounts[1])) 113.9110385
到此,關于“web3j批量轉賬怎么實現”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。