funhi-chart 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * CandleStorageAdapter - Handles loading/saving candles (wrapper around existing service)
3
+ */
4
+ import { CandleData } from '../core/types';
5
+ export declare class CandleStorageAdapter {
6
+ /**
7
+ * Loads candles from storage
8
+ */
9
+ loadCandles(mint: string, timeframeSeconds: number, limit?: number): Promise<CandleData[]>;
10
+ /**
11
+ * Saves candles to storage (if needed in future)
12
+ */
13
+ saveCandles(mint: string, timeframeSeconds: number, candles: CandleData[]): Promise<void>;
14
+ }
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import type { ChartProps } from './FunhiChart';
3
+ /**
4
+ * FunhiChartOOP - OOP-based implementation using FunhiChartController
5
+ * This version eliminates code duplication and uses clean OOP architecture
6
+ */
7
+ declare const FunhiChartOOP: React.FC<ChartProps>;
8
+ export default FunhiChartOOP;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * CandleManager - Manages candle creation, updates, and validation for a single timeframe
3
+ * This class eliminates code duplication by handling all timeframe logic generically
4
+ */
5
+ import { CandleData, CandleUpdateResult } from './types';
6
+ export declare class CandleManager {
7
+ private timeframeSeconds;
8
+ private candles;
9
+ private currentCandle;
10
+ private lastCandleTime;
11
+ private lastProcessedTimestamp;
12
+ private readonly debounceMs;
13
+ constructor(timeframeSeconds: number);
14
+ /**
15
+ * Main method: Creates a new candle or updates the current one
16
+ */
17
+ createOrUpdate(price: number, timestamp: number): CandleUpdateResult | null;
18
+ /**
19
+ * Creates a new candle at bucket boundary
20
+ */
21
+ private createNewCandle;
22
+ /**
23
+ * Updates the current candle with new price data
24
+ */
25
+ private updateCurrentCandle;
26
+ /**
27
+ * Updates candle price while preserving minimum body size
28
+ */
29
+ private updateCandlePrice;
30
+ /**
31
+ * Builds a new candle with minimum body size enforcement
32
+ */
33
+ private buildNewCandle;
34
+ /**
35
+ * Gets the previous candle's close price
36
+ */
37
+ private getPreviousCandleClose;
38
+ /**
39
+ * Gets the previous candle's direction (1 for bullish, -1 for bearish)
40
+ */
41
+ private getPreviousCandleDirection;
42
+ /**
43
+ * Calculates bucket start time for a given timestamp
44
+ */
45
+ private calculateBucketStart;
46
+ /**
47
+ * Checks if a candle has valid data
48
+ */
49
+ private isValidCandle;
50
+ /**
51
+ * Adds a candle to history
52
+ */
53
+ private addCandleToHistory;
54
+ /**
55
+ * Updates a candle in history (only if it matches current bucket)
56
+ */
57
+ private updateCandleInHistory;
58
+ getCandles(): CandleData[];
59
+ getCurrentCandle(): CandleData | null;
60
+ getLastCandleTime(): number;
61
+ /**
62
+ * Loads candles from storage
63
+ */
64
+ loadCandles(candles: CandleData[]): void;
65
+ /**
66
+ * Directly updates candle from external source (e.g., WebSocket)
67
+ */
68
+ updateFromExternal(candle: CandleData, currentBucket: number): void;
69
+ /**
70
+ * Resets the manager (useful for cleanup)
71
+ */
72
+ reset(): void;
73
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * ChartRenderer - Manages the lightweight-charts instance and rendering
3
+ */
4
+ import { IChartApi, ISeriesApi } from 'lightweight-charts';
5
+ import { CandleData, TimeFrame } from './types';
6
+ export interface ChartOptions {
7
+ ticker?: string;
8
+ mint: string;
9
+ formatPrice: (value: number) => string;
10
+ height?: number;
11
+ }
12
+ export declare class ChartRenderer {
13
+ private chart;
14
+ private series;
15
+ private container;
16
+ private options;
17
+ constructor(container: HTMLDivElement, options: ChartOptions);
18
+ /**
19
+ * Initializes the chart
20
+ */
21
+ initialize(): void;
22
+ /**
23
+ * Updates chart data
24
+ */
25
+ updateData(candles: CandleData[]): void;
26
+ /**
27
+ * Sets the current timeframe (for future use if needed)
28
+ */
29
+ setTimeframe(timeframe: TimeFrame): void;
30
+ /**
31
+ * Resizes the chart
32
+ */
33
+ resize(): void;
34
+ /**
35
+ * Destroys the chart instance
36
+ */
37
+ destroy(): void;
38
+ /**
39
+ * Gets the chart instance (for advanced operations)
40
+ */
41
+ getChart(): IChartApi | null;
42
+ /**
43
+ * Gets the series instance (for advanced operations)
44
+ */
45
+ getSeries(): ISeriesApi<"Candlestick"> | null;
46
+ }
@@ -0,0 +1,61 @@
1
+ import { CandleData, TimeFrame } from './types';
2
+ export interface ControllerConfig {
3
+ mint: string;
4
+ ticker?: string;
5
+ tokenName?: string;
6
+ timeframes: TimeFrame[];
7
+ container: HTMLDivElement;
8
+ formatPrice: (value: number) => string;
9
+ }
10
+ export declare class FunhiChartController {
11
+ private chartRenderer;
12
+ private timeframeManager;
13
+ private storageAdapter;
14
+ private updateHandler;
15
+ private currentTimeframe;
16
+ private config;
17
+ constructor(config: ControllerConfig);
18
+ /**
19
+ * Initializes the chart and loads initial data
20
+ */
21
+ initialize(): Promise<void>;
22
+ /**
23
+ * Loads candles for a specific timeframe
24
+ */
25
+ loadTimeframeData(timeframeSeconds: number): Promise<void>;
26
+ /**
27
+ * Updates price for all timeframes
28
+ */
29
+ updatePrice(price: number): void;
30
+ /**
31
+ * Handles WebSocket update
32
+ */
33
+ handleSocketUpdate(data: {
34
+ mint: string;
35
+ candles: Record<string, any>;
36
+ }): void;
37
+ /**
38
+ * Switches to a different timeframe
39
+ */
40
+ switchTimeframe(seconds: number): Promise<void>;
41
+ /**
42
+ * Gets candles for current timeframe
43
+ */
44
+ getCurrentCandles(): CandleData[];
45
+ /**
46
+ * Gets current timeframe
47
+ */
48
+ getCurrentTimeframe(): TimeFrame;
49
+ /**
50
+ * Gets current candle for a specific timeframe
51
+ */
52
+ getCurrentCandle(seconds: number): CandleData | null;
53
+ /**
54
+ * Resizes the chart
55
+ */
56
+ resize(): void;
57
+ /**
58
+ * Destroys the controller and cleans up resources
59
+ */
60
+ destroy(): void;
61
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * TimeframeManager - Orchestrates multiple timeframes and coordinates updates
3
+ */
4
+ import { CandleManager } from './CandleManager';
5
+ import { CandleData, TimeFrame } from './types';
6
+ export declare class TimeframeManager {
7
+ private managers;
8
+ private timeframes;
9
+ constructor(timeframes: TimeFrame[]);
10
+ /**
11
+ * Gets the manager for a specific timeframe
12
+ */
13
+ getManager(seconds: number): CandleManager | undefined;
14
+ /**
15
+ * Updates all timeframes with new price data
16
+ */
17
+ updateAll(price: number, timestamp: number): void;
18
+ /**
19
+ * Gets candles for a specific timeframe
20
+ */
21
+ getCandles(seconds: number): CandleData[];
22
+ /**
23
+ * Gets current candle for a specific timeframe
24
+ */
25
+ getCurrentCandle(seconds: number): CandleData | null;
26
+ /**
27
+ * Loads candles for a specific timeframe
28
+ */
29
+ loadCandles(seconds: number, candles: CandleData[]): void;
30
+ /**
31
+ * Updates candle from external source (e.g., WebSocket)
32
+ */
33
+ updateFromExternal(seconds: number, candle: CandleData, currentBucket: number): void;
34
+ /**
35
+ * Gets all available timeframes
36
+ */
37
+ getTimeframes(): TimeFrame[];
38
+ /**
39
+ * Resets all managers
40
+ */
41
+ reset(): void;
42
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Core exports for FunhiChart OOP architecture
3
+ */
4
+ export * from './types';
5
+ export * from './CandleManager';
6
+ export * from './TimeframeManager';
7
+ export * from './ChartRenderer';
8
+ export * from './FunhiChartController';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Core types for FunhiChart OOP architecture
3
+ */
4
+ export interface CandleData {
5
+ time: number;
6
+ open: number;
7
+ high: number;
8
+ low: number;
9
+ close: number;
10
+ volume: number;
11
+ }
12
+ export interface TimeFrame {
13
+ label: string;
14
+ value: string;
15
+ seconds: number;
16
+ }
17
+ export type { TimeFrame as TimeFrameType };
18
+ export interface ChartConfig {
19
+ mint: string;
20
+ ticker?: string;
21
+ tokenName?: string;
22
+ timeframes: TimeFrame[];
23
+ container: HTMLDivElement;
24
+ }
25
+ export interface CandleManagerConfig {
26
+ timeframeSeconds: number;
27
+ mint: string;
28
+ }
29
+ export interface CandleUpdateResult {
30
+ candle: CandleData | null;
31
+ isNewCandle: boolean;
32
+ wasUpdated: boolean;
33
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * RealTimeUpdateHandler - Handles real-time price updates and WebSocket events
3
+ */
4
+ import { TimeframeManager } from '../core/TimeframeManager';
5
+ export declare class RealTimeUpdateHandler {
6
+ private timeframeManager;
7
+ private debounceMs;
8
+ private lastUpdate;
9
+ constructor(timeframeManager: TimeframeManager);
10
+ /**
11
+ * Handles price update from real-time data
12
+ */
13
+ handlePriceUpdate(price: number, timestamp: number): void;
14
+ /**
15
+ * Handles WebSocket update with pre-calculated candles
16
+ */
17
+ handleSocketUpdate(data: {
18
+ mint: string;
19
+ candles: Record<string, any>;
20
+ }, mint: string): void;
21
+ /**
22
+ * Debounce check
23
+ */
24
+ private debounce;
25
+ /**
26
+ * Converts timeframe key to seconds
27
+ */
28
+ private getTimeframeSeconds;
29
+ }
@@ -7,6 +7,7 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  export { default as FunhiChart } from './components/FunhiChart';
10
+ export { default as FunhiChartOOP } from './components/FunhiChartOOP';
10
11
  export type { ChartProps } from './components/FunhiChart';
11
12
  export { useSocket } from './hooks/useSocket';
12
13
  export { useOptimizedTokenAnalytics } from './hooks/useOptimizedTokenAnalytics';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * CandleValidator - Validates and enforces candle data integrity
3
+ */
4
+ import { CandleData } from '../core/types';
5
+ export declare class CandleValidator {
6
+ /**
7
+ * Minimum body size as percentage of price (0.01%)
8
+ */
9
+ private static readonly MIN_BODY_PERCENTAGE;
10
+ /**
11
+ * Absolute minimum body size in price units
12
+ */
13
+ private static readonly MIN_BODY_ABSOLUTE;
14
+ /**
15
+ * Validates if a candle has valid OHLC data
16
+ */
17
+ static validate(candle: CandleData): boolean;
18
+ /**
19
+ * Ensures candle has minimum body size for visibility
20
+ */
21
+ static ensureMinimumBodySize(candle: CandleData, minBodySize?: number): CandleData;
22
+ /**
23
+ * Validates and corrects OHLC data integrity
24
+ */
25
+ static validateOHLC(candle: CandleData): CandleData;
26
+ /**
27
+ * Checks if a candle belongs to the current time bucket
28
+ */
29
+ static isCurrentBucket(candle: CandleData, timeframeSeconds: number, timestamp: number): boolean;
30
+ /**
31
+ * Validates price data
32
+ */
33
+ static validatePrice(price: number): boolean;
34
+ /**
35
+ * Calculates minimum body size for a given price
36
+ */
37
+ static calculateMinBodySize(price: number): number;
38
+ }
package/dist/index.d.ts CHANGED
@@ -14,6 +14,12 @@ interface ChartProps {
14
14
  }
15
15
  declare const FunhiChart: React.FC<ChartProps>;
16
16
 
17
+ /**
18
+ * FunhiChartOOP - OOP-based implementation using FunhiChartController
19
+ * This version eliminates code duplication and uses clean OOP architecture
20
+ */
21
+ declare const FunhiChartOOP: React.FC<ChartProps>;
22
+
17
23
  interface OHLCVCandle {
18
24
  time: number;
19
25
  open: number;
@@ -291,5 +297,5 @@ declare const API_CONFIG: {
291
297
  readonly RETRY_DELAY: 1000;
292
298
  };
293
299
 
294
- export { API_CONFIG, FunhiChart, candleAggregation, candleStorageService, useOptimizedTokenAnalytics, useSocket };
300
+ export { API_CONFIG, FunhiChart, FunhiChartOOP, candleAggregation, candleStorageService, useOptimizedTokenAnalytics, useSocket };
295
301
  export type { CandleData$1 as CandleData, ChartProps, GetCandlesRequest, SaveCandleRequest, SaveCandlesRequest, TimeFrame };