@pear-protocol/chart-sdk
Advanced tools
+2
-0
@@ -16,3 +16,5 @@ import { Connector } from '@pear-protocol/types'; | ||
| subscribeRealtimeBars(chartType: ChartType, callback: RealtimeBarCallback): string; | ||
| subscribeRealtimeAssetBars(symbol: string, callback: RealtimeBarCallback): string; | ||
| unsubscribeRealtimeBars(id: string): void; | ||
| private assertTokenExists; | ||
| clearCache(): void; | ||
@@ -19,0 +21,0 @@ destroy(): void; |
+41
-3
| import { DataCollector } from './collector'; | ||
| import { computePerformanceCandles, computePriceRatioCandles, computeWeightedRatioCandles, computeAssetBars, computeRealtimePerformanceBar, computeRealtimePriceRatioBar, computeRealtimeWeightedRatioBar } from './compute/index'; | ||
| import { computePerformanceCandles, computePriceRatioCandles, computeWeightedRatioCandles, computeAssetBars, computeRealtimePerformanceBar, computeRealtimePriceRatioBar, computeRealtimeWeightedRatioBar, computeRealtimeAssetBar } from './compute/index'; | ||
@@ -48,5 +48,8 @@ class Chart { | ||
| } | ||
| default: | ||
| throw new Error(`Unsupported chart type: ${chartType}`); | ||
| } | ||
| } | ||
| async getAssetBars(symbol, startTime, endTime) { | ||
| this.assertTokenExists(symbol); | ||
| const interval = this.collector.getCandleInterval(); | ||
@@ -91,10 +94,39 @@ const tokenCandles = await this.collector.fetchHistoricalPriceData(startTime, endTime, interval); | ||
| callback(bar); | ||
| } catch { | ||
| } catch (error) { | ||
| console.warn("Error in realtime bar callback, skipping this update", { | ||
| chartType, | ||
| error | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| const id = Math.random().toString(36).slice(2); | ||
| const formatTokens = (tokens) => tokens.map((t) => `${t.symbol}:${t.weight}`).join(","); | ||
| const id = `${chartType}-${formatTokens(this.collector.getLongTokens())}-${formatTokens(this.collector.getShortTokens())}`; | ||
| this.subscriptions.set(id, { chartType, callback, rawListenerId }); | ||
| return id; | ||
| } | ||
| subscribeRealtimeAssetBars(symbol, callback) { | ||
| this.assertTokenExists(symbol); | ||
| if (!this.collector.isWsConnected) { | ||
| this.collector.connectWs(); | ||
| } | ||
| const rawListenerId = this.collector.addRealtimeListener(() => { | ||
| const snapshot = this.collector.getLatestCandles(); | ||
| if (!snapshot) return; | ||
| const bar = computeRealtimeAssetBar(snapshot, symbol); | ||
| if (bar) { | ||
| try { | ||
| callback(bar); | ||
| } catch (error) { | ||
| console.warn("Error in realtime asset bar callback, skipping this update", { | ||
| symbol, | ||
| error | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| const id = `price-${symbol}`; | ||
| this.subscriptions.set(id, { chartType: "price", callback, rawListenerId }); | ||
| return id; | ||
| } | ||
| unsubscribeRealtimeBars(id) { | ||
@@ -109,2 +141,8 @@ const sub = this.subscriptions.get(id); | ||
| } | ||
| assertTokenExists(symbol) { | ||
| const allTokens = [...this.collector.getLongTokens(), ...this.collector.getShortTokens()]; | ||
| if (!allTokens.some((t) => t.symbol === symbol)) { | ||
| throw new Error(`Symbol "${symbol}" is not part of the configured long or short tokens`); | ||
| } | ||
| } | ||
| clearCache() { | ||
@@ -111,0 +149,0 @@ this.collector.clearCache(); |
@@ -62,3 +62,4 @@ import { CandleCache } from '../cache'; | ||
| cb(symbol, candle); | ||
| } catch { | ||
| } catch (error) { | ||
| console.warn("Error in realtime listener callback", error); | ||
| } | ||
@@ -65,0 +66,0 @@ } |
@@ -5,3 +5,4 @@ import { CandleData, Bar } from '../types.js'; | ||
| declare function computeAssetBars(tokenCandles: Record<string, CandleData[]>, symbol: string): Bar[]; | ||
| declare function computeRealtimeAssetBar(snapshot: Record<string, CandleData>, symbol: string): Bar | null; | ||
| export { computeAssetBars }; | ||
| export { computeAssetBars, computeRealtimeAssetBar }; |
@@ -12,3 +12,14 @@ function computeAssetBars(tokenCandles, symbol) { | ||
| } | ||
| function computeRealtimeAssetBar(snapshot, symbol) { | ||
| const candle = snapshot[symbol]; | ||
| if (!candle) return null; | ||
| return { | ||
| time: candle.t, | ||
| open: candle.o, | ||
| high: candle.h, | ||
| low: candle.l, | ||
| close: candle.c | ||
| }; | ||
| } | ||
| export { computeAssetBars }; | ||
| export { computeAssetBars, computeRealtimeAssetBar }; |
@@ -1,2 +0,2 @@ | ||
| export { computeAssetBars } from './asset.js'; | ||
| export { computeAssetBars, computeRealtimeAssetBar } from './asset.js'; | ||
| export { computePerformanceCandles, computeRealtimePerformanceBar } from './performance.js'; | ||
@@ -3,0 +3,0 @@ export { computePriceRatioCandles, computeRealtimePriceRatioBar } from './price-ratio.js'; |
@@ -1,4 +0,4 @@ | ||
| export { computeAssetBars } from './asset'; | ||
| export { computeAssetBars, computeRealtimeAssetBar } from './asset'; | ||
| export { computePerformanceCandles, computeRealtimePerformanceBar } from './performance'; | ||
| export { computePriceRatioCandles, computeRealtimePriceRatioBar } from './price-ratio'; | ||
| export { computeRealtimeWeightedRatioBar, computeWeightedRatioCandles } from './weighted-ratio'; |
+1
-1
@@ -42,3 +42,3 @@ import { Connector } from '@pear-protocol/types'; | ||
| type RealtimeCandleCallback = (symbol: string, candle: CandleData) => void; | ||
| type ChartType = 'weighted-ratio' | 'price-ratio' | 'performance'; | ||
| type ChartType = 'weighted-ratio' | 'price-ratio' | 'performance' | 'price'; | ||
| interface Bar { | ||
@@ -45,0 +45,0 @@ time: number; |
+1
-1
| { | ||
| "name": "@pear-protocol/chart-sdk", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "description": "Pear Protocol Chart SDK", | ||
@@ -5,0 +5,0 @@ "private": false, |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
61209
3.64%1785
3.06%