web3j批量转帐

使用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库进行区块链以太坊开发的讲解。
相关文章
相关标签/搜索