altangent / ccxws

WebSocket client for 38 cryptocurrency exchanges
MIT License
619 stars 187 forks source link

TypeScript support #268

Closed Industrial closed 3 years ago

Industrial commented 3 years ago

Hi!

I would love to have typescript support for this library :-)

gr,

Tom

bmancini55 commented 3 years ago

Hi Tom, thanks for submitting the issue! Converting the library to TypeScript is a high priority since it enables a lot of refactor work and improvements that are in motion. I'm going to close this issue as a duplicate of #229 but look for updates in the near future.

Shepless commented 3 years ago

@Industrial @bmancini55 I quickly put this together and it's working fine for me. Steps for usage:

1) Create a typings directory 2) Create a new file ccxws.d.ts file 3) Copy and paste type definitions (below) 4) Update your tsconfig.json to include the new typings (example below)

tsconfig.json

"compilerOptions": {
        // ... your existing config
        "typeRoots": [
            "./node_modules/@types",
            "./typings"
        ]
}

Type Defintions

declare module "ccxws" {
    type ExchangeCtor = new (options?: IExchangeOptions) => IExchange;
    type ConnectionEventType = 'connecting' | 'connected' | 'disconnected' | 'closing' | 'closed' | 'reconnecting';
    type ExchangeEventType = 'ticker' | 'candle' | 'trade' | 'l2snapshot' | 'l2update' | 'l3snapshot' | 'l3update' | 'error';
    type EventType = ConnectionEventType | ExchangeEventType;
    type CandlePeriod = {
        _1m: '_1m',
        _2m: '_2m',
        _3m: '_3m',
        _5m: '_5m',
        _15m: '_15m',
        _30m: '_30m',
        _1h: '_1h',
        _2h: '_2h',
        _4h: '_4h',
        _6h: '_6h',
        _8h: '_8h',
        _12h: '_12h',
        _1d: '_1d',
        _3d: '_3d',
        _1w: '_1w',
        _2w: '_2w',
        _1M: '_1M'
    };

    interface ITicker {
        exchange: string;
        base: string;
        quote: string;
        timestamp: number;
        last: string;
        open: string;
        low: string;
        high: string;
        volume: string;
        quoteVolume: string;
        change: string;
        changePercent: string;
        bid: string;
        bidVolume: string;
        ask: string;
        askVolume: string;
    }

    interface ITrade {
        exchange: string;
        base: string;
        quote: string;
        tradeId: string;
        unix: number;
        side: string;
        price: string;
        amount: string;
        buyOrderId?: string;
        sellOrderId?: string;
    }

    interface ICandle {
        timestampMs: number;
        open: string;
        high: string;
        low: string;
        close: string;
        volume: string;
    }

    interface ILevelData<T> {
        exchange: string;
        base: string;
        quote: string;
        timestampMs?: number;
        sequenceId?: number;
        asks: T[];
        bids: T[];
    }

    interface ILevel2Point {
        price: string;
        size: string;
        count?: number;
    }

    interface ILevel3Point {
        orderId: string;
        price: string;
        size: string;
        meta?: any;
    }

    interface ILevel2Data extends ILevelData<ILevel2Point> { }

    interface ILevel3Data extends ILevelData<ILevel3Point> { }

    interface IMarket {
        id: string;
        base: string;
        quote: string;
        type?: string;
    }

    interface IExchangeOptions {
        wssPath?: string;
        watcherMs?: number;
        apiKey?: string
        apiSecret?: string;
    }

    interface EventCallbackMap {
        'connecting': () => void;
        'connected': () => void;
        'disconnected': () => void;
        'closing': () => void;
        'closed': () => void;
        'reconnecting': () => void;
        'error': (error: Error) => void;
        'ticker': (ticker: ITicker, market: IMarket) => void;
        'trade': (trade: ITrade, market: IMarket) => void;
        'candle': (candle: ICandle, market: IMarket) => void;
        'l2snapshot': (snapshot: ILevel2Data, market: IMarket) => void;
        'l2update': (snapshot: ILevel2Data, market: IMarket) => void;
        'l3snapshot': (snapshot: ILevel3Data, market: IMarket) => void;
        'l3update': (snapshot: ILevel3Data, market: IMarket) => void;
    }

    interface IExchange {
        candlePeriod: keyof CandlePeriod;
        hasTickers: boolean;
        hasTrades: boolean;
        hasCandles: boolean;
        hasLevel2Snapshots: boolean;
        hasLevel2Updates: boolean;
        hasLevel3Snapshots: boolean;
        hasLevel3Updates: boolean;
        on<T extends EventType>(event: T, callback: EventCallbackMap[T]): void;
        subscribeTicker(market: IMarket): void;
        unsubscribeTicker(market: IMarket): void;
        subscribeTrades(market: IMarket): void;
        unsubscribeTrades(market: IMarket): void;
        subscribeCandles(market: IMarket): void;
        unsubscribeCandles(market: IMarket): void;
        subscribeLevel2Snapshots(market: IMarket): void;
        unsubscribeLevel2Snapshots(market: IMarket): void;
        subscribeLevel2Updates(market: IMarket): void;
        unsubscribeLevel2Updates(market: IMarket): void;
        subscribeLevel3Snapshots(market: IMarket): void;
        unsubscribeLevel3Snapshots(market: IMarket): void;
        subscribeLevel3Updates(market: IMarket): void;
        unsubscribeLevel3Updates(market: IMarket): void;
    }

    interface ICCXWS {
        Bibox: ExchangeCtor;
        Binance: ExchangeCtor;
        BinanceFuturesCoinM: ExchangeCtor;
        BinanceFuturesUsdtM: ExchangeCtor;
        BinanceJe: ExchangeCtor;
        BinanceUs: ExchangeCtor;
        Bitfinex: ExchangeCtor;
        Bitflyer: ExchangeCtor;
        Bithumb: ExchangeCtor;
        BitMEX: ExchangeCtor;
        Bitstamp: ExchangeCtor;
        Bittrex: ExchangeCtor;
        Cex: ExchangeCtor;
        CoinbasePro: ExchangeCtor;
        Coinex: ExchangeCtor;
        Deribit: ExchangeCtor;
        Digifinex: ExchangeCtor;
        Ethfinex: ExchangeCtor;
        ErisX: ExchangeCtor;
        Ftx: ExchangeCtor;
        FtxUs: ExchangeCtor;
        Gateio: ExchangeCtor;
        Gemini: ExchangeCtor;
        HitBTC: ExchangeCtor;
        Huobi: ExchangeCtor;
        HuobiFutures: ExchangeCtor;
        HuobiSwaps: ExchangeCtor;
        HuobiJapan: ExchangeCtor;
        HuobiKorea: ExchangeCtor;
        HuobiRussia: ExchangeCtor;
        Kucoin: ExchangeCtor;
        Kraken: ExchangeCtor;
        LedgerX: ExchangeCtor;
        Liquid: ExchangeCtor;
        OKEx: ExchangeCtor;
        Poloniex: ExchangeCtor;
        Upbit: ExchangeCtor;
        Zb: ExchangeCtor;
        CandlePeriod: CandlePeriod;
    }

    const ccxws: ICCXWS;
    export default ccxws;
}