Skip to content

Commit

Permalink
Documentation and cleanup of Offer related classes
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Rosseel <[email protected]>
  • Loading branch information
mrosseel committed Oct 13, 2017
1 parent 68048bc commit 0d41170
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
56 changes: 53 additions & 3 deletions core/src/main/java/io/bisq/core/offer/OfferUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,49 @@

import javax.annotation.Nullable;

/**
* This class holds utility methods for the creation of an Offer.
* Most of these are extracted here because they are used both in the GUI and in the API.
*
* Long-term there could be a GUI-agnostic OfferService which provides these and other functionalities to both the
* GUI and the API.
*/
public class OfferUtil {

/**
* Returns the makerfee as Coin, this can be priced in BTC or BSQ.
*
* @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) {
return getMakerFee(isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount, marketPriceAvailable, marketPriceMargin), amount, marketPriceAvailable, marketPriceMargin);
}

/**
* Given the direction, is this a BUY?
*
* @param direction
* @return
*/
public static boolean isBuyOffer(OfferPayload.Direction direction) {
return direction == OfferPayload.Direction.BUY;
}

/**
* 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, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) {
if (amount != null) {
Expand All @@ -59,18 +92,35 @@ public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, Coin amount, bo
}


/**
* Checks if the maker fee should be paid in BTC, this can be the case due to user preference or because the user
* doesn't have enough BSQ.
*
* @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) {
return preferences.getPayFeeInBtc() || !isBsqForFeeAvailable(bsqWalletService, amount, marketPriceAvailable, marketPriceMargin);
}

/**
* Checks if the available BSQ balance is sufficient to pay for the offer's maker fee.
*
* @param bsqWalletService
* @param amount
* @param marketPriceAvailable
* @param marketPriceMargin
* @return
*/
public static boolean isBsqForFeeAvailable(BsqWalletService bsqWalletService, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) {
return BisqEnvironment.isBaseCurrencySupportingBsq() &&
getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin) != null &&
bsqWalletService.getAvailableBalance() != null &&
getMakerFee(false , amount, marketPriceAvailable, marketPriceMargin) != null &&
!bsqWalletService.getAvailableBalance().subtract(getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin)).isNegative();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ Offer createAndGetOffer() {
String countryCode = paymentAccount instanceof CountryBasedPaymentAccount ? ((CountryBasedPaymentAccount) paymentAccount).getCountry().code : null;

checkNotNull(p2PService.getAddress(), "Address must not be null");
checkNotNull(OfferUtil.getMakerFee(bsqWalletService, preferences, Coin.valueOf(amount), marketPriceAvailable, marketPriceMargin), "makerFee must not be null");
checkNotNull(getMakerFee(), "makerFee must not be null");

long maxTradeLimit = paymentAccount.getPaymentMethod().getMaxTradeLimitAsCoin(currencyCode).value;
long maxTradePeriod = paymentAccount.getPaymentMethod().getMaxTradePeriod();
Expand Down Expand Up @@ -380,8 +380,8 @@ Offer createAndGetOffer() {
Version.VERSION,
btcWalletService.getLastBlockSeenHeight(),
txFeeFromFeeService.value,
OfferUtil.getMakerFee(bsqWalletService, preferences, Coin.valueOf(amount), marketPriceAvailable, marketPriceMargin).value,
OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, Coin.valueOf(amount), marketPriceAvailable, marketPriceMargin),
getMakerFee().value,
isCurrencyForMakerFeeBtc(),
buyerSecurityDepositAsCoin.value,
sellerSecurityDeposit.value,
maxTradeLimit,
Expand All @@ -400,7 +400,7 @@ Offer createAndGetOffer() {
}

void onPlaceOffer(Offer offer, TransactionResultHandler resultHandler) {
checkNotNull(OfferUtil.getMakerFee(bsqWalletService, preferences, this.amount.get(), marketPriceAvailable, marketPriceMargin), "makerFee must not be null");
checkNotNull(getMakerFee(), "makerFee must not be null");

Coin reservedFundsForOffer = getSecurityDeposit();
if (!isBuyOffer())
Expand Down Expand Up @@ -586,10 +586,10 @@ void calculateTotalToPay() {
// Maker does not pay the tx fee for the trade txs because the mining fee might be different when maker
// created the offer and reserved his funds, so that would not work well with dynamic fees.
// The mining fee for the createOfferFee tx is deducted from the createOfferFee and not visible to the trader
final Coin makerFee = OfferUtil.getMakerFee(bsqWalletService, preferences, this.amount.get(), marketPriceAvailable, marketPriceMargin);
final Coin makerFee = getMakerFee();
if (direction != null && amount.get() != null && makerFee != null) {
Coin feeAndSecDeposit = getTxFee().add(getSecurityDeposit());
if (OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin))
if (isCurrencyForMakerFeeBtc())
feeAndSecDeposit = feeAndSecDeposit.add(makerFee);
Coin total = isBuyOffer() ? feeAndSecDeposit : feeAndSecDeposit.add(amount.get());
totalToPayAsCoin.set(total);
Expand Down Expand Up @@ -645,10 +645,10 @@ private boolean isBalanceSufficient(Coin balance) {
}

public Coin getTxFee() {
if (OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin))
if (isCurrencyForMakerFeeBtc())
return txFeeFromFeeService;
else
return txFeeFromFeeService.subtract(OfferUtil.getMakerFee(bsqWalletService, preferences, this.amount.get(), marketPriceAvailable, marketPriceMargin));
return txFeeFromFeeService.subtract(getMakerFee());
}

public Preferences getPreferences() {
Expand Down

0 comments on commit 0d41170

Please sign in to comment.