Skip to content

Commit

Permalink
Merge pull request #6065 from ghubstan/6-disambiguate-api-pct-field-n…
Browse files Browse the repository at this point in the history
…ames

Clarify meaning of API/Offer related % fields [#6]
  • Loading branch information
ripcurlx authored Feb 28, 2022
2 parents dbd6f33 + 5acef13 commit a73ee03
Show file tree
Hide file tree
Showing 35 changed files with 260 additions and 288 deletions.
15 changes: 15 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import java.io.IOException;
import java.io.PrintWriter;

import java.math.BigDecimal;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand All @@ -42,6 +45,7 @@
import static bisq.apitest.config.ApiTestRateMeterInterceptorConfig.getTestRateMeterInterceptorConfig;
import static bisq.cli.table.builder.TableType.BSQ_BALANCE_TBL;
import static bisq.cli.table.builder.TableType.BTC_BALANCE_TBL;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.stream;
Expand Down Expand Up @@ -162,6 +166,17 @@ protected final bisq.core.payment.PaymentAccount createPaymentAccount(GrpcClient
return bisq.core.payment.PaymentAccount.fromProto(paymentAccount, CORE_PROTO_RESOLVER);
}

public static final Supplier<Double> defaultBuyerSecurityDepositPct = () -> {
var defaultPct = BigDecimal.valueOf(getDefaultBuyerSecurityDepositAsPercent());
if (defaultPct.precision() != 2)
throw new IllegalStateException(format(
"Unexpected decimal precision, expected 2 but actual is %d%n."
+ "Check for changes to Restrictions.getDefaultBuyerSecurityDepositAsPercent()",
defaultPct.precision()));

return defaultPct.movePointRight(2).doubleValue();
};

public static String formatBalancesTbls(BalancesInfo allBalances) {
StringBuilder balances = new StringBuilder(BTC).append("\n");
balances.append(new TableBuilder(BTC_BALANCE_TBL, allBalances.getBtc()).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import static bisq.apitest.config.BisqAppConfig.bobdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.cli.table.builder.TableType.OFFER_TBL;
import static bisq.common.util.MathUtils.exactMultiply;
import static java.lang.String.format;
import static java.lang.System.out;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -78,29 +77,28 @@ public abstract class AbstractOfferTest extends MethodTest {

@BeforeAll
public static void setUp() {
setUp(false);
}

public static void setUp(boolean startSupportingAppsInDebugMode) {
startSupportingApps(true,
false,
startSupportingAppsInDebugMode,
bitcoind,
seednode,
arbdaemon,
alicedaemon,
bobdaemon);

initSwapPaymentAccounts();
createLegacyBsqPaymentAccounts();
}

// Mkt Price Margin value of offer returned from server is scaled down by 10^-2.
protected final Function<Double, Double> scaledDownMktPriceMargin = (mktPriceMargin) ->
exactMultiply(mktPriceMargin, 0.01);

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

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

protected String calcPriceAsString(double base, double delta, int precision) {
protected static 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.junit.jupiter.api.TestMethodOrder;

import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static protobuf.OfferDirection.BUY;

Expand All @@ -52,7 +51,7 @@ public class CancelOfferTest extends AbstractOfferTest {
10000000L,
10000000L,
0.00,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
paymentAccountId,
BSQ,
NO_TRIGGER_PRICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void testCreateBuy1BTCFor20KBSQOffer() {
100_000_000L,
100_000_000L,
"0.00005", // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("Sell BSQ (Buy BTC) Offer:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -114,7 +113,7 @@ public void testCreateSell1BTCFor20KBSQOffer() {
100_000_000L,
100_000_000L,
"0.00005", // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("SELL 20K BSQ Offer:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -165,7 +164,7 @@ public void testCreateBuyBTCWith1To2KBSQOffer() {
10_000_000L,
5_000_000L,
"0.00005", // FIXED PRICE IN BTC sats FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("BUY 1-2K BSQ Offer:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -216,7 +215,7 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
50_000_000L,
25_000_000L,
"0.00005", // FIXED PRICE IN BTC sats FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("SELL 5-10K BSQ Offer:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -280,6 +279,6 @@ private void genBtcBlockAndWaitForOfferPreparation() {
// Extra time is needed for the OfferUtils#isBsqForMakerFeeAvailable, which
// can sometimes return an incorrect false value if the BsqWallet's
// available confirmed balance is temporarily = zero during BSQ offer prep.
genBtcBlocksThenWait(1, 5000);
genBtcBlocksThenWait(1, 5_000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.apitest.config.ApiTestConfig.EUR;
import static bisq.apitest.config.ApiTestConfig.USD;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand All @@ -55,7 +54,7 @@ public void testCreateAUDBTCBuyOfferUsingFixedPrice16000() {
10_000_000L,
10_000_000L,
"36000",
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
audAccount.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("Offer #1:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -104,7 +103,7 @@ public void testCreateUSDBTCBuyOfferUsingFixedPrice100001234() {
10_000_000L,
10_000_000L,
"30000.1234",
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
usdAccount.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("Offer #2:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -153,7 +152,7 @@ public void testCreateEURBTCSellOfferUsingFixedPrice95001234() {
10_000_000L,
5_000_000L,
"29500.1234",
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
eurAccount.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("Offer #3:\n{}", toOfferTable.apply(newOffer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import static bisq.common.util.MathUtils.roundDouble;
import static bisq.common.util.MathUtils.scaleDownByPowerOf10;
import static bisq.common.util.MathUtils.scaleUpByPowerOf10;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static java.lang.Math.abs;
import static java.lang.String.format;
import static java.math.RoundingMode.HALF_UP;
Expand All @@ -65,13 +64,13 @@ public class CreateOfferUsingMarketPriceMarginTest extends AbstractOfferTest {
@Order(1)
public void testCreateUSDBTCBuyOffer5PctPriceMargin() {
PaymentAccount usdAccount = createDummyF2FAccount(aliceClient, "US");
double priceMarginPctInput = 5.00;
double priceMarginPctInput = 5.00d;
var newOffer = aliceClient.createMarketBasedPricedOffer(BUY.name(),
"usd",
10_000_000L,
10_000_000L,
priceMarginPctInput,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
usdAccount.getId(),
MAKER_FEE_CURRENCY_CODE,
NO_TRIGGER_PRICE);
Expand All @@ -83,6 +82,7 @@ public void testCreateUSDBTCBuyOffer5PctPriceMargin() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -97,6 +97,7 @@ public void testCreateUSDBTCBuyOffer5PctPriceMargin() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -112,13 +113,13 @@ public void testCreateUSDBTCBuyOffer5PctPriceMargin() {
@Order(2)
public void testCreateNZDBTCBuyOfferMinus2PctPriceMargin() {
PaymentAccount nzdAccount = createDummyF2FAccount(aliceClient, "NZ");
double priceMarginPctInput = -2.00;
double priceMarginPctInput = -2.00d; // -2%
var newOffer = aliceClient.createMarketBasedPricedOffer(BUY.name(),
"nzd",
10_000_000L,
10_000_000L,
priceMarginPctInput,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
nzdAccount.getId(),
MAKER_FEE_CURRENCY_CODE,
NO_TRIGGER_PRICE);
Expand All @@ -130,6 +131,7 @@ public void testCreateNZDBTCBuyOfferMinus2PctPriceMargin() {
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -144,6 +146,7 @@ public void testCreateNZDBTCBuyOfferMinus2PctPriceMargin() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(BUY.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(10_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -165,7 +168,7 @@ public void testCreateGBPBTCSellOfferMinus1Point5PctPriceMargin() {
10_000_000L,
5_000_000L,
priceMarginPctInput,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
gbpAccount.getId(),
MAKER_FEE_CURRENCY_CODE,
NO_TRIGGER_PRICE);
Expand All @@ -177,6 +180,7 @@ public void testCreateGBPBTCSellOfferMinus1Point5PctPriceMargin() {
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(5_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -191,6 +195,7 @@ public void testCreateGBPBTCSellOfferMinus1Point5PctPriceMargin() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(SELL.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(5_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -212,7 +217,7 @@ public void testCreateBRLBTCSellOffer6Point55PctPriceMargin() {
10_000_000L,
5_000_000L,
priceMarginPctInput,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
brlAccount.getId(),
MAKER_FEE_CURRENCY_CODE,
NO_TRIGGER_PRICE);
Expand All @@ -224,6 +229,7 @@ public void testCreateBRLBTCSellOffer6Point55PctPriceMargin() {
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(5_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -238,6 +244,7 @@ public void testCreateBRLBTCSellOffer6Point55PctPriceMargin() {
assertEquals(newOfferId, newOffer.getId());
assertEquals(SELL.name(), newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(priceMarginPctInput, newOffer.getMarketPriceMarginPct());
assertEquals(10_000_000, newOffer.getAmount());
assertEquals(5_000_000, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
Expand All @@ -260,7 +267,7 @@ public void testCreateUSDBTCBuyOfferWithTriggerPrice() {
10_000_000L,
5_000_000L,
0.0,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
usdAccount.getId(),
MAKER_FEE_CURRENCY_CODE,
triggerPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.apitest.config.ApiTestConfig.XMR;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -66,7 +65,7 @@ public void testCreateFixedPriceBuy1BTCFor200KXMROffer() {
100_000_000L,
75_000_000L,
"0.005", // FIXED PRICE IN BTC (satoshis) FOR 1 XMR
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesXmrAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("Sell XMR (Buy BTC) offer:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -113,7 +112,7 @@ public void testCreateFixedPriceSell1BTCFor200KXMROffer() {
100_000_000L,
50_000_000L,
"0.005", // FIXED PRICE IN BTC (satoshis) FOR 1 XMR
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesXmrAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.debug("Buy XMR (Sell BTC) offer:\n{}", toOfferTable.apply(newOffer));
Expand Down Expand Up @@ -162,7 +161,7 @@ public void testCreatePriceMarginBasedBuy1BTCOfferWithTriggerPrice() {
100_000_000L,
75_000_000L,
priceMarginPctInput,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesXmrAcct.getId(),
MAKER_FEE_CURRENCY_CODE,
triggerPrice);
Expand Down Expand Up @@ -218,7 +217,7 @@ public void testCreatePriceMarginBasedSell1BTCOffer() {
100_000_000L,
50_000_000L,
priceMarginPctInput,
getDefaultBuyerSecurityDepositAsPercent(),
defaultBuyerSecurityDepositPct.get(),
alicesXmrAcct.getId(),
MAKER_FEE_CURRENCY_CODE,
NO_TRIGGER_PRICE);
Expand Down
Loading

0 comments on commit a73ee03

Please sign in to comment.