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

estonlabs.cxtl.exchanges.binance.lib.BinanceStreamFactory Maven / Gradle / Ivy

There is a newer version: 1.4.14
Show newest version
package estonlabs.cxtl.exchanges.binance.lib;

import estonlabs.cxtl.common.AbstractStreamFactory;
import estonlabs.cxtl.common.EnvironmentType;
import estonlabs.cxtl.common.codec.Codec;
import estonlabs.cxtl.common.codec.JacksonCodec;
import estonlabs.cxtl.common.stream.core.WebsocketConnection;
import estonlabs.cxtl.exchanges.a.specification.domain.AssetClass;
import estonlabs.cxtl.exchanges.a.specification.domain.Exchange;
import estonlabs.cxtl.exchanges.binance.fapi.domain.ListenKey;
import estonlabs.cxtl.exchanges.binance.fapi.domain.stream.BinanceInboundContainer;
import estonlabs.cxtl.exchanges.binance.fapi.domain.stream.BinanceOutboundMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Map;

public class BinanceStreamFactory extends AbstractStreamFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(BinanceStreamFactory.class);

    private static final Map> URLS = Map.of(
            EnvironmentType.PROD,
            Map.of(AssetClass.PERP, URI.create("wss://fstream.binance.com/ws"),
                    AssetClass.PERP_INVERSE, URI.create("wss://dstream.binance.com/ws"),
                    AssetClass.FUTURE, URI.create("wss://dstream.binance.com/ws"),
                    AssetClass.SPOT, URI.create("wss://stream.binance.com/ws")),
            EnvironmentType.TEST_NET,
            Map.of(AssetClass.PERP, URI.create("wss://stream.binancefuture.com/ws"),
                    AssetClass.PERP_INVERSE, URI.create("wss://dstream.binancefuture.com/ws"),
                    AssetClass.FUTURE, URI.create("wss://dstream.binancefuture.com/ws"),
                    AssetClass.SPOT, URI.create("wss://stream.binance.com/ws")));

    private static final Codec CODEC = new JacksonCodec();
    private final EnvironmentType env;

    public BinanceStreamFactory(EnvironmentType env) {
        super(Exchange.BINANCE, CODEC, BinanceInboundContainer.class);
        this.env = env;
    }
    private BinanceCex cex;

    public BinanceStreamFactory cex(BinanceCex cex) {
        this.cex = cex;
        return this;
    }

    public static BinanceStreamFactory prod() {
        return new BinanceStreamFactory(EnvironmentType.PROD);
    }

    @Override
    protected BinanceStreamFactory me() {
        return this;
    }

    public WebsocketConnection createPrivateStream(AssetClass assetClass) {
        if(cex == null){
            throw new IllegalStateException("A BinanceCex must be set to create a private websocket for Binance");
        }

        if(credentials == null){
            throw new IllegalStateException("Credentials must be set to create a private websocket for Binance");
        }
        ListenKey listenKey = cex.createListenKey(credentials, assetClass).block();
        if(listenKey == null || listenKey.getListenKey() == null){
            throw new IllegalStateException("Could not get a listen key for Binance");
        }
        //10 minutes
        ping(1000*60*10, ()->{
            cex.refreshListenKey(credentials, assetClass).subscribe();
        },false);

        String url = URLS.get(env).get(assetClass).toString()+"/"+listenKey.getListenKey();
        return newPrivateWebsocketWithProxy(URI.create(url),httpProxy,null);
    }

    public WebsocketConnection createPublicStream(AssetClass assetClass) {
        return newPublicWebsocket(URLS.get(env).get(assetClass), null);
    }
}