Skip to content

Commit

Permalink
Sort API gettrades's response list by trade date
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubstan committed Jan 26, 2022
1 parent 4c8cec5 commit f231aac
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions core/src/main/java/bisq/core/api/CoreTradesService.java
Expand Up @@ -49,21 +49,27 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import static bisq.core.btc.model.AddressEntry.Context.TRADE_PAYOUT;
import static bisq.proto.grpc.GetTradesRequest.Category.CLOSED;
import static java.lang.String.format;
import static java.util.Comparator.comparing;

@Singleton
@Slf4j
class CoreTradesService {

private final Supplier<Comparator<TradeModel>> dateComparator = () ->
comparing(TradeModel::getDate);

private final CoreContext coreContext;
// Dependencies on core api services in this package must be kept to an absolute
// minimum, but some trading functions require an unlocked wallet's key, so an
Expand Down Expand Up @@ -292,7 +298,10 @@ Trade getTrade(String tradeId) {
List<TradeModel> getOpenTrades() {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
return tradeManager.getTrades().stream().map(t -> (TradeModel) t).collect(Collectors.toList());
return tradeManager.getTrades().stream()
.map(t -> (TradeModel) t)
.sorted(dateComparator.get())
.collect(Collectors.toList());
}

List<TradeModel> getTradeHistory(GetTradesRequest.Category category) {
Expand All @@ -303,16 +312,18 @@ List<TradeModel> getTradeHistory(GetTradesRequest.Category category) {
.map(t -> (TradeModel) t)
.collect(Collectors.toList());
closedTrades.addAll(bsqSwapTradeManager.getBsqSwapTrades());
// TODO Sort closedTrades by date?
return closedTrades;
return closedTrades.stream().sorted(dateComparator.get()).collect(Collectors.toList());
} else {
var failedV1Trades = failedTradesManager.getTrades();
return failedV1Trades.stream().map(t -> (TradeModel) t).collect(Collectors.toList());
return failedV1Trades.stream()
.map(t -> (TradeModel) t)
.sorted(dateComparator.get())
.collect(Collectors.toList());
}
}

void failTrade(String tradeId) {
// TODO Recommend that API users should use this method with extra care because
// TODO Recommend API users call this method with extra care because
// the API lacks methods for diagnosing trade problems, and does not support
// interaction with mediators. Users may accidentally fail valid trades,
// although they can easily be un-failed with the 'unfailtrade' method.
Expand Down

0 comments on commit f231aac

Please sign in to comment.