Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions public/configs/v1/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4dev.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -504,7 +505,8 @@
"neutronValidator": "https://neutron-testnet-rpc.polkachu.com/",
"geoV2": "https://geo-whitelist-web-mainnet-preview.infrastructure-34d.workers.dev/",
"geo": "https://api.dydx.exchange/v4/geo",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -549,7 +551,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "http://dev3-faucet-lb-public-1644791410.us-east-2.elb.amazonaws.com",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -594,7 +597,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4dev4.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.dydx.trade/"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -636,7 +640,8 @@
"neutronValidator": "https://neutron-testnet-rpc.polkachu.com/",
"geoV2": "https://geo-whitelist-web-mainnet-preview.infrastructure-34d.workers.dev/",
"geo": "https://api.dydx.exchange/v4/geo",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.dydx.trade/"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -680,7 +685,8 @@
"neutronValidator": "https://neutron-testnet-rpc.polkachu.com/",
"geo": "https://api.dydx.exchange/v4/geo",
"geoV2": "https://geo-whitelist-web-mainnet-preview.infrastructure-34d.workers.dev/",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -724,7 +730,8 @@
"neutronValidator": "https://neutron-testnet-rpc.polkachu.com/",
"geoV2": "https://geo-whitelist-web-mainnet-preview.infrastructure-34d.workers.dev/",
"geo": "https://api.dydx.exchange/v4/geo",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"apps": {
"ios": {
Expand Down Expand Up @@ -780,7 +787,8 @@
"neutronValidator": "https://neutron-testnet-rpc.polkachu.com/",
"geoV2": "https://geo-whitelist-web-mainnet-preview.infrastructure-34d.workers.dev/",
"geo": "https://api.dydx.exchange/v4/geo",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -826,7 +834,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-staging-e2fb353831a4.herokuapp.com"
"spotApi": "https://dydx-solana-api-staging-e2fb353831a4.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [
"dydxvaloper1vvc9vl6z9pu0vt2y79d0ln8zp6qmpmrhxx99h4",
Expand Down Expand Up @@ -874,7 +883,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -919,7 +929,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.dydx.trade/"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -964,7 +975,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -1009,7 +1021,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -1054,7 +1067,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -1099,7 +1113,8 @@
"stakingAPR": "https://apybara-proxy-web-testnet.infrastructure-34d.workers.dev/v0/protocols/dydx",
"faucet": "https://faucet.v4testnet.dydx.exchange",
"affiliates": "https://dydx.stg.fuul.xyz",
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com"
"spotApi": "https://dydx-solana-api-prod-89bf4c933ba0.herokuapp.com",
"pnlImageApi": "https://image-generator.bonk.trade/generate-trade-card-web"
},
"stakingValidators": [],
"featureFlags": {
Expand Down Expand Up @@ -1144,7 +1159,8 @@
"geoV2": "[geo v2 endpoint for mainnet]",
"stakingAPR": "[staking APR endpoint for mainnet]",
"affiliates": "[affiliates endpoint for mainnet]",
"spotApi": "[spot api endpoint for mainnet]"
"spotApi": "[spot api endpoint for mainnet]",
"pnlImageApi": "[pnl image api endpoint for mainnet]"
},
"stakingValidators": [],
"featureFlags": {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useEndpointsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface EndpointsConfig {
affiliates?: string;
spotApi: string;
geoV2: string;
pnlImageApi: string;
}

export const useEndpointsConfig = () => {
Expand All @@ -38,5 +39,6 @@ export const useEndpointsConfig = () => {
affiliatesBaseUrl: endpointsConfig.affiliates,
spotApi: endpointsConfig.spotApi,
geoV2: endpointsConfig.geoV2,
pnlImageApi: endpointsConfig.pnlImageApi,
};
};
110 changes: 110 additions & 0 deletions src/hooks/useSharePnlImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { logBonsaiError } from '@/bonsai/logs';
import { useQuery } from '@tanstack/react-query';

import { timeUnits } from '@/constants/time';
import { IndexerPositionSide } from '@/types/indexer/indexerApiGen';

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

import { getOpenPositions } from '@/state/accountSelectors';
import { useAppSelector } from '@/state/appTypes';

import { Nullable } from '@/lib/typeUtils';
import { truncateAddress } from '@/lib/wallet';

import { useEndpointsConfig } from './useEndpointsConfig';

export type SharePnlImageParams = {
marketId: string;
side: Nullable<IndexerPositionSide>;
leverage: Nullable<number>;
oraclePrice: Nullable<number>;
entryPrice: Nullable<number>;
unrealizedPnl: Nullable<number>;
type?: 'open' | 'closed' | 'liquidated' | undefined;
};

export const useSharePnlImage = ({
marketId,
side,
leverage,
oraclePrice,
entryPrice,
unrealizedPnl,
type = 'open',
}: SharePnlImageParams) => {
const { pnlImageApi } = useEndpointsConfig();

// Get user wallet address for username
const { dydxAddress } = useAccounts();

// Get full position data from state
const openPositions = useAppSelector(getOpenPositions);
const position = openPositions?.find((p) => p.market === marketId);

const queryFn = async (): Promise<Blob | undefined> => {
if (!dydxAddress) {
return undefined;
}

// Build the request body matching the API's zod schema
const requestBody = {
brand: 'bonk',
ticker: marketId,
type,
leverage: leverage ?? 0,
username: truncateAddress(dydxAddress),
isLong: side === IndexerPositionSide.LONG,
isCross: position?.marginMode === 'CROSS',
// Optional fields - include if available
size: position?.unsignedSize.toNumber(),
userImage: 'https://dydx.trade/hedgie-profile.png',
pnl: position?.realizedPnl.toNumber(),
uPnl: unrealizedPnl ?? undefined,
pnlPercentage: position?.updatedUnrealizedPnlPercent?.toNumber(),
entryPx: entryPrice ?? undefined,
liquidationPx: position?.liquidationPrice?.toNumber(),
markPx: oraclePrice ?? undefined,
};

const response = await fetch(pnlImageApi, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});

if (!response.ok) {
logBonsaiError('useSharePnlImage', 'Failed to fetch share image', { response });
throw new Error(`Failed to fetch share image: ${response.status}`);
}

return response.blob();
};

return useQuery({
queryKey: [
'sharePnlImage',
marketId,
dydxAddress,
side,
leverage,
oraclePrice,
entryPrice,
unrealizedPnl,
type,
position?.marginMode,
position?.unsignedSize.toString(),
position?.liquidationPrice?.toString(),
],
queryFn,
enabled: Boolean(dydxAddress),
refetchOnWindowFocus: false,
refetchOnReconnect: false,
staleTime: 2 * timeUnits.minute, // 2 minutes
retry: 2,
retryDelay: 1 * timeUnits.second,
retryOnMount: true,
});
};
Loading
Loading