All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.bitrich.xchangestream.coinjar.CoinjarStreamingTradeService Maven / Gradle / Ivy

package info.bitrich.xchangestream.coinjar;

import com.fasterxml.jackson.databind.ObjectMapper;
import info.bitrich.xchangestream.coinjar.dto.CoinjarWebSocketOrderEvent;
import info.bitrich.xchangestream.coinjar.dto.CoinjarWebSocketUserTradeEvent;
import info.bitrich.xchangestream.core.StreamingTradeService;
import info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper;
import io.reactivex.rxjava3.core.Observable;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.trade.UserTrade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class CoinjarStreamingTradeService implements StreamingTradeService {

  private static final Logger logger = LoggerFactory.getLogger(CoinjarStreamingTradeService.class);

  private final ObjectMapper mapper = StreamingObjectMapperHelper.getObjectMapper();

  private final CoinjarStreamingService service;

  private final String userTradeChannel = "private";

  public CoinjarStreamingTradeService(CoinjarStreamingService service) {
    this.service = service;
  }

  @Override
  public Observable getUserTrades(CurrencyPair currencyPair, Object... args) {
    return service
        .subscribeChannel(userTradeChannel)
        .filter(node -> node.get("event").textValue().equals("private:fill"))
        .map(
            node -> {
              return mapper.treeToValue(node, CoinjarWebSocketUserTradeEvent.class);
            })
        .map(CoinjarStreamingAdapters::adaptUserTrade)
        .filter(userTrade -> currencyPair == null || currencyPair == userTrade.getInstrument());
  }

  @Override
  public Observable getOrderChanges(CurrencyPair currencyPair, Object... args) {
    return service
        .subscribeChannel(userTradeChannel)
        .filter(node -> node.get("event").textValue().equals("private:order"))
        .map(
            node -> {
              return mapper.treeToValue(node, CoinjarWebSocketOrderEvent.class);
            })
        .map(CoinjarStreamingAdapters::adaptOrder)
        .filter(order -> currencyPair == null || currencyPair == order.getInstrument());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy