use of co.rsk.bitcoinj.core.InsufficientMoneyException in project rskj by rsksmart.
the class BridgeSupport method createMigrationTransaction.
private Pair<BtcTransaction, List<UTXO>> createMigrationTransaction(Wallet originWallet, Address destinationAddress) {
Coin expectedMigrationValue = originWallet.getBalance();
logger.debug("[createMigrationTransaction] Balance to migrate: {}", expectedMigrationValue);
for (; ; ) {
BtcTransaction migrationBtcTx = new BtcTransaction(originWallet.getParams());
migrationBtcTx.addOutput(expectedMigrationValue, destinationAddress);
SendRequest sr = SendRequest.forTx(migrationBtcTx);
sr.changeAddress = destinationAddress;
sr.feePerKb = getFeePerKb();
sr.missingSigsMode = Wallet.MissingSigsMode.USE_OP_ZERO;
sr.recipientsPayFees = true;
try {
originWallet.completeTx(sr);
for (TransactionInput transactionInput : migrationBtcTx.getInputs()) {
transactionInput.disconnect();
}
List<UTXO> selectedUTXOs = originWallet.getUTXOProvider().getOpenTransactionOutputs(originWallet.getWatchedAddresses()).stream().filter(utxo -> migrationBtcTx.getInputs().stream().anyMatch(input -> input.getOutpoint().getHash().equals(utxo.getHash()) && input.getOutpoint().getIndex() == utxo.getIndex())).collect(Collectors.toList());
return Pair.of(migrationBtcTx, selectedUTXOs);
} catch (InsufficientMoneyException | Wallet.ExceededMaxTransactionSize | Wallet.CouldNotAdjustDownwards e) {
expectedMigrationValue = expectedMigrationValue.divide(2);
} catch (Wallet.DustySendRequested e) {
throw new IllegalStateException("Retiring federation wallet cannot be emptied", e);
} catch (UTXOProviderException e) {
throw new RuntimeException("Unexpected UTXO provider error", e);
}
}
}
Aggregations