Skip to content

Commit

Permalink
Merge pull request #6062 from ghubstan/5-change-tradeinfo-proto-prc-n…
Browse files Browse the repository at this point in the history
…-vol-to-string

Change TradeInfo proto's tradePrice & tradeVolume fields to string [#5]
  • Loading branch information
ripcurlx authored Feb 28, 2022
2 parents 532efcc + 543c573 commit dbd6f33
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ public static void createXmrPaymentAccounts() {
XMR,
"44G4jWmSvTEfifSUZzTDnJVLPvYATmq9XhhtDqUof1BGCLceG82EQsVYG9Q9GN4bJcjbAJEc1JD1m5G7iK4UPZqACubV4Mq",
false);
log.debug("Alices XMR Account: {}", alicesXmrAcct);
log.trace("Alices XMR Account: {}", alicesXmrAcct);
bobsXmrAcct = bobClient.createCryptoCurrencyPaymentAccount("Bob's XMR Account",
XMR,
"4BDRhdSBKZqAXs3PuNTbMtaXBNqFj5idC2yMVnQj8Rm61AyKY8AxLTt9vGRJ8pwcG4EtpyD8YpGqdZWCZ2VZj6yVBN2RVKs",
false);
log.debug("Bob's XMR Account: {}", bobsXmrAcct);
log.trace("Bob's XMR Account: {}", bobsXmrAcct);
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ private void createBsqSwapOffer() {
1_000_000L,
1_000_000L,
"0.00005");
log.debug("BsqSwap Sell BSQ (Buy BTC) OFFER:\n{}", bsqSwapOffer);
log.debug("BsqSwap SELL BSQ (BUY BTC) Offer:\n{}", toOfferTable.apply(bsqSwapOffer));
var newOfferId = bsqSwapOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), bsqSwapOffer.getDirection());
assertEquals("0.00005000", bsqSwapOffer.getPrice());
assertEquals(1_000_000L, bsqSwapOffer.getAmount());
assertEquals(1_000_000L, bsqSwapOffer.getMinAmount());
assertEquals("200.00", bsqSwapOffer.getVolume());
assertEquals("200.00", bsqSwapOffer.getMinVolume());
assertEquals(BSQ, bsqSwapOffer.getBaseCurrencyCode());
assertEquals(BTC, bsqSwapOffer.getCounterCurrencyCode());

