溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2021-12-29 14:03:06 來(lái)源:億速云 閱讀:191 作者:iii 欄目:互聯(lián)網(wǎng)科技

這篇文章主要介紹“web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)”,在日常操作中,相信很多人在web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

使用web3j來(lái)連接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:

> web3.formwei(eth.getBalance(eth.accounts[1]))
102.9110385

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

web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)

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

> web3.formwei(eth.getBalance(eth.accounts[1]))
103.9110385

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

web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)

可見失敗了9筆交易,只有第一筆交易成功了。失敗的信息是因?yàn)橛薪灰字貜?fù)。初步判斷是nonce設(shè)置出問題了。對(duì)于單筆轉(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信息,可以看到提交了多筆交易:

web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)

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

> web3.formwei(eth.getBalance(eth.accounts[1]))
113.9110385

到此,關(guān)于“web3j批量轉(zhuǎn)賬怎么實(shí)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI