@@ -2,7 +2,7 @@ import { logBonsaiError } from '@/bonsai/logs';
22import { useQuery } from '@tanstack/react-query' ;
33
44import { timeUnits } from '@/constants/time' ;
5- import { IndexerPositionSide } from '@/types/indexer/indexerApiGen' ;
5+ import { IndexerPerpetualPositionStatus , IndexerPositionSide } from '@/types/indexer/indexerApiGen' ;
66
77import { useAccounts } from '@/hooks/useAccounts' ;
88
@@ -15,16 +15,18 @@ import { truncateAddress } from '@/lib/wallet';
1515import { useEndpointsConfig } from './useEndpointsConfig' ;
1616
1717export type SharePnlImageParams = {
18+ assetId : string ;
1819 marketId : string ;
1920 side : Nullable < IndexerPositionSide > ;
2021 leverage : Nullable < number > ;
2122 oraclePrice : Nullable < number > ;
2223 entryPrice : Nullable < number > ;
2324 unrealizedPnl : Nullable < number > ;
24- type ?: 'open' | 'closed ' | 'liquidated' | undefined ;
25+ type ?: 'open' | 'close ' | 'liquidated' | undefined ;
2526} ;
2627
2728export const useSharePnlImage = ( {
29+ assetId,
2830 marketId,
2931 side,
3032 leverage,
@@ -34,35 +36,39 @@ export const useSharePnlImage = ({
3436 type = 'open' ,
3537} : SharePnlImageParams ) => {
3638 const { pnlImageApi } = useEndpointsConfig ( ) ;
37-
38- // Get user wallet address for username
3939 const { dydxAddress } = useAccounts ( ) ;
40-
41- // Get full position data from state
4240 const openPositions = useAppSelector ( getOpenPositions ) ;
41+
4342 const position = openPositions ?. find ( ( p ) => p . market === marketId ) ;
4443
44+ const positionType =
45+ position ?. status === IndexerPerpetualPositionStatus . CLOSED
46+ ? 'close'
47+ : position ?. status === IndexerPerpetualPositionStatus . LIQUIDATED
48+ ? 'liquidated'
49+ : 'open' ;
50+
51+ const pnl = ( position ?. realizedPnl . toNumber ( ) ?? 0 ) + ( unrealizedPnl ?? 0 ) ;
52+
4553 const queryFn = async ( ) : Promise < Blob | undefined > => {
4654 if ( ! dydxAddress ) {
4755 return undefined ;
4856 }
4957
50- // Build the request body matching the API's zod schema
5158 const requestBody = {
52- brand : 'dydx' ,
53- ticker : marketId ,
54- type,
59+ ticker : assetId ,
60+ type : positionType ,
5561 leverage : leverage ?? 0 ,
5662 username : truncateAddress ( dydxAddress ) ,
5763 isLong : side === IndexerPositionSide . LONG ,
5864 isCross : position ?. marginMode === 'CROSS' ,
5965 // Optional fields - include if available
60- size : position ?. unsignedSize . toNumber ( ) ,
61- userImage : 'https://dydx.trade/hedgie-profile.png' ,
62- pnl : position ?. realizedPnl . toNumber ( ) ,
66+ size : position ?. value . toNumber ( ) ,
67+ pnl,
6368 uPnl : unrealizedPnl ?? undefined ,
6469 pnlPercentage : position ?. updatedUnrealizedPnlPercent ?. toNumber ( ) ,
6570 entryPx : entryPrice ?? undefined ,
71+ exitPx : position ?. exitPrice ?. toNumber ( ) ,
6672 liquidationPx : position ?. liquidationPrice ?. toNumber ( ) ,
6773 markPx : oraclePrice ?? undefined ,
6874 } ;
@@ -104,7 +110,7 @@ export const useSharePnlImage = ({
104110 refetchOnReconnect : false ,
105111 staleTime : 2 * timeUnits . minute , // 2 minutes
106112 retry : 2 ,
107- retryDelay : 1 * timeUnits . second ,
113+ retryDelay : 1 * timeUnits . second , // 1 second
108114 retryOnMount : true ,
109115 } ) ;
110116} ;
0 commit comments