web3j批量转账
使用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监听功能代码研究
- 下一篇: 《码农翻身》读后感---程序员的潜规则