Skip to content

Commit

Permalink
Merge pull request #2212 from ManfredKarrer/simplify-fee-model
Browse files Browse the repository at this point in the history
Simplify fee model
  • Loading branch information
ManfredKarrer authored Jan 7, 2019
2 parents 5553b3b + f0eb12d commit c947b85
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 66 deletions.
42 changes: 9 additions & 33 deletions core/src/main/java/bisq/core/offer/OfferUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,45 +73,26 @@ public static boolean isBuyOffer(OfferPayload.Direction direction) {
* @param bsqWalletService
* @param preferences preferences are used to see if the user indicated a preference for paying fees in BTC
* @param amount
* @param marketPriceAvailable
* @param marketPriceMargin
* @return
*/
@Nullable
public static Coin getMakerFee(BsqWalletService bsqWalletService, Preferences preferences, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) {
final boolean isCurrencyForMakerFeeBtc = isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount, marketPriceAvailable, marketPriceMargin);
return getMakerFee(isCurrencyForMakerFeeBtc,
amount,
marketPriceAvailable,
marketPriceMargin);
public static Coin getMakerFee(BsqWalletService bsqWalletService, Preferences preferences, Coin amount) {
boolean isCurrencyForMakerFeeBtc = isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount);
return getMakerFee(isCurrencyForMakerFeeBtc, amount);
}

