use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.
the class BtcWalletService method getSendRequest.
private SendRequest getSendRequest(String fromAddress, String toAddress, Coin amount, Coin fee, @Nullable KeyParameter aesKey, AddressEntry.Context context) throws AddressFormatException, AddressEntryException {
Transaction tx = new Transaction(params);
Preconditions.checkArgument(Restrictions.isAboveDust(amount, fee), "The amount is too low (dust limit).");
tx.addOutput(amount.subtract(fee), Address.fromBase58(params, toAddress));
SendRequest sendRequest = SendRequest.forTx(tx);
sendRequest.fee = fee;
sendRequest.feePerKb = Coin.ZERO;
sendRequest.ensureMinRequiredFee = false;
sendRequest.aesKey = aesKey;
sendRequest.shuffleOutputs = false;
Optional<AddressEntry> addressEntry = findAddressEntry(fromAddress, context);
if (!addressEntry.isPresent())
throw new AddressEntryException("WithdrawFromAddress is not found in our wallet.");
checkNotNull(addressEntry.get(), "addressEntry.get() must not be null");
checkNotNull(addressEntry.get().getAddress(), "addressEntry.get().getAddress() must not be null");
sendRequest.coinSelector = new BtcCoinSelector(addressEntry.get().getAddress());
sendRequest.changeAddress = addressEntry.get().getAddress();
return sendRequest;
}
use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.
the class BtcWalletService method getSendRequestForMultipleAddresses.
private SendRequest getSendRequestForMultipleAddresses(Set<String> fromAddresses, String toAddress, Coin amount, Coin fee, @Nullable String changeAddress, @Nullable KeyParameter aesKey) throws AddressFormatException, AddressEntryException, InsufficientMoneyException {
Transaction tx = new Transaction(params);
checkArgument(Restrictions.isAboveDust(amount), "The amount is too low (dust limit).");
final Coin netValue = amount.subtract(fee);
if (netValue.isNegative())
throw new InsufficientMoneyException(netValue.multiply(-1), "The mining fee for that transaction exceed the available amount.");
tx.addOutput(netValue, Address.fromBase58(params, toAddress));
SendRequest sendRequest = SendRequest.forTx(tx);
sendRequest.fee = fee;
sendRequest.feePerKb = Coin.ZERO;
sendRequest.ensureMinRequiredFee = false;
sendRequest.aesKey = aesKey;
sendRequest.shuffleOutputs = false;
Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
return addressEntryOptional;
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
if (addressEntries.isEmpty())
throw new AddressEntryException("No Addresses for withdraw found in our wallet");
sendRequest.coinSelector = new BtcCoinSelector(walletsSetup.getAddressesFromAddressEntries(addressEntries));
Optional<AddressEntry> addressEntryOptional = Optional.<AddressEntry>empty();
AddressEntry changeAddressAddressEntry = null;
if (changeAddress != null)
addressEntryOptional = findAddressEntry(changeAddress, AddressEntry.Context.AVAILABLE);
if (addressEntryOptional.isPresent()) {
changeAddressAddressEntry = addressEntryOptional.get();
} else {
ArrayList<AddressEntry> list = new ArrayList<>(addressEntries);
if (!list.isEmpty())
changeAddressAddressEntry = list.get(0);
}
checkNotNull(changeAddressAddressEntry, "change address must not be null");
sendRequest.changeAddress = changeAddressAddressEntry.getAddress();
return sendRequest;
}
use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.
the class BtcWalletService method getFeeEstimationTransactionForMultipleAddresses.
public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> fromAddresses, Coin amount) throws AddressFormatException, AddressEntryException, InsufficientFundsException {
Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
return addressEntryOptional;
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
if (addressEntries.isEmpty())
throw new AddressEntryException("No Addresses for withdraw found in our wallet");
try {
Coin fee;
int counter = 0;
int txSize = 0;
Transaction tx;
Coin txFeeForWithdrawalPerByte = getTxFeeForWithdrawalPerByte();
do {
counter++;
fee = txFeeForWithdrawalPerByte.multiply(txSize);
// We use a dummy address for the output
SendRequest sendRequest = getSendRequestForMultipleAddresses(fromAddresses, getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddressString(), amount, fee, null, aesKey);
wallet.completeTx(sendRequest);
tx = sendRequest.tx;
txSize = tx.bitcoinSerialize().length;
printTx("FeeEstimationTransactionForMultipleAddresses", tx);
} while (feeEstimationNotSatisfied(counter, tx));
if (counter == 10)
log.error("Could not calculate the fee. Tx=" + tx);
return tx;
} catch (InsufficientMoneyException e) {
throw new InsufficientFundsException("The fees for that transaction exceed the available funds " + "or the resulting output value is below the min. dust value:\n" + "Missing " + (e.missing != null ? e.missing.toFriendlyString() : "null"));
}
}
use of bisq.core.btc.AddressEntryException in project bisq-desktop by bisq-network.
the class BuyerStep4View method reviewWithdrawal.
@SuppressWarnings("PointlessBooleanExpression")
private void reviewWithdrawal() {
Coin amount = trade.getPayoutAmount();
BtcWalletService walletService = model.dataModel.btcWalletService;
AddressEntry fromAddressesEntry = walletService.getOrCreateAddressEntry(trade.getId(), AddressEntry.Context.TRADE_PAYOUT);
String fromAddresses = fromAddressesEntry.getAddressString();
String toAddresses = withdrawAddressTextField.getText();
if (new BtcAddressValidator().validate(toAddresses).isValid) {
Coin balance = walletService.getBalanceForAddress(fromAddressesEntry.getAddress());
try {
Transaction feeEstimationTransaction = walletService.getFeeEstimationTransaction(fromAddresses, toAddresses, amount, AddressEntry.Context.TRADE_PAYOUT);
Coin fee = feeEstimationTransaction.getFee();
// noinspection UnusedAssignment
Coin receiverAmount = amount.subtract(fee);
if (balance.isZero()) {
new Popup<>().warning(Res.get("portfolio.pending.step5_buyer.alreadyWithdrawn")).show();
model.dataModel.tradeManager.addTradeToClosedTrades(trade);
} else {
if (toAddresses.isEmpty()) {
validateWithdrawAddress();
} else if (Restrictions.isAboveDust(amount, fee)) {
BSFormatter formatter = model.btcFormatter;
int txSize = feeEstimationTransaction.bitcoinSerialize().length;
double feePerByte = CoinUtil.getFeePerByte(fee, txSize);
double kb = txSize / 1000d;
String recAmount = formatter.formatCoinWithCode(receiverAmount);
new Popup<>().headLine(Res.get("portfolio.pending.step5_buyer.confirmWithdrawal")).confirmation(Res.get("shared.sendFundsDetailsWithFee", formatter.formatCoinWithCode(amount), fromAddresses, toAddresses, formatter.formatCoinWithCode(fee), feePerByte, kb, recAmount)).actionButtonText(Res.get("shared.yes")).onAction(() -> doWithdrawal(amount, fee)).closeButtonText(Res.get("shared.cancel")).onClose(() -> {
useSavingsWalletButton.setDisable(false);
withdrawToExternalWalletButton.setDisable(false);
}).show();
} else {
new Popup<>().warning(Res.get("portfolio.pending.step5_buyer.amountTooLow")).show();
}
}
} catch (AddressFormatException e) {
validateWithdrawAddress();
} catch (AddressEntryException e) {
log.error(e.getMessage());
} catch (InsufficientFundsException e) {
log.error(e.getMessage());
e.printStackTrace();
new Popup<>().warning(e.getMessage()).show();
}
} else {
new Popup<>().warning(Res.get("validation.btc.invalidAddress")).show();
}
}
use of bisq.core.btc.AddressEntryException in project bisq-core by bisq-network.
the class BtcWalletService method getFeeEstimationTransaction.
// /////////////////////////////////////////////////////////////////////////////////////////
// Withdrawal Fee calculation
// /////////////////////////////////////////////////////////////////////////////////////////
public Transaction getFeeEstimationTransaction(String fromAddress, String toAddress, Coin amount, AddressEntry.Context context) throws AddressFormatException, AddressEntryException, InsufficientFundsException {
Optional<AddressEntry> addressEntry = findAddressEntry(fromAddress, context);
if (!addressEntry.isPresent())
throw new AddressEntryException("WithdrawFromAddress is not found in our wallet.");
checkNotNull(addressEntry.get().getAddress(), "addressEntry.get().getAddress() must nto be null");
try {
Coin fee;
int counter = 0;
int txSize = 0;
Transaction tx;
Coin txFeeForWithdrawalPerByte = getTxFeeForWithdrawalPerByte();
do {
counter++;
fee = txFeeForWithdrawalPerByte.multiply(txSize);
SendRequest sendRequest = getSendRequest(fromAddress, toAddress, amount, fee, aesKey, context);
wallet.completeTx(sendRequest);
tx = sendRequest.tx;
txSize = tx.bitcoinSerialize().length;
printTx("FeeEstimationTransaction", tx);
} while (feeEstimationNotSatisfied(counter, tx));
if (counter == 10)
log.error("Could not calculate the fee. Tx=" + tx);
return tx;
} catch (InsufficientMoneyException e) {
throw new InsufficientFundsException("The fees for that transaction exceed the available funds " + "or the resulting output value is below the min. dust value:\n" + "Missing " + (e.missing != null ? e.missing.toFriendlyString() : "null"));
}
}
Aggregations