Documentation and cleanup of Offer related classes

Signed-off-by: Mike Rosseel <mike@eon-consult.be>
This commit is contained in:
Mike Rosseel 2017-10-13 14:40:18 +02:00
parent 68048bc2d3
commit 0d411707e5
No known key found for this signature in database
GPG Key ID: 1D9920AF2A8E6BF0
2 changed files with 61 additions and 11 deletions

View File

@ -27,16 +27,49 @@ import org.bitcoinj.core.Coin;
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) {
@ -59,10 +92,30 @@ public class OfferUtil {
}
/**
* 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 &&
@ -70,7 +123,4 @@ public class OfferUtil {
getMakerFee(false , amount, marketPriceAvailable, marketPriceMargin) != null &&
!bsqWalletService.getAvailableBalance().subtract(getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin)).isNegative();
}
}

View File

@ -328,7 +328,7 @@ class CreateOfferDataModel extends ActivatableDataModel {
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();
@ -380,8 +380,8 @@ class CreateOfferDataModel extends ActivatableDataModel {
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,
@ -400,7 +400,7 @@ class CreateOfferDataModel extends ActivatableDataModel {
}
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())
@ -586,10 +586,10 @@ class CreateOfferDataModel extends ActivatableDataModel {
// 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);
@ -645,10 +645,10 @@ class CreateOfferDataModel extends ActivatableDataModel {
}
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() {