Use TradeModel instead of Trade where appropriate.

Extend methods with additional params.
Cleanups
This commit is contained in:
chimp1984 2021-10-21 15:28:58 +02:00
parent d063aff5bf
commit b194960c63
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
17 changed files with 105 additions and 75 deletions

View File

@ -47,7 +47,7 @@ public final class RawTransactionInput implements NetworkPayload, PersistablePay
// Added at Bsq swap release
// id of the org.bitcoinj.script.Script.ScriptType. Useful to know if input is segwit.
// Lowest Script.ScriptType.id value is 1, so we use 0 as value for not defined
public final int scriptTypeId; // Lowest Script.ScriptType.id value is 1, so we use 0 as value for not defined
public final int scriptTypeId;
public RawTransactionInput(TransactionInput input) {
this(input.getOutpoint().getIndex(),

View File

@ -523,7 +523,6 @@ public class BsqTxView extends ActivatableView<GridPane, Void> implements BsqBal
setGraphic(field);
}
} else {
if (item.isWithdrawalToBTCWallet())
labelString = Res.get("dao.tx.withdrawnFromWallet");
@ -539,7 +538,6 @@ public class BsqTxView extends ActivatableView<GridPane, Void> implements BsqBal
};
}
});
tableView.getColumns().add(column);
}

View File

