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
30 changes: 30 additions & 0 deletions apps/hyperdrive-trading/src/base/latestBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type PositionType = "long" | "short" | "lp";

export const LATEST_POSITION_BLOCKS_BY_CHAIN_ID: Record<
number,
Record<PositionType, bigint>
> = {
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,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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: [],
};
}
Comment on lines +74 to +83
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents the entire query from failing when one of the chains fails.

}),
)
: undefined,
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
Loading