/**
* Calculates the maker fee for the given amount, marketPrice and marketPriceMargin.
*
* @param isCurrencyForMakerFeeBtc
* @param amount
* @param marketPriceAvailable
* @param marketPriceMargin
* @return
*/
@Nullable
public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, @Nullable Coin amount, boolean marketPriceAvailable, double marketPriceMargin) {
public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, @Nullable Coin amount) {
if (amount != null) {
Coin feePerBtc = CoinUtil.getFeePerBtc(FeeService.getMakerFeePerBtc(isCurrencyForMakerFeeBtc), amount);
double makerFeeAsDouble = (double) feePerBtc.value;
if (marketPriceAvailable) {
if (marketPriceMargin > 0)
makerFeeAsDouble = makerFeeAsDouble * Math.sqrt(marketPriceMargin * 100);
else
makerFeeAsDouble = 0;

// For BTC we round so min value change is 100 satoshi
if (isCurrencyForMakerFeeBtc)
makerFeeAsDouble = MathUtils.roundDouble(makerFeeAsDouble / 100, 0) * 100;
}

return CoinUtil.maxCoin(Coin.valueOf(MathUtils.doubleToLong(makerFeeAsDouble)), FeeService.getMinMakerFee(isCurrencyForMakerFeeBtc));
return CoinUtil.maxCoin(feePerBtc, FeeService.getMinMakerFee(isCurrencyForMakerFeeBtc));
} else {
return null;
}
Expand All @@ -124,14 +105,11 @@ public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, @Nullable Coin
* @param preferences
* @param bsqWalletService
* @param amount
* @param marketPriceAvailable
* @param marketPriceMargin
* @return
*/
public static boolean isCurrencyForMakerFeeBtc(Preferences preferences, BsqWalletService bsqWalletService, Coin amount,
boolean marketPriceAvailable, double marketPriceMargin) {
public static boolean isCurrencyForMakerFeeBtc(Preferences preferences, BsqWalletService bsqWalletService, Coin amount) {
boolean payFeeInBtc = preferences.getPayFeeInBtc();
boolean bsqForFeeAvailable = isBsqForMakerFeeAvailable(bsqWalletService, amount, marketPriceAvailable, marketPriceMargin);
boolean bsqForFeeAvailable = isBsqForMakerFeeAvailable(bsqWalletService, amount);
return payFeeInBtc || !bsqForFeeAvailable;
}

Expand All @@ -140,13 +118,11 @@ public static boolean isCurrencyForMakerFeeBtc(Preferences preferences, BsqWalle
*
* @param bsqWalletService
* @param amount
* @param marketPriceAvailable
* @param marketPriceMargin
* @return
*/
public static boolean isBsqForMakerFeeAvailable(BsqWalletService bsqWalletService, @Nullable Coin amount, boolean marketPriceAvailable, double marketPriceMargin) {
public static boolean isBsqForMakerFeeAvailable(BsqWalletService bsqWalletService, @Nullable Coin amount) {
Coin availableBalance = bsqWalletService.getAvailableBalance();
Coin makerFee = getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin);
Coin makerFee = getMakerFee(false, amount);

// If we don't know yet the maker fee (amount is not set) we return true, otherwise we would disable BSQ
// fee each time we open the create offer screen as there the amount is not set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.BSFormatter;
import bisq.core.util.BsqFormatter;

import bisq.network.p2p.P2PService;

Expand Down Expand Up @@ -110,7 +109,6 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs
private final TradeWalletService tradeWalletService;
private final FeeService feeService;
private final ReferralIdService referralIdService;
private final BsqFormatter bsqFormatter;
private final BSFormatter btcFormatter;
private final String offerId;
private final BalanceListener btcBalanceListener;
Expand All @@ -137,10 +135,10 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs
protected PaymentAccount paymentAccount;
protected boolean isTabSelected;
protected double marketPriceMargin = 0;
protected Coin txFeeFromFeeService = Coin.ZERO;
protected boolean marketPriceAvailable;
protected int feeTxSize = 260; // size of typical tx with 1 input
protected int feeTxSizeEstimationRecursionCounter;
private Coin txFeeFromFeeService = Coin.ZERO;
private boolean marketPriceAvailable;
private int feeTxSize = 260; // size of typical tx with 1 input
private int feeTxSizeEstimationRecursionCounter;
protected boolean allowAmountUpdate = true;


Expand All @@ -153,7 +151,7 @@ public MutableOfferDataModel(OpenOfferManager openOfferManager, BtcWalletService
Preferences preferences, User user, KeyRing keyRing, P2PService p2PService,
PriceFeedService priceFeedService, FilterManager filterManager,
AccountAgeWitnessService accountAgeWitnessService, TradeWalletService tradeWalletService,
FeeService feeService, ReferralIdService referralIdService, BsqFormatter bsqFormatter,
FeeService feeService, ReferralIdService referralIdService,
BSFormatter btcFormatter) {
super(btcWalletService);

Expand All @@ -169,7 +167,6 @@ public MutableOfferDataModel(OpenOfferManager openOfferManager, BtcWalletService
this.tradeWalletService = tradeWalletService;
this.feeService = feeService;
this.referralIdService = referralIdService;
this.bsqFormatter = bsqFormatter;
this.btcFormatter = btcFormatter;

offerId = Utilities.getRandomPrefix(5, 8) + "-" +
Expand Down Expand Up @@ -328,11 +325,9 @@ Offer createAndGetOffer() {

ArrayList<String> acceptedCountryCodes = null;
if (paymentAccount instanceof SepaAccount) {
acceptedCountryCodes = new ArrayList<>();
acceptedCountryCodes.addAll(((SepaAccount) paymentAccount).getAcceptedCountryCodes());
acceptedCountryCodes = new ArrayList<>(((SepaAccount) paymentAccount).getAcceptedCountryCodes());
} else if (paymentAccount instanceof SepaInstantAccount) {
acceptedCountryCodes = new ArrayList<>();
acceptedCountryCodes.addAll(((SepaInstantAccount) paymentAccount).getAcceptedCountryCodes());
acceptedCountryCodes = new ArrayList<>(((SepaInstantAccount) paymentAccount).getAcceptedCountryCodes());
} else if (paymentAccount instanceof CountryBasedPaymentAccount) {
acceptedCountryCodes = new ArrayList<>();
acceptedCountryCodes.add(((CountryBasedPaymentAccount) paymentAccount).getCountry().code);
Expand Down Expand Up @@ -825,31 +820,31 @@ public void setMarketPriceAvailable(boolean marketPriceAvailable) {
}

public Coin getMakerFee(boolean isCurrencyForMakerFeeBtc) {
return OfferUtil.getMakerFee(isCurrencyForMakerFeeBtc, amount.get(), marketPriceAvailable, marketPriceMargin);
return OfferUtil.getMakerFee(isCurrencyForMakerFeeBtc, amount.get());
}

public Coin getMakerFee() {
return OfferUtil.getMakerFee(bsqWalletService, preferences, amount.get(), marketPriceAvailable, marketPriceMargin);
return OfferUtil.getMakerFee(bsqWalletService, preferences, amount.get());
}

public Coin getMakerFeeInBtc() {
return OfferUtil.getMakerFee(true, amount.get(), marketPriceAvailable, marketPriceMargin);
return OfferUtil.getMakerFee(true, amount.get());
}

public Coin getMakerFeeInBsq() {
return OfferUtil.getMakerFee(false, amount.get(), marketPriceAvailable, marketPriceMargin);
return OfferUtil.getMakerFee(false, amount.get());
}

public boolean isCurrencyForMakerFeeBtc() {
return OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin);
return OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get());
}

public boolean isPreferredFeeCurrencyBtc() {
return preferences.isPayFeeInBtc();
}

public boolean isBsqForFeeAvailable() {
return OfferUtil.isBsqForMakerFeeAvailable(bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin);
return OfferUtil.isBsqForMakerFeeAvailable(bsqWalletService, amount.get());
}

public boolean isHalCashAccount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.BSFormatter;
import bisq.core.util.BsqFormatter;

import bisq.network.p2p.P2PService;

Expand Down Expand Up @@ -64,7 +63,6 @@ public CreateOfferDataModel(OpenOfferManager openOfferManager,
TradeWalletService tradeWalletService,
FeeService feeService,
ReferralIdService referralIdService,
BsqFormatter bsqFormatter,
BSFormatter btcFormatter) {
super(openOfferManager,
btcWalletService,
Expand All @@ -79,7 +77,6 @@ public CreateOfferDataModel(OpenOfferManager openOfferManager,
tradeWalletService,
feeService,
referralIdService,
bsqFormatter,
btcFormatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.BSFormatter;
import bisq.core.util.BsqFormatter;

import bisq.network.p2p.P2PService;

Expand All @@ -49,6 +48,8 @@

import com.google.inject.Inject;

import java.util.Optional;

class EditOfferDataModel extends MutableOfferDataModel {

private final CorePersistenceProtoResolver corePersistenceProtoResolver;
Expand All @@ -69,7 +70,6 @@ class EditOfferDataModel extends MutableOfferDataModel {
TradeWalletService tradeWalletService,
FeeService feeService,
ReferralIdService referralIdService,
BsqFormatter bsqFormatter,
BSFormatter btcFormatter,
CorePersistenceProtoResolver corePersistenceProtoResolver) {
super(openOfferManager,
Expand All @@ -85,7 +85,6 @@ class EditOfferDataModel extends MutableOfferDataModel {
tradeWalletService,
feeService,
referralIdService,
bsqFormatter,
btcFormatter);
this.corePersistenceProtoResolver = corePersistenceProtoResolver;
}
Expand Down Expand Up @@ -117,14 +116,15 @@ public void applyOpenOffer(OpenOffer openOffer) {

this.initialState = openOffer.getState();
PaymentAccount tmpPaymentAccount = user.getPaymentAccount(openOffer.getOffer().getMakerPaymentAccountId());
TradeCurrency selectedTradeCurrency = CurrencyUtil.getTradeCurrency(openOffer.getOffer().getCurrencyCode()).get();

this.paymentAccount = PaymentAccount.fromProto(tmpPaymentAccount.toProtoMessage(), corePersistenceProtoResolver);

if (paymentAccount.getSingleTradeCurrency() != null)
paymentAccount.setSingleTradeCurrency(selectedTradeCurrency);
else
paymentAccount.setSelectedTradeCurrency(selectedTradeCurrency);
Optional<TradeCurrency> optionalTradeCurrency = CurrencyUtil.getTradeCurrency(openOffer.getOffer().getCurrencyCode());
if (optionalTradeCurrency.isPresent() && tmpPaymentAccount != null) {
TradeCurrency selectedTradeCurrency = optionalTradeCurrency.get();
this.paymentAccount = PaymentAccount.fromProto(tmpPaymentAccount.toProtoMessage(), corePersistenceProtoResolver);
if (paymentAccount.getSingleTradeCurrency() != null)
paymentAccount.setSingleTradeCurrency(selectedTradeCurrency);
else
paymentAccount.setSelectedTradeCurrency(selectedTradeCurrency);
}

allowAmountUpdate = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void setUp() {
when(bsqFormatter.formatCoin(any())).thenReturn("0");
when(bsqWalletService.getAvailableBalance()).thenReturn(Coin.ZERO);

CreateOfferDataModel dataModel = new CreateOfferDataModel(null, btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, null, feeService, null, bsqFormatter, bsFormatter);
CreateOfferDataModel dataModel = new CreateOfferDataModel(null, btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, null, feeService, null, bsFormatter);
dataModel.initWithData(OfferPayload.Direction.BUY, new CryptoCurrency("BTC", "bitcoin"));
dataModel.activate();

Expand Down

0 comments on commit c947b85

Please sign in to comment.