@drift-labs/common
Version:
Common functions for Drift
75 lines (74 loc) • 3.22 kB
TypeScript
import { CandleResolution } from '@drift-labs/sdk';
import { JsonCandle, MarketId } from '../types';
import { UIEnv } from '../types/UIEnv';
/**
* # CANDLE CLIENT HIGH LEVEL EXPLANATION:
* The Candle Client uses the Data API (see https://data.api.drift.trade/playground) to source candles to display.
*
* There are two key parts of the client:
* - Fetching Candles
* - Subscribing to Candles
*
* ## Fetching Candles:
* - We can fetch candles between any timestamp range.
* - The maximum number of candles we can fetch in a single request is 1000 (see CANDLE_FETCH_LIMIT).
* - We define "recent history" to be the last 1000 candles .. basically whatever comes back from the infra when we don't use a startTs and use the maximum fetch limit.
* - We want to avoid using high cardinality parameters in the fetch because otherwise we will miss the cache in the infra.
* - A concrete example of this is that we don't attach a startTs parameter when we are fetching candles within recent history (past 1000 candles)
* - We cache the recent candles in memory so that any subsequent fetches within recent history after the first one will be served from cache.
* - e.g. moving back to a further timeframe on TradingView - the required candles could potentially be in the cache, so we don't need to refetch them.
*
* ## Subscribing to Candles:
* - We subscribe to a websocket endpoint for a given market and resolution.
* - We allow the client to support multiple concurrent subscriptions, because the TradingView comopnent will sometimes do this when switching between markets.
*
* ## Possible Improvements:
* - Create a more advanced cache which can store more than the most recent 1000 candles, dynamically growing as more candles are added (for now seems unnecessary, rare for someone to go back further than 1000 candles)
*/
type CandleFetchConfig = {
env: UIEnv;
marketId: MarketId;
resolution: CandleResolution;
fromTs: number;
toTs: number;
};
type CandleSubscriptionConfig = {
resolution: CandleResolution;
marketId: MarketId;
env: UIEnv;
};
type CandleSubscriberEvents = {
'candle-update': JsonCandle;
};
/**
* This class will subscribe to candles from the Drift Data API.
*
* Note: If you are using TradingView you probably want to just use the DriftTvFeed class instead.
*/
export declare class CandleClient {
private activeSubscriptions;
constructor();
subscribe: (config: CandleSubscriptionConfig, subscriptionKey: string) => Promise<void>;
/**
*
* @param config {
*
* env: UIEnv;
*
* marketId: MarketId;
*
* resolution: CandleResolution;
*
* fromTs: number; // Seconds :: This should be the START (oldest) timestamp of the candles to fetch
*
* toTs: number; // Seconds :: This should be the END (newest) timestamp of the candles to fetch
*
* }
* @returns
*/
fetch: (config: CandleFetchConfig) => Promise<JsonCandle[]>;
unsubscribe: (subscriptionKey: string) => void;
unsubscribeAll: () => void;
on(subscriptionKey: string, event: keyof CandleSubscriberEvents, listener: (candle: JsonCandle) => void): void;
}
export {};