Add isReadyForTxBroadcast check for WithdrawalView

This commit is contained in:
Manfred Karrer 2018-01-20 09:04:36 -05:00
parent a805d5fd0e
commit 725bf14b22
No known key found for this signature in database
GPG key ID: 401250966A6B2C46

View file

@ -27,6 +27,7 @@ import io.bisq.core.btc.InsufficientFundsException;
import io.bisq.core.btc.Restrictions; import io.bisq.core.btc.Restrictions;
import io.bisq.core.btc.listeners.BalanceListener; import io.bisq.core.btc.listeners.BalanceListener;
import io.bisq.core.btc.wallet.BtcWalletService; import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.btc.wallet.WalletsSetup;
import io.bisq.core.trade.Tradable; import io.bisq.core.trade.Tradable;
import io.bisq.core.trade.Trade; import io.bisq.core.trade.Trade;
import io.bisq.core.trade.TradeManager; import io.bisq.core.trade.TradeManager;
@ -42,6 +43,7 @@ import io.bisq.gui.main.overlays.windows.WalletPasswordWindow;
import io.bisq.gui.util.BSFormatter; import io.bisq.gui.util.BSFormatter;
import io.bisq.gui.util.GUIUtil; import io.bisq.gui.util.GUIUtil;
import io.bisq.gui.util.validation.BtcAddressValidator; import io.bisq.gui.util.validation.BtcAddressValidator;
import io.bisq.network.p2p.P2PService;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
@ -89,6 +91,8 @@ public class WithdrawalView extends ActivatableView<VBox, Void> {
private final TradeManager tradeManager; private final TradeManager tradeManager;
private final ClosedTradableManager closedTradableManager; private final ClosedTradableManager closedTradableManager;
private final FailedTradesManager failedTradesManager; private final FailedTradesManager failedTradesManager;
private final P2PService p2PService;
private final WalletsSetup walletsSetup;
private final BSFormatter formatter; private final BSFormatter formatter;
private final Preferences preferences; private final Preferences preferences;
private final BtcAddressValidator btcAddressValidator; private final BtcAddressValidator btcAddressValidator;
@ -118,13 +122,18 @@ public class WithdrawalView extends ActivatableView<VBox, Void> {
TradeManager tradeManager, TradeManager tradeManager,
ClosedTradableManager closedTradableManager, ClosedTradableManager closedTradableManager,
FailedTradesManager failedTradesManager, FailedTradesManager failedTradesManager,
BSFormatter formatter, Preferences preferences, P2PService p2PService,
WalletsSetup walletsSetup,
BSFormatter formatter,
Preferences preferences,
BtcAddressValidator btcAddressValidator, BtcAddressValidator btcAddressValidator,
WalletPasswordWindow walletPasswordWindow) { WalletPasswordWindow walletPasswordWindow) {
this.walletService = walletService; this.walletService = walletService;
this.tradeManager = tradeManager; this.tradeManager = tradeManager;
this.closedTradableManager = closedTradableManager; this.closedTradableManager = closedTradableManager;
this.failedTradesManager = failedTradesManager; this.failedTradesManager = failedTradesManager;
this.p2PService = p2PService;
this.walletsSetup = walletsSetup;
this.formatter = formatter; this.formatter = formatter;
this.preferences = preferences; this.preferences = preferences;
this.btcAddressValidator = btcAddressValidator; this.btcAddressValidator = btcAddressValidator;
@ -252,72 +261,76 @@ public class WithdrawalView extends ActivatableView<VBox, Void> {
@FXML @FXML
public void onWithdraw() { public void onWithdraw() {
try { if (GUIUtil.isReadyForTxBroadcast(p2PService, walletsSetup)) {
// We do not know sendersAmount if senderPaysFee is true. We repeat fee calculation after first attempt if senderPaysFee is true. try {
Transaction feeEstimationTransaction = walletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, amountAsCoin); // We do not know sendersAmount if senderPaysFee is true. We repeat fee calculation after first attempt if senderPaysFee is true.
if (feeExcluded && feeEstimationTransaction != null) { Transaction feeEstimationTransaction = walletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, amountAsCoin);
sendersAmount = amountAsCoin.add(feeEstimationTransaction.getFee()); if (feeExcluded && feeEstimationTransaction != null) {
feeEstimationTransaction = walletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, sendersAmount); sendersAmount = amountAsCoin.add(feeEstimationTransaction.getFee());
} feeEstimationTransaction = walletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, sendersAmount);
checkNotNull(feeEstimationTransaction, "feeEstimationTransaction must not be null"); }
Coin fee = feeEstimationTransaction.getFee(); checkNotNull(feeEstimationTransaction, "feeEstimationTransaction must not be null");
sendersAmount = feeExcluded ? amountAsCoin.add(fee) : amountAsCoin; Coin fee = feeEstimationTransaction.getFee();
Coin receiverAmount = feeExcluded ? amountAsCoin : amountAsCoin.subtract(fee); sendersAmount = feeExcluded ? amountAsCoin.add(fee) : amountAsCoin;
if (areInputsValid()) { Coin receiverAmount = feeExcluded ? amountAsCoin : amountAsCoin.subtract(fee);
int txSize = feeEstimationTransaction.bitcoinSerialize().length; if (areInputsValid()) {
log.info("Fee for tx with size {}: {} " + Res.getBaseCurrencyCode() + "", txSize, fee.toPlainString()); int txSize = feeEstimationTransaction.bitcoinSerialize().length;
log.info("Fee for tx with size {}: {} " + Res.getBaseCurrencyCode() + "", txSize, fee.toPlainString());
if (receiverAmount.isPositive()) { if (receiverAmount.isPositive()) {
double feePerByte = CoinUtil.getFeePerByte(fee, txSize); double feePerByte = CoinUtil.getFeePerByte(fee, txSize);
double kb = txSize / 1000d; double kb = txSize / 1000d;
new Popup<>().headLine(Res.get("funds.withdrawal.confirmWithdrawalRequest")) new Popup<>().headLine(Res.get("funds.withdrawal.confirmWithdrawalRequest"))
.confirmation(Res.get("shared.sendFundsDetailsWithFee", .confirmation(Res.get("shared.sendFundsDetailsWithFee",
formatter.formatCoinWithCode(sendersAmount), formatter.formatCoinWithCode(sendersAmount),
withdrawFromTextField.getText(), withdrawFromTextField.getText(),
withdrawToTextField.getText(), withdrawToTextField.getText(),
formatter.formatCoinWithCode(fee), formatter.formatCoinWithCode(fee),
feePerByte, feePerByte,
kb, kb,
formatter.formatCoinWithCode(receiverAmount))) formatter.formatCoinWithCode(receiverAmount)))
.actionButtonText(Res.get("shared.yes")) .actionButtonText(Res.get("shared.yes"))
.onAction(() -> doWithdraw(sendersAmount, fee, new FutureCallback<Transaction>() { .onAction(() -> doWithdraw(sendersAmount, fee, new FutureCallback<Transaction>() {
@Override @Override
public void onSuccess(@javax.annotation.Nullable Transaction transaction) { public void onSuccess(@javax.annotation.Nullable Transaction transaction) {
if (transaction != null) { if (transaction != null) {
log.debug("onWithdraw onSuccess tx ID:" + transaction.getHashAsString()); log.debug("onWithdraw onSuccess tx ID:" + transaction.getHashAsString());
} else { } else {
log.error("onWithdraw transaction is null"); log.error("onWithdraw transaction is null");
}
List<Trade> trades = new ArrayList<>(tradeManager.getTradableList());
trades.stream()
.filter(Trade::isPayoutPublished)
.forEach(trade -> {
walletService.getAddressEntry(trade.getId(), AddressEntry.Context.TRADE_PAYOUT)
.ifPresent(addressEntry -> {
if (walletService.getBalanceForAddress(addressEntry.getAddress()).isZero())
tradeManager.addTradeToClosedTrades(trade);
});
});
} }
List<Trade> trades = new ArrayList<>(tradeManager.getTradableList()); @Override
trades.stream() public void onFailure(@NotNull Throwable t) {
.filter(Trade::isPayoutPublished) log.error("onWithdraw onFailure");
.forEach(trade -> { }
walletService.getAddressEntry(trade.getId(), AddressEntry.Context.TRADE_PAYOUT) }))
.ifPresent(addressEntry -> { .closeButtonText(Res.get("shared.cancel"))
if (walletService.getBalanceForAddress(addressEntry.getAddress()).isZero()) .show();
tradeManager.addTradeToClosedTrades(trade); } else {
}); new Popup<>().warning(Res.get("portfolio.pending.step5_buyer.amountTooLow")).show();
}); }
}
@Override
public void onFailure(@NotNull Throwable t) {
log.error("onWithdraw onFailure");
}
}))
.closeButtonText(Res.get("shared.cancel"))
.show();
} else {
new Popup<>().warning(Res.get("portfolio.pending.step5_buyer.amountTooLow")).show();
} }
} catch (InsufficientFundsException e) {
new Popup<>().warning(Res.get("funds.withdrawal.warn.amountExceeds") + "\n\nError message:\n" + e.getMessage()).show();
} catch (Throwable e) {
e.printStackTrace();
log.error(e.toString());
new Popup<>().warning(e.toString()).show();
} }
} catch (InsufficientFundsException e) { } else {
new Popup<>().warning(Res.get("funds.withdrawal.warn.amountExceeds")+ "\n\nError message:\n" + e.getMessage()).show(); GUIUtil.showNotReadyForTxBroadcastPopups(p2PService, walletsSetup);
} catch (Throwable e) {
e.printStackTrace();
log.error(e.toString());
new Popup<>().warning(e.toString()).show();
} }
} }