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:
運行批量轉賬函數,會發現控制臺報錯:
查詢余額,發現只轉成功了一筆:
查看打印的錯誤信息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了,轉賬成功:
總結
 
                            
                        - 上一篇: Web3j监听功能代码研究
- 下一篇: python刷题软件_教你用python