Expand All @@ -142,7 +144,7 @@ private void testGetMyBsqSwapOffer(OfferInfo bsqSwapOffer) {
if (numFetchAttempts >= 9)
fail(format("Alice giving up on fetching her (my) bsq swap offer after %d attempts.", numFetchAttempts), ex);

sleep(1000);
sleep(1500);
}
}
}
Expand All @@ -162,7 +164,7 @@ private void testGetBsqSwapOffer(OfferInfo bsqSwapOffer) {
if (numFetchAttempts > 9)
fail(format("Bob gave up on fetching available bsq swap offer after %d attempts.", numFetchAttempts), ex);

sleep(1000);
sleep(1500);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private boolean isCalculatedPriceWithinErrorTolerance(double delta,
actualDiffPct,
mktPrice,
scaledOfferPrice);
log.warn(offer.toString());
log.trace(offer.toString());
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ public void testEditBsqSwapOfferShouldThrowException() {
1_250_000L,
750_000L,
"0.00005");
log.debug("BsqSwap Buy BSQ (Buy BTC) offer:\n{}", originalOffer);
log.debug("BsqSwap BUY BSQ (SELL BTC) Offer:\n{}", toOfferTable.apply(originalOffer));
var newOfferId = originalOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), originalOffer.getDirection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected final void waitForSellerSeesPaymentInitiatedMessage(Logger log,

TradeInfo trade = grpcClient.getTrade(tradeId);
if (!isTradeInPaymentReceiptConfirmedStateAndPhase.test(trade)) {
fail(format("INVALID_PHASE for {}'s trade %s in STATE=%s PHASE=%s, cannot confirm payment received.",
fail(format("INVALID_PHASE for %s's trade %s in STATE=%s PHASE=%s, cannot confirm payment received.",
userName,
trade.getShortId(),
trade.getState(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
Expand All @@ -49,7 +48,7 @@
import bisq.apitest.method.offer.AbstractOfferTest;
import bisq.cli.GrpcClient;

@Disabled
// @Disabled
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BsqSwapBuyBtcTradeTest extends AbstractTradeTest {
Expand Down Expand Up @@ -85,7 +84,7 @@ public void testAliceCreateBsqSwapBuyBtcOffer() {
1_000_000L, // 0.01 BTC
1_000_000L,
"0.00005");
log.debug("Pending BsqSwap Sell BSQ (Buy BTC) OFFER:\n{}", toOfferTable.apply(mySwapOffer));
log.debug("Pending BsqSwap SELL BSQ (BUY BTC) Offer:\n{}", toOfferTable.apply(mySwapOffer));
var newOfferId = mySwapOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(OfferDirection.BUY.name(), mySwapOffer.getDirection());
Expand All @@ -98,7 +97,7 @@ public void testAliceCreateBsqSwapBuyBtcOffer() {
genBtcBlocksThenWait(1, 2_500);

mySwapOffer = aliceClient.getOffer(newOfferId);
log.debug("My fetched BsqSwap Sell BSQ (Buy BTC) OFFER:\n{}", toOfferTable.apply(mySwapOffer));
log.debug("My fetched BsqSwap SELL BSQ (BUY BTC) Offer:\n{}", toOfferTable.apply(mySwapOffer));
assertNotEquals(0, mySwapOffer.getMakerFee());

runCliGetOffer(newOfferId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testAliceCreateBsqSwapSellBtcOffer() {
1_000_000L, // 0.01 BTC
1_000_000L,
"0.00005");
log.debug("Pending BsqSwap Buy BSQ (Sell BTC) OFFER:\n{}", toOfferTable.apply(mySwapOffer));
log.debug("Pending BsqSwap BUY BSQ (SELL BTC) Offer:\n{}", toOfferTable.apply(mySwapOffer));
var newOfferId = mySwapOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), mySwapOffer.getDirection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void testTakeAlicesSellBTCForXMROffer(final TestInfo testInfo) {
getDefaultBuyerSecurityDepositAsPercent(),
alicesXmrAcct.getId(),
TRADE_FEE_CURRENCY_CODE);
log.debug("Alice's Buy XMR (Sell BTC) offer:\n{}", new TableBuilder(OFFER_TBL, alicesOffer).build());
log.debug("Alice's BUY XMR (SELL BTC) Offer:\n{}", new TableBuilder(OFFER_TBL, alicesOffer).build());
genBtcBlocksThenWait(1, 5000);
var offerId = alicesOffer.getId();
assertFalse(alicesOffer.getIsCurrencyForMakerFeeBtc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testTakeAlicesBuyBTCForXMROffer(final TestInfo testInfo) {
alicesXmrAcct.getId(),
TRADE_FEE_CURRENCY_CODE,
NO_TRIGGER_PRICE);
log.debug("Alice's SELL XMR (Buy BTC) offer:\n{}", new TableBuilder(OFFER_TBL, alicesOffer).build());
log.debug("Alice's SELL XMR (BUY BTC) Offer:\n{}", new TableBuilder(OFFER_TBL, alicesOffer).build());
genBtcBlocksThenWait(1, 4000);
var offerId = alicesOffer.getId();
assertTrue(alicesOffer.getIsCurrencyForMakerFeeBtc());
Expand Down
28 changes: 4 additions & 24 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
import java.io.PrintStream;
import java.io.PrintWriter;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import static bisq.cli.CurrencyFormat.*;
import static bisq.cli.CurrencyFormat.formatInternalFiatPrice;
import static bisq.cli.CurrencyFormat.formatTxFeeRateInfo;
import static bisq.cli.CurrencyFormat.toSatoshis;
import static bisq.cli.CurrencyFormat.toSecurityDepositAsPct;
import static bisq.cli.Method.*;
import static bisq.cli.opts.OptLabel.*;
import static bisq.cli.table.builder.TableType.*;
Expand All @@ -53,7 +54,6 @@
import static java.lang.System.err;
import static java.lang.System.exit;
import static java.lang.System.out;
import static java.math.BigDecimal.ZERO;



Expand Down Expand Up @@ -789,26 +789,6 @@ private static long toLong(String param) {
}
}

@Deprecated
private static long toInternalTriggerPrice(GrpcClient client,
String offerId,
BigDecimal unscaledTriggerPrice) {
if (unscaledTriggerPrice.compareTo(ZERO) >= 0) {
// Unfortunately, the EditOffer proto triggerPrice field was declared as
// a long instead of a string, so the CLI has to look at the offer to know
// how to scale the trigger-price (for a fiat or altcoin offer) param sent
// to the server in its 'editoffer' request. That means a preliminary round
// trip to the server: a 'getmyoffer' request.
var offer = client.getOffer(offerId);
if (offer.getCounterCurrencyCode().equals("BTC"))
return toInternalCryptoCurrencyPrice(unscaledTriggerPrice);
else
return toInternalFiatPrice(unscaledTriggerPrice);
} else {
return 0L;
}
}

private static File saveFileToDisk(String prefix,
@SuppressWarnings("SameParameterValue") String suffix,
String text) {
Expand Down
65 changes: 13 additions & 52 deletions cli/src/main/java/bisq/cli/CurrencyFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import static java.math.RoundingMode.HALF_UP;
import static java.math.RoundingMode.UNNECESSARY;

/**
* Utility for formatting amounts, volumes and fees; there is no i18n support in the CLI.
*/
@VisibleForTesting
public class CurrencyFormat {

// Use the US locale for all DecimalFormat objects.
// Use the US locale as a base for all DecimalFormats, but commas should be omitted from number strings.
private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS = DecimalFormatSymbols.getInstance(Locale.US);

// Format all numbers displayed in the CLI console with the US locale (no i18n support for CLI).
// Use the US locale as a base for all NumberFormats, but commas should be omitted from number strings.
private static final NumberFormat US_LOCALE_NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);

// Formats numbers for internal use, i.e., grpc request parameters.
Expand All @@ -53,38 +56,26 @@ public class CurrencyFormat {
static final BigDecimal BSQ_SATOSHI_DIVISOR = new BigDecimal(100);
static final DecimalFormat BSQ_FORMAT = new DecimalFormat("###,###,###,##0.00", DECIMAL_FORMAT_SYMBOLS);

static final DecimalFormat ALTCOIN_VOLUME_FORMAT = new DecimalFormat("########0.00", DECIMAL_FORMAT_SYMBOLS);
static final DecimalFormat FIAT_VOLUME_FORMAT = new DecimalFormat("###########0", DECIMAL_FORMAT_SYMBOLS);

static final BigDecimal SECURITY_DEPOSIT_MULTIPLICAND = new BigDecimal("0.01");

public static String formatSatoshis(String sats) {
//noinspection BigDecimalMethodWithoutRoundingCalled
return SATOSHI_FORMAT.format(new BigDecimal(sats).divide(SATOSHI_DIVISOR));
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatSatoshis(long sats) {
return SATOSHI_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
return SATOSHI_FORMAT.format(new BigDecimal(sats).divide(SATOSHI_DIVISOR));
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatBtc(long sats) {
return BTC_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
return BTC_FORMAT.format(new BigDecimal(sats).divide(SATOSHI_DIVISOR));
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatBsq(long sats) {
return BSQ_FORMAT.format(BigDecimal.valueOf(sats).divide(BSQ_SATOSHI_DIVISOR));
}

public static String formatVolumeString(String volumeString, int precision) {
if (volumeString == null || volumeString.isBlank())
return "";

US_LOCALE_NUMBER_FORMAT.setMinimumFractionDigits(precision);
US_LOCALE_NUMBER_FORMAT.setMaximumFractionDigits(precision);
US_LOCALE_NUMBER_FORMAT.setRoundingMode(HALF_UP);
var volAsDouble = new BigDecimal(volumeString).doubleValue();
if (precision == 0)
return FIAT_VOLUME_FORMAT.format(volAsDouble);
else
return ALTCOIN_VOLUME_FORMAT.format(volAsDouble);
return BSQ_FORMAT.format(new BigDecimal(sats).divide(BSQ_SATOSHI_DIVISOR));
}

public static String formatTxFeeRateInfo(TxFeeRateInfo txFeeRateInfo) {
Expand All @@ -111,50 +102,20 @@ public static String formatInternalFiatPrice(double price) {
return US_LOCALE_NUMBER_FORMAT.format(price);
}

@Deprecated
public static String formatPrice(long price) {
US_LOCALE_NUMBER_FORMAT.setMinimumFractionDigits(4);
US_LOCALE_NUMBER_FORMAT.setMaximumFractionDigits(4);
US_LOCALE_NUMBER_FORMAT.setRoundingMode(UNNECESSARY);
return US_LOCALE_NUMBER_FORMAT.format((double) price / 10_000);
}

public static String formatCryptoCurrencyPrice(long price) {
US_LOCALE_NUMBER_FORMAT.setMinimumFractionDigits(8);
US_LOCALE_NUMBER_FORMAT.setMaximumFractionDigits(8);
US_LOCALE_NUMBER_FORMAT.setRoundingMode(UNNECESSARY);
return US_LOCALE_NUMBER_FORMAT.format((double) price / SATOSHI_DIVISOR.doubleValue());
}

public static String formatFiatVolume(long volume) {
US_LOCALE_NUMBER_FORMAT.setMinimumFractionDigits(0);
US_LOCALE_NUMBER_FORMAT.setMaximumFractionDigits(0);
US_LOCALE_NUMBER_FORMAT.setRoundingMode(HALF_UP);
return US_LOCALE_NUMBER_FORMAT.format((double) volume / 10_000);
}

public static String formatCryptoCurrencyVolume(long volume) {
int defaultPrecision = 2;
return formatCryptoCurrencyVolume(volume, defaultPrecision);
}

public static String formatCryptoCurrencyVolume(long volume, int precision) {
US_LOCALE_NUMBER_FORMAT.setMinimumFractionDigits(precision);
US_LOCALE_NUMBER_FORMAT.setMaximumFractionDigits(precision);
US_LOCALE_NUMBER_FORMAT.setRoundingMode(HALF_UP);
return US_LOCALE_NUMBER_FORMAT.format((double) volume / SATOSHI_DIVISOR.doubleValue());
}

@Deprecated
public static long toInternalFiatPrice(BigDecimal fiatPrice) {
return fiatPrice.multiply(new BigDecimal(10_000)).longValue();
}

@Deprecated
public static long toInternalCryptoCurrencyPrice(BigDecimal altcoinPrice) {
return altcoinPrice.multiply(new BigDecimal(100_000_000)).longValue();
}

public static long toSatoshis(String btc) {
if (btc.startsWith("-"))
throw new IllegalArgumentException(format("'%s' is not a positive number", btc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,3 @@ abstract class AbstractTableBuilder {

public abstract Table build();
}

/*
var currencyCode = isFiatOffer.test(o);
*/
Loading

0 comments on commit dbd6f33

Please sign in to comment.