Skip to content

Commit

Permalink
Round volume of fiat amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
ManfredKarrer committed May 26, 2018
1 parent 7f5c815 commit 78dece9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/main/java/bisq/core/monetary/Price.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bisq.core.monetary;

import bisq.core.locale.CurrencyUtil;
import bisq.core.util.CoinUtil;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Monetary;
Expand Down Expand Up @@ -57,12 +58,15 @@ public static Price valueOf(String currencyCode, long value) {
}

public Volume getVolumeByAmount(Coin amount) {
if (monetary instanceof Fiat)
return new Volume(new ExchangeRate((Fiat) monetary).coinToFiat(amount));
else if (monetary instanceof Altcoin)
if (monetary instanceof Fiat) {
final Fiat fiat = new ExchangeRate((Fiat) this.monetary).coinToFiat(amount);
Volume volume = new Volume(fiat);
return CoinUtil.roundVolume(volume);
} else if (monetary instanceof Altcoin) {
return new Volume(new AltcoinExchangeRate((Altcoin) monetary).coinToAltcoin(amount));
else
} else {
throw new IllegalStateException("Monetary must be either of type Fiat or Altcoin");
}
}

public Coin getAmountByVolume(Volume volume) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/bisq/core/offer/Offer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.provider.price.MarketPrice;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.util.CoinUtil;

import bisq.network.p2p.NodeAddress;

Expand Down Expand Up @@ -225,12 +226,8 @@ public void checkTradePriceTolerance(long takersTradePrice) throws TradePriceOut
public Volume getVolumeByAmount(Coin amount) {
Price price = getPrice();
if (price != null && amount != null) {
// try {
return price.getVolumeByAmount(amount);
/* } catch (Throwable t) {
log.error("getVolumeByAmount failed. Error=" + t.getMessage());
return null;
}*/
final Volume volumeByAmount = price.getVolumeByAmount(amount);
return CoinUtil.roundVolume(volumeByAmount);
} else {
return null;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/bisq/core/util/CoinUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package bisq.core.util;

import bisq.core.monetary.Volume;

import bisq.common.util.MathUtils;

import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.Fiat;

public class CoinUtil {

Expand All @@ -41,4 +44,14 @@ public static Coin maxCoin(Coin a, Coin b) {
public static double getFeePerByte(Coin miningFee, int txSize) {
return MathUtils.roundDouble(((double) miningFee.value / (double) txSize), 2);
}

public static Volume roundVolume(Volume volumeByAmount) {
if (volumeByAmount.getMonetary() instanceof Fiat) {
final long rounded = MathUtils.roundDoubleToLong(volumeByAmount.getValue() / 10000D) * 10000L;
long val = Math.max(10000L, rounded); // We don't allow 0 value
return new Volume(Fiat.valueOf(volumeByAmount.getCurrencyCode(), val));
} else {
return volumeByAmount;
}
}
}

0 comments on commit 78dece9

Please sign in to comment.