fix bug getting crypto offers caused by base/counter inversion (#245)

This commit is contained in:
woodser 2022-02-16 10:09:52 -05:00 committed by GitHub
parent 5b038697c3
commit 45a61ae835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View File

@ -81,7 +81,6 @@ class CoreOffersService {
private final OfferBookService offerBookService; private final OfferBookService offerBookService;
private final OfferFilter offerFilter; private final OfferFilter offerFilter;
private final OpenOfferManager openOfferManager; private final OpenOfferManager openOfferManager;
private final OfferUtil offerUtil;
private final User user; private final User user;
private final XmrWalletService xmrWalletService; private final XmrWalletService xmrWalletService;
@ -103,7 +102,6 @@ class CoreOffersService {
this.offerBookService = offerBookService; this.offerBookService = offerBookService;
this.offerFilter = offerFilter; this.offerFilter = offerFilter;
this.openOfferManager = openOfferManager; this.openOfferManager = openOfferManager;
this.offerUtil = offerUtil;
this.user = user; this.user = user;
this.xmrWalletService = xmrWalletService; this.xmrWalletService = xmrWalletService;
} }
@ -121,11 +119,13 @@ class CoreOffersService {
} }
Offer getMyOffer(String id) { Offer getMyOffer(String id) {
return offerBookService.getOffers().stream() Offer offer = offerBookService.getOffers().stream()
.filter(o -> o.getId().equals(id)) .filter(o -> o.getId().equals(id))
.filter(o -> o.isMyOffer(keyRing)) .filter(o -> o.isMyOffer(keyRing))
.findAny().orElseThrow(() -> .findAny().orElseThrow(() ->
new IllegalStateException(format("offer with id '%s' not found", id))); new IllegalStateException(format("offer with id '%s' not found", id)));
setOpenOfferState(offer);
return offer;
} }
List<Offer> getOffers(String direction, String currencyCode) { List<Offer> getOffers(String direction, String currencyCode) {
@ -152,7 +152,7 @@ class CoreOffersService {
.collect(Collectors.toList()); .collect(Collectors.toList());
// remove unreserved offers // remove unreserved offers
Set<Offer> unreservedOffers = getUnreservedOffers(offers); Set<Offer> unreservedOffers = getUnreservedOffers(offers); // TODO (woodser): optimize performance, probably don't call here
offers.removeAll(unreservedOffers); offers.removeAll(unreservedOffers);
// remove my unreserved offers from offer manager // remove my unreserved offers from offer manager
@ -162,11 +162,8 @@ class CoreOffersService {
} }
openOfferManager.removeOpenOffers(unreservedOpenOffers, null); openOfferManager.removeOpenOffers(unreservedOpenOffers, null);
// set offer state // set offer states
for (Offer offer : offers) { for (Offer offer : offers) setOpenOfferState(offer);
Optional<OpenOffer> openOffer = openOfferManager.getOpenOfferById(offer.getId());
if (openOffer.isPresent()) offer.setState(openOffer.get().getState() == OpenOffer.State.AVAILABLE ? Offer.State.AVAILABLE : Offer.State.NOT_AVAILABLE);
}
return offers; return offers;
} }
@ -322,11 +319,16 @@ class CoreOffersService {
String direction, String direction,
String currencyCode) { String currencyCode) {
var offerOfWantedDirection = offer.getDirection().name().equalsIgnoreCase(direction); var offerOfWantedDirection = offer.getDirection().name().equalsIgnoreCase(direction);
var offerInWantedCurrency = offer.getOfferPayload().getCounterCurrencyCode() var counterAssetCode = isCryptoCurrency(currencyCode) ? offer.getOfferPayload().getBaseCurrencyCode() : offer.getOfferPayload().getCounterCurrencyCode(); // TODO: crypto pairs invert base and counter currencies
.equalsIgnoreCase(currencyCode); var offerInWantedCurrency = counterAssetCode.equalsIgnoreCase(currencyCode);
return offerOfWantedDirection && offerInWantedCurrency; return offerOfWantedDirection && offerInWantedCurrency;
} }
private void setOpenOfferState(Offer offer) {
Optional<OpenOffer> openOffer = openOfferManager.getOpenOfferById(offer.getId());
if (openOffer.isPresent()) offer.setState(openOffer.get().getState() == OpenOffer.State.AVAILABLE ? Offer.State.AVAILABLE : Offer.State.NOT_AVAILABLE);
}
private Comparator<Offer> priceComparator(String direction) { private Comparator<Offer> priceComparator(String direction) {
// A buyer probably wants to see sell orders in price ascending order. // A buyer probably wants to see sell orders in price ascending order.
// A seller probably wants to see buy orders in price descending order. // A seller probably wants to see buy orders in price descending order.

View File

@ -193,8 +193,8 @@ class GrpcOffersService extends OffersImplBase {
new HashMap<>() {{ new HashMap<>() {{
put(getGetOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getGetOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS));
put(getGetMyOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getGetMyOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS));
put(getGetOffersMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getGetOffersMethod().getFullMethodName(), new GrpcCallRateMeter(20, SECONDS));
put(getGetMyOffersMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getGetMyOffersMethod().getFullMethodName(), new GrpcCallRateMeter(20, SECONDS));
put(getCreateOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getCreateOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS));
put(getCancelOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS)); put(getCancelOfferMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS));
}} }}