diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadHyperdrive.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadHyperdrive.ts index cec794346..d537b8c3d 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadHyperdrive.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadHyperdrive.ts @@ -33,6 +33,7 @@ export function useReadHyperdrive({ address, drift, earliestBlock: initializationBlock, + zapContractAddress: appConfig.zaps[chainId].address, }); } : undefined, diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadWriteHyperdrive.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadWriteHyperdrive.ts index d5df13051..a6a9c9123 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadWriteHyperdrive.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useReadWriteHyperdrive.ts @@ -34,6 +34,7 @@ export function useReadWriteHyperdrive({ address, drift, earliestBlock: initializationBlock, + zapContractAddress: appConfig.zaps[chainId].address, }); } : undefined, diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useUnpausedPools.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useUnpausedPools.ts index b2d508244..38edd9ff2 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useUnpausedPools.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useUnpausedPools.ts @@ -46,6 +46,7 @@ export function useUnpausedPools(): { address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: appConfig.zaps[hyperdrive.chainId].address, }); // We only show hyperdrives that are not paused diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useTotalOpenLongsValue.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useTotalOpenLongsValue.ts index 156f42386..a31b83017 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useTotalOpenLongsValue.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useTotalOpenLongsValue.ts @@ -6,6 +6,7 @@ import { import { useQuery } from "@tanstack/react-query"; import { makeQueryKey2 } from "src/base/makeQueryKey"; import { getDrift } from "src/drift/getDrift"; +import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain"; import { Address } from "viem"; export function useTotalOpenLongsValueTwo({ account, @@ -22,6 +23,7 @@ export function useTotalOpenLongsValueTwo({ isLoading: boolean; totalOpenLongsValueError: Error; } { + const appConfig = useAppConfigForConnectedChain(); const queryEnabled = !!account && !!longs && enabled; const { data: totalOpenLongsValue, @@ -45,6 +47,8 @@ export function useTotalOpenLongsValueTwo({ address: long.hyperdrive.address, drift: getDrift({ chainId: long.hyperdrive.chainId }), earliestBlock: long.hyperdrive.initializationBlock, + zapContractAddress: + appConfig.zaps[long.hyperdrive.chainId].address, }); const preview = await readHyperdrive.previewCloseLong({ maturityTime: long.details?.maturity || 0n, diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/lp/hooks/useTotalOpenLpPositions.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/lp/hooks/useTotalOpenLpPositions.ts index 84cd95252..eb19647b8 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/lp/hooks/useTotalOpenLpPositions.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/lp/hooks/useTotalOpenLpPositions.ts @@ -51,6 +51,8 @@ export function useTotalOpenLpPositions({ address: position.hyperdrive.address, drift: getDrift({ chainId: position.hyperdrive.chainId }), earliestBlock: position.hyperdrive.initializationBlock, + zapContractAddress: + appConfig.zaps[position.hyperdrive.chainId].address, }); const openLpPosition = await readHyperdrive.getOpenLpPosition({ account, diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalOpenShortsValue.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalOpenShortsValue.ts index 49f06b12b..2a611d0c2 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalOpenShortsValue.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalOpenShortsValue.ts @@ -3,6 +3,7 @@ import { getHyperdrive, OpenShort } from "@delvtech/hyperdrive-js"; import { useQuery } from "@tanstack/react-query"; import { makeQueryKey2 } from "src/base/makeQueryKey"; import { getDrift } from "src/drift/getDrift"; +import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain"; import { Address } from "viem"; export function useTotalOpenShortsValue({ @@ -15,7 +16,7 @@ export function useTotalOpenShortsValue({ enabled: boolean; }): { totalOpenShortsValue: bigint | undefined; isLoading: boolean } { const queryEnabled = !!account && !!shorts && enabled; - + const appConfig = useAppConfigForConnectedChain(); const { data: totalOpenShortsValue, isLoading } = useQuery({ queryKey: makeQueryKey2({ namespace: "portfolio", @@ -34,6 +35,8 @@ export function useTotalOpenShortsValue({ address: short.hyperdrive.address, drift: getDrift({ chainId: short.hyperdrive.chainId }), earliestBlock: short.hyperdrive.initializationBlock, + zapContractAddress: + appConfig.zaps[short.hyperdrive.chainId].address, }); const preview = await readHyperdrive.previewCloseShort({ maturityTime: short.maturity, diff --git a/apps/hyperdrive-trading/src/ui/markets/hooks/usePoolsList.ts b/apps/hyperdrive-trading/src/ui/markets/hooks/usePoolsList.ts index c386e0bb7..854dc7f10 100644 --- a/apps/hyperdrive-trading/src/ui/markets/hooks/usePoolsList.ts +++ b/apps/hyperdrive-trading/src/ui/markets/hooks/usePoolsList.ts @@ -130,6 +130,7 @@ function useSortedPools({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: appConfig.zaps[hyperdrive.chainId].address, }); const publicClient = getPublicClient(wagmiConfig as any, { chainId: hyperdrive.chainId, diff --git a/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts b/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts index 4f5157006..366a29ee9 100644 --- a/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts +++ b/apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts @@ -24,7 +24,6 @@ export function usePortfolioLongsData({ } { const appConfigForConnectedChain = useAppConfigForConnectedChain(); const queryEnabled = !!account && !!appConfigForConnectedChain; - const { data: openLongPositions, status: openLongPositionsStatus } = useQuery( { queryKey: makeQueryKey("portfolioLongs", { account }), @@ -37,6 +36,9 @@ export function usePortfolioLongsData({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, }); const allLongs = await readHyperdrive.getOpenLongPositions({ @@ -51,6 +53,7 @@ export function usePortfolioLongsData({ }), })), ); + return { hyperdrive, openLongs, @@ -80,7 +83,7 @@ export function usePortfolioLongsDataFromHyperdrives({ openLongPositionsStatus: "error" | "success" | "loading"; } { const queryEnabled = !!account && !!hyperdrives.length; - + const appConfigForConnectedChain = useAppConfigForConnectedChain(); const { data: openLongPositions, status: openLongPositionsStatus } = useQuery( { queryKey: makeQueryKey2({ @@ -96,6 +99,9 @@ export function usePortfolioLongsDataFromHyperdrives({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, }); const allLongs = await readHyperdrive.getOpenLongPositions({ account, diff --git a/apps/hyperdrive-trading/src/ui/portfolio/lp/usePortfolioLpData.ts b/apps/hyperdrive-trading/src/ui/portfolio/lp/usePortfolioLpData.ts index 399e35d17..b63c88b93 100644 --- a/apps/hyperdrive-trading/src/ui/portfolio/lp/usePortfolioLpData.ts +++ b/apps/hyperdrive-trading/src/ui/portfolio/lp/usePortfolioLpData.ts @@ -22,6 +22,7 @@ export function usePortfolioLpDataFromHyperdrives({ openLpPositions: LpPosition[] | undefined; openLpPositionStatus: "error" | "success" | "loading"; } { + const appConfigForConnectedChain = useAppConfigForConnectedChain(); const queryEnabled = !!account && !!hyperdrives.length; const { data, status } = useQuery({ queryKey: makeQueryKey("portfolioLp", { @@ -39,6 +40,8 @@ export function usePortfolioLpDataFromHyperdrives({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId]?.address, }); const [lpShares, withdrawalShares] = await Promise.all([ @@ -88,6 +91,8 @@ export function usePortfolioLpData({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId]?.address, }); const [lpShares, withdrawalShares] = await Promise.all([ diff --git a/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts b/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts index bff0a786a..99489a787 100644 --- a/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts +++ b/apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts @@ -34,6 +34,9 @@ export function usePortfolioShortsData({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, }); return { @@ -64,6 +67,7 @@ export function usePortfolioShortsDataFromHyperdrives({ | undefined; openShortPositionsStatus: "error" | "success" | "loading"; } { + const appConfigForConnectedChain = useAppConfigForConnectedChain(); const queryEnabled = !!account && !!hyperdrives.length; const { data: openShortPositions, status: openShortPositionsStatus } = useQuery({ @@ -80,6 +84,9 @@ export function usePortfolioShortsDataFromHyperdrives({ address: hyperdrive.address, drift: getDrift({ chainId: hyperdrive.chainId }), earliestBlock: hyperdrive.initializationBlock, + zapContractAddress: + appConfigForConnectedChain.zaps[hyperdrive.chainId] + ?.address, }); const openShorts = await readHyperdrive.getOpenShorts({ account, diff --git a/apps/sdk-sandbox/scripts/example.ts b/apps/sdk-sandbox/scripts/example.ts index 046a0d62b..03bc1320a 100644 --- a/apps/sdk-sandbox/scripts/example.ts +++ b/apps/sdk-sandbox/scripts/example.ts @@ -27,7 +27,7 @@ const writePool = new ReadWriteHyperdrive({ const readPool = new ReadHyperdrive({ address: poolAddress, drift, - auxiliaryContractAddress: zapsConfig.address, + zapContractAddress: zapsConfig.address, earliestBlock, }); diff --git a/packages/hyperdrive-js/src/hyperdrive/ReadHyperdrive.ts b/packages/hyperdrive-js/src/hyperdrive/ReadHyperdrive.ts index f3dd225eb..3d5844a8f 100644 --- a/packages/hyperdrive-js/src/hyperdrive/ReadHyperdrive.ts +++ b/packages/hyperdrive-js/src/hyperdrive/ReadHyperdrive.ts @@ -52,7 +52,7 @@ export class ReadHyperdrive extends ReadClient { readonly contract: ReadContract; /** - * The optional address of an auxiliary contract such as a zap contract. + * The optional address of the zap contract. */ readonly zapContractAddress?: Address; @@ -859,28 +859,33 @@ export class ReadHyperdrive extends ReadClient { filter: { trader: account, assetId }, }); - // Handle transfers sent to the contract. - const transfersSentToAux = await this.contract.getEvents("TransferSingle", { - filter: { from: account, to: this.zapContractAddress }, - toBlock: options?.block, - }); - - if (transfersSentToAux.length) { - const accountTxHashes = transfersSentToAux.map( - ({ transactionHash }) => transactionHash, - ); - // Fetch CloseLong events emitted by the auxiliary contract in the relevant block range. - const allAuxCloses = await this.contract.getEvents("CloseLong", { - filter: { trader: this.zapContractAddress, assetId }, - fromBlock: transfersSentToAux[0].blockNumber, - toBlock: transfersSentToAux.at(-1)?.blockNumber, - }); - // Only include events that occurred in the same transactions. - const auxClosesForAccount = allAuxCloses.filter(({ transactionHash }) => - accountTxHashes.includes(transactionHash as `0x${string}`), + if (this.zapContractAddress) { + // Handle transfers sent to the zap contract. + const transfersSentToZap = await this.contract.getEvents( + "TransferSingle", + { + filter: { from: account, to: this.zapContractAddress }, + toBlock: options?.block, + }, ); - for (const event of auxClosesForAccount) { - closeLongEvents.push(event); + + if (transfersSentToZap.length) { + const accountTxHashes = transfersSentToZap.map( + ({ transactionHash }) => transactionHash, + ); + // Fetch CloseLong events emitted by the zap contract in the relevant block range. + const allZapCloses = await this.contract.getEvents("CloseLong", { + filter: { trader: this.zapContractAddress, assetId }, + fromBlock: transfersSentToZap[0].blockNumber, + toBlock: transfersSentToZap.at(-1)?.blockNumber, + }); + // Only include events that occurred in the same transactions. + const zapClosesForAccount = allZapCloses.filter(({ transactionHash }) => + accountTxHashes.includes(transactionHash as `0x${string}`), + ); + for (const event of zapClosesForAccount) { + closeLongEvents.push(event); + } } } diff --git a/packages/hyperdrive-js/src/hyperdrive/getHyperdrive.ts b/packages/hyperdrive-js/src/hyperdrive/getHyperdrive.ts index 062016ff7..efc2f2811 100644 --- a/packages/hyperdrive-js/src/hyperdrive/getHyperdrive.ts +++ b/packages/hyperdrive-js/src/hyperdrive/getHyperdrive.ts @@ -31,6 +31,7 @@ export async function getHyperdrive({ cacheNamespace, earliestBlock, debugName, + zapContractAddress, }: HyperdriveOptions): Promise> { cacheNamespace ??= await drift.getChainId(); @@ -41,6 +42,7 @@ export async function getHyperdrive({ cacheNamespace, earliestBlock, debugName, + zapContractAddress, }; const isReadWrite = isReadWriteOptions(options);