Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to duplicate BSQ swap offers #5886

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.monetary.Price;
import bisq.core.monetary.Volume;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.OfferUtil;
import bisq.core.payment.payload.PaymentMethod;
Expand Down Expand Up @@ -124,10 +125,20 @@ public BsqSwapOfferModel(OfferUtil offerUtil,
// API
///////////////////////////////////////////////////////////////////////////////////////////

public void init(OfferDirection direction, boolean isMaker) {
public void init(OfferDirection direction, boolean isMaker, @Nullable Offer offer) {
this.direction = direction;
this.isMaker = isMaker;

if (offer != null) {
setPrice(offer.getPrice());

setBtcAmount(Coin.valueOf(Math.min(offer.getAmount().value, getMaxTradeLimit())));
calculateVolumeForAmount(getBtcAmount());

setMinAmount(offer.getMinAmount());
calculateMinVolume();
}

createListeners();
applyTxFeePerVbyte();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,9 @@ public BsqSwapTakeOfferModel(OfferUtil offerUtil,
///////////////////////////////////////////////////////////////////////////////////////////

public void initWithData(Offer offer) {
super.init(offer.getDirection(), false);
super.init(offer.getDirection(), false, offer);

this.offer = offer;
setPrice(offer.getPrice());

setBtcAmount(Coin.valueOf(Math.min(offer.getAmount().value, getMaxTradeLimit())));
calculateVolumeForAmount(getBtcAmount());

setMinAmount(offer.getMinAmount());
calculateMinVolume();

offer.resetState();
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ shared.BTCMinMax=BTC (min - max)
shared.removeOffer=Remove offer
shared.dontRemoveOffer=Don't remove offer
shared.editOffer=Edit offer
shared.duplicateOffer=Duplicate offer
shared.openLargeQRWindow=Open large QR code window
shared.tradingAccount=Trading account
shared.faq=Visit FAQ page
Expand Down
9 changes: 6 additions & 3 deletions desktop/src/main/java/bisq/desktop/main/offer/OfferView.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import bisq.core.locale.TradeCurrency;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.bsq_swap.BsqSwapOfferPayload;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
import bisq.core.user.Preferences;
Expand All @@ -56,6 +57,8 @@
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Nullable;

public abstract class OfferView extends ActivatableView<TabPane, Void> {

private OfferBookView offerBookView;
Expand Down Expand Up @@ -102,7 +105,7 @@ protected OfferView(ViewLoader viewLoader,
protected void initialize() {
navigationListener = (viewPath, data) -> {
if (viewPath.size() == 3 && viewPath.indexOf(this.getClass()) == 1)
loadView(viewPath.tip());
loadView(viewPath.tip(), data);
};
tabChangeListener = (observableValue, oldValue, newValue) -> {
if (newValue != null) {
Expand Down Expand Up @@ -198,7 +201,7 @@ private String getTakeOfferTabName() {
return Res.get("offerbook.takeOffer").toUpperCase();
}

private void loadView(Class<? extends View> viewClass) {
private void loadView(Class<? extends View> viewClass, @Nullable Object data) {
TabPane tabPane = root;
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);
View view;
Expand Down Expand Up @@ -237,7 +240,7 @@ private void loadView(Class<? extends View> viewClass) {
} else if (viewClass == BsqSwapCreateOfferView.class && bsqSwapCreateOfferView == null) {
view = viewLoader.load(viewClass);
bsqSwapCreateOfferView = (BsqSwapCreateOfferView) view;
bsqSwapCreateOfferView.initWithData(direction, offerActionHandler);
bsqSwapCreateOfferView.initWithData(direction, offerActionHandler, (BsqSwapOfferPayload) data);
createOfferPane = bsqSwapCreateOfferView.getRoot();
createOfferTab = new Tab(getCreateOfferTabName(viewClass));
createOfferTab.setClosable(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import bisq.core.offer.OfferDirection;
import bisq.core.offer.OfferUtil;
import bisq.core.offer.bsq_swap.BsqSwapOfferModel;
import bisq.core.offer.bsq_swap.BsqSwapOfferPayload;
import bisq.core.offer.bsq_swap.OpenBsqSwapOfferService;
import bisq.core.payment.PaymentAccount;
import bisq.core.user.User;
Expand All @@ -52,6 +53,8 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Comparator.comparing;

Expand Down Expand Up @@ -91,8 +94,8 @@ class BsqSwapCreateOfferDataModel extends BsqSwapOfferDataModel {
// API
///////////////////////////////////////////////////////////////////////////////////////////

void initWithData(OfferDirection direction) {
bsqSwapOfferModel.init(direction, true);
void initWithData(OfferDirection direction, @Nullable BsqSwapOfferPayload offerPayload) {
bsqSwapOfferModel.init(direction, true, offerPayload != null ? new Offer(offerPayload) : null);

fillPaymentAccounts();
applyPaymentAccount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.bsq_swap.BsqSwapOfferPayload;
import bisq.core.payment.PaymentAccount;
import bisq.core.user.DontShowAgainLookup;

Expand Down Expand Up @@ -74,6 +75,8 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

import static bisq.core.offer.bsq_swap.BsqSwapOfferModel.BSQ;
import static bisq.desktop.util.FormBuilder.*;

Expand Down Expand Up @@ -159,9 +162,12 @@ protected void deactivate() {
// API
///////////////////////////////////////////////////////////////////////////////////////////

public void initWithData(OfferDirection direction, OfferView.OfferActionHandler offerActionHandler) {
public void initWithData(OfferDirection direction,
OfferView.OfferActionHandler offerActionHandler,
@Nullable BsqSwapOfferPayload offerPayload) {
this.offerActionHandler = offerActionHandler;
model.initWithData(direction);

model.initWithData(offerPayload != null ? offerPayload.getDirection() : direction, offerPayload);

if (model.dataModel.isBuyOffer()) {
actionButton.setId("buy-button-big");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import bisq.core.monetary.Volume;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.OfferRestrictions;
import bisq.core.offer.bsq_swap.BsqSwapOfferPayload;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.util.FormattingUtils;
import bisq.core.util.VolumeUtil;
Expand Down Expand Up @@ -60,6 +61,8 @@

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

import static bisq.core.offer.bsq_swap.BsqSwapOfferModel.BSQ;

@Slf4j
Expand Down Expand Up @@ -144,6 +147,8 @@ protected void activate() {
addBindings();
addListeners();

maybeInitializeWithData();

updateButtonDisableState();
}

Expand All @@ -159,8 +164,8 @@ protected void deactivate() {
// API
///////////////////////////////////////////////////////////////////////////////////////////

void initWithData(OfferDirection direction) {
dataModel.initWithData(direction);
void initWithData(OfferDirection direction, @Nullable BsqSwapOfferPayload offerPayload) {
dataModel.initWithData(direction, offerPayload);

btcValidator.setMaxValue(PaymentMethod.BSQ_SWAP.getMaxTradeLimitAsCoin(BSQ));
btcValidator.setMaxTradeLimit(Coin.valueOf(dataModel.getMaxTradeLimit()));
Expand Down Expand Up @@ -572,6 +577,33 @@ private void updateButtonDisableState() {
isPlaceOfferButtonDisabled.set(createOfferRequested || !inputDataValid || miningPoW.get());
}

private void maybeInitializeWithData() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to maybeApplyFromDuplicateOffer or the like, to make the use case more clear. I check if the offer is null at the caller would also help to make it more clear when its applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the naming more general, as I though that there could be other use cases as well. Like some onboarding form in the future that automatic pre-fills a buy BSQ form.
Regarding the offer null check. Where do you want to see this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to have the if check for null could be done at the caller of maybeInitializeWithData and then its not a maybe anymore... but no strong opinion either... It just took a bit more effort to understand the code that ways as one need to jump to the method to see whats really going on there to see that its only an optional case if the param is null.

ObjectProperty<Coin> btcMinAmount = dataModel.getMinAmount();
if (btcMinAmount.get() != null) {
minAmountAsCoinListener.changed(btcMinAmount, null, btcMinAmount.get());
}

ObjectProperty<Coin> btcAmount = dataModel.getBtcAmount();

if (btcAmount.get() != null && btcMinAmount.get() != null) {
syncMinAmountWithAmount = btcMinAmount.get().equals(dataModel.getBtcAmount().get());
}

if (btcAmount.get() != null) {
amountAsCoinListener.changed(btcAmount, null, btcAmount.get());
}

ObjectProperty<Price> price = dataModel.getPrice();
if (price.get() != null) {
priceListener.changed(price, null, price.get());
}

ObjectProperty<Volume> volume = dataModel.getVolume();
if (volume.get() != null) {
volumeListener.changed(volume, null, volume.get());
}
}

private void stopTimeoutTimer() {
if (timeoutTimer != null) {
timeoutTimer.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<TableColumn fx:id="sellerSecurityDepositColumn" visible="false" minWidth="75"/>
<TableColumn fx:id="directionColumn" minWidth="70"/>
<TableColumn fx:id="stateColumn" minWidth="80"/>
<TableColumn fx:id="duplicateColumn" minWidth="30" maxWidth="30" sortable="false"/>
<TableColumn fx:id="avatarColumn" minWidth="40" maxWidth="40"/>
</columns>
</TableView>
Expand Down
Loading