Add pricefeed to offer, display offerprice with perc. (WIP)

This commit is contained in:
Manfred Karrer 2016-04-15 00:35:31 +02:00
parent 7fe4d37501
commit 13a62b1342
11 changed files with 115 additions and 38 deletions

View file

@ -301,7 +301,8 @@ class CreateOfferDataModel extends ActivatableDataModel {
countryCode,
acceptedCountryCodes,
bankId,
acceptedBanks);
acceptedBanks,
priceFeed);
}
void onPlaceOffer(Offer offer, TransactionResultHandler resultHandler) {

View file

@ -117,6 +117,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
private Timer timeoutTimer;
private PriceFeed.Type priceFeedType;
private boolean priceAsPercentageIsInput;
private ChangeListener<Boolean> usePercentageBasedPriceListener;
///////////////////////////////////////////////////////////////////////////////////////////
@ -239,18 +240,20 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
calculateVolume();
dataModel.calculateTotalToPay();
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
if (marketPrice != null) {
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
try {
double priceAsDouble = formatter.parseNumberStringToDouble(price.get());
double priceFactor = priceAsDouble / marketPriceAsDouble;
priceFactor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - priceFactor : 1 + priceFactor;
priceAsPercentage.set(formatter.formatToPercent(priceFactor, 2));
} catch (NumberFormatException t) {
priceAsPercentage.set("");
new Popup().warning("Your input is not a valid number.")
.show();
if (!priceAsPercentageIsInput) {
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
if (marketPrice != null) {
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
try {
double priceAsDouble = formatter.parseNumberStringToDouble(price.get());
double priceFactor = priceAsDouble / marketPriceAsDouble;
priceFactor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - priceFactor : 1 + priceFactor;
priceAsPercentage.set(formatter.formatToPercent(priceFactor, 2));
} catch (NumberFormatException t) {
priceAsPercentage.set("");
new Popup().warning("Your input is not a valid number.")
.show();
}
}
}
}
@ -295,6 +298,11 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
}
}
};
usePercentageBasedPriceListener = (observable, oldValue, newValue) -> {
if (newValue)
priceValidationResult.set(new InputValidator.ValidationResult(true));
};
volumeListener = (ov, oldValue, newValue) -> {
if (isFiatInputValid(newValue).isValid) {
setVolumeToModel();
@ -324,6 +332,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
minAmount.addListener(minAmountListener);
price.addListener(priceListener);
priceAsPercentage.addListener(priceAsPercentageListener);
dataModel.usePercentageBasedPrice.addListener(usePercentageBasedPriceListener);
volume.addListener(volumeListener);
// Binding with Bindings.createObjectBinding does not work because of bi-directional binding
@ -341,6 +350,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
minAmount.removeListener(minAmountListener);
price.removeListener(priceListener);
priceAsPercentage.removeListener(priceAsPercentageListener);
dataModel.usePercentageBasedPrice.removeListener(usePercentageBasedPriceListener);
volume.removeListener(volumeListener);
// Binding with Bindings.createObjectBinding does not work because of bi-directional binding
@ -366,7 +376,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
if (dataModel.paymentAccount != null)
btcValidator.setMaxTradeLimitInBitcoin(dataModel.paymentAccount.getPaymentMethod().getMaxTradeLimit());
priceFeedType = direction == Offer.Direction.BUY ? PriceFeed.Type.ASK : PriceFeed.Type.BID;
priceFeedType = direction == Offer.Direction.SELL ? PriceFeed.Type.ASK : PriceFeed.Type.BID;
return result;
}
@ -565,7 +575,8 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
Fiat priceAsFiat = dataModel.priceAsFiat.get();
long shiftDivisor = checkedPow(10, priceAsFiat.smallestUnitExponent());
double offerPrice = ((double) priceAsFiat.longValue()) / ((double) shiftDivisor);
if (marketPriceAsDouble != 0 && Math.abs(1 - (offerPrice / marketPriceAsDouble)) > preferences.getMaxPriceDistanceInPercent()) {
double percentage = Math.abs(1 - (offerPrice / marketPriceAsDouble));
if (marketPriceAsDouble != 0 && percentage > preferences.getMaxPriceDistanceInPercent()) {
displayPriceOutofRangePopup();
return false;
} else {

View file

@ -257,10 +257,19 @@ class OfferBookViewModel extends ActivatableViewModel {
}
String getPrice(OfferBookListItem item) {
if ((item == null))
return "";
Offer offer = item.getOffer();
Fiat price = offer.getPrice();
String postFix = "";
if (offer.isUsePercentageBasedPrice()) {
postFix = " (" + formatter.formatToPercentWithSymbol(offer.getMarketPriceMargin()) + ")";
}
if (showAllTradeCurrenciesProperty.get())
return (item != null) ? formatter.formatFiatWithCode(item.getOffer().getPrice()) : "";
return formatter.formatPriceWithCode(price) + postFix;
else
return (item != null) ? formatter.formatFiat(item.getOffer().getPrice()) : "";
return formatter.formatFiat(price) + postFix;
}
String getVolume(OfferBookListItem item) {

View file

@ -18,6 +18,7 @@
package io.bitsquare.gui.main.offer.takeoffer;
import io.bitsquare.arbitration.Arbitrator;
import io.bitsquare.btc.pricefeed.PriceFeed;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.common.model.ActivatableWithDataModel;
import io.bitsquare.gui.common.model.ViewModel;
@ -52,6 +53,7 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
final TakeOfferDataModel dataModel;
private final BtcValidator btcValidator;
private final P2PService p2PService;
private PriceFeed priceFeed;
private final Navigation navigation;
final BSFormatter formatter;
@ -101,13 +103,14 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public TakeOfferViewModel(TakeOfferDataModel dataModel, BtcValidator btcValidator, P2PService p2PService,
public TakeOfferViewModel(TakeOfferDataModel dataModel, BtcValidator btcValidator, P2PService p2PService, PriceFeed priceFeed,
Navigation navigation, BSFormatter formatter) {
super(dataModel);
this.dataModel = dataModel;
this.btcValidator = btcValidator;
this.p2PService = p2PService;
this.priceFeed = priceFeed;
this.navigation = navigation;
this.formatter = formatter;

View file

@ -277,6 +277,7 @@ public class OfferBookViewModelTest {
countryCode,
acceptedCountryCodes,
bankId,
acceptedBanks);
acceptedBanks,
null);
}
}