目录

我的学习分享

记录精彩的程序人生

【conflux】关于批量转账

图片.png

/**
     * 批量转账
     * @param privateKey    私钥
     * @param to        接收地址列表
     * @param value     转账数量列表
     * @param gas       矿工费
     * @throws java.lang.Exception
     */
    @Override
    public void batchTransfer(String privateKey, String[] tos, BigInteger[] values, BigInteger gas, BatchTransferCallback callback) throws Exception {
	
	if(tos.length != values.length) {
	    throw new java.lang.IllegalArgumentException("tos.length must equal with values.length");
	}
	
        CFXBlockChain bc = Lookup.getDefault().lookup(CFXBlockChain.class);
        Cfx cfx = bc.getCfx();
        
        Account account = Account.create(cfx, privateKey);
	BigInteger nonce = account.getNonce();
	
	for(int i=0; i<tos.length; i++) {
	    String to = tos[i];
	    BigInteger value = values[i];
            
            // 过滤掉非法地址
            if(bc.isValidAddress(to)) {
                account.setNonce(nonce);
                String hash = account.transfer(new Option().withGasPrice(gas).withGasLimit(gasLimit()), to, value);
                callback.transferFinished(to, hash, i, tos.length);

                nonce = nonce.add(BigInteger.ONE);
            }
	}
    }