Skip to content

Commit 259ca89

Browse files
authored
fix: pnl api request (#2062)
1 parent 9b47c9d commit 259ca89

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/hooks/useSharePnlImage.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { logBonsaiError } from '@/bonsai/logs';
22
import { useQuery } from '@tanstack/react-query';
33

44
import { timeUnits } from '@/constants/time';
5-
import { IndexerPositionSide } from '@/types/indexer/indexerApiGen';
5+
import { IndexerPerpetualPositionStatus, IndexerPositionSide } from '@/types/indexer/indexerApiGen';
66

77
import { useAccounts } from '@/hooks/useAccounts';
88

@@ -15,16 +15,18 @@ import { truncateAddress } from '@/lib/wallet';
1515
import { useEndpointsConfig } from './useEndpointsConfig';
1616

1717
export 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

2728
export 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
};

src/views/dialogs/SharePNLAnalyticsDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ export const SharePNLAnalyticsDialog = ({
5959
const [isCopied, setIsCopied] = useState(false);
6060

6161
const getPnlImage = useSharePnlImage({
62+
assetId,
6263
marketId,
6364
side,
6465
leverage,
6566
oraclePrice,
6667
entryPrice,
6768
unrealizedPnl,
68-
type: 'open',
6969
});
7070

7171
const pnlImage = useMemo(() => getPnlImage.data ?? undefined, [getPnlImage.data]);

0 commit comments

Comments
 (0)