使用web3j來鏈接geth並轉帳,基本轉帳函數能夠這樣寫:java
//以太坊轉帳 //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中。android
批量轉帳就是在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:web
> web3.formwei(eth.getBalance(eth.accounts[1])) 102.9110385
運行批量轉帳函數,會發現控制檯報錯:ide
查詢餘額,發現只轉成功了一筆:函數
> web3.formwei(eth.getBalance(eth.accounts[1])) 103.9110385
查看打印的錯誤信息textErr.txt:區塊鏈
可見失敗了9筆交易,只有第一筆交易成功了。失敗的信息是由於有交易重複。初步判斷是nonce設置出問題了。對於單筆轉帳,nonce能夠從區塊鏈查詢到,在for循環裏,須要本身去遞增nonce。3d
修改transferEth函數以下:code
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信息,能夠看到提交了多筆交易:orm
查詢addr1的帳戶餘額,從103變成113了,轉帳成功:
> web3.formwei(eth.getBalance(eth.accounts[1])) 113.9110385
分享個很受歡迎全網稀缺的互動教程:
- web3j,主要是針對java和android程序員圍繞web3j庫進行區塊鏈以太坊開發的講解。