forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfferBookViewModel.java
673 lines (555 loc) · 27.8 KB
/
OfferBookViewModel.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* 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.offer.offerbook;
import bisq.desktop.Navigation;
import bisq.desktop.common.model.ActivatableViewModel;
import bisq.desktop.main.MainView;
import bisq.desktop.main.PriceUtil;
import bisq.desktop.main.settings.SettingsView;
import bisq.desktop.main.settings.preferences.PreferencesView;
import bisq.desktop.util.DisplayUtils;
import bisq.desktop.util.GUIUtil;
import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.btc.setup.WalletsSetup;
import bisq.core.locale.BankUtil;
import bisq.core.locale.CountryUtil;
import bisq.core.locale.CryptoCurrency;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.GlobalSettings;
import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.monetary.Price;
import bisq.core.monetary.Volume;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.OfferFilterService;
import bisq.core.offer.OpenOfferManager;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.PaymentAccountUtil;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.bisq_v1.ClosedTradableManager;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.FormattingUtils;
import bisq.core.util.VolumeUtil;
import bisq.core.util.coin.BsqFormatter;
import bisq.core.util.coin.CoinFormatter;
import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.P2PService;
import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.handlers.ResultHandler;
import org.bitcoinj.core.Coin;
import com.google.inject.Inject;
import javax.inject.Named;
import com.google.common.base.Joiner;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
@Slf4j
class OfferBookViewModel extends ActivatableViewModel {
private final OpenOfferManager openOfferManager;
private final User user;
private final OfferBook offerBook;
final Preferences preferences;
private final WalletsSetup walletsSetup;
private final P2PService p2PService;
final PriceFeedService priceFeedService;
private final ClosedTradableManager closedTradableManager;
final AccountAgeWitnessService accountAgeWitnessService;
private final Navigation navigation;
private final PriceUtil priceUtil;
final OfferFilterService offerFilterService;
private final CoinFormatter btcFormatter;
private final BsqFormatter bsqFormatter;
private final FilteredList<OfferBookListItem> filteredItems;
private final SortedList<OfferBookListItem> sortedItems;
private final ListChangeListener<TradeCurrency> tradeCurrencyListChangeListener;
private final ListChangeListener<OfferBookListItem> filterItemsListener;
private TradeCurrency selectedTradeCurrency;
private final ObservableList<TradeCurrency> allTradeCurrencies = FXCollections.observableArrayList();
private OfferDirection direction;
final StringProperty tradeCurrencyCode = new SimpleStringProperty();
// If id is empty string we ignore filter (display all methods)
PaymentMethod selectedPaymentMethod = getShowAllEntryForPaymentMethod();
private boolean isTabSelected;
final BooleanProperty showAllTradeCurrenciesProperty = new SimpleBooleanProperty(true);
final BooleanProperty disableMatchToggle = new SimpleBooleanProperty();
final IntegerProperty maxPlacesForAmount = new SimpleIntegerProperty();
final IntegerProperty maxPlacesForVolume = new SimpleIntegerProperty();
final IntegerProperty maxPlacesForPrice = new SimpleIntegerProperty();
final IntegerProperty maxPlacesForMarketPriceMargin = new SimpleIntegerProperty();
boolean showAllPaymentMethods = true;
boolean useOffersMatchingMyAccountsFilter;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, lifecycle
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public OfferBookViewModel(User user,
OpenOfferManager openOfferManager,
OfferBook offerBook,
Preferences preferences,
WalletsSetup walletsSetup,
P2PService p2PService,
PriceFeedService priceFeedService,
ClosedTradableManager closedTradableManager,
AccountAgeWitnessService accountAgeWitnessService,
Navigation navigation,
PriceUtil priceUtil,
OfferFilterService offerFilterService,
@Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter,
BsqFormatter bsqFormatter) {
super();
this.openOfferManager = openOfferManager;
this.user = user;
this.offerBook = offerBook;
this.preferences = preferences;
this.walletsSetup = walletsSetup;
this.p2PService = p2PService;
this.priceFeedService = priceFeedService;
this.closedTradableManager = closedTradableManager;
this.accountAgeWitnessService = accountAgeWitnessService;
this.navigation = navigation;
this.priceUtil = priceUtil;
this.offerFilterService = offerFilterService;
this.btcFormatter = btcFormatter;
this.bsqFormatter = bsqFormatter;
this.filteredItems = new FilteredList<>(offerBook.getOfferBookListItems());
this.sortedItems = new SortedList<>(filteredItems);
tradeCurrencyListChangeListener = c -> fillAllTradeCurrencies();
filterItemsListener = c -> {
final Optional<OfferBookListItem> highestAmountOffer = filteredItems.stream()
.max(Comparator.comparingLong(o -> o.getOffer().getAmount().getValue()));
final boolean containsRangeAmount = filteredItems.stream().anyMatch(o -> o.getOffer().isRange());
if (highestAmountOffer.isPresent()) {
final OfferBookListItem item = highestAmountOffer.get();
if (!item.getOffer().isRange() && containsRangeAmount) {
maxPlacesForAmount.set(formatAmount(item.getOffer(), false)
.length() * 2 + FormattingUtils.RANGE_SEPARATOR.length());
maxPlacesForVolume.set(formatVolume(item.getOffer(), false)
.length() * 2 + FormattingUtils.RANGE_SEPARATOR.length());
} else {
maxPlacesForAmount.set(formatAmount(item.getOffer(), false).length());
maxPlacesForVolume.set(formatVolume(item.getOffer(), false).length());
}
}
final Optional<OfferBookListItem> highestPriceOffer = filteredItems.stream()
.filter(o -> o.getOffer().getPrice() != null)
.max(Comparator.comparingLong(o -> o.getOffer().getPrice() != null ? o.getOffer().getPrice().getValue() : 0));
highestPriceOffer.ifPresent(offerBookListItem -> maxPlacesForPrice.set(formatPrice(offerBookListItem.getOffer(), false).length()));
final Optional<OfferBookListItem> highestMarketPriceMarginOffer = filteredItems.stream()
.filter(o -> o.getOffer().isUseMarketBasedPrice())
.max(Comparator.comparing(o -> new DecimalFormat("#0.00").format(o.getOffer().getMarketPriceMargin() * 100).length()));
highestMarketPriceMarginOffer.ifPresent(offerBookListItem -> maxPlacesForMarketPriceMargin.set(formatMarketPriceMargin(offerBookListItem.getOffer(), false).length()));
};
}
@Override
protected void activate() {
filteredItems.addListener(filterItemsListener);
String code = direction == OfferDirection.BUY ? preferences.getBuyScreenCurrencyCode() : preferences.getSellScreenCurrencyCode();
if (code != null && !code.isEmpty() && !isShowAllEntry(code) &&
CurrencyUtil.getTradeCurrency(code).isPresent()) {
showAllTradeCurrenciesProperty.set(false);
selectedTradeCurrency = CurrencyUtil.getTradeCurrency(code).get();
} else {
showAllTradeCurrenciesProperty.set(true);
selectedTradeCurrency = GlobalSettings.getDefaultTradeCurrency();
}
tradeCurrencyCode.set(selectedTradeCurrency.getCode());
if (user != null) {
disableMatchToggle.set(user.getPaymentAccounts() == null || user.getPaymentAccounts().isEmpty());
}
useOffersMatchingMyAccountsFilter = !disableMatchToggle.get() && isShowOffersMatchingMyAccounts();
fillAllTradeCurrencies();
preferences.getTradeCurrenciesAsObservable().addListener(tradeCurrencyListChangeListener);
offerBook.fillOfferBookListItems();
filterOffers();
setMarketPriceFeedCurrency();
priceUtil.recalculateBsq30DayAveragePrice();
}
@Override
protected void deactivate() {
filteredItems.removeListener(filterItemsListener);
preferences.getTradeCurrenciesAsObservable().removeListener(tradeCurrencyListChangeListener);
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
void initWithDirection(OfferDirection direction) {
this.direction = direction;
}
void onTabSelected(boolean isSelected) {
this.isTabSelected = isSelected;
setMarketPriceFeedCurrency();
}
///////////////////////////////////////////////////////////////////////////////////////////
// UI actions
///////////////////////////////////////////////////////////////////////////////////////////
void onSetTradeCurrency(TradeCurrency tradeCurrency) {
if (tradeCurrency != null) {
String code = tradeCurrency.getCode();
boolean showAllEntry = isShowAllEntry(code);
showAllTradeCurrenciesProperty.set(showAllEntry);
if (isEditEntry(code))
navigation.navigateTo(MainView.class, SettingsView.class, PreferencesView.class);
else if (!showAllEntry) {
this.selectedTradeCurrency = tradeCurrency;
tradeCurrencyCode.set(code);
}
setMarketPriceFeedCurrency();
filterOffers();
if (direction == OfferDirection.BUY)
preferences.setBuyScreenCurrencyCode(code);
else
preferences.setSellScreenCurrencyCode(code);
}
}
void onSetPaymentMethod(PaymentMethod paymentMethod) {
if (paymentMethod == null)
return;
showAllPaymentMethods = isShowAllEntry(paymentMethod.getId());
if (!showAllPaymentMethods) {
this.selectedPaymentMethod = paymentMethod;
// If we select TransferWise we switch to show all currencies as TransferWise supports
// sending to most currencies.
if (paymentMethod.getId().equals(PaymentMethod.TRANSFERWISE_ID)) {
onSetTradeCurrency(getShowAllEntryForCurrency());
}
} else {
this.selectedPaymentMethod = getShowAllEntryForPaymentMethod();
}
filterOffers();
}
void onRemoveOpenOffer(Offer offer, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
openOfferManager.removeOffer(offer, resultHandler, errorMessageHandler);
}
void onShowOffersMatchingMyAccounts(boolean isSelected) {
useOffersMatchingMyAccountsFilter = isSelected;
preferences.setShowOffersMatchingMyAccounts(useOffersMatchingMyAccountsFilter);
filterOffers();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////////////////
boolean isShowOffersMatchingMyAccounts() {
return preferences.isShowOffersMatchingMyAccounts();
}
SortedList<OfferBookListItem> getOfferList() {
return sortedItems;
}
Map<String, Integer> getBuyOfferCounts() {
return offerBook.getBuyOfferCountMap();
}
Map<String, Integer> getSellOfferCounts() {
return offerBook.getSellOfferCountMap();
}
boolean isMyOffer(Offer offer) {
return openOfferManager.isMyOffer(offer);
}
OfferDirection getDirection() {
return direction;
}
public ObservableList<TradeCurrency> getTradeCurrencies() {
return allTradeCurrencies;
}
boolean isBootstrappedOrShowPopup() {
return GUIUtil.isBootstrappedOrShowPopup(p2PService);
}
TradeCurrency getSelectedTradeCurrency() {
return selectedTradeCurrency;
}
ObservableList<PaymentMethod> getPaymentMethods() {
ObservableList<PaymentMethod> list = FXCollections.observableArrayList(PaymentMethod.getPaymentMethods());
if (preferences.isHideNonAccountPaymentMethods() && user.getPaymentAccounts() != null) {
Set<PaymentMethod> supportedPaymentMethods = user.getPaymentAccounts().stream()
.map(PaymentAccount::getPaymentMethod).collect(Collectors.toSet());
if (!supportedPaymentMethods.isEmpty()) {
list = FXCollections.observableArrayList(supportedPaymentMethods);
}
}
list.sort(Comparator.naturalOrder());
list.add(0, getShowAllEntryForPaymentMethod());
return list;
}
String getAmount(OfferBookListItem item) {
return formatAmount(item.getOffer(), true);
}
private String formatAmount(Offer offer, boolean decimalAligned) {
return DisplayUtils.formatAmount(offer, GUIUtil.AMOUNT_DECIMALS, decimalAligned, maxPlacesForAmount.get(), btcFormatter);
}
String getPrice(OfferBookListItem item) {
if ((item == null)) {
return "";
}
Offer offer = item.getOffer();
Price price = offer.getPrice();
if (price != null) {
return formatPrice(offer, true);
} else {
return Res.get("shared.na");
}
}
String getAbsolutePriceMargin(Offer offer) {
return FormattingUtils.formatPercentagePrice(Math.abs(offer.getMarketPriceMargin()));
}
private String formatPrice(Offer offer, boolean decimalAligned) {
return DisplayUtils.formatPrice(offer.getPrice(), decimalAligned, maxPlacesForPrice.get());
}
String getPriceAsPercentage(OfferBookListItem item) {
return getMarketBasedPrice(item.getOffer())
.map(price -> "(" + FormattingUtils.formatPercentagePrice(price) + ")")
.orElse("");
}
public Optional<Double> getMarketBasedPrice(Offer offer) {
return priceUtil.getMarketBasedPrice(offer, direction);
}
String formatMarketPriceMargin(Offer offer, boolean decimalAligned) {
String postFix = "";
if (offer.isUseMarketBasedPrice()) {
postFix = " (" + FormattingUtils.formatPercentagePrice(offer.getMarketPriceMargin()) + ")";
}
if (decimalAligned) {
postFix = FormattingUtils.fillUpPlacesWithEmptyStrings(postFix, maxPlacesForMarketPriceMargin.get());
}
return postFix;
}
String getVolume(OfferBookListItem item) {
return formatVolume(item.getOffer(), true);
}
private String formatVolume(Offer offer, boolean decimalAligned) {
Volume offerVolume = offer.getVolume();
Volume minOfferVolume = offer.getMinVolume();
if (offerVolume != null && minOfferVolume != null) {
String postFix = showAllTradeCurrenciesProperty.get() ? " " + offer.getCurrencyCode() : "";
decimalAligned = decimalAligned && !showAllTradeCurrenciesProperty.get();
return VolumeUtil.formatVolume(offer, decimalAligned, maxPlacesForVolume.get()) + postFix;
} else {
return Res.get("shared.na");
}
}
int getNumberOfDecimalsForVolume(OfferBookListItem item) {
return CurrencyUtil.isFiatCurrency(item.getOffer().getCurrencyCode()) ? GUIUtil.FIAT_DECIMALS_WITH_ZEROS : GUIUtil.ALTCOINS_DECIMALS_WITH_ZEROS;
}
String getPaymentMethod(OfferBookListItem item) {
String result = "";
if (item != null) {
Offer offer = item.getOffer();
String method = Res.get(offer.getPaymentMethod().getId() + "_SHORT");
String methodCountryCode = offer.getCountryCode();
if (isF2F(offer)) {
result = method + " (" + methodCountryCode + ", " + offer.getF2FCity() + ")";
} else {
if (methodCountryCode != null)
result = method + " (" + methodCountryCode + ")";
else
result = method;
}
}
return result;
}
String getPaymentMethodToolTip(OfferBookListItem item) {
String result = "";
if (item != null) {
Offer offer = item.getOffer();
result = Res.getWithCol("shared.paymentMethod") + " " + Res.get(offer.getPaymentMethod().getId());
result += "\n" + Res.getWithCol("shared.currency") + " " + CurrencyUtil.getNameAndCode(offer.getCurrencyCode());
if (offer.isXmr()) {
String isAutoConf = offer.isXmrAutoConf() ?
Res.get("shared.yes") :
Res.get("shared.no");
result += "\n" + Res.getWithCol("offerbook.xmrAutoConf") + " " + isAutoConf;
}
String countryCode = offer.getCountryCode();
if (isF2F(offer)) {
if (countryCode != null) {
result += "\n" + Res.get("payment.f2f.offerbook.tooltip.countryAndCity",
CountryUtil.getNameByCode(countryCode), offer.getF2FCity());
result += "\n" + Res.get("payment.f2f.offerbook.tooltip.extra", offer.getExtraInfo());
}
} else {
if (countryCode != null) {
String bankId = offer.getBankId();
if (bankId != null && !bankId.equals("null")) {
if (BankUtil.isBankIdRequired(countryCode))
result += "\n" + Res.get("offerbook.offerersBankId", bankId);
else if (BankUtil.isBankNameRequired(countryCode))
result += "\n" + Res.get("offerbook.offerersBankName", bankId);
}
}
if (countryCode != null)
result += "\n" + Res.get("offerbook.offerersBankSeat", CountryUtil.getNameByCode(countryCode));
List<String> acceptedCountryCodes = offer.getAcceptedCountryCodes();
List<String> acceptedBanks = offer.getAcceptedBankIds();
if (acceptedCountryCodes != null && !acceptedCountryCodes.isEmpty()) {
if (CountryUtil.containsAllSepaEuroCountries(acceptedCountryCodes))
result += "\n" + Res.get("offerbook.offerersAcceptedBankSeatsEuro");
else
result += "\n" + Res.get("offerbook.offerersAcceptedBankSeats", CountryUtil.getNamesByCodesString(acceptedCountryCodes));
} else if (acceptedBanks != null && !acceptedBanks.isEmpty()) {
if (offer.getPaymentMethod().equals(PaymentMethod.SAME_BANK))
result += "\n" + Res.getWithCol("shared.bankName") + " " + acceptedBanks.get(0);
else if (offer.getPaymentMethod().equals(PaymentMethod.SPECIFIC_BANKS))
result += "\n" + Res.getWithCol("shared.acceptedBanks") + " " + Joiner.on(", ").join(acceptedBanks);
}
}
}
return result;
}
private boolean isF2F(Offer offer) {
return offer.getPaymentMethod().equals(PaymentMethod.F2F);
}
String getDirectionLabelTooltip(Offer offer) {
return getDirectionWithCodeDetailed(offer.getMirroredDirection(), offer.getCurrencyCode());
}
Optional<PaymentAccount> getMostMaturePaymentAccountForOffer(Offer offer) {
return PaymentAccountUtil.getMostMaturePaymentAccountForOffer(offer, user.getPaymentAccounts(), accountAgeWitnessService);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private void setMarketPriceFeedCurrency() {
if (isTabSelected) {
if (showAllTradeCurrenciesProperty.get())
priceFeedService.setCurrencyCode(GlobalSettings.getDefaultTradeCurrency().getCode());
else
priceFeedService.setCurrencyCode(tradeCurrencyCode.get());
}
}
private void fillAllTradeCurrencies() {
allTradeCurrencies.clear();
// Used for ignoring filter (show all)
allTradeCurrencies.add(getShowAllEntryForCurrency());
allTradeCurrencies.addAll(preferences.getTradeCurrenciesAsObservable());
allTradeCurrencies.add(getEditEntryForCurrency());
}
///////////////////////////////////////////////////////////////////////////////////////////
// Checks
///////////////////////////////////////////////////////////////////////////////////////////
boolean hasPaymentAccountForCurrency() {
return (showAllTradeCurrenciesProperty.get() &&
user.getPaymentAccounts() != null &&
!user.getPaymentAccounts().isEmpty()) ||
user.hasPaymentAccountForCurrency(selectedTradeCurrency);
}
boolean canCreateOrTakeOffer() {
return GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation) &&
GUIUtil.isChainHeightSyncedWithinToleranceOrShowPopup(walletsSetup) &&
GUIUtil.isBootstrappedOrShowPopup(p2PService);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Filters
///////////////////////////////////////////////////////////////////////////////////////////
private void filterOffers() {
Predicate<OfferBookListItem> predicate = useOffersMatchingMyAccountsFilter ?
getCurrencyAndMethodPredicate().and(getOffersMatchingMyAccountsPredicate()) :
getCurrencyAndMethodPredicate();
filteredItems.setPredicate(predicate);
}
private Predicate<OfferBookListItem> getCurrencyAndMethodPredicate() {
return offerBookListItem -> {
Offer offer = offerBookListItem.getOffer();
boolean directionResult = offer.getDirection() != direction;
boolean currencyResult = (showAllTradeCurrenciesProperty.get()) ||
offer.getCurrencyCode().equals(selectedTradeCurrency.getCode());
boolean paymentMethodResult = showAllPaymentMethods ||
offer.getPaymentMethod().equals(selectedPaymentMethod);
boolean notMyOfferOrShowMyOffersActivated = !isMyOffer(offerBookListItem.getOffer()) || preferences.isShowOwnOffersInOfferBook();
return directionResult && currencyResult && paymentMethodResult && notMyOfferOrShowMyOffersActivated;
};
}
private Predicate<OfferBookListItem> getOffersMatchingMyAccountsPredicate() {
// This code duplicates code in the view at the button column. We need there the different results for
// display in popups so we cannot replace that with the predicate. Any change need to be applied in both
// places.
return offerBookListItem -> offerFilterService.canTakeOffer(offerBookListItem.getOffer(), false).isValid();
}
boolean isOfferBanned(Offer offer) {
return offerFilterService.isOfferBanned(offer);
}
private boolean isShowAllEntry(String id) {
return id.equals(GUIUtil.SHOW_ALL_FLAG);
}
private boolean isEditEntry(String id) {
return id.equals(GUIUtil.EDIT_FLAG);
}
int getNumTrades(Offer offer) {
return closedTradableManager.getObservableList().stream()
.filter(e -> {
final NodeAddress tradingPeerNodeAddress = e instanceof Trade ? ((Trade) e).getTradingPeerNodeAddress() : null;
return tradingPeerNodeAddress != null &&
tradingPeerNodeAddress.getFullAddress().equals(offer.getMakerNodeAddress().getFullAddress());
})
.collect(Collectors.toSet())
.size();
}
public boolean hasSelectionAccountSigning() {
if (showAllTradeCurrenciesProperty.get()) {
if (!isShowAllEntry(selectedPaymentMethod.getId())) {
return PaymentMethod.hasChargebackRisk(selectedPaymentMethod);
}
} else {
if (isShowAllEntry(selectedPaymentMethod.getId()))
return CurrencyUtil.getMatureMarketCurrencies().stream()
.anyMatch(c -> c.getCode().equals(selectedTradeCurrency.getCode()));
else
return PaymentMethod.hasChargebackRisk(selectedPaymentMethod, tradeCurrencyCode.get());
}
return true;
}
public String getMakerFeeAsString(Offer offer) {
return offer.isCurrencyForMakerFeeBtc() ?
btcFormatter.formatCoinWithCode(offer.getMakerFee()) :
bsqFormatter.formatCoinWithCode(offer.getMakerFee());
}
private static String getDirectionWithCodeDetailed(OfferDirection direction, String currencyCode) {
if (CurrencyUtil.isFiatCurrency(currencyCode))
return (direction == OfferDirection.BUY) ? Res.get("shared.buyingBTCWith", currencyCode) : Res.get("shared.sellingBTCFor", currencyCode);
else
return (direction == OfferDirection.SELL) ? Res.get("shared.buyingCurrency", currencyCode) : Res.get("shared.sellingCurrency", currencyCode);
}
public String formatDepositString(Coin deposit, long amount) {
var percentage = FormattingUtils.formatToRoundedPercentWithSymbol(deposit.getValue() / (double) amount);
return btcFormatter.formatCoin(deposit) + " (" + percentage + ")";
}
private TradeCurrency getShowAllEntryForCurrency() {
return new CryptoCurrency(GUIUtil.SHOW_ALL_FLAG, "");
}
private TradeCurrency getEditEntryForCurrency() {
return new CryptoCurrency(GUIUtil.EDIT_FLAG, "");
}
PaymentMethod getShowAllEntryForPaymentMethod() {
return PaymentMethod.getDummyPaymentMethod(GUIUtil.SHOW_ALL_FLAG);
}
}