use Fiat for price

This commit is contained in:
Manfred Karrer 2014-08-30 02:00:01 +02:00
parent c8699dd66b
commit 454dc94965
12 changed files with 47 additions and 60 deletions

View file

@ -36,7 +36,7 @@ public class OfferListItem {
this.offer = offer; this.offer = offer;
this.date.set(BSFormatter.formatDateTime(offer.getCreationDate())); this.date.set(BSFormatter.formatDateTime(offer.getCreationDate()));
this.price.set(BSFormatter.formatPrice(offer.getPrice())); this.price.set(BSFormatter.formatFiat(offer.getPrice()));
this.amount.set(BSFormatter.formatCoin( this.amount.set(BSFormatter.formatCoin(
offer.getAmount()) + " (" + BSFormatter.formatCoin(offer.getMinAmount()) + ")"); offer.getAmount()) + " (" + BSFormatter.formatCoin(offer.getMinAmount()) + ")");

View file

@ -279,7 +279,7 @@ public class PendingTradeController extends CachedViewController {
secondaryBankAccountIDTitleLabel.setText("Refunded collateral:"); secondaryBankAccountIDTitleLabel.setText("Refunded collateral:");
bankAccountTypeTextField.setText(BSFormatter.formatCoinWithCode(trade.getTradeAmount())); bankAccountTypeTextField.setText(BSFormatter.formatCoinWithCode(trade.getTradeAmount()));
holderNameTextField.setText(BSFormatter.formatVolume(trade.getTradeVolume())); holderNameTextField.setText(BSFormatter.formatFiat(trade.getTradeVolume()));
primaryBankAccountIDTextField.setText( primaryBankAccountIDTextField.setText(
BSFormatter.formatCoinWithCode(FeePolicy.CREATE_OFFER_FEE.add(FeePolicy.TX_FEE))); BSFormatter.formatCoinWithCode(FeePolicy.CREATE_OFFER_FEE.add(FeePolicy.TX_FEE)));
secondaryBankAccountIDTextField.setText(BSFormatter.formatCoinWithCode(trade.getCollateralAmount())); secondaryBankAccountIDTextField.setText(BSFormatter.formatCoinWithCode(trade.getCollateralAmount()));

View file

@ -90,7 +90,7 @@ public class TradeController extends CachedViewController {
((TabPane) root).getTabs().addListener((ListChangeListener<Tab>) change -> { ((TabPane) root).getTabs().addListener((ListChangeListener<Tab>) change -> {
change.next(); change.next();
List<? extends Tab> removedTabs = change.getRemoved(); List<? extends Tab> removedTabs = change.getRemoved();
if (removedTabs.size() == 1 && createOfferView.equals(removedTabs.get(0).getContent())) { if (removedTabs.size() == 1 && removedTabs.get(0).getContent().equals(createOfferView)) {
if (createOfferCodeBehind != null) { if (createOfferCodeBehind != null) {
createOfferCodeBehind.terminate(); createOfferCodeBehind.terminate();
createOfferCodeBehind = null; createOfferCodeBehind = null;

View file

@ -45,6 +45,7 @@ import io.bitsquare.util.Utilities;
import com.google.bitcoin.core.Coin; import com.google.bitcoin.core.Coin;
import com.google.bitcoin.core.InsufficientMoneyException; import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.Transaction; import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.utils.Fiat;
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
@ -549,7 +550,8 @@ public class OrderBookController extends CachedViewController {
private void updateVolume() { private void updateVolume() {
double a = textInputToNumber(amount.getText(), amount.getText()); double a = textInputToNumber(amount.getText(), amount.getText());
double p = textInputToNumber(price.getText(), price.getText()); double p = textInputToNumber(price.getText(), price.getText());
volume.setText(BSFormatter.formatPrice(a * p)); //TODO
volume.setText(BSFormatter.formatFiat(Fiat.valueOf("EUR", (long) (a * p))));
} }
public void onCreateOfferViewRemoved() { public void onCreateOfferViewRemoved() {

View file

@ -34,7 +34,8 @@ public class OrderBookListItem {
public OrderBookListItem(Offer offer) { public OrderBookListItem(Offer offer) {
this.offer = offer; this.offer = offer;
this.price.set(BSFormatter.formatPrice(offer.getPrice())); this.price.set(BSFormatter.formatFiat(offer.getPrice()));
this.price.set(BSFormatter.formatFiat(offer.getPrice()));
this.amount.set(BSFormatter.formatCoin( this.amount.set(BSFormatter.formatCoin(
offer.getAmount()) + " (" + BSFormatter.formatCoin(offer.getMinAmount()) + ")"); offer.getAmount()) + " (" + BSFormatter.formatCoin(offer.getMinAmount()) + ")");
this.volume.set(BSFormatter.formatVolumeWithMinVolume( this.volume.set(BSFormatter.formatVolumeWithMinVolume(

View file

@ -33,6 +33,7 @@ import io.bitsquare.trade.protocol.trade.taker.SellerTakesOfferProtocol;
import io.bitsquare.trade.protocol.trade.taker.SellerTakesOfferProtocolListener; import io.bitsquare.trade.protocol.trade.taker.SellerTakesOfferProtocolListener;
import com.google.bitcoin.core.Coin; import com.google.bitcoin.core.Coin;
import com.google.bitcoin.utils.Fiat;
import java.net.URL; import java.net.URL;
@ -120,7 +121,7 @@ public class TakeOfferController extends CachedViewController {
amountTextField.setText(requestedAmount.toPlainString()); amountTextField.setText(requestedAmount.toPlainString());
amountTextField.setPromptText(BSFormatter.formatCoinWithCode( amountTextField.setPromptText(BSFormatter.formatCoinWithCode(
offer.getMinAmount()) + " - " + BSFormatter.formatCoinWithCode(offer.getAmount())); offer.getMinAmount()) + " - " + BSFormatter.formatCoinWithCode(offer.getAmount()));
priceTextField.setText(BSFormatter.formatPrice(offer.getPrice())); priceTextField.setText(BSFormatter.formatFiat(offer.getPrice()));
applyVolume(); applyVolume();
collateralLabel.setText("Collateral (" + getCollateralAsPercent() + "):"); collateralLabel.setText("Collateral (" + getCollateralAsPercent() + "):");
applyCollateral(); applyCollateral();
@ -197,7 +198,7 @@ public class TakeOfferController extends CachedViewController {
accordion.setExpandedPane(summaryTitledPane); accordion.setExpandedPane(summaryTitledPane);
summaryPaidTextField.setText(BSFormatter.formatCoinWithCode(trade.getTradeAmount())); summaryPaidTextField.setText(BSFormatter.formatCoinWithCode(trade.getTradeAmount()));
summaryReceivedTextField.setText(BSFormatter.formatVolume(trade.getTradeVolume())); summaryReceivedTextField.setText(BSFormatter.formatFiat(trade.getTradeVolume()));
summaryFeesTextField.setText(BSFormatter.formatCoinWithCode( summaryFeesTextField.setText(BSFormatter.formatCoinWithCode(
FeePolicy.TAKE_OFFER_FEE.add(FeePolicy.TX_FEE))); FeePolicy.TAKE_OFFER_FEE.add(FeePolicy.TX_FEE)));
summaryCollateralTextField.setText(BSFormatter.formatCoinWithCode( summaryCollateralTextField.setText(BSFormatter.formatCoinWithCode(
@ -265,7 +266,7 @@ public class TakeOfferController extends CachedViewController {
// formatted // formatted
private String getFormattedVolume() { private String getFormattedVolume() {
return BSFormatter.formatVolume(getVolume()); return BSFormatter.formatFiat(getVolume());
} }
private String getFormattedTotal() { private String getFormattedTotal() {
@ -291,8 +292,9 @@ public class TakeOfferController extends CachedViewController {
} }
} }
private double getVolume() { private Fiat getVolume() {
return offer.getPrice() * getAmountAsDouble(); //TODO
return Fiat.valueOf("EUR", (long) (offer.getPrice().longValue() * getAmountAsDouble()));
} }
private Coin getFee() { private Coin getFee() {
@ -305,13 +307,11 @@ public class TakeOfferController extends CachedViewController {
private Coin getCollateralAsCoin() { private Coin getCollateralAsCoin() {
Coin amountAsCoin = BSFormatter.parseToCoin(getAmountString()); Coin amountAsCoin = BSFormatter.parseToCoin(getAmountString());
return amountAsCoin.divide((long) (1d / offer.getCollateral())); return amountAsCoin.multiply(getCollateral()).divide(1000L);
} }
private String getFormattedCollateralAsBtc() { private String getFormattedCollateralAsBtc() {
Coin amountAsCoin = BSFormatter.parseToCoin(getAmountString()); return BSFormatter.formatCoin(getCollateralAsCoin());
Coin collateralAsCoin = amountAsCoin.divide((long) (1d / getCollateral()));
return BSFormatter.formatCoin(collateralAsCoin);
} }
private String getCollateralAsPercent() { private String getCollateralAsPercent() {
@ -319,7 +319,6 @@ public class TakeOfferController extends CachedViewController {
} }
private long getCollateral() { private long getCollateral() {
// TODO
return offer.getCollateral(); return offer.getCollateral();
} }

View file

@ -70,11 +70,6 @@ public class BSFormatter {
private static String currencyCode = Currency.getInstance(Locale.getDefault()).getCurrencyCode(); private static String currencyCode = Currency.getInstance(Locale.getDefault()).getCurrencyCode();
static {
//useMilliBitFormat(true);
// setLocale(Locale.US);
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Config // Config
@ -252,11 +247,11 @@ public class BSFormatter {
return result; return result;
} }
public static String formatDouble(double value) { public static String formatDouble(Fiat value) {
return formatDouble(value, 4); return formatDouble(value, 4);
} }
public static String formatDouble(double value, int fractionDigits) { public static String formatDouble(Fiat value, int fractionDigits) {
DecimalFormat decimalFormat = getDecimalFormat(fractionDigits); DecimalFormat decimalFormat = getDecimalFormat(fractionDigits);
return decimalFormat.format(value); return decimalFormat.format(value);
} }
@ -333,19 +328,8 @@ public class BSFormatter {
return getDecimalFormat(1).format(collateral / 10) + " %"; return getDecimalFormat(1).format(collateral / 10) + " %";
} }
@Deprecated public static String formatVolumeWithMinVolume(Fiat volume, Fiat minVolume) {
public static String formatPrice(double volume) { return formatFiat(volume) + " (" + formatFiat(minVolume) + ")";
return formatDouble(volume);
}
@Deprecated
public static String formatVolume(double volume) {
return formatDouble(volume);
}
@Deprecated
public static String formatVolumeWithMinVolume(double volume, double minVolume) {
return formatDouble(volume) + " (" + formatDouble(minVolume) + ")";
} }
/* /*
@Deprecated @Deprecated

View file

@ -22,6 +22,7 @@ import io.bitsquare.bank.BankAccountType;
import io.bitsquare.locale.Country; import io.bitsquare.locale.Country;
import com.google.bitcoin.core.Coin; import com.google.bitcoin.core.Coin;
import com.google.bitcoin.utils.Fiat;
import java.io.Serializable; import java.io.Serializable;
@ -47,7 +48,7 @@ public class Offer implements Serializable {
private final Date creationDate; private final Date creationDate;
private final double price; private final Fiat price;
private final Coin amount; private final Coin amount;
private final Coin minAmount; private final Coin minAmount;
//TODO use hex string //TODO use hex string
@ -70,7 +71,7 @@ public class Offer implements Serializable {
public Offer(String id, public Offer(String id,
PublicKey messagePublicKey, PublicKey messagePublicKey,
Direction direction, Direction direction,
double price, Fiat price,
Coin amount, Coin amount,
Coin minAmount, Coin minAmount,
BankAccountType bankAccountType, BankAccountType bankAccountType,
@ -118,7 +119,7 @@ public class Offer implements Serializable {
return id; return id;
} }
public double getPrice() { public Fiat getPrice() {
return price; return price;
} }
@ -154,17 +155,18 @@ public class Offer implements Serializable {
return acceptedLanguageLocales; return acceptedLanguageLocales;
} }
public double getVolumeForCoin(Coin coin) { public Fiat getVolumeForCoin(Coin coin) {
BigDecimal amountBD = BigDecimal.valueOf(coin.longValue()); BigDecimal amountBD = BigDecimal.valueOf(coin.longValue());
BigDecimal volumeBD = amountBD.multiply(BigDecimal.valueOf(price)); BigDecimal volumeBD = amountBD.multiply(BigDecimal.valueOf(price.longValue() / 10000));
return volumeBD.divide(BigDecimal.valueOf(Coin.COIN.value)).doubleValue(); long fiatAsDouble = volumeBD.divide(BigDecimal.valueOf(Coin.COIN.value)).longValue();
return Fiat.valueOf("EUR", fiatAsDouble);
} }
public double getOfferVolume() { public Fiat getOfferVolume() {
return getVolumeForCoin(amount); return getVolumeForCoin(amount);
} }
public double getMinOfferVolume() { public Fiat getMinOfferVolume() {
return getVolumeForCoin(minAmount); return getVolumeForCoin(minAmount);
} }

View file

@ -19,6 +19,7 @@ package io.bitsquare.trade;
import com.google.bitcoin.core.Coin; import com.google.bitcoin.core.Coin;
import com.google.bitcoin.core.Transaction; import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.utils.Fiat;
import java.io.Serializable; import java.io.Serializable;
@ -57,7 +58,7 @@ public class Trade implements Serializable {
_stateChangedProperty = new SimpleStringProperty(); _stateChangedProperty = new SimpleStringProperty();
} }
public double getTradeVolume() { public Fiat getTradeVolume() {
return offer.getVolumeForCoin(tradeAmount); return offer.getVolumeForCoin(tradeAmount);
} }
@ -173,7 +174,7 @@ public class Trade implements Serializable {
} }
public Coin getCollateralAmount() { public Coin getCollateralAmount() {
return tradeAmount.divide((long) (1d / offer.getCollateral())); return tradeAmount.multiply(offer.getCollateral()).divide(1000L);
} }

View file

@ -167,11 +167,10 @@ public class TradeManager {
TransactionResultHandler resultHandler, TransactionResultHandler resultHandler,
ErrorMessageHandler errorMessageHandler) { ErrorMessageHandler errorMessageHandler) {
// TODO price.value -> use Fiat in Offer for price
Offer offer = new Offer(id, Offer offer = new Offer(id,
user.getMessagePublicKey(), user.getMessagePublicKey(),
direction, direction,
price.value, price,
amount, amount,
minAmount, minAmount,
user.getCurrentBankAccount().getBankAccountType(), user.getCurrentBankAccount().getBankAccountType(),
@ -216,7 +215,7 @@ public class TradeManager {
private void addOffer(Offer offer) throws IOException { private void addOffer(Offer offer) throws IOException {
if (offers.containsKey(offer.getId())) if (offers.containsKey(offer.getId()))
throw new IllegalStateException("An offer with the id " + offer.getId() + " already exists. "); log.error("An offer with the id " + offer.getId() + " already exists. ");
offers.put(offer.getId(), offer); offers.put(offer.getId(), offer);
persistOffers(); persistOffers();
@ -224,7 +223,7 @@ public class TradeManager {
public void removeOffer(Offer offer) { public void removeOffer(Offer offer) {
if (!offers.containsKey(offer.getId())) { if (!offers.containsKey(offer.getId())) {
throw new IllegalStateException("offers does not contain the offer with the ID " + offer.getId()); log.error("offers does not contain the offer with the ID " + offer.getId());
} }
offers.remove(offer.getId()); offers.remove(offer.getId());
@ -251,9 +250,8 @@ public class TradeManager {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
public Trade createTrade(Offer offer) { public Trade createTrade(Offer offer) {
if (trades.containsKey(offer.getId())) { if (trades.containsKey(offer.getId()))
throw new IllegalStateException("trades contains already an trade with the ID " + offer.getId()); log.error("trades contains already an trade with the ID " + offer.getId());
}
Trade trade = new Trade(offer); Trade trade = new Trade(offer);
trades.put(offer.getId(), trade); trades.put(offer.getId(), trade);
@ -266,9 +264,8 @@ public class TradeManager {
} }
public void removeTrade(Trade trade) { public void removeTrade(Trade trade) {
if (!trades.containsKey(trade.getId())) { if (!trades.containsKey(trade.getId()))
throw new IllegalStateException("trades does not contain the trade with the ID " + trade.getId()); log.error("trades does not contain the trade with the ID " + trade.getId());
}
trades.remove(trade.getId()); trades.remove(trade.getId());
saveTrades(); saveTrades();

View file

@ -25,7 +25,6 @@ import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.msg.MessageFacade; import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OrderBookListener; import io.bitsquare.msg.listeners.OrderBookListener;
import io.bitsquare.settings.Settings; import io.bitsquare.settings.Settings;
import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Offer; import io.bitsquare.trade.Offer;
import io.bitsquare.trade.TradeManager; import io.bitsquare.trade.TradeManager;
import io.bitsquare.user.User; import io.bitsquare.user.User;
@ -137,12 +136,14 @@ public class OrderBook implements OrderBookListener {
// Apply applyFilter only if there is a valid value set // Apply applyFilter only if there is a valid value set
boolean priceResult = true; boolean priceResult = true;
if (orderBookFilter.getPrice() > 0) { if (orderBookFilter.getPrice() > 0) {
if (offer.getDirection() == Direction.SELL) { //TODO
priceResult = orderBookFilter.getPrice() >= offer.getPrice(); /* if (offer.getDirection() == Direction.SELL) {
priceResult = orderBookFilter.getPrice() //>= offer.getPrice();
} }
else { else {
priceResult = orderBookFilter.getPrice() <= offer.getPrice(); priceResult = orderBookFilter.getPrice() <= offer.getPrice();
} }*/
} }
// The arbitrator defined in the offer must match one of the accepted arbitrators defined in the settings // The arbitrator defined in the offer must match one of the accepted arbitrators defined in the settings

View file

@ -58,7 +58,7 @@ public class VerifyOffer {
"Amount is less then " + Restrictions.MIN_TRADE_AMOUNT); "Amount is less then " + Restrictions.MIN_TRADE_AMOUNT);
checkArgument(offer.getAmount().compareTo(offer.getMinAmount()) >= 0, "MinAmount is larger then Amount"); checkArgument(offer.getAmount().compareTo(offer.getMinAmount()) >= 0, "MinAmount is larger then Amount");
checkArgument(offer.getCollateral() > 0, "Collateral is 0"); checkArgument(offer.getCollateral() > 0, "Collateral is 0");
checkArgument(offer.getPrice() > 0, "Price is 0"); checkArgument(offer.getPrice().isPositive(), "Price is 0 or negative");
// TODO check balance // TODO check balance
// Coin collateralAsCoin = offer.getAmount().divide((long) (1d / offer.getCollateral())); // Coin collateralAsCoin = offer.getAmount().divide((long) (1d / offer.getCollateral()));