@ -48,7 +48,7 @@ public class TradableRepository {
this.failedTradesManager = failedTradesManager;
}
Set<Tradable> getAll() {
public Set<Tradable> getAll() {
return ImmutableSet.<Tradable>builder()
.addAll(openOfferManager.getObservableList())
.addAll(tradeManager.getObservableList())

View File

@ -22,7 +22,7 @@ import bisq.core.offer.OpenOffer;
import bisq.core.support.dispute.arbitration.ArbitrationManager;
import bisq.core.support.dispute.refund.RefundManager;
import bisq.core.trade.model.Tradable;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.model.TradeModel;
import bisq.common.crypto.PubKeyRing;
@ -48,17 +48,17 @@ public class TransactionAwareTradableFactory {
this.pubKeyRing = pubKeyRing;
}
TransactionAwareTradable create(Tradable delegate) {
if (delegate instanceof OpenOffer) {
return new TransactionAwareOpenOffer((OpenOffer) delegate);
} else if (delegate instanceof Trade) {
return new TransactionAwareTrade((Trade) delegate,
TransactionAwareTradable create(Tradable tradable) {
if (tradable instanceof OpenOffer) {
return new TransactionAwareOpenOffer((OpenOffer) tradable);
} else if (tradable instanceof TradeModel) {
return new TransactionAwareTrade((TradeModel) tradable,
arbitrationManager,
refundManager,
btcWalletService,
pubKeyRing);
} else {
return new DummyTransactionAwareTradable(delegate);
return new DummyTransactionAwareTradable(tradable);
}
}
}

View File

@ -23,6 +23,7 @@ import bisq.core.support.dispute.Dispute;
import bisq.core.support.dispute.arbitration.ArbitrationManager;
import bisq.core.support.dispute.refund.RefundManager;
import bisq.core.trade.model.Tradable;
import bisq.core.trade.model.TradeModel;
import bisq.core.trade.model.bisq_v1.Contract;
import bisq.core.trade.model.bisq_v1.Trade;
@ -43,18 +44,18 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j
class TransactionAwareTrade implements TransactionAwareTradable {
private final Trade trade;
private final TradeModel tradeModel;
private final ArbitrationManager arbitrationManager;
private final RefundManager refundManager;
private final BtcWalletService btcWalletService;
private final PubKeyRing pubKeyRing;
TransactionAwareTrade(Trade trade,
TransactionAwareTrade(TradeModel tradeModel,
ArbitrationManager arbitrationManager,
RefundManager refundManager,
BtcWalletService btcWalletService,
PubKeyRing pubKeyRing) {
this.trade = trade;
this.tradeModel = tradeModel;
this.arbitrationManager = arbitrationManager;
this.refundManager = refundManager;
this.btcWalletService = btcWalletService;
@ -66,19 +67,30 @@ class TransactionAwareTrade implements TransactionAwareTradable {
Sha256Hash hash = transaction.getTxId();
String txId = hash.toString();
boolean isTakerOfferFeeTx = txId.equals(trade.getTakerFeeTxId());
boolean isOfferFeeTx = isOfferFeeTx(txId);
boolean isDepositTx = isDepositTx(hash);
boolean isPayoutTx = isPayoutTx(hash);
boolean isDisputedPayoutTx = isDisputedPayoutTx(txId);
boolean isDelayedPayoutTx = transaction.getLockTime() != 0 && isDelayedPayoutTx(txId);
boolean isRefundPayoutTx = isRefundPayoutTx(txId);
boolean tradeRelated = false;
if (tradeModel instanceof Trade) {
Trade trade = (Trade) tradeModel;
boolean isTakerOfferFeeTx = txId.equals(trade.getTakerFeeTxId());
boolean isOfferFeeTx = isOfferFeeTx(txId);
boolean isDepositTx = isDepositTx(hash);
boolean isPayoutTx = isPayoutTx(hash);
boolean isDisputedPayoutTx = isDisputedPayoutTx(txId);
boolean isDelayedPayoutTx = transaction.getLockTime() != 0 && isDelayedPayoutTx(txId);
boolean isRefundPayoutTx = isRefundPayoutTx(trade, txId);
tradeRelated = isTakerOfferFeeTx ||
isOfferFeeTx ||
isDepositTx ||
isPayoutTx ||
isDisputedPayoutTx ||
isDelayedPayoutTx ||
isRefundPayoutTx;
}
return isTakerOfferFeeTx || isOfferFeeTx || isDepositTx || isPayoutTx ||
isDisputedPayoutTx || isDelayedPayoutTx || isRefundPayoutTx;
return tradeRelated;
}
private boolean isPayoutTx(Sha256Hash txId) {
Trade trade = (Trade) tradeModel;
return Optional.ofNullable(trade.getPayoutTx())
.map(Transaction::getTxId)
.map(hash -> hash.equals(txId))
@ -86,6 +98,7 @@ class TransactionAwareTrade implements TransactionAwareTradable {
}
private boolean isDepositTx(Sha256Hash txId) {
Trade trade = (Trade) tradeModel;
return Optional.ofNullable(trade.getDepositTx())
.map(Transaction::getTxId)
.map(hash -> hash.equals(txId))
@ -93,17 +106,17 @@ class TransactionAwareTrade implements TransactionAwareTradable {
}
private boolean isOfferFeeTx(String txId) {
return Optional.ofNullable(trade.getOffer())
return Optional.ofNullable(tradeModel.getOffer())
.map(Offer::getOfferFeePaymentTxId)
.map(paymentTxId -> paymentTxId.equals(txId))
.orElse(false);
}
private boolean isDisputedPayoutTx(String txId) {
String delegateId = trade.getId();
String delegateId = tradeModel.getId();
ObservableList<Dispute> disputes = arbitrationManager.getDisputesAsObservableList();
boolean isAnyDisputeRelatedToThis = arbitrationManager.getDisputedTradeIds().contains(trade.getId());
boolean isAnyDisputeRelatedToThis = arbitrationManager.getDisputedTradeIds().contains(tradeModel.getId());
return isAnyDisputeRelatedToThis && disputes.stream()
.anyMatch(dispute -> {
@ -142,8 +155,8 @@ class TransactionAwareTrade implements TransactionAwareTradable {
});
}
private boolean isRefundPayoutTx(String txId) {
String tradeId = trade.getId();
private boolean isRefundPayoutTx(Trade trade, String txId) {
String tradeId = tradeModel.getId();
ObservableList<Dispute> disputes = refundManager.getDisputesAsObservableList();
boolean isAnyDisputeRelatedToThis = refundManager.getDisputedTradeIds().contains(tradeId);
@ -173,6 +186,6 @@ class TransactionAwareTrade implements TransactionAwareTradable {
@Override
public Tradable asTradable() {
return trade;
return tradeModel;
}
}

View File

@ -35,6 +35,7 @@ import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferDirection;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
import bisq.core.user.Preferences;
import bisq.core.user.User;
@ -75,6 +76,7 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
private Navigation.Listener navigationListener;
private ChangeListener<Tab> tabChangeListener;
private ListChangeListener<Tab> tabListChangeListener;
private OfferView.OfferActionHandler offerActionHandler;
protected OfferView(ViewLoader viewLoader,
Navigation navigation,
@ -128,6 +130,29 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
onTakeOfferViewRemoved();
}
};
offerActionHandler = new OfferActionHandler() {
@Override
public void onCreateOffer(TradeCurrency tradeCurrency, PaymentMethod paymentMethod) {
if (createOfferViewOpen) {
root.getTabs().remove(createOfferTab);
}
if (canCreateOrTakeOffer(tradeCurrency)) {
openCreateOffer(tradeCurrency, paymentMethod);
}
}
@Override
public void onTakeOffer(Offer offer) {
if (takeOfferViewOpen) {
root.getTabs().remove(takeOfferTab);
}
if (canCreateOrTakeOffer(CurrencyUtil.getTradeCurrency(offer.getCurrencyCode()).get())) {
openTakeOffer(offer);
}
}
};
}
@Override
@ -150,7 +175,7 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
root.getTabs().removeListener(tabListChangeListener);
}
private String getCreateOfferTabName() {
private String getCreateOfferTabName(Class<? extends View> viewClass) {
return Res.get("offerbook.createOffer").toUpperCase();
}
@ -173,28 +198,6 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
tabPane.getTabs().add(offerBookTab);
offerBookView = (OfferBookView) view;
offerBookView.onTabSelected(true);
OfferActionHandler offerActionHandler = new OfferActionHandler() {
@Override
public void onCreateOffer(TradeCurrency tradeCurrency) {
if (createOfferViewOpen) {
tabPane.getTabs().remove(createOfferTab);
}
if (canCreateOrTakeOffer()) {
openCreateOffer(tradeCurrency);
}
}
@Override
public void onTakeOffer(Offer offer) {
if (takeOfferViewOpen) {
tabPane.getTabs().remove(takeOfferTab);
}
if (canCreateOrTakeOffer()) {
openTakeOffer(offer);
}
}
};
offerBookView.setOfferActionHandler(offerActionHandler);
offerBookView.setDirection(direction);
} else if (viewClass == CreateOfferView.class && createOfferView == null) {
@ -202,9 +205,9 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
// CreateOffer and TakeOffer must not be cached by ViewLoader as we cannot use a view multiple times
// in different graphs
createOfferView = (CreateOfferView) view;
createOfferView.initWithData(direction, tradeCurrency);
createOfferView.initWithData(direction, tradeCurrency, offerActionHandler);
createOfferPane = createOfferView.getRoot();
createOfferTab = new Tab(getCreateOfferTabName());
createOfferTab = new Tab(getCreateOfferTabName(viewClass));
createOfferTab.setClosable(true);
// close handler from close on create offer action
createOfferView.setCloseHandler(() -> tabPane.getTabs().remove(createOfferTab));
@ -228,9 +231,9 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
}
}
protected boolean canCreateOrTakeOffer() {
protected boolean canCreateOrTakeOffer(TradeCurrency tradeCurrency) {
return GUIUtil.isBootstrappedOrShowPopup(p2PService) &&
GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation);
GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation, tradeCurrency);
}
private void showNoArbitratorForUserLocaleWarning() {
@ -256,7 +259,7 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
OfferView.this.navigation.navigateTo(MainView.class, OfferView.this.getClass(), TakeOfferView.class);
}
private void openCreateOffer(TradeCurrency tradeCurrency) {
private void openCreateOffer(TradeCurrency tradeCurrency, PaymentMethod paymentMethod) {
OfferView.this.createOfferViewOpen = true;
OfferView.this.tradeCurrency = tradeCurrency;
OfferView.this.navigation.navigateTo(MainView.class, OfferView.this.getClass(), CreateOfferView.class);
@ -285,7 +288,7 @@ public abstract class OfferView extends ActivatableView<TabPane, Void> {
}
public interface OfferActionHandler {
void onCreateOffer(TradeCurrency tradeCurrency);
void onCreateOffer(TradeCurrency tradeCurrency, PaymentMethod paymentMethod);
void onTakeOffer(Offer offer);
}

View File

@ -772,7 +772,7 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs
boolean canPlaceOffer() {
return GUIUtil.isBootstrappedOrShowPopup(p2PService) &&
GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation);
GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation, tradeCurrency);
}
public boolean isMinBuyerSecurityDeposit() {

View File

@ -117,6 +117,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import static bisq.core.payment.payload.PaymentMethod.HAL_CASH_ID;
@ -178,6 +180,9 @@ public abstract class MutableOfferView<M extends MutableOfferViewModel<?>> exten
private AutoTooltipSlideToggleButton tradeFeeInBtcToggle, tradeFeeInBsqToggle;
private Text xIcon, fakeXIcon;
@Setter
private OfferView.OfferActionHandler offerActionHandler;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, lifecycle
@ -291,16 +296,21 @@ public abstract class MutableOfferView<M extends MutableOfferViewModel<?>> exten
///////////////////////////////////////////////////////////////////////////////////////////
public void onTabSelected(boolean isSelected) {
if (isSelected && !model.getDataModel().isTabSelected)
if (isSelected && !model.getDataModel().isTabSelected) {
doActivate();
else
} else {
// todo check
deactivate();
}
isActivated = isSelected;
model.getDataModel().onTabSelected(isSelected);
}
public void initWithData(OfferDirection direction, TradeCurrency tradeCurrency) {
public void initWithData(OfferDirection direction, TradeCurrency tradeCurrency,
OfferView.OfferActionHandler offerActionHandler) {
this.offerActionHandler = offerActionHandler;
boolean result = model.initWithData(direction, tradeCurrency);
if (!result) {

View File

@ -463,7 +463,7 @@ class TakeOfferDataModel extends OfferDataModel {
}
boolean canTakeOffer() {
return GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation) &&
return GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation, paymentAccount.getSelectedTradeCurrency()) &&
GUIUtil.isBootstrappedOrShowPopup(p2PService);
}

View File

@ -590,6 +590,7 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
private void onCreateOffer() {
if (model.canCreateOrTakeOffer()) {
PaymentMethod selectedPaymentMethod = model.selectedPaymentMethod;
TradeCurrency selectedTradeCurrency = model.getSelectedTradeCurrency();
if (!model.hasPaymentAccountForCurrency()) {
new Popup().headLine(Res.get("offerbook.warning.noTradingAccountForCurrency.headline"))
@ -597,7 +598,7 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
.actionButtonText(Res.get("offerbook.yesCreateOffer"))
.onAction(() -> {
createOfferButton.setDisable(true);
offerActionHandler.onCreateOffer(selectedTradeCurrency);
offerActionHandler.onCreateOffer(selectedTradeCurrency, selectedPaymentMethod);
})
.secondaryActionButtonText(Res.get("offerbook.setupNewAccount"))
.onSecondaryAction(() -> {
@ -610,7 +611,7 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
}
createOfferButton.setDisable(true);
offerActionHandler.onCreateOffer(selectedTradeCurrency);
offerActionHandler.onCreateOffer(selectedTradeCurrency, selectedPaymentMethod);
}
}

View File

@ -566,7 +566,7 @@ class OfferBookViewModel extends ActivatableViewModel {
}
boolean canCreateOrTakeOffer() {
return GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation) &&
return GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation, selectedTradeCurrency) &&
GUIUtil.isChainHeightSyncedWithinToleranceOrShowPopup(walletsSetup) &&
GUIUtil.isBootstrappedOrShowPopup(p2PService);
}

View File

@ -29,6 +29,7 @@ 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;
@ -423,7 +424,8 @@ public class OfferDetailsWindow extends Overlay<OfferDetailsWindow> {
placeOfferTuple.fourth.getChildren().add(cancelButton);
button.setOnAction(e -> {
if (GUIUtil.canCreateOrTakeOfferOrShowPopup(user, navigation)) {
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)

View File

@ -63,7 +63,8 @@ public class DuplicateOfferView extends MutableOfferView<DuplicateOfferViewModel
public void initWithData(OfferPayload offerPayload) {
initWithData(offerPayload.getDirection(),
CurrencyUtil.getTradeCurrency(offerPayload.getCurrencyCode()).get());
CurrencyUtil.getTradeCurrency(offerPayload.getCurrencyCode()).get(),
null);
model.initWithData(offerPayload);
}
}

View File

@ -184,7 +184,8 @@ class EditOfferDataModel extends MutableOfferDataModel {
}
public void onPublishOffer(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
OfferPayload offerPayload = createAndGetOffer().getOfferPayload().orElseThrow();
Offer offer = createAndGetOffer();
OfferPayload offerPayload = offer.getOfferPayload().orElseThrow();
var mutableOfferPayloadFields = new MutableOfferPayloadFields(offerPayload);
OfferPayload editedPayload = offerUtil.getMergedOfferPayload(openOffer, mutableOfferPayloadFields);
Offer editedOffer = new Offer(editedPayload);

View File

@ -145,7 +145,8 @@ public class EditOfferView extends MutableOfferView<EditOfferViewModel> {
model.applyOpenOffer(openOffer);
initWithData(openOffer.getOffer().getDirection(),
CurrencyUtil.getTradeCurrency(openOffer.getOffer().getCurrencyCode()).get());
CurrencyUtil.getTradeCurrency(openOffer.getOffer().getCurrencyCode()).get(),
null);
model.onStartEditOffer(errorMessage -> {
log.error(errorMessage);

View File

@ -37,8 +37,8 @@ import bisq.desktop.util.GUIUtil;
import bisq.core.locale.Res;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferPayloadBase;
import bisq.core.offer.OpenOffer;
import bisq.core.offer.bisq_v1.OfferPayload;
import bisq.core.user.DontShowAgainLookup;
import com.googlecode.jcsv.writer.CSVEntryConverter;
@ -188,8 +188,8 @@ public class OpenOffersView extends ActivatableViewAndModel<VBox, OpenOffersView
MenuItem editItem = new MenuItem(Res.get("portfolio.context.offerLikeThis"));
editItem.setOnAction((event) -> {
try {
OfferPayload offerPayload = row.getItem().getOffer().getOfferPayload().orElseThrow();
navigation.navigateToWithData(offerPayload, MainView.class, PortfolioView.class,
OfferPayloadBase offerPayloadBase = row.getItem().getOffer().getOfferPayloadBase();
navigation.navigateToWithData(offerPayloadBase, MainView.class, PortfolioView.class,
DuplicateOfferView.class);
} catch (NullPointerException e) {
log.warn("Unable to get offerPayload - {}", e.toString());
@ -576,7 +576,7 @@ public class OpenOffersView extends ActivatableViewAndModel<VBox, OpenOffersView
super.updateItem(item, empty);
getStyleClass().removeAll("offer-disabled");
if (item != null) {
if (model.isDeactivated(item)) getStyleClass().add("offer-disabled");
if (model.isNotPublished(item)) getStyleClass().add("offer-disabled");
setGraphic(new AutoTooltipLabel(model.getTriggerPrice(item)));
} else {
setGraphic(null);

View File

@ -835,7 +835,7 @@ public class GUIUtil {
return true;
}
public static boolean canCreateOrTakeOfferOrShowPopup(User user, Navigation navigation) {
public static boolean canCreateOrTakeOfferOrShowPopup(User user, Navigation navigation, TradeCurrency currency) {
if (!user.hasAcceptedRefundAgents()) {
new Popup().warning(Res.get("popup.warning.noArbitratorsAvailable")).show();
return false;