forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OfferDetailsWindow.java
452 lines (400 loc) · 20.4 KB
/
OfferDetailsWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.main.overlays.windows;
import bisq.desktop.Navigation;
import bisq.desktop.components.AutoTooltipButton;
import bisq.desktop.components.BusyAnimation;
import bisq.desktop.components.TitledGroupBg;
import bisq.desktop.components.TxIdTextField;
import bisq.desktop.main.offer.OfferViewUtil;
import bisq.desktop.main.overlays.Overlay;
import bisq.desktop.util.DisplayUtils;
import bisq.desktop.util.GUIUtil;
import bisq.desktop.util.Layout;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.locale.CountryUtil;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.monetary.Price;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.OfferUtil;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.user.User;
import bisq.core.util.FormattingUtils;
import bisq.core.util.VolumeUtil;
import bisq.core.util.coin.CoinFormatter;
import bisq.common.crypto.KeyRing;
import bisq.common.util.Tuple2;
import bisq.common.util.Tuple4;
import org.bitcoinj.core.Coin;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.common.base.Joiner;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.geometry.Insets;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static bisq.desktop.util.FormBuilder.*;
public class OfferDetailsWindow extends Overlay<OfferDetailsWindow> {
protected static final Logger log = LoggerFactory.getLogger(OfferDetailsWindow.class);
private final CoinFormatter formatter;
private final User user;
private final KeyRing keyRing;
private final Navigation navigation;
private final BtcWalletService btcWalletService;
private Offer offer;
private Coin tradeAmount;
private Price tradePrice;
private Optional<Runnable> placeOfferHandlerOptional = Optional.empty();
private Optional<Runnable> takeOfferHandlerOptional = Optional.empty();
private BusyAnimation busyAnimation;
///////////////////////////////////////////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public OfferDetailsWindow(@Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter,
User user,
KeyRing keyRing,
Navigation navigation,
BtcWalletService btcWalletService) {
this.formatter = formatter;
this.user = user;
this.keyRing = keyRing;
this.navigation = navigation;
this.btcWalletService = btcWalletService;
type = Type.Confirmation;
}
public void show(Offer offer, Coin tradeAmount, Price tradePrice) {
this.offer = offer;
this.tradeAmount = tradeAmount;
this.tradePrice = tradePrice;
rowIndex = -1;
width = 1118;
createGridPane();
addContent();
display();
}
public void show(Offer offer) {
this.offer = offer;
rowIndex = -1;
width = 1118;
createGridPane();
addContent();
display();
}
public OfferDetailsWindow onPlaceOffer(Runnable placeOfferHandler) {
this.placeOfferHandlerOptional = Optional.of(placeOfferHandler);
return this;
}
public OfferDetailsWindow onTakeOffer(Runnable takeOfferHandler) {
this.takeOfferHandlerOptional = Optional.of(takeOfferHandler);
return this;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Protected
///////////////////////////////////////////////////////////////////////////////////////////
@Override
protected void onHidden() {
if (busyAnimation != null)
busyAnimation.stop();
}
@Override
protected void createGridPane() {
super.createGridPane();
gridPane.setPadding(new Insets(35, 40, 30, 40));
gridPane.getStyleClass().add("grid-pane");
}
private void addContent() {
gridPane.getColumnConstraints().get(0).setMinWidth(224);
int rows = 5;
List<String> acceptedBanks = offer.getAcceptedBankIds();
boolean showAcceptedBanks = acceptedBanks != null && !acceptedBanks.isEmpty();
List<String> acceptedCountryCodes = offer.getAcceptedCountryCodes();
boolean showAcceptedCountryCodes = acceptedCountryCodes != null && !acceptedCountryCodes.isEmpty();
boolean isF2F = offer.getPaymentMethod().equals(PaymentMethod.F2F);
boolean showExtraInfo = offer.getPaymentMethod().equals(PaymentMethod.F2F) || offer.getPaymentMethod().equals(PaymentMethod.CASH_BY_MAIL);
if (!takeOfferHandlerOptional.isPresent())
rows++;
if (showAcceptedBanks)
rows++;
if (showAcceptedCountryCodes)
rows++;
if (showExtraInfo)
rows++;
if (isF2F)
rows++;
boolean showXmrAutoConf = offer.isXmr() && offer.getDirection() == OfferDirection.SELL;
if (showXmrAutoConf) {
rows++;
}
addTitledGroupBg(gridPane, ++rowIndex, rows, Res.get("shared.Offer"));
String fiatDirectionInfo = "";
String btcDirectionInfo = "";
OfferDirection direction = offer.getDirection();
String currencyCode = offer.getCurrencyCode();
String offerTypeLabel = Res.get("shared.offerType");
String toReceive = " " + Res.get("shared.toReceive");
String toSpend = " " + Res.get("shared.toSpend");
double firstRowDistance = Layout.TWICE_FIRST_ROW_DISTANCE;
if (takeOfferHandlerOptional.isPresent()) {
addConfirmationLabelLabel(gridPane, rowIndex, offerTypeLabel,
DisplayUtils.getDirectionForTakeOffer(direction, currencyCode), firstRowDistance);
fiatDirectionInfo = direction == OfferDirection.BUY ? toReceive : toSpend;
btcDirectionInfo = direction == OfferDirection.SELL ? toReceive : toSpend;
} else if (placeOfferHandlerOptional.isPresent()) {
addConfirmationLabelLabel(gridPane, rowIndex, offerTypeLabel,
DisplayUtils.getOfferDirectionForCreateOffer(direction, currencyCode), firstRowDistance);
fiatDirectionInfo = direction == OfferDirection.SELL ? toReceive : toSpend;
btcDirectionInfo = direction == OfferDirection.BUY ? toReceive : toSpend;
} else {
addConfirmationLabelLabel(gridPane, rowIndex, offerTypeLabel,
DisplayUtils.getDirectionBothSides(direction), firstRowDistance);
}
String btcAmount = Res.get("shared.btcAmount");
if (takeOfferHandlerOptional.isPresent()) {
addConfirmationLabelLabel(gridPane, ++rowIndex, btcAmount + btcDirectionInfo,
formatter.formatCoinWithCode(tradeAmount));
addConfirmationLabelLabel(gridPane,
++rowIndex,
VolumeUtil.formatVolumeLabel(currencyCode) + fiatDirectionInfo,
VolumeUtil.formatVolumeWithCode(offer.getVolumeByAmount(tradeAmount)));
} else {
addConfirmationLabelLabel(gridPane, ++rowIndex, btcAmount + btcDirectionInfo,
formatter.formatCoinWithCode(offer.getAmount()));
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("offerDetailsWindow.minBtcAmount"),
formatter.formatCoinWithCode(offer.getMinAmount()));
String volume = VolumeUtil.formatVolumeWithCode(offer.getVolume());
String minVolume = "";
if (offer.getVolume() != null && offer.getMinVolume() != null &&
!offer.getVolume().equals(offer.getMinVolume()))
minVolume = " " + Res.get("offerDetailsWindow.min",
VolumeUtil.formatVolumeWithCode(offer.getMinVolume()));
addConfirmationLabelLabel(gridPane,
++rowIndex,
VolumeUtil.formatVolumeLabel(currencyCode) + fiatDirectionInfo,
volume + minVolume);
}
String priceLabel = Res.get("shared.price");
if (takeOfferHandlerOptional.isPresent()) {
addConfirmationLabelLabel(gridPane, ++rowIndex, priceLabel, FormattingUtils.formatPrice(tradePrice));
} else {
Price price = offer.getPrice();
if (offer.isUseMarketBasedPrice()) {
addConfirmationLabelLabel(gridPane, ++rowIndex, priceLabel, FormattingUtils.formatPrice(price) +
" " + Res.get("offerDetailsWindow.distance",
FormattingUtils.formatPercentagePrice(offer.getMarketPriceMargin())));
} else {
addConfirmationLabelLabel(gridPane, ++rowIndex, priceLabel, FormattingUtils.formatPrice(price));
}
}
final PaymentMethod paymentMethod = offer.getPaymentMethod();
String bankId = offer.getBankId();
if (bankId == null || bankId.equals("null"))
bankId = "";
else
bankId = " (" + bankId + ")";
final boolean isSpecificBanks = paymentMethod.equals(PaymentMethod.SPECIFIC_BANKS);
final boolean isNationalBanks = paymentMethod.equals(PaymentMethod.NATIONAL_BANK);
final boolean isSepa = paymentMethod.equals(PaymentMethod.SEPA);
final String makerPaymentAccountId = offer.getMakerPaymentAccountId();
final PaymentAccount myPaymentAccount = user.getPaymentAccount(makerPaymentAccountId);
String countryCode = offer.getCountryCode();
if (offer.isMyOffer(keyRing) && makerPaymentAccountId != null && myPaymentAccount != null) {
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("offerDetailsWindow.myTradingAccount"), myPaymentAccount.getAccountName());
} else {
final String method = Res.get(paymentMethod.getId());
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("shared.paymentMethod"), method);
}
if (showXmrAutoConf) {
String isAutoConf = offer.isXmrAutoConf() ?
Res.get("shared.yes") :
Res.get("shared.no");
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("offerbook.xmrAutoConf"), isAutoConf);
}
if (showAcceptedBanks) {
if (paymentMethod.equals(PaymentMethod.SAME_BANK)) {
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("offerDetailsWindow.bankId"), acceptedBanks.get(0));
} else if (isSpecificBanks) {
String value = Joiner.on(", ").join(acceptedBanks);
String acceptedBanksLabel = Res.get("shared.acceptedBanks");
Tooltip tooltip = new Tooltip(acceptedBanksLabel + " " + value);
Label acceptedBanksTextField = addConfirmationLabelLabel(gridPane, ++rowIndex, acceptedBanksLabel, value).second;
acceptedBanksTextField.setMouseTransparent(false);
acceptedBanksTextField.setTooltip(tooltip);
}
}
if (showAcceptedCountryCodes) {
String countries;
Tooltip tooltip = null;
if (CountryUtil.containsAllSepaEuroCountries(acceptedCountryCodes)) {
countries = Res.get("shared.allEuroCountries");
} else {
if (acceptedCountryCodes.size() == 1) {
countries = CountryUtil.getNameAndCode(acceptedCountryCodes.get(0));
tooltip = new Tooltip(countries);
} else {
countries = CountryUtil.getCodesString(acceptedCountryCodes);
tooltip = new Tooltip(CountryUtil.getNamesByCodesString(acceptedCountryCodes));
}
}
Label acceptedCountries = addConfirmationLabelLabel(gridPane, ++rowIndex,
Res.get("shared.acceptedTakerCountries"), countries).second;
if (tooltip != null) {
acceptedCountries.setMouseTransparent(false);
acceptedCountries.setTooltip(tooltip);
}
}
if (isF2F) {
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("payment.f2f.city"), offer.getF2FCity());
}
if (showExtraInfo) {
TextArea textArea = addConfirmationLabelTextArea(gridPane, ++rowIndex, Res.get("payment.shared.extraInfo"), "", 0).second;
textArea.setText(offer.getExtraInfo());
textArea.setMinHeight(33);
textArea.setMaxHeight(textArea.getMinHeight());
textArea.setEditable(false);
}
rows = 3;
if (countryCode != null)
rows++;
if (!isF2F)
rows++;
// At create offer we do not show the makerFeeTxId
if (!placeOfferHandlerOptional.isPresent()) {
rows++;
}
TitledGroupBg titledGroupBg = addTitledGroupBg(gridPane, ++rowIndex, rows, Res.get("shared.details"), Layout.GROUP_DISTANCE);
addConfirmationLabelTextFieldWithCopyIcon(gridPane, rowIndex, Res.get("shared.offerId"), offer.getId(),
Layout.TWICE_FIRST_ROW_AND_GROUP_DISTANCE);
addConfirmationLabelTextFieldWithCopyIcon(gridPane, ++rowIndex, Res.get("offerDetailsWindow.makersOnion"),
offer.getMakerNodeAddress().getFullAddress());
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("offerDetailsWindow.creationDate"),
DisplayUtils.formatDateTime(offer.getDate()));
String value = Res.getWithColAndCap("shared.buyer") +
" " +
formatter.formatCoinWithCode(offer.getBuyerSecurityDeposit()) +
" / " +
Res.getWithColAndCap("shared.seller") +
" " +
formatter.formatCoinWithCode(offer.getSellerSecurityDeposit());
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("shared.securityDeposit"), value);
// At create offer we do not show the makerFeeTxId
if (!placeOfferHandlerOptional.isPresent()) {
TxIdTextField makerFeeTxIdTextField = addLabelTxIdTextField(gridPane, ++rowIndex,
Res.get("shared.makerFeeTxId"), offer.getOfferFeePaymentTxId()).second;
int finalRows = rows;
OfferUtil.getInvalidMakerFeeTxErrorMessage(offer, btcWalletService)
.ifPresent(errorMsg -> {
makerFeeTxIdTextField.getTextField().setId("address-text-field-error");
GridPane.setRowSpan(titledGroupBg, finalRows + 1);
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("shared.errorMessage"),
errorMsg.replace("\n\n", "\n"));
});
}
if (countryCode != null && !isF2F)
addConfirmationLabelLabel(gridPane, ++rowIndex, Res.get("offerDetailsWindow.countryBank"),
CountryUtil.getNameAndCode(countryCode));
if (placeOfferHandlerOptional.isPresent()) {
addTitledGroupBg(gridPane, ++rowIndex, 1, Res.get("offerDetailsWindow.commitment"), Layout.GROUP_DISTANCE);
final Tuple2<Label, Label> labelLabelTuple2 = addConfirmationLabelLabel(gridPane, rowIndex, Res.get("offerDetailsWindow.agree"), Res.get("createOffer.tac"),
Layout.TWICE_FIRST_ROW_AND_GROUP_DISTANCE);
labelLabelTuple2.second.setWrapText(true);
addConfirmAndCancelButtons(true);
} else if (takeOfferHandlerOptional.isPresent()) {
addTitledGroupBg(gridPane, ++rowIndex, 1, Res.get("shared.contract"), Layout.GROUP_DISTANCE);
final Tuple2<Label, Label> labelLabelTuple2 = addConfirmationLabelLabel(gridPane, rowIndex, Res.get("offerDetailsWindow.tac"), Res.get("takeOffer.tac"),
Layout.TWICE_FIRST_ROW_AND_GROUP_DISTANCE);
labelLabelTuple2.second.setWrapText(true);
addConfirmAndCancelButtons(false);
} else {
addCloseButton(gridPane, ++rowIndex, () -> {
closeHandlerOptional.ifPresent(Runnable::run);
hide();
});
}
}
private void addConfirmAndCancelButtons(boolean isPlaceOffer) {
boolean isBuyOffer = OfferViewUtil.isShownAsBuyOffer(offer);
boolean isBuyerRole = isPlaceOffer == isBuyOffer;
String tradeCurrencyByCode = offer.getCurrencyCode();
String placeOfferButtonText = isBuyerRole ?
offer.isFiatOffer() ?
Res.get("offerDetailsWindow.confirm.maker", Res.get("shared.buy")) :
Res.get("offerDetailsWindow.confirm.makerAltcoin", Res.get("shared.buy"),
tradeCurrencyByCode) :
offer.isFiatOffer() ?
Res.get("offerDetailsWindow.confirm.maker", Res.get("shared.sell")) :
Res.get("offerDetailsWindow.confirm.makerAltcoin", Res.get("shared.sell"),
tradeCurrencyByCode);
String takeOfferButtonText = isBuyerRole ?
offer.isFiatOffer() ?
Res.get("offerDetailsWindow.confirm.taker", Res.get("shared.buy")) :
Res.get("offerDetailsWindow.confirm.takerAltcoin", Res.get("shared.buy"),
tradeCurrencyByCode) :
offer.isFiatOffer() ?
Res.get("offerDetailsWindow.confirm.taker", Res.get("shared.sell")) :
Res.get("offerDetailsWindow.confirm.takerAltcoin", Res.get("shared.sell"),
tradeCurrencyByCode);
ImageView iconView = new ImageView();
iconView.setId(isBuyerRole ? "image-buy-white" : "image-sell-white");
Tuple4<Button, BusyAnimation, Label, HBox> placeOfferTuple = addButtonBusyAnimationLabelAfterGroup(gridPane,
++rowIndex, 1,
isPlaceOffer ? placeOfferButtonText : takeOfferButtonText);
AutoTooltipButton button = (AutoTooltipButton) placeOfferTuple.first;
button.setMinHeight(40);
button.setPadding(new Insets(0, 20, 0, 20));
button.setGraphic(iconView);
button.setGraphicTextGap(10);
button.setId(isBuyerRole ? "buy-button-big" : "sell-button-big");
button.updateText(isPlaceOffer ? placeOfferButtonText : takeOfferButtonText);
busyAnimation = placeOfferTuple.second;
Label spinnerInfoLabel = placeOfferTuple.third;
Button cancelButton = new AutoTooltipButton(Res.get("shared.cancel"));
cancelButton.setDefaultButton(false);
cancelButton.setOnAction(e -> {
closeHandlerOptional.ifPresent(Runnable::run);
hide();
});
placeOfferTuple.fourth.getChildren().add(cancelButton);
button.setOnAction(e -> {
if (GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation,
CurrencyUtil.getTradeCurrency(offer.getCurrencyCode()).get())) {
button.setDisable(true);
cancelButton.setDisable(true);
// temporarily disabled due to high CPU usage (per issue #4649)
// busyAnimation.play();
if (isPlaceOffer) {
spinnerInfoLabel.setText(Res.get("createOffer.fundsBox.placeOfferSpinnerInfo"));
placeOfferHandlerOptional.ifPresent(Runnable::run);
} else {
spinnerInfoLabel.setText(Res.get("takeOffer.fundsBox.takeOfferSpinnerInfo"));
takeOfferHandlerOptional.ifPresent(Runnable::run);
}
}
});
}
}