From 9aa6bbeff6fdb746ae6052a0778ced4082f1ec42 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 26 Nov 2022 13:02:07 +0000 Subject: [PATCH] must provide fixed price unless using market price --- .../bisq/cli/request/OffersServiceRequest.java | 6 +++--- core/src/main/java/bisq/core/api/CoreApi.java | 4 ++-- .../java/bisq/core/api/CoreOffersService.java | 4 ++-- .../main/java/bisq/core/monetary/Price.java | 4 ++-- .../bisq/core/offer/CreateOfferService.java | 12 +++++++++--- .../offer/placeoffer/tasks/ValidateOffer.java | 4 ++-- .../java/bisq/core/monetary/PriceTest.java | 18 +++++++++--------- .../bisq/daemon/grpc/GrpcOffersService.java | 16 ++++++++-------- proto/src/main/proto/grpc.proto | 6 +++--- 9 files changed, 40 insertions(+), 34 deletions(-) diff --git a/cli/src/main/java/bisq/cli/request/OffersServiceRequest.java b/cli/src/main/java/bisq/cli/request/OffersServiceRequest.java index 9526bee4dc..ac89c53e22 100644 --- a/cli/src/main/java/bisq/cli/request/OffersServiceRequest.java +++ b/cli/src/main/java/bisq/cli/request/OffersServiceRequest.java @@ -18,7 +18,7 @@ package bisq.cli.request; import bisq.proto.grpc.CancelOfferRequest; -import bisq.proto.grpc.CreateOfferRequest; +import bisq.proto.grpc.PostOfferRequest; import bisq.proto.grpc.GetMyOfferRequest; import bisq.proto.grpc.GetMyOffersRequest; import bisq.proto.grpc.GetOfferRequest; @@ -76,7 +76,7 @@ public class OffersServiceRequest { double securityDepositPct, String paymentAcctId, String triggerPrice) { - var request = CreateOfferRequest.newBuilder() + var request = PostOfferRequest.newBuilder() .setDirection(direction) .setCurrencyCode(currencyCode) .setAmount(amount) @@ -88,7 +88,7 @@ public class OffersServiceRequest { .setPaymentAccountId(paymentAcctId) .setTriggerPrice(triggerPrice) .build(); - return grpcStubs.offersService.createOffer(request).getOffer(); + return grpcStubs.offersService.postOffer(request).getOffer(); } public void cancelOffer(String offerId) { diff --git a/core/src/main/java/bisq/core/api/CoreApi.java b/core/src/main/java/bisq/core/api/CoreApi.java index 8291c11b09..8ee6b664af 100644 --- a/core/src/main/java/bisq/core/api/CoreApi.java +++ b/core/src/main/java/bisq/core/api/CoreApi.java @@ -438,7 +438,7 @@ public class CoreApi { return coreOffersService.getMyOpenOffer(id); } - public void createAnPlaceOffer(String currencyCode, + public void postOffer(String currencyCode, String directionAsString, String priceAsString, boolean useMarketBasedPrice, @@ -450,7 +450,7 @@ public class CoreApi { String paymentAccountId, Consumer resultHandler, ErrorMessageHandler errorMessageHandler) { - coreOffersService.createAndPlaceOffer(currencyCode, + coreOffersService.postOffer(currencyCode, directionAsString, priceAsString, useMarketBasedPrice, diff --git a/core/src/main/java/bisq/core/api/CoreOffersService.java b/core/src/main/java/bisq/core/api/CoreOffersService.java index c39f7bbfa8..25933e218e 100644 --- a/core/src/main/java/bisq/core/api/CoreOffersService.java +++ b/core/src/main/java/bisq/core/api/CoreOffersService.java @@ -212,7 +212,7 @@ public class CoreOffersService { } // Create and place new offer. - void createAndPlaceOffer(String currencyCode, + void postOffer(String currencyCode, String directionAsString, String priceAsString, boolean useMarketBasedPrice, @@ -234,7 +234,7 @@ public class CoreOffersService { String upperCaseCurrencyCode = currencyCode.toUpperCase(); String offerId = createOfferService.getRandomOfferId(); OfferDirection direction = OfferDirection.valueOf(directionAsString.toUpperCase()); - Price price = Price.valueOf(upperCaseCurrencyCode, priceStringToLong(priceAsString, upperCaseCurrencyCode)); + Price price = priceAsString.isEmpty() ? null : Price.valueOf(upperCaseCurrencyCode, priceStringToLong(priceAsString, upperCaseCurrencyCode)); Coin amount = Coin.valueOf(amountAsLong); Coin minAmount = Coin.valueOf(minAmountAsLong); Coin useDefaultTxFee = Coin.ZERO; diff --git a/core/src/main/java/bisq/core/monetary/Price.java b/core/src/main/java/bisq/core/monetary/Price.java index edc72df94a..a3129e5c11 100644 --- a/core/src/main/java/bisq/core/monetary/Price.java +++ b/core/src/main/java/bisq/core/monetary/Price.java @@ -140,8 +140,8 @@ public class Price extends MonetaryWrapper implements Comparable { public String toFriendlyString() { return monetary instanceof Altcoin ? - ((Altcoin) monetary).toFriendlyString() + "/BTC" : - ((Fiat) monetary).toFriendlyString().replace(((Fiat) monetary).currencyCode, "") + "BTC/" + ((Fiat) monetary).currencyCode; + ((Altcoin) monetary).toFriendlyString() + "/XMR" : + ((Fiat) monetary).toFriendlyString().replace(((Fiat) monetary).currencyCode, "") + "XMR/" + ((Fiat) monetary).currencyCode; } public String toPlainString() { diff --git a/core/src/main/java/bisq/core/offer/CreateOfferService.java b/core/src/main/java/bisq/core/offer/CreateOfferService.java index 67ff62eea5..9b1fb9762a 100644 --- a/core/src/main/java/bisq/core/offer/CreateOfferService.java +++ b/core/src/main/java/bisq/core/offer/CreateOfferService.java @@ -129,7 +129,7 @@ public class CreateOfferService { offerId, currencyCode, direction, - price.getValue(), + price == null ? null : price.getValue(), useMarketBasedPrice, marketPriceMargin, amount.value, @@ -138,11 +138,17 @@ public class CreateOfferService { long creationTime = new Date().getTime(); NodeAddress makerAddress = p2PService.getAddress(); - boolean useMarketBasedPriceValue = useMarketBasedPrice && + boolean useMarketBasedPriceValue = price == null && + useMarketBasedPrice && isMarketPriceAvailable(currencyCode) && !paymentAccount.hasPaymentMethodWithId(HAL_CASH_ID); - long priceAsLong = price != null && !useMarketBasedPriceValue ? price.getValue() : 0L; + // verify price + if (price == null && !useMarketBasedPriceValue) { + throw new IllegalArgumentException("Must provide fixed price because market price is unavailable"); + } + + long priceAsLong = price != null ? price.getValue() : 0L; double marketPriceMarginParam = useMarketBasedPriceValue ? marketPriceMargin : 0; long amountAsLong = amount != null ? amount.getValue() : 0L; long minAmountAsLong = minAmount != null ? minAmount.getValue() : 0L; diff --git a/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java b/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java index f5e3d321e2..1cd51c4d80 100644 --- a/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java +++ b/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java @@ -70,8 +70,8 @@ public class ValidateOffer extends Task { checkArgument(offer.getAmount().compareTo(offer.getMinAmount()) >= 0, "MinAmount is larger than Amount"); checkNotNull(offer.getPrice(), "Price is null"); - checkArgument(offer.getPrice().isPositive(), - "Price must be positive. price=" + offer.getPrice().toFriendlyString()); + if (!offer.isUseMarketBasedPrice()) checkArgument(offer.getPrice().isPositive(), + "Price must be positive unless using market based price. price=" + offer.getPrice().toFriendlyString()); checkArgument(offer.getDate().getTime() > 0, "Date must not be 0. date=" + offer.getDate().toString()); diff --git a/core/src/test/java/bisq/core/monetary/PriceTest.java b/core/src/test/java/bisq/core/monetary/PriceTest.java index a4e85d4235..e9f5a400b4 100644 --- a/core/src/test/java/bisq/core/monetary/PriceTest.java +++ b/core/src/test/java/bisq/core/monetary/PriceTest.java @@ -27,14 +27,14 @@ public class PriceTest { Price result = Price.parse("USD", "0.1"); Assert.assertEquals( "Fiat value should be formatted with two decimals.", - "0.10 BTC/USD", + "0.10 XMR/USD", result.toFriendlyString() ); result = Price.parse("EUR", "0.1234"); Assert.assertEquals( "Fiat value should be given two decimals", - "0.1234 BTC/EUR", + "0.1234 XMR/EUR", result.toFriendlyString() ); @@ -57,19 +57,19 @@ public class PriceTest { Assert.assertEquals( "Comma (',') as decimal separator should be converted to period ('.')", - "0.0001 BTC/USD", + "0.0001 XMR/USD", Price.parse("USD", "0,0001").toFriendlyString() ); Assert.assertEquals( "Too many decimals should get rounded up properly.", - "10000.2346 LTC/BTC", + "10000.2346 LTC/XMR", Price.parse("LTC", "10000,23456789").toFriendlyString() ); Assert.assertEquals( "Too many decimals should get rounded down properly.", - "10000.2345 LTC/BTC", + "10000.2345 LTC/XMR", Price.parse("LTC", "10000,23454999").toFriendlyString() ); @@ -95,14 +95,14 @@ public class PriceTest { Price result = Price.valueOf("USD", 1); Assert.assertEquals( "Fiat value should have four decimals.", - "0.0001 BTC/USD", + "0.0001 XMR/USD", result.toFriendlyString() ); result = Price.valueOf("EUR", 1234); Assert.assertEquals( "Fiat value should be given two decimals", - "0.1234 BTC/EUR", + "0.1234 XMR/EUR", result.toFriendlyString() ); @@ -114,13 +114,13 @@ public class PriceTest { Assert.assertEquals( "Too many decimals should get rounded up properly.", - "10000.2346 LTC/BTC", + "10000.2346 LTC/XMR", Price.valueOf("LTC", 1000023456789L).toFriendlyString() ); Assert.assertEquals( "Too many decimals should get rounded down properly.", - "10000.2345 LTC/BTC", + "10000.2345 LTC/XMR", Price.valueOf("LTC", 1000023454999L).toFriendlyString() ); diff --git a/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java b/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java index 307247a3cb..7f75998010 100644 --- a/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java +++ b/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java @@ -24,8 +24,8 @@ import bisq.core.offer.OpenOffer; import bisq.core.util.ParsingUtils; import bisq.proto.grpc.CancelOfferReply; import bisq.proto.grpc.CancelOfferRequest; -import bisq.proto.grpc.CreateOfferReply; -import bisq.proto.grpc.CreateOfferRequest; +import bisq.proto.grpc.PostOfferReply; +import bisq.proto.grpc.PostOfferRequest; import bisq.proto.grpc.GetMyOfferReply; import bisq.proto.grpc.GetMyOfferRequest; import bisq.proto.grpc.GetMyOffersReply; @@ -140,15 +140,15 @@ class GrpcOffersService extends OffersImplBase { } @Override - public void createOffer(CreateOfferRequest req, - StreamObserver responseObserver) { + public void postOffer(PostOfferRequest req, + StreamObserver responseObserver) { GrpcErrorMessageHandler errorMessageHandler = - new GrpcErrorMessageHandler(getCreateOfferMethod().getFullMethodName(), + new GrpcErrorMessageHandler(getPostOfferMethod().getFullMethodName(), responseObserver, exceptionHandler, log); try { - coreApi.createAnPlaceOffer( + coreApi.postOffer( req.getCurrencyCode(), req.getDirection(), req.getPrice(), @@ -164,7 +164,7 @@ class GrpcOffersService extends OffersImplBase { // the new offer to the gRPC client after async placement is done. OpenOffer openOffer = coreApi.getMyOpenOffer(offer.getId()); OfferInfo offerInfo = OfferInfo.toMyOfferInfo(openOffer); - CreateOfferReply reply = CreateOfferReply.newBuilder() + PostOfferReply reply = PostOfferReply.newBuilder() .setOffer(offerInfo.toProtoMessage()) .build(); responseObserver.onNext(reply); @@ -206,7 +206,7 @@ class GrpcOffersService extends OffersImplBase { put(getGetMyOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getGetOffersMethod().getFullMethodName(), new GrpcCallRateMeter(20, SECONDS)); put(getGetMyOffersMethod().getFullMethodName(), new GrpcCallRateMeter(20, SECONDS)); - put(getCreateOfferMethod().getFullMethodName(), new GrpcCallRateMeter(20, SECONDS)); + put(getPostOfferMethod().getFullMethodName(), new GrpcCallRateMeter(20, SECONDS)); put(getCancelOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); }} ))); diff --git a/proto/src/main/proto/grpc.proto b/proto/src/main/proto/grpc.proto index 932f5ce2a1..ea52d5cd98 100644 --- a/proto/src/main/proto/grpc.proto +++ b/proto/src/main/proto/grpc.proto @@ -446,7 +446,7 @@ service Offers { } rpc GetMyOffers (GetMyOffersRequest) returns (GetMyOffersReply) { } - rpc CreateOffer (CreateOfferRequest) returns (CreateOfferReply) { + rpc PostOffer (PostOfferRequest) returns (PostOfferReply) { } rpc CancelOffer (CancelOfferRequest) returns (CancelOfferReply) { } @@ -486,7 +486,7 @@ message GetMyOffersReply { repeated OfferInfo offers = 1; } -message CreateOfferRequest { +message PostOfferRequest { string currency_code = 1; string direction = 2; string price = 3; @@ -499,7 +499,7 @@ message CreateOfferRequest { string payment_account_id = 10; } -message CreateOfferReply { +message PostOfferReply { OfferInfo offer = 1; }