Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change API grpc.proto triggerPrice field to string [#3] #6057

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import protobuf.PaymentAccount;

import java.math.BigDecimal;
import java.math.MathContext;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

import lombok.Setter;
Expand Down Expand Up @@ -62,7 +62,7 @@ public abstract class AbstractOfferTest extends MethodTest {

protected static final int ACTIVATE_OFFER = 1;
protected static final int DEACTIVATE_OFFER = 0;
protected static final long NO_TRIGGER_PRICE = 0;
protected static final String NO_TRIGGER_PRICE = "0";

@Setter
protected static boolean isLongRunningTest;
Expand Down Expand Up @@ -94,41 +94,20 @@ public static void setUp() {
protected final Function<Double, Double> scaledDownMktPriceMargin = (mktPriceMargin) ->
exactMultiply(mktPriceMargin, 0.01);

// Price value of fiat offer returned from server will be scaled up by 10^4.
protected final Function<BigDecimal, Long> scaledUpFiatOfferPrice = (price) -> {
BigDecimal factor = new BigDecimal(10).pow(4);
return price.multiply(factor).longValue();
};

// Price value of altcoin offer returned from server will be scaled up by 10^8.
protected final Function<String, Long> scaledUpAltcoinOfferPrice = (altcoinPriceAsString) -> {
BigDecimal factor = new BigDecimal(10).pow(8);
BigDecimal priceAsBigDecimal = new BigDecimal(altcoinPriceAsString);
return priceAsBigDecimal.multiply(factor).longValue();
};

protected final BiFunction<Double, Double, Long> calcFiatTriggerPriceAsLong = (base, delta) -> {
var priceAsDouble = new BigDecimal(base).add(new BigDecimal(delta)).doubleValue();
return Double.valueOf(exactMultiply(priceAsDouble, 10_000)).longValue();
};

protected final BiFunction<Double, Double, Long> calcAltcoinTriggerPriceAsLong = (base, delta) -> {
var priceAsDouble = new BigDecimal(base).add(new BigDecimal(delta)).doubleValue();
return Double.valueOf(exactMultiply(priceAsDouble, 100_000_000)).longValue();
};

protected final BiFunction<Double, Double, String> calcPriceAsString = (base, delta) -> {
var priceAsBigDecimal = new BigDecimal(Double.toString(base))
.add(new BigDecimal(Double.toString(delta)));
return priceAsBigDecimal.toPlainString();
};

protected final Function<OfferInfo, String> toOfferTable = (offer) ->
new TableBuilder(OFFER_TBL, offer).build().toString();

protected final Function<List<OfferInfo>, String> toOffersTable = (offers) ->
new TableBuilder(OFFER_TBL, offers).build().toString();

protected String calcPriceAsString(double base, double delta, int precision) {
var mathContext = new MathContext(precision);
var priceAsBigDecimal = new BigDecimal(Double.toString(base), mathContext)
.add(new BigDecimal(Double.toString(delta), mathContext))
.round(mathContext);
return format("%." + precision + "f", priceAsBigDecimal.doubleValue());
}

protected OfferInfo getAvailableBsqSwapOffer(GrpcClient client,
OfferDirection direction,
boolean checkForLoggedExceptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void createBsqSwapOffer() {
var newOfferId = bsqSwapOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), bsqSwapOffer.getDirection());
assertEquals(5_000, bsqSwapOffer.getPrice());
assertEquals("0.00005000", bsqSwapOffer.getPrice());
assertEquals(1_000_000L, bsqSwapOffer.getAmount());
assertEquals(1_000_000L, bsqSwapOffer.getMinAmount());
assertEquals(BSQ, bsqSwapOffer.getBaseCurrencyCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testCreateBuy1BTCFor20KBSQOffer() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -91,7 +91,7 @@ public void testCreateBuy1BTCFor20KBSQOffer() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand Down Expand Up @@ -121,7 +121,7 @@ public void testCreateSell1BTCFor20KBSQOffer() {
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -138,7 +138,7 @@ public void testCreateSell1BTCFor20KBSQOffer() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand Down Expand Up @@ -168,7 +168,7 @@ public void testCreateBuyBTCWith1To2KBSQOffer() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(10_000_000L, newOffer.getAmount());
assertEquals(5_000_000L, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -185,7 +185,7 @@ public void testCreateBuyBTCWith1To2KBSQOffer() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(10_000_000L, newOffer.getAmount());
assertEquals(5_000_000L, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand Down Expand Up @@ -215,7 +215,7 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(50_000_000L, newOffer.getAmount());
assertEquals(25_000_000L, newOffer.getMinAmount());
assertEquals(7_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -232,7 +232,7 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(5_000, newOffer.getPrice());
assertEquals("0.00005000", newOffer.getPrice());
assertEquals(50_000_000L, newOffer.getAmount());
assertEquals(25_000_000L, newOffer.getMinAmount());
assertEquals(7_500_000, newOffer.getBuyerSecurityDeposit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testCreateAUDBTCBuyOfferUsingFixedPrice16000() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(360_000_000, newOffer.getPrice());
assertEquals("36000.0000", newOffer.getPrice());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -81,7 +81,7 @@ public void testCreateAUDBTCBuyOfferUsingFixedPrice16000() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(360_000_000, newOffer.getPrice());
assertEquals("36000.0000", newOffer.getPrice());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand Down Expand Up @@ -111,7 +111,7 @@ public void testCreateUSDBTCBuyOfferUsingFixedPrice100001234() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(300_001_234, newOffer.getPrice());
assertEquals("30000.1234", newOffer.getPrice());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -126,7 +126,7 @@ public void testCreateUSDBTCBuyOfferUsingFixedPrice100001234() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(300_001_234, newOffer.getPrice());
assertEquals("30000.1234", newOffer.getPrice());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand Down Expand Up @@ -156,7 +156,7 @@ public void testCreateEURBTCSellOfferUsingFixedPrice95001234() {
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(295_001_234, newOffer.getPrice());
assertEquals("29500.1234", newOffer.getPrice());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(5_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -171,7 +171,7 @@ public void testCreateEURBTCSellOfferUsingFixedPrice95001234() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(295_001_234, newOffer.getPrice());
assertEquals("29500.1234", newOffer.getPrice());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(5_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@

package bisq.apitest.method.offer;

import bisq.core.monetary.Altcoin;
import bisq.core.monetary.Price;
import bisq.core.payment.PaymentAccount;

import bisq.proto.grpc.OfferInfo;

import org.bitcoinj.utils.Fiat;

import java.text.DecimalFormat;

import java.math.BigDecimal;
Expand All @@ -43,7 +39,6 @@
import static bisq.common.util.MathUtils.scaleDownByPowerOf10;
import static bisq.common.util.MathUtils.scaleUpByPowerOf10;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static bisq.core.locale.CurrencyUtil.isCryptoCurrency;
import static java.lang.Math.abs;
import static java.lang.String.format;
import static java.math.RoundingMode.HALF_UP;
Expand Down Expand Up @@ -259,10 +254,7 @@ public void testCreateBRLBTCSellOffer6Point55PctPriceMargin() {
public void testCreateUSDBTCBuyOfferWithTriggerPrice() {
PaymentAccount usdAccount = createDummyF2FAccount(aliceClient, "US");
double mktPriceAsDouble = aliceClient.getBtcPrice("usd");
BigDecimal mktPrice = new BigDecimal(Double.toString(mktPriceAsDouble));
BigDecimal triggerPrice = mktPrice.add(new BigDecimal("1000.9999"));
long triggerPriceAsLong = Price.parse(USD, triggerPrice.toString()).getValue();

String triggerPrice = calcPriceAsString(mktPriceAsDouble, Double.parseDouble("1000.9999"), 4);
var newOffer = aliceClient.createMarketBasedPricedOffer(BUY.name(),
"usd",
10_000_000L,
Expand All @@ -271,7 +263,7 @@ public void testCreateUSDBTCBuyOfferWithTriggerPrice() {
getDefaultBuyerSecurityDepositAsPercent(),
usdAccount.getId(),
MAKER_FEE_CURRENCY_CODE,
triggerPriceAsLong);
triggerPrice);
assertTrue(newOffer.getIsMyOffer());
assertTrue(newOffer.getIsMyPendingOffer());

Expand All @@ -280,24 +272,24 @@ public void testCreateUSDBTCBuyOfferWithTriggerPrice() {
log.debug("OFFER #5:\n{}", toOfferTable.apply(newOffer));
assertTrue(newOffer.getIsMyOffer());
assertFalse(newOffer.getIsMyPendingOffer());
assertEquals(triggerPriceAsLong, newOffer.getTriggerPrice());
assertEquals(triggerPrice, newOffer.getTriggerPrice());
}

private void assertCalculatedPriceIsCorrect(OfferInfo offer, double priceMarginPctInput) {
assertTrue(() -> {
String counterCurrencyCode = offer.getCounterCurrencyCode();
double mktPrice = aliceClient.getBtcPrice(counterCurrencyCode);
double scaledOfferPrice = getScaledOfferPrice(offer.getPrice(), counterCurrencyCode);
double priceAsDouble = Double.parseDouble(offer.getPrice());
double expectedDiffPct = scaleDownByPowerOf10(priceMarginPctInput, 2);
double actualDiffPct = offer.getDirection().equals(BUY.name())
? getPercentageDifference(scaledOfferPrice, mktPrice)
: getPercentageDifference(mktPrice, scaledOfferPrice);
? getPercentageDifference(priceAsDouble, mktPrice)
: getPercentageDifference(mktPrice, priceAsDouble);
double pctDiffDelta = abs(expectedDiffPct) - abs(actualDiffPct);
return isCalculatedPriceWithinErrorTolerance(pctDiffDelta,
expectedDiffPct,
actualDiffPct,
mktPrice,
scaledOfferPrice,
priceAsDouble,
offer);
});
}
Expand All @@ -308,11 +300,6 @@ private double getPercentageDifference(double price1, double price2) {
.doubleValue();
}

private double getScaledOfferPrice(double offerPrice, String currencyCode) {
int precision = isCryptoCurrency(currencyCode) ? Altcoin.SMALLEST_UNIT_EXPONENT : Fiat.SMALLEST_UNIT_EXPONENT;
return scaleDownByPowerOf10(offerPrice, precision);
}

private boolean isCalculatedPriceWithinErrorTolerance(double delta,
double expectedDiffPct,
double actualDiffPct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testCreateFixedPriceBuy1BTCFor200KXMROffer() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(500_000L, newOffer.getPrice());
assertEquals("0.00500000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(75_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -94,7 +94,7 @@ public void testCreateFixedPriceBuy1BTCFor200KXMROffer() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(500_000L, newOffer.getPrice());
assertEquals("0.00500000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(75_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand Down Expand Up @@ -124,7 +124,7 @@ public void testCreateFixedPriceSell1BTCFor200KXMROffer() {
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(500_000L, newOffer.getPrice());
assertEquals("0.00500000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(50_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -141,7 +141,7 @@ public void testCreateFixedPriceSell1BTCFor200KXMROffer() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(SELL.name(), newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(500_000L, newOffer.getPrice());
assertEquals("0.00500000", newOffer.getPrice());
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(50_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -156,7 +156,7 @@ public void testCreateFixedPriceSell1BTCFor200KXMROffer() {
public void testCreatePriceMarginBasedBuy1BTCOfferWithTriggerPrice() {
double priceMarginPctInput = 1.00;
double mktPriceAsDouble = aliceClient.getBtcPrice(XMR);
long triggerPriceAsLong = calcAltcoinTriggerPriceAsLong.apply(mktPriceAsDouble, -0.001);
String triggerPrice = calcPriceAsString(mktPriceAsDouble, Double.parseDouble("-0.001"), 8);
var newOffer = aliceClient.createMarketBasedPricedOffer(BUY.name(),
XMR,
100_000_000L,
Expand All @@ -165,7 +165,7 @@ public void testCreatePriceMarginBasedBuy1BTCOfferWithTriggerPrice() {
getDefaultBuyerSecurityDepositAsPercent(),
alicesXmrAcct.getId(),
MAKER_FEE_CURRENCY_CODE,
triggerPriceAsLong);
triggerPrice);
log.debug("Pending Sell XMR (Buy BTC) offer:\n{}", toOfferTable.apply(newOffer));
assertTrue(newOffer.getIsMyOffer());
assertTrue(newOffer.getIsMyPendingOffer());
Expand All @@ -176,7 +176,7 @@ public void testCreatePriceMarginBasedBuy1BTCOfferWithTriggerPrice() {
assertTrue(newOffer.getUseMarketBasedPrice());

// There is no trigger price while offer is pending.
assertEquals(0, newOffer.getTriggerPrice());
assertEquals(NO_TRIGGER_PRICE, newOffer.getTriggerPrice());

assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(75_000_000L, newOffer.getMinAmount());
Expand All @@ -197,7 +197,7 @@ public void testCreatePriceMarginBasedBuy1BTCOfferWithTriggerPrice() {
assertTrue(newOffer.getUseMarketBasedPrice());

// The trigger price should exist on the prepared offer.
assertEquals(triggerPriceAsLong, newOffer.getTriggerPrice());
assertEquals(triggerPrice, newOffer.getTriggerPrice());

assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(75_000_000L, newOffer.getMinAmount());
Expand Down
Loading