From 82b7b79c5b4252fd6ecfcaac82b7cd4f5769c3b7 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:06:08 -0300 Subject: [PATCH] Factor out duplicated OfferInfo wrapping --- .../bisq/daemon/grpc/GrpcOffersService.java | 72 ++++++++----------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java b/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java index fd9e41a772..258886aed8 100644 --- a/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java +++ b/daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java @@ -35,6 +35,7 @@ import io.grpc.stub.StreamObserver; import javax.inject.Inject; import java.util.List; +import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.stream.Collectors; @@ -53,30 +54,9 @@ class GrpcOffersService extends OffersGrpc.OffersImplBase { @Override public void getOffers(GetOffersRequest req, StreamObserver responseObserver) { - // The client cannot see bisq.core.Offer or its fromProto method. - // We use the lighter weight OfferInfo proto wrapper instead, containing just - // enough fields to view and create offers. List result = coreApi.getOffers(req.getDirection(), req.getCurrencyCode()) - .stream().map(offer -> new OfferInfo.OfferInfoBuilder() - .withId(offer.getId()) - .withDirection(offer.getDirection().name()) - .withPrice(offer.getPrice().getValue()) - .withUseMarketBasedPrice(offer.isUseMarketBasedPrice()) - .withMarketPriceMargin(offer.getMarketPriceMargin()) - .withAmount(offer.getAmount().value) - .withMinAmount(offer.getMinAmount().value) - .withVolume(offer.getVolume().getValue()) - .withMinVolume(offer.getMinVolume().getValue()) - .withBuyerSecurityDeposit(offer.getBuyerSecurityDeposit().value) - .withPaymentAccountId("") // only used when creating offer (?) - .withPaymentMethodId(offer.getPaymentMethod().getId()) - .withPaymentMethodShortName(offer.getPaymentMethod().getShortName()) - .withBaseCurrencyCode(offer.getOfferPayload().getBaseCurrencyCode()) - .withCounterCurrencyCode(offer.getOfferPayload().getCounterCurrencyCode()) - .withDate(offer.getDate().getTime()) - .build()) + .stream().map(this::toOfferInfo) .collect(Collectors.toList()); - var reply = GetOffersReply.newBuilder() .addAllOffers( result.stream() @@ -92,9 +72,7 @@ class GrpcOffersService extends OffersGrpc.OffersImplBase { StreamObserver responseObserver) { CountDownLatch latch = new CountDownLatch(1); try { - TransactionResultHandler resultHandler = transaction -> { - latch.countDown(); - }; + TransactionResultHandler resultHandler = transaction -> latch.countDown(); Offer offer = coreApi.createOffer( req.getCurrencyCode(), req.getDirection(), @@ -111,25 +89,7 @@ class GrpcOffersService extends OffersGrpc.OffersImplBase { } catch (InterruptedException ignored) { // empty } - - OfferInfo offerInfo = new OfferInfo.OfferInfoBuilder() - .withId(offer.getId()) - .withDirection(offer.getDirection().name()) - .withPrice(offer.getPrice().getValue()) - .withUseMarketBasedPrice(offer.isUseMarketBasedPrice()) - .withMarketPriceMargin(offer.getMarketPriceMargin()) - .withAmount(offer.getAmount().value) - .withMinAmount(offer.getMinAmount().value) - .withVolume(offer.getVolume().getValue()) - .withMinVolume(offer.getMinVolume().getValue()) - .withBuyerSecurityDeposit(offer.getBuyerSecurityDeposit().value) - .withPaymentAccountId(offer.getMakerPaymentAccountId()) - .withPaymentMethodId(offer.getPaymentMethod().getId()) - .withPaymentMethodShortName(offer.getPaymentMethod().getShortName()) - .withBaseCurrencyCode(offer.getOfferPayload().getBaseCurrencyCode()) - .withCounterCurrencyCode(offer.getOfferPayload().getCounterCurrencyCode()) - .withDate(offer.getDate().getTime()) - .build(); + OfferInfo offerInfo = toOfferInfo(offer); CreateOfferReply reply = CreateOfferReply.newBuilder().setOffer(offerInfo.toProtoMessage()).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); @@ -139,4 +99,28 @@ class GrpcOffersService extends OffersGrpc.OffersImplBase { throw ex; } } + + // The client cannot see bisq.core.Offer or its fromProto method. + // We use the lighter weight OfferInfo proto wrapper instead, containing just + // enough fields to view and create offers. + private OfferInfo toOfferInfo(Offer offer) { + return new OfferInfo.OfferInfoBuilder() + .withId(offer.getId()) + .withDirection(offer.getDirection().name()) + .withPrice(Objects.requireNonNull(offer.getPrice()).getValue()) + .withUseMarketBasedPrice(offer.isUseMarketBasedPrice()) + .withMarketPriceMargin(offer.getMarketPriceMargin()) + .withAmount(offer.getAmount().value) + .withMinAmount(offer.getMinAmount().value) + .withVolume(Objects.requireNonNull(offer.getVolume()).getValue()) + .withMinVolume(Objects.requireNonNull(offer.getMinVolume()).getValue()) + .withBuyerSecurityDeposit(offer.getBuyerSecurityDeposit().value) + .withPaymentAccountId(offer.getMakerPaymentAccountId()) + .withPaymentMethodId(offer.getPaymentMethod().getId()) + .withPaymentMethodShortName(offer.getPaymentMethod().getShortName()) + .withBaseCurrencyCode(offer.getOfferPayload().getBaseCurrencyCode()) + .withCounterCurrencyCode(offer.getOfferPayload().getCounterCurrencyCode()) + .withDate(offer.getDate().getTime()) + .build(); + } }