From c5c75f53c66a1851f3af10fa182196aa62ff37ec Mon Sep 17 00:00:00 2001 From: Ryan Goree Date: Tue, 2 Sep 2025 15:18:46 -0500 Subject: [PATCH 1/2] Add latest position blocks to portfolio long queries --- .../src/base/latestBlocks.ts | 30 ++++ .../portfolio/longs/usePortfolioLongsData.ts | 137 ++++++++++++------ 2 files changed, 121 insertions(+), 46 deletions(-) create mode 100644 apps/hyperdrive-trading/src/base/latestBlocks.ts diff --git a/apps/hyperdrive-trading/src/base/latestBlocks.ts b/apps/hyperdrive-trading/src/base/latestBlocks.ts new file mode 100644 index 000000000..821dbed5d --- /dev/null +++ b/apps/hyperdrive-trading/src/base/latestBlocks.ts @@ -0,0 +1,30 @@ +type PositionType = "long" | "short" | "lp"; + +export const LATEST_POSITION_BLOCKS_BY_CHAIN_ID: Record< + number, + Record +> = { + 1: { + long: 22090872n, + short: 21391567n, + lp: 22108665n, + }, + // Gnosis + 100: { + long: 39086666n, + short: 39145215n, + lp: 39083204n, + }, + // Linea + 59144: { + long: 10622294n, + short: 10621975n, + lp: 15639198n, + }, + // Base + 8453: { + long: 28017409n, + short: 26080016n, + lp: 27852111n, + }, +}; diff --git a/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts b/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts index 366a29ee9..7efb9f809 100644 --- a/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts +++ b/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts @@ -4,6 +4,7 @@ import { OpenLongPositionReceived, } from "@delvtech/hyperdrive-js"; import { useQuery } from "@tanstack/react-query"; +import { LATEST_POSITION_BLOCKS_BY_CHAIN_ID } from "src/base/latestBlocks"; import { makeQueryKey, makeQueryKey2 } from "src/base/makeQueryKey"; import { getDrift } from "src/drift/getDrift"; import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain"; @@ -32,32 +33,54 @@ export function usePortfolioLongsData({ ? async () => await Promise.all( appConfigForConnectedChain.hyperdrives.map(async (hyperdrive) => { - const readHyperdrive = await getHyperdrive({ - address: hyperdrive.address, - drift: getDrift({ chainId: hyperdrive.chainId }), - earliestBlock: hyperdrive.initializationBlock, - zapContractAddress: - appConfigForConnectedChain.zaps[hyperdrive.chainId] - ?.address, - }); + try { + const readHyperdrive = await getHyperdrive({ + address: hyperdrive.address, + drift: getDrift({ chainId: hyperdrive.chainId }), + earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, + }); - const allLongs = await readHyperdrive.getOpenLongPositions({ - account, - }); - const openLongs = await Promise.all( - allLongs.map(async (long) => ({ - ...long, - details: await readHyperdrive.getOpenLongDetails({ - assetId: long.assetId, - account, - }), - })), - ); + const allLongs = await readHyperdrive.getOpenLongPositions({ + account, + options: { + block: + LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId] + .long, + }, + }); + const openLongs = await Promise.all( + allLongs.map(async (long) => ({ + ...long, + details: await readHyperdrive.getOpenLongDetails({ + assetId: long.assetId, + account, + options: { + block: + LATEST_POSITION_BLOCKS_BY_CHAIN_ID[ + hyperdrive.chainId + ].long, + }, + }), + })), + ); - return { - hyperdrive, - openLongs, - }; + return { + hyperdrive, + openLongs, + }; + } catch (e) { + console.error( + `Error fetching longs for ${hyperdrive.address}:`, + e, + ); + return { + hyperdrive, + openLongs: [] as OpenLongPositionReceived[], + }; + } }), ) : undefined, @@ -95,30 +118,52 @@ export function usePortfolioLongsDataFromHyperdrives({ ? async () => { const results = await Promise.all( hyperdrives.map(async (hyperdrive) => { - const readHyperdrive = await getHyperdrive({ - address: hyperdrive.address, - drift: getDrift({ chainId: hyperdrive.chainId }), - earliestBlock: hyperdrive.initializationBlock, - zapContractAddress: - appConfigForConnectedChain.zaps[hyperdrive.chainId] - ?.address, - }); - const allLongs = await readHyperdrive.getOpenLongPositions({ - account, - }); + try { + const readHyperdrive = await getHyperdrive({ + address: hyperdrive.address, + drift: getDrift({ chainId: hyperdrive.chainId }), + earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, + }); + const allLongs = await readHyperdrive.getOpenLongPositions({ + account, + options: { + block: + LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId] + .long, + }, + }); - const openLongs = await Promise.all( - allLongs.map(async (long) => ({ - ...long, - hyperdrive, - details: await readHyperdrive.getOpenLongDetails({ - assetId: long.assetId, - account: account, - }), - })), - ); + const openLongs = await Promise.all( + allLongs.map(async (long) => ({ + ...long, + hyperdrive, + details: await readHyperdrive.getOpenLongDetails({ + assetId: long.assetId, + account: account, + options: { + block: + LATEST_POSITION_BLOCKS_BY_CHAIN_ID[ + hyperdrive.chainId + ].long, + }, + }), + })), + ); - return openLongs; + return openLongs; + } catch (e) { + console.error( + `Error fetching long data for ${hyperdrive.address}:`, + e, + ); + return [] as (OpenLongPositionReceived & { + hyperdrive: HyperdriveConfig; + details: any; + })[]; + } }), ); return results.flat(); From 3be2776831cc814f4cdc6298d1d6191aeba8c0d1 Mon Sep 17 00:00:00 2001 From: Ryan Goree Date: Tue, 2 Sep 2025 15:23:05 -0500 Subject: [PATCH 2/2] Add latest position blocks to portfolio short queries --- .../portfolio/longs/usePortfolioLongsData.ts | 2 +- .../shorts/usePortfolioShortsData.ts | 90 ++++++++++++------- 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts b/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts index 7efb9f809..73b746d30 100644 --- a/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts +++ b/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts @@ -78,7 +78,7 @@ export function usePortfolioLongsData({ ); return { hyperdrive, - openLongs: [] as OpenLongPositionReceived[], + openLongs: [], }; } }), diff --git a/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts b/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts index c7c97504e..ffd6b6061 100644 --- a/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts +++ b/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts @@ -1,6 +1,7 @@ import { HyperdriveConfig } from "@delvtech/hyperdrive-appconfig"; import { getHyperdrive, OpenShort } from "@delvtech/hyperdrive-js"; import { useQuery } from "@tanstack/react-query"; +import { LATEST_POSITION_BLOCKS_BY_CHAIN_ID } from "src/base/latestBlocks"; import { makeQueryKey, makeQueryKey2 } from "src/base/makeQueryKey"; import { getDrift } from "src/drift/getDrift"; import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain"; @@ -30,21 +31,37 @@ export function usePortfolioShortsData({ ? async () => await Promise.all( appConfigForConnectedChain.hyperdrives.map(async (hyperdrive) => { - const readHyperdrive = await getHyperdrive({ - address: hyperdrive.address, - drift: getDrift({ chainId: hyperdrive.chainId }), - earliestBlock: hyperdrive.initializationBlock, - zapContractAddress: - appConfigForConnectedChain.zaps[hyperdrive.chainId] - ?.address, - }); + try { + const readHyperdrive = await getHyperdrive({ + address: hyperdrive.address, + drift: getDrift({ chainId: hyperdrive.chainId }), + earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, + }); - return { - hyperdrive, - openShorts: await readHyperdrive.getOpenShorts({ - account, - }), - }; + return { + hyperdrive, + openShorts: await readHyperdrive.getOpenShorts({ + account, + options: { + block: + LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId] + .short, + }, + }), + }; + } catch (e) { + console.error( + `Error fetching shorts for hyperdrive ${hyperdrive.address} on chain ${hyperdrive.chainId}:`, + e, + ); + return { + hyperdrive, + openShorts: [], + }; + } }), ) : undefined, @@ -80,22 +97,35 @@ export function usePortfolioShortsDataFromHyperdrives({ ? async () => { const results = await Promise.all( hyperdrives.map(async (hyperdrive) => { - const readHyperdrive = await getHyperdrive({ - address: hyperdrive.address, - drift: getDrift({ chainId: hyperdrive.chainId }), - earliestBlock: hyperdrive.initializationBlock, - debugName: hyperdrive.name, - zapContractAddress: - appConfigForConnectedChain.zaps[hyperdrive.chainId] - ?.address, - }); - const openShorts = await readHyperdrive.getOpenShorts({ - account, - }); - return openShorts.map((openShort) => ({ - ...openShort, - hyperdrive, - })); + try { + const readHyperdrive = await getHyperdrive({ + address: hyperdrive.address, + drift: getDrift({ chainId: hyperdrive.chainId }), + earliestBlock: hyperdrive.initializationBlock, + debugName: hyperdrive.name, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, + }); + const openShorts = await readHyperdrive.getOpenShorts({ + account, + options: { + block: + LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId] + .short, + }, + }); + return openShorts.map((openShort) => ({ + ...openShort, + hyperdrive, + })); + } catch (e) { + console.error( + `Error fetching shorts data for ${hyperdrive.address}:`, + e, + ); + return []; + } }), ); return results.flat();