Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function useReadHyperdrive({
address,
drift,
earliestBlock: initializationBlock,
zapContractAddress: appConfig.zaps[chainId].address,
});
}
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function useReadWriteHyperdrive({
address,
drift,
earliestBlock: initializationBlock,
zapContractAddress: appConfig.zaps[chainId].address,
});
}
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,6 +23,7 @@ export function useTotalOpenLongsValueTwo({
isLoading: boolean;
totalOpenLongsValueError: Error;
} {
const appConfig = useAppConfigForConnectedChain();
const queryEnabled = !!account && !!longs && enabled;
const {
data: totalOpenLongsValue,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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",
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export function usePortfolioLongsData({
} {
const appConfigForConnectedChain = useAppConfigForConnectedChain();
const queryEnabled = !!account && !!appConfigForConnectedChain;

const { data: openLongPositions, status: openLongPositionsStatus } = useQuery(
{
queryKey: makeQueryKey("portfolioLongs", { account }),
Expand All @@ -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({
Expand All @@ -51,6 +53,7 @@ export function usePortfolioLongsData({
}),
})),
);

return {
hyperdrive,
openLongs,
Expand Down Expand Up @@ -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({
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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([
Expand Down Expand Up @@ -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([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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({
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion apps/sdk-sandbox/scripts/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const writePool = new ReadWriteHyperdrive({
const readPool = new ReadHyperdrive({
address: poolAddress,
drift,
auxiliaryContractAddress: zapsConfig.address,
zapContractAddress: zapsConfig.address,
earliestBlock,
});

Expand Down
49 changes: 27 additions & 22 deletions packages/hyperdrive-js/src/hyperdrive/ReadHyperdrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ReadHyperdrive extends ReadClient {
readonly contract: ReadContract<HyperdriveAbi>;

/**
* The optional address of an auxiliary contract such as a zap contract.
* The optional address of the zap contract.
*/
readonly zapContractAddress?: Address;

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/hyperdrive-js/src/hyperdrive/getHyperdrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function getHyperdrive<T extends Drift = Drift>({
cacheNamespace,
earliestBlock,
debugName,
zapContractAddress,
}: HyperdriveOptions<T>): Promise<Hyperdrive<T>> {
cacheNamespace ??= await drift.getChainId();

Expand All @@ -41,6 +42,7 @@ export async function getHyperdrive<T extends Drift = Drift>({
cacheNamespace,
earliestBlock,
debugName,
zapContractAddress,
};
const isReadWrite = isReadWriteOptions(options);

Expand Down
Loading