Refactor: Move code from PendingTradesViewModel.getTimeWhenDisputeOpens to Trade.getTradeStartTime.

This commit is contained in:
Manfred Karrer 2018-01-26 20:11:03 -05:00
parent 4af8d44d7e
commit 6d98ce4af2
No known key found for this signature in database
GPG Key ID: 401250966A6B2C46
5 changed files with 57 additions and 81 deletions

View File

@ -29,6 +29,7 @@ import org.bitcoinj.core.Coin;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.TimeUnit;
@EqualsAndHashCode(exclude = {"maxTradePeriod", "maxTradeLimit"})
@ToString
@ -40,8 +41,7 @@ public final class PaymentMethod implements PersistablePayload, Comparable {
///////////////////////////////////////////////////////////////////////////////////////////
// time in blocks (average 10 min for one block confirmation
private static final long HOUR = 3600_000;
private static final long DAY = HOUR * 24;
private static final long DAY = TimeUnit.HOURS.toMillis(24);
public static final String OK_PAY_ID = "OK_PAY";
public static final String PERFECT_MONEY_ID = "PERFECT_MONEY";

View File

@ -17,7 +17,6 @@
package io.bisq.core.trade;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
@ -329,15 +328,12 @@ public abstract class Trade implements Tradable, Model {
transient final private ObjectProperty<Phase> statePhaseProperty = new SimpleObjectProperty<>(state.phase);
transient final private ObjectProperty<DisputeState> disputeStateProperty = new SimpleObjectProperty<>(disputeState);
transient final private ObjectProperty<TradePeriodState> tradePeriodStateProperty = new SimpleObjectProperty<>(tradePeriodState);
transient final private StringProperty errorMessageProperty = new SimpleStringProperty(errorMessage);
transient final private StringProperty errorMessageProperty = new SimpleStringProperty();
// Mutable
@Getter
transient protected TradeProtocol tradeProtocol;
@Nullable
@Setter
transient private Date maxTradePeriodDate, halfTradePeriodDate;
@Nullable
transient private Transaction payoutTx;
@Nullable
transient private Transaction depositTx;
@ -503,7 +499,7 @@ public abstract class Trade implements Tradable, Model {
// or async calls there.
// Clone to avoid ConcurrentModificationException. We remove items at the applyMailboxMessage call...
HashSet<DecryptedMessageWithPubKey> set = new HashSet<>(decryptedMessageWithPubKeySet);
set.stream().forEach(msg -> tradeProtocol.applyMailboxMessage(msg, this));
set.forEach(msg -> tradeProtocol.applyMailboxMessage(msg, this));
}
@ -690,20 +686,46 @@ public abstract class Trade implements Tradable, Model {
return null;
}
@Nullable
public Date getMaxTradePeriodDate() {
if (maxTradePeriodDate == null && getTakeOfferDate() != null)
maxTradePeriodDate = new Date(getTakeOfferDate().getTime() + getOffer().getPaymentMethod().getMaxTradePeriod());
return maxTradePeriodDate;
public Date getHalfTradePeriodDate() {
return new Date(getTradeStartTime() + getMaxTradePeriod() / 2);
}
@Nullable
public Date getHalfTradePeriodDate() {
if (halfTradePeriodDate == null && getTakeOfferDate() != null)
halfTradePeriodDate = new Date(getTakeOfferDate().getTime() + getOffer().getPaymentMethod().getMaxTradePeriod() / 2);
public Date getMaxTradePeriodDate() {
return new Date(getTradeStartTime() + getMaxTradePeriod());
}
return halfTradePeriodDate;
private long getMaxTradePeriod() {
return getOffer().getPaymentMethod().getMaxTradePeriod();
}
private long getTradeStartTime() {
final long now = System.currentTimeMillis();
long startTime;
final Transaction depositTx = getDepositTx();
if (depositTx != null && getTakeOfferDate() != null) {
if (depositTx.getConfidence().getDepthInBlocks() > 0) {
final long tradeTime = getTakeOfferDate().getTime();
long blockTime = depositTx.getUpdateTime().getTime();
// If block date is in future (Date in Bitcoin blocks can be off by +/- 2 hours) we use our current date.
// If block date is earlier than our trade date we use our trade date.
if (blockTime > now)
startTime = now;
else if (blockTime < tradeTime)
startTime = tradeTime;
else
startTime = blockTime;
log.debug("We set the start for the trade period to {}. Trade started at: {}. Block got mined at: {}",
new Date(startTime), new Date(tradeTime), new Date(blockTime));
} else {
log.debug("depositTx not confirmed yet. We don't start counting remaining trade period yet. txId={}", depositTx.getHashAsString());
startTime = now;
}
} else {
log.warn("depositTx is null");
startTime = now;
}
return startTime;
}
public boolean hasFailed() {
@ -863,7 +885,7 @@ public abstract class Trade implements Tradable, Model {
public void onFailure(@NotNull Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
Throwables.propagate(t);
throw new RuntimeException(t);
}
});
}
@ -916,8 +938,6 @@ public abstract class Trade implements Tradable, Model {
",\n tradePeriodStateProperty=" + tradePeriodStateProperty +
",\n errorMessageProperty=" + errorMessageProperty +
",\n tradeProtocol=" + tradeProtocol +
",\n maxTradePeriodDate=" + maxTradePeriodDate +
",\n halfTradePeriodDate=" + halfTradePeriodDate +
",\n payoutTx=" + payoutTx +
",\n depositTx=" + depositTx +
",\n tradeAmount=" + tradeAmount +

View File

@ -836,7 +836,7 @@ public class MainViewModel implements ViewModel {
}
private void updateTradePeriodState() {
tradeManager.getTradableList().stream().forEach(trade -> {
tradeManager.getTradableList().forEach(trade -> {
if (!trade.isPayoutPublished()) {
Date maxTradePeriodDate = trade.getMaxTradePeriodDate();
Date halfTradePeriodDate = trade.getHalfTradePeriodDate();

View File

@ -198,7 +198,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
switch (BisqEnvironment.getBaseCurrencyNetwork().getCurrencyCode()) {
case "BTC":
amount.set("0.0001");
price.set("14029");
price.set("11029");
break;
case "LTC":
amount.set("50");

View File

@ -22,7 +22,6 @@ import io.bisq.common.Clock;
import io.bisq.common.app.DevEnv;
import io.bisq.common.app.Log;
import io.bisq.common.locale.Res;
import io.bisq.core.btc.wallet.WalletsSetup;
import io.bisq.core.offer.Offer;
import io.bisq.core.payment.AccountAgeWitnessService;
import io.bisq.core.payment.payload.PaymentMethod;
@ -40,7 +39,6 @@ import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
@ -79,7 +77,6 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
public final BtcAddressValidator btcAddressValidator;
final AccountAgeWitnessService accountAgeWitnessService;
public final P2PService p2PService;
private final WalletsSetup walletsSetup;
private final ClosedTradableManager closedTradableManager;
public final Clock clock;
@ -97,7 +94,6 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
BsqFormatter bsqFormatter,
BtcAddressValidator btcAddressValidator,
P2PService p2PService,
WalletsSetup walletsSetup,
ClosedTradableManager closedTradableManager,
AccountAgeWitnessService accountAgeWitnessService,
Clock clock) {
@ -107,7 +103,6 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
this.bsqFormatter = bsqFormatter;
this.btcAddressValidator = btcAddressValidator;
this.p2PService = p2PService;
this.walletsSetup = walletsSetup;
this.closedTradableManager = closedTradableManager;
this.accountAgeWitnessService = accountAgeWitnessService;
this.clock = clock;
@ -117,7 +112,7 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
protected void activate() {
}
// Dont set own listener as we need to control the order of the calls
// Don't set own listener as we need to control the order of the calls
public void onSelectedItemChanged(PendingTradesListItem selectedItem) {
if (tradeStateSubscription != null) {
tradeStateSubscription.unsubscribe();
@ -161,62 +156,23 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
return btcFormatter.getCurrencyPair(item.getTrade().getOffer().getCurrencyCode());
}
// trade period
private long getMaxTradePeriod() {
return dataModel.getOffer() != null ? dataModel.getOffer().getPaymentMethod().getMaxTradePeriod() : 0;
}
private long getTimeWhenDisputeOpens() {
final Trade trade = dataModel.getTrade();
if (trade != null) {
final long now = System.currentTimeMillis();
long startTime;
final Transaction depositTx = trade.getDepositTx();
if (depositTx != null) {
if (depositTx.getConfidence().getDepthInBlocks() > 0) {
final long tradeTime = trade.getDate().getTime();
long blockTime = depositTx.getUpdateTime().getTime();
// If block date is in future (Date in Bitcoin blocks can be off by +/- 2 hours) we use our current date.
// If block date is earlier than our trade date we use our trade date.
if (blockTime > now)
startTime = now;
else if (blockTime < tradeTime)
startTime = tradeTime;
else
startTime = blockTime;
log.debug("We set the start for the trade period to {}. Trade started at: {}. Block got mined at: {}",
new Date(startTime), new Date(tradeTime), new Date(blockTime));
} else {
log.debug("depositTx not confirmed yet. We don't start counting remaining trade period yet. txId={}", depositTx.getHashAsString());
startTime = now;
}
return startTime + getMaxTradePeriod();
} else {
log.warn("depositTx is null");
return 0;
}
} else {
log.warn("trade is null");
return 0;
}
@Nullable
private Date getMaxTradePeriodDate() {
return dataModel.getTrade() != null ? dataModel.getTrade().getMaxTradePeriodDate() : null;
}
private long getTimeWhenHalfPeriodReached() {
return dataModel.getTrade() != null ? dataModel.getTrade().getDate().getTime() + getMaxTradePeriod() / 2 : 0;
}
private Date getDateWhenDisputeOpens() {
return new Date(getTimeWhenDisputeOpens());
}
private Date getDateWhenHalfPeriodReached() {
return new Date(getTimeWhenHalfPeriodReached());
@Nullable
private Date getHalfTradePeriodDate() {
return dataModel.getTrade() != null ? dataModel.getTrade().getHalfTradePeriodDate() : null;
}
private long getRemainingTradeDuration() {
return getDateWhenDisputeOpens().getTime() - new Date().getTime();
return getMaxTradePeriodDate() != null ? getMaxTradePeriodDate().getTime() - new Date().getTime() : getMaxTradePeriod();
}
public String getRemainingTradeDurationAsWords() {
@ -237,11 +193,11 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
}
public boolean showWarning() {
return new Date().after(getDateWhenHalfPeriodReached());
return getHalfTradePeriodDate() != null && new Date().after(getHalfTradePeriodDate());
}
public boolean showDispute() {
return new Date().after(getDateWhenDisputeOpens());
return getMaxTradePeriodDate() != null && new Date().after(getMaxTradePeriodDate());
}
//
@ -282,7 +238,7 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
}
public String getTxFee() {
if (trade != null) {
if (trade != null && trade.getTradeAmount() != null) {
Coin txFee = dataModel.getTxFee();
String percentage = GUIUtil.getPercentageOfTradeAmount(txFee, trade.getTradeAmount(), btcFormatter);
return btcFormatter.formatCoinWithCode(txFee) + percentage;
@ -293,7 +249,7 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
public String getTradeFee() {
if (trade != null && dataModel.getOffer() != null) {
if (dataModel.getOffer().isCurrencyForMakerFeeBtc()) {
if (dataModel.getOffer().isCurrencyForMakerFeeBtc() && trade.getTradeAmount() != null) {
Coin tradeFeeInBTC = dataModel.getTradeFeeInBTC();
String percentage = GUIUtil.getPercentageOfTradeAmount(tradeFeeInBTC, trade.getTradeAmount(), btcFormatter);
return btcFormatter.formatCoinWithCode(tradeFeeInBTC) + percentage;
@ -308,7 +264,7 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
public String getSecurityDeposit() {
Offer offer = dataModel.getOffer();
Trade trade = dataModel.getTrade();
if (offer != null && trade != null) {
if (offer != null && trade != null && trade.getTradeAmount() != null) {
Coin securityDeposit = dataModel.isBuyer() ?
offer.getBuyerSecurityDeposit()
: offer.getSellerSecurityDeposit();