-
Notifications
You must be signed in to change notification settings - Fork 12
fix: safeguard against getEtherspotProvider crash in delegated mode #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cce4d64
fix: safeguard against getEtherspotProvider crash in delegated mode
aldin4u 6885edd
fix: remove duplicate else block causing build error
aldin4u 9adfc47
fix: resolve linting issues in useAlgoInsightsStats
aldin4u 0b8260f
fix: update test snapshots
aldin4u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { useMemo } from 'react'; | ||
| import { useTradingSignals } from '../../insights/hooks/useTradingSignals'; | ||
| import { TradingSignal as InsightSignal } from '../../insights/types'; | ||
|
|
||
| export interface AlgoInsightsStats { | ||
| pnl1m: number; | ||
| pnl3m: number; | ||
| pnl6m: number; | ||
| riskLevel: string; | ||
| pnlStatus: { | ||
| winning: number; | ||
| losing: number; | ||
| neutral: number; | ||
| }; | ||
| cumulativePnl: { | ||
| '1w': { value: number; history: { timestamp: number; value: number }[] }; | ||
| '1m': { value: number; history: { timestamp: number; value: number }[] }; | ||
| '3m': { value: number; history: { timestamp: number; value: number }[] }; | ||
| '6m': { value: number; history: { timestamp: number; value: number }[] }; | ||
| }; | ||
| } | ||
|
|
||
| export const useAlgoInsightsStats = () => { | ||
| const { signals, loading } = useTradingSignals({ enabled: true }); | ||
|
|
||
| const stats = useMemo<AlgoInsightsStats | null>(() => { | ||
| if (loading || signals.length === 0) return null; | ||
|
|
||
| // Helper to calculate PnL for a set of signals | ||
| const calculatePnL = (filterFn: (s: InsightSignal) => boolean) => { | ||
| return signals | ||
| .filter(filterFn) | ||
| .reduce((sum, s) => sum + (s.realized_pnl_percent || 0), 0); | ||
| }; | ||
|
|
||
| const now = Date.now(); | ||
| const oneMonthAgo = now - 30 * 24 * 60 * 60 * 1000; | ||
| const threeMonthsAgo = now - 90 * 24 * 60 * 60 * 1000; | ||
| const sixMonthsAgo = now - 180 * 24 * 60 * 60 * 1000; | ||
|
|
||
| // PnL metrics | ||
| const pnl1m = calculatePnL((s) => { | ||
| const closedAt = s.closed_at ? new Date(s.closed_at).getTime() : 0; | ||
| return closedAt > oneMonthAgo; | ||
| }); | ||
|
|
||
| const pnl3m = calculatePnL((s) => { | ||
| const closedAt = s.closed_at ? new Date(s.closed_at).getTime() : 0; | ||
| return closedAt > threeMonthsAgo; | ||
| }); | ||
|
|
||
| const pnl6m = calculatePnL((s) => { | ||
| const closedAt = s.closed_at ? new Date(s.closed_at).getTime() : 0; | ||
| return closedAt > sixMonthsAgo; | ||
| }); | ||
|
|
||
| // PnL Status (Winning/Losing/Neutral) | ||
| const closedSignals = signals.filter( | ||
| (s) => | ||
| s.status === 'closed' || | ||
| s.status === 'completed' || | ||
| s.status === 'stopped' | ||
| ); | ||
| const totalClosed = closedSignals.length; | ||
|
|
||
| let winning = 0; | ||
| let losing = 0; | ||
| let neutral = 0; | ||
|
|
||
| if (totalClosed > 0) { | ||
| winning = | ||
| (closedSignals.filter((s) => (s.realized_pnl_percent || 0) > 0).length / | ||
| totalClosed) * | ||
| 100; | ||
| losing = | ||
| (closedSignals.filter((s) => (s.realized_pnl_percent || 0) < 0).length / | ||
| totalClosed) * | ||
| 100; | ||
| neutral = | ||
| (closedSignals.filter((s) => (s.realized_pnl_percent || 0) === 0) | ||
| .length / | ||
| totalClosed) * | ||
| 100; | ||
| } | ||
|
|
||
| // Cumulative PnL History Generation | ||
| const generateHistory = (cutoffTime: number) => { | ||
| // Sort signals by close time | ||
| const relevantSignals = signals | ||
| .filter( | ||
| (s) => s.closed_at && new Date(s.closed_at).getTime() > cutoffTime | ||
| ) | ||
| .sort( | ||
| (a, b) => | ||
| new Date(a.closed_at!).getTime() - new Date(b.closed_at!).getTime() | ||
| ); | ||
|
|
||
| let runningPnL = 0; | ||
| const history = relevantSignals.map((s) => { | ||
| runningPnL += s.realized_pnl_percent || 0; | ||
| return { | ||
| timestamp: new Date(s.closed_at!).getTime() / 1000, | ||
| value: runningPnL, | ||
| }; | ||
| }); | ||
|
|
||
| // Ensure we have a starting point? Or just the trade history. | ||
| // If history is empty, return empty array | ||
| return history; | ||
| }; | ||
|
|
||
| // For cumulative values in the object, we use the specific period totals calculated above | ||
| // but the history needs to be generated point-by-point. | ||
|
|
||
| return { | ||
| pnl1m: Number(pnl1m.toFixed(2)), | ||
| pnl3m: Number(pnl3m.toFixed(2)), | ||
| pnl6m: Number(pnl6m.toFixed(2)), | ||
| riskLevel: 'Low Risk', // Placeholder logic | ||
| pnlStatus: { | ||
| winning: Number(winning.toFixed(1)), | ||
| losing: Number(losing.toFixed(1)), | ||
| neutral: Number(neutral.toFixed(1)), | ||
| }, | ||
| cumulativePnl: { | ||
| '1w': { | ||
| value: Number( | ||
| calculatePnL((s) => | ||
| s.closed_at | ||
| ? new Date(s.closed_at).getTime() > | ||
| now - 7 * 24 * 60 * 60 * 1000 | ||
| : false | ||
| ).toFixed(2) | ||
| ), | ||
| history: generateHistory(now - 7 * 24 * 60 * 60 * 1000), | ||
| }, | ||
| '1m': { | ||
| value: Number(pnl1m.toFixed(2)), | ||
| history: generateHistory(oneMonthAgo), | ||
| }, | ||
| '3m': { | ||
| value: Number(pnl3m.toFixed(2)), | ||
| history: generateHistory(threeMonthsAgo), | ||
| }, | ||
| '6m': { | ||
| value: Number(pnl6m.toFixed(2)), | ||
| history: generateHistory(sixMonthsAgo), | ||
| }, | ||
| }, | ||
| }; | ||
| }, [signals, loading]); | ||
|
|
||
| return { stats, loading }; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error state propagation.
The
useTradingSignalshook returns anerrorstate (per the relevant code snippet), but it's not destructured or exposed. If signal fetching fails, consumers cannot distinguish between "no data available" and "fetch error occurred" since both result instats: null.Consider exposing the error state:
🛡️ Proposed fix to propagate error state
export const useAlgoInsightsStats = () => { - const { signals, loading } = useTradingSignals({ enabled: true }); + const { signals, loading, error } = useTradingSignals({ enabled: true }); const stats = useMemo<AlgoInsightsStats | null>(() => { // ...existing code... }, [signals, loading]); - return { stats, loading }; + return { stats, loading, error }; };🤖 Prompt for AI Agents