use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.
the class BridgeSupport method adjustBalancesIfChangeOutputWasDust.
/**
* If federation change output value had to be increased to be non-dust, the federation now has
* more BTC than it should. So, we burn some sBTC to make balances match.
*
* @param btcTx The btc tx that was just completed
* @param sentByUser The number of sBTC originaly sent by the user
*/
private void adjustBalancesIfChangeOutputWasDust(BtcTransaction btcTx, Coin sentByUser) {
if (btcTx.getOutputs().size() <= 1) {
// If there is no change, do-nothing
return;
}
Coin sumInputs = Coin.ZERO;
for (TransactionInput transactionInput : btcTx.getInputs()) {
sumInputs = sumInputs.add(transactionInput.getValue());
}
Coin change = btcTx.getOutput(1).getValue();
Coin spentByFederation = sumInputs.subtract(change);
if (spentByFederation.isLessThan(sentByUser)) {
Coin coinsToBurn = sentByUser.subtract(spentByFederation);
this.transferTo(BURN_ADDRESS, co.rsk.core.Coin.fromBitcoin(coinsToBurn));
}
}
use of co.rsk.bitcoinj.core.TransactionInput 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