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

info.bitrich.xchangestream.serum.SerumStreamingExchange Maven / Gradle / Ivy

The newest version!
package info.bitrich.xchangestream.serum;

import com.knowm.xchange.serum.SerumConfigs.Solana;
import com.knowm.xchange.serum.SerumExchange;
import info.bitrich.xchangestream.core.ProductSubscription;
import info.bitrich.xchangestream.core.StreamingExchange;
import info.bitrich.xchangestream.core.StreamingMarketDataService;
import info.bitrich.xchangestream.service.netty.ConnectionStateModel;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Observable;
import org.knowm.xchange.ExchangeSpecification;

public class SerumStreamingExchange extends SerumExchange implements StreamingExchange {

  private SerumStreamingService streamingService;

  @Override
  public Completable connect(ProductSubscription... args) {

    final String url =
        Solana.valueOf(
                String.valueOf(getExchangeSpecification().getExchangeSpecificParametersItem("Env")))
            .wsUrl();
    this.streamingService = new SerumStreamingService(url);
    applyStreamingSpecification(getExchangeSpecification(), this.streamingService);
    return this.streamingService.connect();
  }

  @Override
  public Completable disconnect() {
    final SerumStreamingService service = streamingService;
    streamingService = null;
    return service.disconnect();
  }

  @Override
  public Observable reconnectFailure() {
    return streamingService.subscribeReconnectFailure();
  }

  @Override
  public Observable connectionSuccess() {
    return streamingService.subscribeConnectionSuccess();
  }

  @Override
  public Observable connectionStateObservable() {
    return streamingService.subscribeConnectionState();
  }

  @Override
  public boolean isAlive() {
    return streamingService != null && streamingService.isSocketOpen();
  }

  @Override
  public StreamingMarketDataService getStreamingMarketDataService() {
    return null;
  }

  @Override
  public void useCompressedMessages(boolean compressedMessages) {
    streamingService.useCompressedMessages(compressedMessages);
  }

  @Override
  public ExchangeSpecification getDefaultExchangeSpecification() {
    final ExchangeSpecification exchangeSpec = new ExchangeSpecification(this.getClass());
    exchangeSpec.setSslUri(Solana.MAINNET.restUrl());
    exchangeSpec.setHost("projectserum.com");
    exchangeSpec.setPort(80);
    exchangeSpec.setExchangeName("Serum");
    exchangeSpec.setExchangeDescription(
        "Serum is a decentralized cryptocurrency exchange built on Solana.");
    exchangeSpec.setExchangeSpecificParametersItem("Env", "MAINNET");

    return exchangeSpec;
  }
}