Refactor EditOfferDataModel for new editoffer api method

- Define set of editable offer payload fields in MutableOfferPayloadFields.

- Move bulk of EditOfferDataModel#onPublishOffer logic to OfferUtil.
This commit is contained in:
ghubstan 2021-06-12 18:35:27 -03:00
parent 2b1a7aac82
commit 81da6fbe5a
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
3 changed files with 140 additions and 45 deletions

View File

@ -0,0 +1,89 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.offer;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.annotation.Nullable;
/**
* The set of editable OfferPayload fields.
*/
@Getter
@Setter
@ToString
public final class MutableOfferPayloadFields {
private final long price;
private final double marketPriceMargin;
private final boolean useMarketBasedPrice;
private final String baseCurrencyCode;
private final String counterCurrencyCode;
private final String paymentMethodId;
private final String makerPaymentAccountId;
@Nullable
private final String countryCode;
@Nullable
private final List<String> acceptedCountryCodes;
@Nullable
private final String bankId;
@Nullable
private final List<String> acceptedBankIds;
public MutableOfferPayloadFields(OfferPayload offerPayload) {
this(offerPayload.getPrice(),
offerPayload.getMarketPriceMargin(),
offerPayload.isUseMarketBasedPrice(),
offerPayload.getBaseCurrencyCode(),
offerPayload.getCounterCurrencyCode(),
offerPayload.getPaymentMethodId(),
offerPayload.getMakerPaymentAccountId(),
offerPayload.getCountryCode(),
offerPayload.getAcceptedCountryCodes(),
offerPayload.getBankId(),
offerPayload.getAcceptedBankIds());
}
public MutableOfferPayloadFields(long price,
double marketPriceMargin,
boolean useMarketBasedPrice,
String baseCurrencyCode,
String counterCurrencyCode,
String paymentMethodId,
String makerPaymentAccountId,
@Nullable String countryCode,
@Nullable List<String> acceptedCountryCodes,
@Nullable String bankId,
@Nullable List<String> acceptedBankIds) {
this.price = price;
this.marketPriceMargin = marketPriceMargin;
this.useMarketBasedPrice = useMarketBasedPrice;
this.baseCurrencyCode = baseCurrencyCode;
this.counterCurrencyCode = counterCurrencyCode;
this.paymentMethodId = paymentMethodId;
this.makerPaymentAccountId = makerPaymentAccountId;
this.countryCode = countryCode;
this.acceptedCountryCodes = acceptedCountryCodes;
this.bankId = bankId;
this.acceptedBankIds = acceptedBankIds;
}
}

View File

@ -364,6 +364,53 @@ public class OfferUtil {
Res.get("offerbook.warning.paymentMethodBanned"));
}
// Returns an edited payload: a merge of the original offerPayload and
// editedOfferPayload fields. Mutable fields are sourced from
// mutableOfferPayloadFields param, e.g., payment account details, price, etc.
// Immutable fields are sourced from the original openOffer payload param.
public OfferPayload getMergedOfferPayload(OpenOffer openOffer,
MutableOfferPayloadFields mutableOfferPayloadFields) {
OfferPayload originalOfferPayload = openOffer.getOffer().getOfferPayload();
return new OfferPayload(originalOfferPayload.getId(),
originalOfferPayload.getDate(),
originalOfferPayload.getOwnerNodeAddress(),
originalOfferPayload.getPubKeyRing(),
originalOfferPayload.getDirection(),
mutableOfferPayloadFields.getPrice(),
mutableOfferPayloadFields.getMarketPriceMargin(),
mutableOfferPayloadFields.isUseMarketBasedPrice(),
originalOfferPayload.getAmount(),
originalOfferPayload.getMinAmount(),
mutableOfferPayloadFields.getBaseCurrencyCode(),
mutableOfferPayloadFields.getCounterCurrencyCode(),
originalOfferPayload.getArbitratorNodeAddresses(),
originalOfferPayload.getMediatorNodeAddresses(),
mutableOfferPayloadFields.getPaymentMethodId(),
mutableOfferPayloadFields.getMakerPaymentAccountId(),
originalOfferPayload.getOfferFeePaymentTxId(),
mutableOfferPayloadFields.getCountryCode(),
mutableOfferPayloadFields.getAcceptedCountryCodes(),
mutableOfferPayloadFields.getBankId(),
mutableOfferPayloadFields.getAcceptedBankIds(),
originalOfferPayload.getVersionNr(),
originalOfferPayload.getBlockHeightAtOfferCreation(),
originalOfferPayload.getTxFee(),
originalOfferPayload.getMakerFee(),
originalOfferPayload.isCurrencyForMakerFeeBtc(),
originalOfferPayload.getBuyerSecurityDeposit(),
originalOfferPayload.getSellerSecurityDeposit(),
originalOfferPayload.getMaxTradeLimit(),
originalOfferPayload.getMaxTradePeriod(),
originalOfferPayload.isUseAutoClose(),
originalOfferPayload.isUseReOpenAfterAutoClose(),
originalOfferPayload.getLowerClosePrice(),
originalOfferPayload.getUpperClosePrice(),
originalOfferPayload.isPrivateOffer(),
originalOfferPayload.getHashOfChallenge(),
originalOfferPayload.getExtraDataMap(),
originalOfferPayload.getProtocolVersion());
}
private Optional<Volume> getFeeInUserFiatCurrency(Coin makerFee,
boolean isCurrencyForMakerFeeBtc,
String userCurrencyCode,

View File

@ -28,6 +28,7 @@ import bisq.core.btc.wallet.Restrictions;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.TradeCurrency;
import bisq.core.offer.CreateOfferService;
import bisq.core.offer.MutableOfferPayloadFields;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferPayload;
import bisq.core.offer.OfferUtil;
@ -182,54 +183,12 @@ class EditOfferDataModel extends MutableOfferDataModel {
}
public void onPublishOffer(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
// editedPayload is a merge of the original offerPayload and newOfferPayload
// fields which are editable are merged in from newOfferPayload (such as payment account details)
// fields which cannot change (most importantly BTC amount) are sourced from the original offerPayload
final OfferPayload offerPayload = openOffer.getOffer().getOfferPayload();
final OfferPayload newOfferPayload = createAndGetOffer().getOfferPayload();
final OfferPayload editedPayload = new OfferPayload(offerPayload.getId(),
offerPayload.getDate(),
offerPayload.getOwnerNodeAddress(),
offerPayload.getPubKeyRing(),
offerPayload.getDirection(),
newOfferPayload.getPrice(),
newOfferPayload.getMarketPriceMargin(),
newOfferPayload.isUseMarketBasedPrice(),
offerPayload.getAmount(),
offerPayload.getMinAmount(),
newOfferPayload.getBaseCurrencyCode(),
newOfferPayload.getCounterCurrencyCode(),
offerPayload.getArbitratorNodeAddresses(),
offerPayload.getMediatorNodeAddresses(),
newOfferPayload.getPaymentMethodId(),
newOfferPayload.getMakerPaymentAccountId(),
offerPayload.getOfferFeePaymentTxId(),
newOfferPayload.getCountryCode(),
newOfferPayload.getAcceptedCountryCodes(),
newOfferPayload.getBankId(),
newOfferPayload.getAcceptedBankIds(),
offerPayload.getVersionNr(),
offerPayload.getBlockHeightAtOfferCreation(),
offerPayload.getTxFee(),
offerPayload.getMakerFee(),
offerPayload.isCurrencyForMakerFeeBtc(),
offerPayload.getBuyerSecurityDeposit(),
offerPayload.getSellerSecurityDeposit(),
offerPayload.getMaxTradeLimit(),
offerPayload.getMaxTradePeriod(),
offerPayload.isUseAutoClose(),
offerPayload.isUseReOpenAfterAutoClose(),
offerPayload.getLowerClosePrice(),
offerPayload.getUpperClosePrice(),
offerPayload.isPrivateOffer(),
offerPayload.getHashOfChallenge(),
offerPayload.getExtraDataMap(),
offerPayload.getProtocolVersion());
MutableOfferPayloadFields mutableOfferPayloadFields =
new MutableOfferPayloadFields(createAndGetOffer().getOfferPayload());
final OfferPayload editedPayload = offerUtil.getMergedOfferPayload(openOffer, mutableOfferPayloadFields);
final Offer editedOffer = new Offer(editedPayload);
editedOffer.setPriceFeedService(priceFeedService);
editedOffer.setState(Offer.State.AVAILABLE);
openOfferManager.editOpenOfferPublish(editedOffer, triggerPrice, initialState, () -> {
openOffer = null;
resultHandler.handleResult();