Skip to content
This repository has been archived by the owner on Jun 17, 2020. It is now read-only.

[WIP] Round volume of fiat amounts #116

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package bisq.core.util;

import bisq.core.monetary.Volume;

import org.bitcoinj.core.Coin;

import org.slf4j.Logger;
Expand All @@ -26,8 +28,8 @@

import static org.junit.Assert.assertEquals;

public class CoinCryptoUtilsTest {
private static final Logger log = LoggerFactory.getLogger(CoinCryptoUtilsTest.class);
public class CoinUtilTest {
private static final Logger log = LoggerFactory.getLogger(CoinUtilTest.class);

@Test
public void testGetFeePerBtc() {
Expand Down Expand Up @@ -55,4 +57,10 @@ public void testMaxCoin() {
assertEquals(Coin.parseCoin("0.05"), CoinUtil.maxCoin(Coin.parseCoin("0.05"), Coin.parseCoin("0")));
}

@Test
public void testRoundFiatVolume() {
assertEquals(1000000L, CoinUtil.roundVolume(Volume.parse("100.12", "USD")).getValue());
assertEquals(1010000L, CoinUtil.roundVolume(Volume.parse("100.51", "USD")).getValue());
}

}