Skip to content

Commit

Permalink
Fix incorrect rounding of BSQ dollar price to whole number
Browse files Browse the repository at this point in the history
Add a new method to DisplayUtils to restore the old rounding behaviour
of formatVolumeWithCode whenever a fractional volume is required. This
fixes a regression caused by #3926 to remove unnecessarily displayed
decimals for fiat volumes - it appears that in every case but the avg.
dollar price on the BSQ dashboard a whole number should be shown.

Also add a relevant test.
  • Loading branch information
stejbac committed Feb 8, 2020
1 parent 2e78dc6 commit c2b519a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions desktop/src/main/java/bisq/desktop/util/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public static String formatVolumeWithCode(Volume volume) {
return formatVolume(volume, FIAT_VOLUME_FORMAT, true);
}

static String formatAverageVolumeWithCode(Volume volume) {
return formatVolume(volume, FIAT_VOLUME_FORMAT.minDecimals(2), true);
}

public static String formatVolumeLabel(String currencyCode) {
return formatVolumeLabel(currencyCode, "");
}
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/main/java/bisq/desktop/util/GUIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ public static void showBsqFeeInfoPopup(Coin fee, Coin miningFee, int txSize, Bsq
showBsqFeeInfoPopup(fee, miningFee, null, txSize, bsqFormatter, btcFormatter, type, actionHandler);
}

public static void setFitToRowsForTableView(TableView tableView,
public static void setFitToRowsForTableView(TableView<?> tableView,
int rowHeight,
int headerHeight,
int minNumRows,
Expand Down Expand Up @@ -1100,7 +1100,7 @@ public static String getBsqInUsd(Price bsqPrice,
Volume bsqAmountAsVolume = Volume.parse(bsqAmountAsString, "BSQ");
Coin requiredBtc = bsqPrice.getAmountByVolume(bsqAmountAsVolume);
Volume volumeByAmount = usdPrice.getVolumeByAmount(requiredBtc);
return DisplayUtils.formatVolumeWithCode(volumeByAmount);
return DisplayUtils.formatAverageVolumeWithCode(volumeByAmount);
}

public static MaterialDesignIcon getIconForSignState(AccountAgeWitnessService.SignState state) {
Expand Down
23 changes: 19 additions & 4 deletions desktop/src/test/java/bisq/desktop/util/GUIUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
import bisq.core.locale.GlobalSettings;
import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.monetary.Price;
import bisq.core.provider.price.MarketPrice;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.user.DontShowAgainLookup;
import bisq.core.user.Preferences;
import bisq.core.util.coin.BsqFormatter;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.CoinMaker;
Expand Down Expand Up @@ -51,7 +55,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


@Ignore
public class GUIUtilTest {

Expand All @@ -65,7 +68,7 @@ public void setup() {

@Test
public void testTradeCurrencyConverter() {
Map<String, Integer> offerCounts = new HashMap<String, Integer>() {{
Map<String, Integer> offerCounts = new HashMap<>() {{
put("BTC", 11);
put("EUR", 10);
}};
Expand All @@ -80,7 +83,7 @@ public void testTradeCurrencyConverter() {
}

@Test
public void testOpenURLWithCampaignParameters() throws Exception {
public void testOpenURLWithCampaignParameters() {
Preferences preferences = mock(Preferences.class);
DontShowAgainLookup.setPreferences(preferences);
GUIUtil.setPreferences(preferences);
Expand All @@ -101,7 +104,7 @@ public void testOpenURLWithCampaignParameters() throws Exception {
}

@Test
public void testOpenURLWithoutCampaignParameters() throws Exception {
public void testOpenURLWithoutCampaignParameters() {
Preferences preferences = mock(Preferences.class);
DontShowAgainLookup.setPreferences(preferences);
GUIUtil.setPreferences(preferences);
Expand Down Expand Up @@ -143,6 +146,18 @@ public void testAddressRegexValidator() {
assertFalse(regexValidator.validate("12.34.56.78:").isValid);
}

@Test
public void testGetBsqInUsd() {
PriceFeedService priceFeedService = mock(PriceFeedService.class);
when(priceFeedService.getMarketPrice("USD"))
.thenReturn(new MarketPrice("USD", 12345.6789, 0, true));

Coin oneBsq = Coin.valueOf(100);
Price avgPrice = Price.valueOf("BSQ", 10000);

assertEquals("1.23 USD", GUIUtil.getBsqInUsd(avgPrice, oneBsq, priceFeedService, new BsqFormatter()));
}

@Test
public void percentageOfTradeAmount_higherFeeAsMin() {

Expand Down

0 comments on commit c2b519a

Please sign in to comment.