refactor trade state TRADE_COMPLETED to boolean

This commit is contained in:
woodser 2023-12-15 09:24:21 -05:00
parent 593bced55a
commit c42471191a
8 changed files with 50 additions and 50 deletions

View file

@ -84,8 +84,6 @@ public class TradeEvents {
if (trade.getContract() != null && pubKeyRingProvider.get().equals(trade.getContract().getBuyerPubKeyRing())) if (trade.getContract() != null && pubKeyRingProvider.get().equals(trade.getContract().getBuyerPubKeyRing()))
msg = Res.get("account.notifications.trade.message.msg.completed", shortId); msg = Res.get("account.notifications.trade.message.msg.completed", shortId);
break; break;
case COMPLETED:
break;
} }
if (msg != null) { if (msg != null) {
MobileMessage message = new MobileMessage(Res.get("account.notifications.trade.message.title"), MobileMessage message = new MobileMessage(Res.get("account.notifications.trade.message.title"),

View file

@ -62,7 +62,7 @@ public class TradeChatSession extends SupportSession {
@Override @Override
public boolean chatIsOpen() { public boolean chatIsOpen() {
return trade != null && trade.getState() != Trade.State.TRADE_COMPLETED; return trade != null && !trade.isCompleted();
} }
@Override @Override

View file

@ -163,10 +163,7 @@ public abstract class Trade implements Tradable, Model {
SELLER_SENT_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED), SELLER_SENT_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED),
SELLER_SEND_FAILED_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED), SELLER_SEND_FAILED_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED),
SELLER_STORED_IN_MAILBOX_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED), SELLER_STORED_IN_MAILBOX_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED),
SELLER_SAW_ARRIVED_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED), SELLER_SAW_ARRIVED_PAYMENT_RECEIVED_MSG(Phase.PAYMENT_RECEIVED);
// trade completed
TRADE_COMPLETED(Phase.COMPLETED);
@NotNull @NotNull
public Phase getPhase() { public Phase getPhase() {
@ -205,8 +202,7 @@ public abstract class Trade implements Tradable, Model {
DEPOSITS_CONFIRMED, DEPOSITS_CONFIRMED,
DEPOSITS_UNLOCKED, DEPOSITS_UNLOCKED,
PAYMENT_SENT, PAYMENT_SENT,
PAYMENT_RECEIVED, PAYMENT_RECEIVED;
COMPLETED;
public static Trade.Phase fromProto(protobuf.Trade.Phase phase) { public static Trade.Phase fromProto(protobuf.Trade.Phase phase) {
return ProtoUtil.enumFromProto(Trade.Phase.class, phase.name()); return ProtoUtil.enumFromProto(Trade.Phase.class, phase.name());
@ -456,6 +452,10 @@ public abstract class Trade implements Tradable, Model {
private Long payoutHeight; private Long payoutHeight;
private IdlePayoutSyncer idlePayoutSyncer; private IdlePayoutSyncer idlePayoutSyncer;
@Getter
@Setter
private boolean isCompleted;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Constructors // Constructors
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -622,7 +622,7 @@ public abstract class Trade implements Tradable, Model {
// handle trade phase events // handle trade phase events
tradePhaseSubscription = EasyBind.subscribe(phaseProperty, newValue -> { tradePhaseSubscription = EasyBind.subscribe(phaseProperty, newValue -> {
if (isDepositsPublished() && !isPayoutUnlocked()) updateWalletRefreshPeriod(); if (isDepositsPublished() && !isPayoutUnlocked()) updateWalletRefreshPeriod();
if (isCompleted()) { if (isPaymentReceived()) {
UserThread.execute(() -> { UserThread.execute(() -> {
if (tradePhaseSubscription != null) { if (tradePhaseSubscription != null) {
tradePhaseSubscription.unsubscribe(); tradePhaseSubscription.unsubscribe();
@ -650,7 +650,7 @@ public abstract class Trade implements Tradable, Model {
// complete disputed trade // complete disputed trade
if (getDisputeState().isArbitrated() && !getDisputeState().isClosed()) processModel.getTradeManager().closeDisputedTrade(getId(), Trade.DisputeState.DISPUTE_CLOSED); if (getDisputeState().isArbitrated() && !getDisputeState().isClosed()) processModel.getTradeManager().closeDisputedTrade(getId(), Trade.DisputeState.DISPUTE_CLOSED);
// complete arbitrator trade // auto complete arbitrator trade
if (isArbitrator() && !isCompleted()) processModel.getTradeManager().onTradeCompleted(this); if (isArbitrator() && !isCompleted()) processModel.getTradeManager().onTradeCompleted(this);
// reset address entries // reset address entries
@ -1334,15 +1334,19 @@ public abstract class Trade implements Tradable, Model {
} }
public void setPayoutTx(MoneroTxWallet payoutTx) { public void setPayoutTx(MoneroTxWallet payoutTx) {
// set payout tx fields
this.payoutTx = payoutTx; this.payoutTx = payoutTx;
payoutTxId = payoutTx.getHash();
if ("".equals(payoutTxId)) payoutTxId = null; // tx id is empty until signed
payoutTxKey = payoutTx.getKey(); payoutTxKey = payoutTx.getKey();
payoutTxFee = payoutTx.getFee().longValueExact(); payoutTxFee = payoutTx.getFee().longValueExact();
payoutTxId = payoutTx.getHash();
if ("".equals(payoutTxId)) payoutTxId = null; // tx id is empty until signed
// set payout tx id in dispute(s)
for (Dispute dispute : getDisputes()) dispute.setDisputePayoutTxId(payoutTxId); for (Dispute dispute : getDisputes()) dispute.setDisputePayoutTxId(payoutTxId);
// set final payout amounts // set final payout amounts
if (hasPaymentReceivedMessage()) { // TODO: replace with isPaymentReceived() but only if correct when trade completes with dispute if (isPaymentReceived()) {
BigInteger splitTxFee = payoutTx.getFee().divide(BigInteger.valueOf(2)); BigInteger splitTxFee = payoutTx.getFee().divide(BigInteger.valueOf(2));
getBuyer().setPayoutTxFee(splitTxFee); getBuyer().setPayoutTxFee(splitTxFee);
getSeller().setPayoutTxFee(splitTxFee); getSeller().setPayoutTxFee(splitTxFee);
@ -1622,10 +1626,6 @@ public abstract class Trade implements Tradable, Model {
return getState().getPhase().ordinal() >= Phase.PAYMENT_RECEIVED.ordinal(); return getState().getPhase().ordinal() >= Phase.PAYMENT_RECEIVED.ordinal();
} }
public boolean isCompleted() {
return getState().getPhase().ordinal() >= Phase.COMPLETED.ordinal();
}
public boolean isPayoutPublished() { public boolean isPayoutPublished() {
return getPayoutState().ordinal() >= PayoutState.PAYOUT_PUBLISHED.ordinal(); return getPayoutState().ordinal() >= PayoutState.PAYOUT_PUBLISHED.ordinal();
} }
@ -2110,7 +2110,8 @@ public abstract class Trade implements Tradable, Model {
.collect(Collectors.toList())) .collect(Collectors.toList()))
.setLockTime(lockTime) .setLockTime(lockTime)
.setStartTime(startTime) .setStartTime(startTime)
.setUid(uid); .setUid(uid)
.setIsCompleted(isCompleted);
Optional.ofNullable(payoutTxId).ifPresent(builder::setPayoutTxId); Optional.ofNullable(payoutTxId).ifPresent(builder::setPayoutTxId);
Optional.ofNullable(contract).ifPresent(e -> builder.setContract(contract.toProtoMessage())); Optional.ofNullable(contract).ifPresent(e -> builder.setContract(contract.toProtoMessage()));
@ -2145,6 +2146,7 @@ public abstract class Trade implements Tradable, Model {
trade.setLockTime(proto.getLockTime()); trade.setLockTime(proto.getLockTime());
trade.setStartTime(proto.getStartTime()); trade.setStartTime(proto.getStartTime());
trade.setCounterCurrencyExtraData(ProtoUtil.stringOrNullFromProto(proto.getCounterCurrencyExtraData())); trade.setCounterCurrencyExtraData(ProtoUtil.stringOrNullFromProto(proto.getCounterCurrencyExtraData()));
trade.setCompleted(proto.getIsCompleted());
trade.chatMessages.addAll(proto.getChatMessageList().stream() trade.chatMessages.addAll(proto.getChatMessageList().stream()
.map(ChatMessage::fromPayloadProto) .map(ChatMessage::fromPayloadProto)
@ -2192,6 +2194,7 @@ public abstract class Trade implements Tradable, Model {
",\n startTime=" + startTime + ",\n startTime=" + startTime +
",\n refundResultState=" + refundResultState + ",\n refundResultState=" + refundResultState +
",\n refundResultStateProperty=" + refundResultStateProperty + ",\n refundResultStateProperty=" + refundResultStateProperty +
",\n isCompleted=" + isCompleted +
"\n}"; "\n}";
} }
} }

View file

@ -938,7 +938,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
public void onTradeCompleted(Trade trade) { public void onTradeCompleted(Trade trade) {
if (trade.isCompleted()) throw new RuntimeException("Trade " + trade.getId() + " was already completed"); if (trade.isCompleted()) throw new RuntimeException("Trade " + trade.getId() + " was already completed");
closedTradableManager.add(trade); closedTradableManager.add(trade);
trade.setState(Trade.State.TRADE_COMPLETED); trade.setCompleted(true);
removeTrade(trade); removeTrade(trade);
// TODO The address entry should have been removed already. Check and if its the case remove that. // TODO The address entry should have been removed already. Check and if its the case remove that.

View file

@ -402,6 +402,8 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
if (errorMessage != null) { if (errorMessage != null) {
String appendMsg = ""; String appendMsg = "";
if (trade != null) { if (trade != null) {
if (trade.isPayoutPublished()) appendMsg = Res.get("takeOffer.error.payoutPublished");
else {
switch (trade.getState().getPhase()) { switch (trade.getState().getPhase()) {
case INIT: case INIT:
appendMsg = Res.get("takeOffer.error.noFundsLost"); appendMsg = Res.get("takeOffer.error.noFundsLost");
@ -414,13 +416,11 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
case PAYMENT_RECEIVED: case PAYMENT_RECEIVED:
appendMsg = Res.get("takeOffer.error.depositPublished"); appendMsg = Res.get("takeOffer.error.depositPublished");
break; break;
case COMPLETED:
appendMsg = Res.get("takeOffer.error.payoutPublished");
break;
default: default:
break; break;
} }
} }
}
this.errorMessage.set(errorMessage + appendMsg); this.errorMessage.set(errorMessage + appendMsg);
updateSpinnerInfo(); updateSpinnerInfo();

View file

@ -363,6 +363,12 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
return; return;
} }
if (trade.isCompleted()) {
sellerState.set(UNDEFINED);
buyerState.set(BuyerState.UNDEFINED);
return;
}
switch (tradeState) { switch (tradeState) {
// initialization // initialization
@ -425,11 +431,6 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
sellerState.set(trade.isPayoutPublished() ? SellerState.STEP4 : SellerState.STEP3); sellerState.set(trade.isPayoutPublished() ? SellerState.STEP4 : SellerState.STEP3);
break; break;
case TRADE_COMPLETED:
sellerState.set(UNDEFINED);
buyerState.set(BuyerState.UNDEFINED);
break;
default: default:
sellerState.set(UNDEFINED); sellerState.set(UNDEFINED);
buyerState.set(BuyerState.UNDEFINED); buyerState.set(BuyerState.UNDEFINED);

View file

@ -111,7 +111,11 @@ public class SellerStep3View extends TradeStepView {
if (trade.isPaymentSent() && !trade.isPaymentReceived()) { if (trade.isPaymentSent() && !trade.isPaymentReceived()) {
showPopup(); showPopup();
} else if (trade.isPaymentReceived()) { } else if (trade.isPaymentReceived()) {
switch (state) { if (trade.isCompleted()) {
if (!trade.isPayoutPublished()) log.warn("Payout is expected to be published for {} {} state {}", trade.getClass().getSimpleName(), trade.getId(), trade.getState());
busyAnimation.stop();
statusLabel.setText("");
} else switch (state) {
case SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT: case SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT:
busyAnimation.play(); busyAnimation.play();
statusLabel.setText(Res.get("shared.preparingConfirmation")); statusLabel.setText(Res.get("shared.preparingConfirmation"));
@ -138,11 +142,6 @@ public class SellerStep3View extends TradeStepView {
busyAnimation.stop(); busyAnimation.stop();
statusLabel.setText(""); statusLabel.setText("");
break; break;
case TRADE_COMPLETED:
if (!trade.isPayoutPublished()) log.warn("Payout is expected to be published for {} {} state {}", trade.getClass().getSimpleName(), trade.getId(), trade.getState());
busyAnimation.stop();
statusLabel.setText("");
break;
default: default:
log.warn("Unexpected case: State={}, tradeId={} " + state.name(), trade.getId()); log.warn("Unexpected case: State={}, tradeId={} " + state.name(), trade.getId());
busyAnimation.stop(); busyAnimation.stop();

View file

@ -1434,7 +1434,6 @@ message Trade {
SELLER_SEND_FAILED_PAYMENT_RECEIVED_MSG = 24; SELLER_SEND_FAILED_PAYMENT_RECEIVED_MSG = 24;
SELLER_STORED_IN_MAILBOX_PAYMENT_RECEIVED_MSG = 25; SELLER_STORED_IN_MAILBOX_PAYMENT_RECEIVED_MSG = 25;
SELLER_SAW_ARRIVED_PAYMENT_RECEIVED_MSG = 26; SELLER_SAW_ARRIVED_PAYMENT_RECEIVED_MSG = 26;
TRADE_COMPLETED = 27;
} }
enum Phase { enum Phase {
@ -1446,7 +1445,6 @@ message Trade {
DEPOSITS_UNLOCKED = 5; DEPOSITS_UNLOCKED = 5;
PAYMENT_SENT = 6; PAYMENT_SENT = 6;
PAYMENT_RECEIVED = 7; PAYMENT_RECEIVED = 7;
COMPLETED = 8;
} }
enum PayoutState { enum PayoutState {
@ -1509,6 +1507,7 @@ message Trade {
RefundResultState refund_result_state = 27; RefundResultState refund_result_state = 27;
string counter_currency_extra_data = 28; string counter_currency_extra_data = 28;
string uid = 29; string uid = 29;
bool is_completed = 30;
} }
message BuyerAsMakerTrade { message BuyerAsMakerTrade {