info.bitrich.xchangestream.kraken.KrakenStreamingExchange Maven / Gradle / Ivy
package info.bitrich.xchangestream.kraken;
import com.google.common.base.MoreObjects;
import info.bitrich.xchangestream.core.ProductSubscription;
import info.bitrich.xchangestream.core.StreamingExchange;
import info.bitrich.xchangestream.core.StreamingMarketDataService;
import info.bitrich.xchangestream.core.StreamingTradeService;
import info.bitrich.xchangestream.kraken.dto.KrakenSubscriptionStatusMessage;
import info.bitrich.xchangestream.kraken.dto.KrakenSystemStatus;
import info.bitrich.xchangestream.kraken.dto.enums.KrakenEventType;
import info.bitrich.xchangestream.service.netty.ConnectionStateModel.State;
import io.reactivex.Completable;
import io.reactivex.Observable;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.knowm.xchange.ExchangeSpecification;
import org.knowm.xchange.kraken.KrakenExchange;
import org.knowm.xchange.kraken.dto.account.KrakenWebsocketToken;
import org.knowm.xchange.kraken.service.KrakenAccountServiceRaw;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** @author makarid */
public class KrakenStreamingExchange extends KrakenExchange implements StreamingExchange {
private static final Logger LOG = LoggerFactory.getLogger(KrakenStreamingExchange.class);
private static final String USE_BETA = "Use_Beta";
private static final String USE_SPREAD_FOR_TICKER = "Spread_For_Ticker";
private static final String API_URI = "wss://ws.kraken.com";
private static final String API_AUTH_URI = "wss://ws-auth.kraken.com";
private static final String API_BETA_URI = "wss://beta-ws.kraken.com";
private KrakenStreamingService streamingService, privateStreamingService;
private KrakenStreamingMarketDataService streamingMarketDataService;
private KrakenStreamingTradeService streamingTradeService;
public KrakenStreamingExchange() {}
public static String pickUri(boolean isPrivate, boolean useBeta) {
return useBeta ? API_BETA_URI : isPrivate ? API_AUTH_URI : API_URI;
}
@Override
protected void initServices() {
super.initServices();
Boolean useBeta =
MoreObjects.firstNonNull(
(Boolean) exchangeSpecification.getExchangeSpecificParametersItem(USE_BETA),
Boolean.FALSE);
Boolean spreadForTicker =
MoreObjects.firstNonNull(
(Boolean) exchangeSpecification.getExchangeSpecificParametersItem(USE_SPREAD_FOR_TICKER),
Boolean.FALSE);
KrakenAccountServiceRaw accountService = (KrakenAccountServiceRaw) getAccountService();
this.streamingService =
new KrakenStreamingService(this, false, pickUri(false, useBeta), () -> authData(accountService));
applyStreamingSpecification(getExchangeSpecification(), streamingService);
this.streamingMarketDataService = new KrakenStreamingMarketDataService(streamingService, spreadForTicker);
if (StringUtils.isNotEmpty(exchangeSpecification.getApiKey())) {
this.privateStreamingService =
new KrakenStreamingService(this, true, pickUri(true, useBeta), () -> authData(accountService));
applyStreamingSpecification(getExchangeSpecification(), privateStreamingService);
}
streamingTradeService = new KrakenStreamingTradeService(privateStreamingService);
}
@Override
public Completable connect(ProductSubscription... args) {
if (privateStreamingService != null)
return privateStreamingService.connect().mergeWith(streamingService.connect());
return streamingService.connect();
}
@Override
public Completable disconnect() {
if (privateStreamingService != null)
return privateStreamingService.disconnect().mergeWith(streamingService.disconnect());
return streamingService.disconnect();
}
@Override
public boolean isAlive() {
return streamingService.isSocketOpen()
&& (privateStreamingService == null || privateStreamingService.isSocketOpen());
}
@Override
public Observable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy