Skip to content

Commit

Permalink
Refactor PendingTradesViewModel methods -> TradeUtil & OfferUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubstan committed Oct 25, 2020
1 parent ccd3c99 commit 24ba921
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 75 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/offer/OfferUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public boolean isBsqForTakerFeeAvailable(@Nullable Coin amount) {
return !availableBalance.subtract(takerFee).isNegative();
}

public boolean isBlockChainPaymentMethod(Offer offer) {
return offer != null && offer.getPaymentMethod().isAsset();
}

public Optional<Volume> getFeeInUserFiatCurrency(Coin makerFee,
boolean isCurrencyForMakerFeeBtc,
CoinFormatter bsqFormatter) {
Expand Down
96 changes: 96 additions & 0 deletions core/src/main/java/bisq/core/trade/TradeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package bisq.core.trade;

import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.locale.Res;
import bisq.core.offer.Offer;

import bisq.common.crypto.KeyRing;
import bisq.common.util.Tuple2;
Expand All @@ -26,10 +28,18 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.Date;
import java.util.Objects;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

import static bisq.core.locale.CurrencyUtil.getCurrencyPair;
import static bisq.core.locale.CurrencyUtil.isFiatCurrency;
import static bisq.core.util.FormattingUtils.formatDurationAsWords;
import static com.google.common.base.Preconditions.checkNotNull;

/**
* This class contains trade utility methods.
*/
Expand Down Expand Up @@ -108,4 +118,90 @@ public Tuple2<String, String> getTradeAddresses(Trade trade) {

return new Tuple2<>(multiSigAddress.getAddressString(), payoutAddress);
}

public long getRemainingTradeDuration(Trade trade) {
return trade.getMaxTradePeriodDate() != null
? trade.getMaxTradePeriodDate().getTime() - new Date().getTime()
: getMaxTradePeriod(trade);
}

public long getMaxTradePeriod(Trade trade) {
return trade.getOffer() != null
? trade.getOffer().getPaymentMethod().getMaxTradePeriod()
: 0;
}

public double getRemainingTradeDurationAsPercentage(Trade trade) {
long maxPeriod = getMaxTradePeriod(trade);
long remaining = getRemainingTradeDuration(trade);
if (maxPeriod != 0) {
return 1 - (double) remaining / (double) maxPeriod;
} else
return 0;
}

public String getRemainingTradeDurationAsWords(Trade trade) {
return formatDurationAsWords(Math.max(0, getRemainingTradeDuration(trade)));
}

@Nullable
public Date getHalfTradePeriodDate(Trade trade) {
return trade != null ? trade.getHalfTradePeriodDate() : null;
}

public Date getDateForOpenDispute(Trade trade) {
return new Date(new Date().getTime() + getRemainingTradeDuration(trade));
}

public String getMarketDescription(Trade trade) {
if (trade == null)
return "";

checkNotNull(trade.getOffer());
checkNotNull(trade.getOffer().getCurrencyCode());
return getCurrencyPair(trade.getOffer().getCurrencyCode());
}

public String getPaymentMethodNameWithCountryCode(Trade trade) {
String paymentMethodDescription = "";
if (trade != null) {
Offer offer = trade.getOffer();
checkNotNull(offer);
checkNotNull(offer.getPaymentMethod());
paymentMethodDescription = offer.getPaymentMethodNameWithCountryCode();
}
return paymentMethodDescription;
}

/**
* Returns a string describing a trader's role.
*
* @param isBuyerMakerAndSellerTaker boolean
* @param isMaker boolean
* @param currencyCode String
* @return String describing a trader's role
*/
public String getRole(boolean isBuyerMakerAndSellerTaker, boolean isMaker, String currencyCode) {
if (isFiatCurrency(currencyCode)) {
String baseCurrencyCode = Res.getBaseCurrencyCode();
if (isBuyerMakerAndSellerTaker)
return isMaker
? Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.buyer"))
: Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.seller"));
else
return isMaker
? Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.seller"))
: Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.buyer"));
} else {
if (isBuyerMakerAndSellerTaker)
return isMaker
? Res.get("formatter.asMaker", currencyCode, Res.get("shared.seller"))
: Res.get("formatter.asTaker", currencyCode, Res.get("shared.buyer"));
else
return isMaker
? Res.get("formatter.asMaker", currencyCode, Res.get("shared.buyer"))
: Res.get("formatter.asTaker", currencyCode, Res.get("shared.seller"));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.btc.wallet.Restrictions;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.network.MessageState;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferUtil;
import bisq.core.provider.fee.FeeService;
import bisq.core.trade.Contract;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeUtil;
import bisq.core.trade.closed.ClosedTradableManager;
import bisq.core.user.User;
import bisq.core.util.FormattingUtils;
Expand Down Expand Up @@ -97,6 +97,8 @@ enum SellerState implements State {
final AccountAgeWitnessService accountAgeWitnessService;
public final P2PService p2PService;
private final ClosedTradableManager closedTradableManager;
private final OfferUtil offerUtil;
private final TradeUtil tradeUtil;
public final ClockWatcher clockWatcher;
@Getter
private final User user;
Expand All @@ -120,6 +122,8 @@ public PendingTradesViewModel(PendingTradesDataModel dataModel,
BtcAddressValidator btcAddressValidator,
P2PService p2PService,
ClosedTradableManager closedTradableManager,
OfferUtil offerUtil,
TradeUtil tradeUtil,
AccountAgeWitnessService accountAgeWitnessService,
ClockWatcher clockWatcher,
User user) {
Expand All @@ -130,6 +134,8 @@ public PendingTradesViewModel(PendingTradesDataModel dataModel,
this.btcAddressValidator = btcAddressValidator;
this.p2PService = p2PService;
this.closedTradableManager = closedTradableManager;
this.offerUtil = offerUtil;
this.tradeUtil = tradeUtil;
this.accountAgeWitnessService = accountAgeWitnessService;
this.clockWatcher = clockWatcher;
this.user = user;
Expand Down Expand Up @@ -199,55 +205,30 @@ ReadOnlyObjectProperty<SellerState> getSellerState() {
}

public String getPayoutAmount() {
return dataModel.getTrade() != null ? btcFormatter.formatCoinWithCode(dataModel.getTrade().getPayoutAmount()) : "";
return dataModel.getTrade() != null
? btcFormatter.formatCoinWithCode(dataModel.getTrade().getPayoutAmount())
: "";
}

String getMarketLabel(PendingTradesListItem item) {
if ((item == null))
return "";

checkNotNull(item.getTrade().getOffer());
checkNotNull(item.getTrade().getOffer().getCurrencyCode());
return CurrencyUtil.getCurrencyPair(item.getTrade().getOffer().getCurrencyCode());
}

private long getMaxTradePeriod() {
return dataModel.getOffer() != null ? dataModel.getOffer().getPaymentMethod().getMaxTradePeriod() : 0;
}

@Nullable
private Date getMaxTradePeriodDate() {
return dataModel.getTrade() != null ? dataModel.getTrade().getMaxTradePeriodDate() : null;
}

@Nullable
private Date getHalfTradePeriodDate() {
return dataModel.getTrade() != null ? dataModel.getTrade().getHalfTradePeriodDate() : null;
}

private long getRemainingTradeDuration() {
return getMaxTradePeriodDate() != null ? getMaxTradePeriodDate().getTime() - new Date().getTime() : getMaxTradePeriod();
return item == null ? "" : tradeUtil.getMarketDescription(item.getTrade());
}

public String getRemainingTradeDurationAsWords() {
return FormattingUtils.formatDurationAsWords(Math.max(0, getRemainingTradeDuration()));
return tradeUtil.getRemainingTradeDurationAsWords(dataModel.getTrade());
}

public double getRemainingTradeDurationAsPercentage() {
long maxPeriod = getMaxTradePeriod();
long remaining = getRemainingTradeDuration();
if (maxPeriod != 0) {
return 1 - (double) remaining / (double) maxPeriod;
} else
return 0;
return tradeUtil.getRemainingTradeDurationAsPercentage(dataModel.getTrade());
}

public String getDateForOpenDispute() {
return DisplayUtils.formatDateTime(new Date(new Date().getTime() + getRemainingTradeDuration()));
return DisplayUtils.formatDateTime(tradeUtil.getDateForOpenDispute(dataModel.getTrade()));
}

public boolean showWarning() {
return getHalfTradePeriodDate() != null && new Date().after(getHalfTradePeriodDate());
Date halfTradePeriodDate = tradeUtil.getHalfTradePeriodDate(dataModel.getTrade());
return halfTradePeriodDate != null && new Date().after(halfTradePeriodDate);
}

public boolean showDispute() {
Expand All @@ -263,36 +244,36 @@ String getMyRole(PendingTradesListItem item) {
Offer offer = trade.getOffer();
checkNotNull(offer);
checkNotNull(offer.getCurrencyCode());
return getRole(contract.isBuyerMakerAndSellerTaker(), dataModel.isMaker(offer), offer.getCurrencyCode());
return tradeUtil.getRole(contract.isBuyerMakerAndSellerTaker(),
dataModel.isMaker(offer),
offer.getCurrencyCode());
} else {
return "";
}
}

String getPaymentMethod(PendingTradesListItem item) {
String result = "";
if (item != null) {
Offer offer = item.getTrade().getOffer();
checkNotNull(offer);
checkNotNull(offer.getPaymentMethod());
result = offer.getPaymentMethodNameWithCountryCode();
}
return result;
return item == null ? "" : tradeUtil.getPaymentMethodNameWithCountryCode(item.getTrade());
}

// summary
public String getTradeVolume() {
return dataModel.getTrade() != null ? btcFormatter.formatCoinWithCode(dataModel.getTrade().getTradeAmount()) : "";
return dataModel.getTrade() != null
? btcFormatter.formatCoinWithCode(dataModel.getTrade().getTradeAmount())
: "";
}

public String getFiatVolume() {
return dataModel.getTrade() != null ? DisplayUtils.formatVolumeWithCode(dataModel.getTrade().getTradeVolume()) : "";
return dataModel.getTrade() != null
? DisplayUtils.formatVolumeWithCode(dataModel.getTrade().getTradeVolume())
: "";
}

public String getTxFee() {
if (trade != null && trade.getTradeAmount() != null) {
Coin txFee = dataModel.getTxFee();
String percentage = GUIUtil.getPercentageOfTradeAmount(txFee, trade.getTradeAmount(),
String percentage = GUIUtil.getPercentageOfTradeAmount(txFee,
trade.getTradeAmount(),
Coin.ZERO);
return btcFormatter.formatCoinWithCode(txFee) + percentage;
} else {
Expand Down Expand Up @@ -344,7 +325,7 @@ public String getSecurityDeposit() {
}

public boolean isBlockChainMethod() {
return dataModel.getOffer() != null && dataModel.getOffer().getPaymentMethod().isAsset();
return offerUtil.isBlockChainPaymentMethod(dataModel.getOffer());
}

public int getNumPastTrades(Trade trade) {
Expand All @@ -362,7 +343,13 @@ public int getNumPastTrades(Trade trade) {
.collect(Collectors.toSet())
.size();
}


@Nullable
private Date getMaxTradePeriodDate() {
return dataModel.getTrade() != null
? dataModel.getTrade().getMaxTradePeriodDate()
: null;
}

///////////////////////////////////////////////////////////////////////////////////////////
// States
Expand Down Expand Up @@ -479,29 +466,4 @@ private void onTradeStateChanged(Trade.State tradeState) {
break;
}
}

private static String getRole(boolean isBuyerMakerAndSellerTaker, boolean isMaker, String currencyCode) {
if (CurrencyUtil.isFiatCurrency(currencyCode)) {
String baseCurrencyCode = Res.getBaseCurrencyCode();
if (isBuyerMakerAndSellerTaker)
return isMaker ?
Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.buyer")) :
Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.seller"));
else
return isMaker ?
Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.seller")) :
Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.buyer"));
} else {
if (isBuyerMakerAndSellerTaker)
return isMaker ?
Res.get("formatter.asMaker", currencyCode, Res.get("shared.seller")) :
Res.get("formatter.asTaker", currencyCode, Res.get("shared.buyer"));
else
return isMaker ?
Res.get("formatter.asMaker", currencyCode, Res.get("shared.buyer")) :
Res.get("formatter.asTaker", currencyCode, Res.get("shared.seller"));
}

}

}

0 comments on commit 24ba921

Please sign in to comment.