Factor out duplicated OfferInfo wrapping

This commit is contained in:
ghubstan 2020-09-27 17:06:08 -03:00
parent 70531693f0
commit 82b7b79c5b
No known key found for this signature in database
GPG key ID: E35592D6800A861E

View file

@ -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<GetOffersReply> 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<OfferInfo> 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<CreateOfferReply> 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();
}
}