Skip to content

Commit 8939286

Browse files
authored
Add latest position blocks to portfolio queries (#1846)
* Add latest position blocks to portfolio long queries * Add latest position blocks to portfolio short queries
1 parent 65e89ae commit 8939286

File tree

3 files changed

+181
-76
lines changed

3 files changed

+181
-76
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
type PositionType = "long" | "short" | "lp";
2+
3+
export const LATEST_POSITION_BLOCKS_BY_CHAIN_ID: Record<
4+
number,
5+
Record<PositionType, bigint>
6+
> = {
7+
1: {
8+
long: 22090872n,
9+
short: 21391567n,
10+
lp: 22108665n,
11+
},
12+
// Gnosis
13+
100: {
14+
long: 39086666n,
15+
short: 39145215n,
16+
lp: 39083204n,
17+
},
18+
// Linea
19+
59144: {
20+
long: 10622294n,
21+
short: 10621975n,
22+
lp: 15639198n,
23+
},
24+
// Base
25+
8453: {
26+
long: 28017409n,
27+
short: 26080016n,
28+
lp: 27852111n,
29+
},
30+
};

apps/hyperdrive-trading/src/ui/portfolio/longs/usePortfolioLongsData.ts

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
OpenLongPositionReceived,
55
} from "@delvtech/hyperdrive-js";
66
import { useQuery } from "@tanstack/react-query";
7+
import { LATEST_POSITION_BLOCKS_BY_CHAIN_ID } from "src/base/latestBlocks";
78
import { makeQueryKey, makeQueryKey2 } from "src/base/makeQueryKey";
89
import { getDrift } from "src/drift/getDrift";
910
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
@@ -32,32 +33,54 @@ export function usePortfolioLongsData({
3233
? async () =>
3334
await Promise.all(
3435
appConfigForConnectedChain.hyperdrives.map(async (hyperdrive) => {
35-
const readHyperdrive = await getHyperdrive({
36-
address: hyperdrive.address,
37-
drift: getDrift({ chainId: hyperdrive.chainId }),
38-
earliestBlock: hyperdrive.initializationBlock,
39-
zapContractAddress:
40-
appConfigForConnectedChain.zaps[hyperdrive.chainId]
41-
?.address,
42-
});
36+
try {
37+
const readHyperdrive = await getHyperdrive({
38+
address: hyperdrive.address,
39+
drift: getDrift({ chainId: hyperdrive.chainId }),
40+
earliestBlock: hyperdrive.initializationBlock,
41+
zapContractAddress:
42+
appConfigForConnectedChain.zaps[hyperdrive.chainId]
43+
?.address,
44+
});
4345

44-
const allLongs = await readHyperdrive.getOpenLongPositions({
45-
account,
46-
});
47-
const openLongs = await Promise.all(
48-
allLongs.map(async (long) => ({
49-
...long,
50-
details: await readHyperdrive.getOpenLongDetails({
51-
assetId: long.assetId,
52-
account,
53-
}),
54-
})),
55-
);
46+
const allLongs = await readHyperdrive.getOpenLongPositions({
47+
account,
48+
options: {
49+
block:
50+
LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId]
51+
.long,
52+
},
53+
});
54+
const openLongs = await Promise.all(
55+
allLongs.map(async (long) => ({
56+
...long,
57+
details: await readHyperdrive.getOpenLongDetails({
58+
assetId: long.assetId,
59+
account,
60+
options: {
61+
block:
62+
LATEST_POSITION_BLOCKS_BY_CHAIN_ID[
63+
hyperdrive.chainId
64+
].long,
65+
},
66+
}),
67+
})),
68+
);
5669

57-
return {
58-
hyperdrive,
59-
openLongs,
60-
};
70+
return {
71+
hyperdrive,
72+
openLongs,
73+
};
74+
} catch (e) {
75+
console.error(
76+
`Error fetching longs for ${hyperdrive.address}:`,
77+
e,
78+
);
79+
return {
80+
hyperdrive,
81+
openLongs: [],
82+
};
83+
}
6184
}),
6285
)
6386
: undefined,
@@ -95,30 +118,52 @@ export function usePortfolioLongsDataFromHyperdrives({
95118
? async () => {
96119
const results = await Promise.all(
97120
hyperdrives.map(async (hyperdrive) => {
98-
const readHyperdrive = await getHyperdrive({
99-
address: hyperdrive.address,
100-
drift: getDrift({ chainId: hyperdrive.chainId }),
101-
earliestBlock: hyperdrive.initializationBlock,
102-
zapContractAddress:
103-
appConfigForConnectedChain.zaps[hyperdrive.chainId]
104-
?.address,
105-
});
106-
const allLongs = await readHyperdrive.getOpenLongPositions({
107-
account,
108-
});
121+
try {
122+
const readHyperdrive = await getHyperdrive({
123+
address: hyperdrive.address,
124+
drift: getDrift({ chainId: hyperdrive.chainId }),
125+
earliestBlock: hyperdrive.initializationBlock,
126+
zapContractAddress:
127+
appConfigForConnectedChain.zaps[hyperdrive.chainId]
128+
?.address,
129+
});
130+
const allLongs = await readHyperdrive.getOpenLongPositions({
131+
account,
132+
options: {
133+
block:
134+
LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId]
135+
.long,
136+
},
137+
});
109138

110-
const openLongs = await Promise.all(
111-
allLongs.map(async (long) => ({
112-
...long,
113-
hyperdrive,
114-
details: await readHyperdrive.getOpenLongDetails({
115-
assetId: long.assetId,
116-
account: account,
117-
}),
118-
})),
119-
);
139+
const openLongs = await Promise.all(
140+
allLongs.map(async (long) => ({
141+
...long,
142+
hyperdrive,
143+
details: await readHyperdrive.getOpenLongDetails({
144+
assetId: long.assetId,
145+
account: account,
146+
options: {
147+
block:
148+
LATEST_POSITION_BLOCKS_BY_CHAIN_ID[
149+
hyperdrive.chainId
150+
].long,
151+
},
152+
}),
153+
})),
154+
);
120155

121-
return openLongs;
156+
return openLongs;
157+
} catch (e) {
158+
console.error(
159+
`Error fetching long data for ${hyperdrive.address}:`,
160+
e,
161+
);
162+
return [] as (OpenLongPositionReceived & {
163+
hyperdrive: HyperdriveConfig;
164+
details: any;
165+
})[];
166+
}
122167
}),
123168
);
124169
return results.flat();

apps/hyperdrive-trading/src/ui/portfolio/shorts/usePortfolioShortsData.ts

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { HyperdriveConfig } from "@delvtech/hyperdrive-appconfig";
22
import { getHyperdrive, OpenShort } from "@delvtech/hyperdrive-js";
33
import { useQuery } from "@tanstack/react-query";
4+
import { LATEST_POSITION_BLOCKS_BY_CHAIN_ID } from "src/base/latestBlocks";
45
import { makeQueryKey, makeQueryKey2 } from "src/base/makeQueryKey";
56
import { getDrift } from "src/drift/getDrift";
67
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
@@ -30,21 +31,37 @@ export function usePortfolioShortsData({
3031
? async () =>
3132
await Promise.all(
3233
appConfigForConnectedChain.hyperdrives.map(async (hyperdrive) => {
33-
const readHyperdrive = await getHyperdrive({
34-
address: hyperdrive.address,
35-
drift: getDrift({ chainId: hyperdrive.chainId }),
36-
earliestBlock: hyperdrive.initializationBlock,
37-
zapContractAddress:
38-
appConfigForConnectedChain.zaps[hyperdrive.chainId]
39-
?.address,
40-
});
34+
try {
35+
const readHyperdrive = await getHyperdrive({
36+
address: hyperdrive.address,
37+
drift: getDrift({ chainId: hyperdrive.chainId }),
38+
earliestBlock: hyperdrive.initializationBlock,
39+
zapContractAddress:
40+
appConfigForConnectedChain.zaps[hyperdrive.chainId]
41+
?.address,
42+
});
4143

42-
return {
43-
hyperdrive,
44-
openShorts: await readHyperdrive.getOpenShorts({
45-
account,
46-
}),
47-
};
44+
return {
45+
hyperdrive,
46+
openShorts: await readHyperdrive.getOpenShorts({
47+
account,
48+
options: {
49+
block:
50+
LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId]
51+
.short,
52+
},
53+
}),
54+
};
55+
} catch (e) {
56+
console.error(
57+
`Error fetching shorts for hyperdrive ${hyperdrive.address} on chain ${hyperdrive.chainId}:`,
58+
e,
59+
);
60+
return {
61+
hyperdrive,
62+
openShorts: [],
63+
};
64+
}
4865
}),
4966
)
5067
: undefined,
@@ -80,22 +97,35 @@ export function usePortfolioShortsDataFromHyperdrives({
8097
? async () => {
8198
const results = await Promise.all(
8299
hyperdrives.map(async (hyperdrive) => {
83-
const readHyperdrive = await getHyperdrive({
84-
address: hyperdrive.address,
85-
drift: getDrift({ chainId: hyperdrive.chainId }),
86-
earliestBlock: hyperdrive.initializationBlock,
87-
debugName: hyperdrive.name,
88-
zapContractAddress:
89-
appConfigForConnectedChain.zaps[hyperdrive.chainId]
90-
?.address,
91-
});
92-
const openShorts = await readHyperdrive.getOpenShorts({
93-
account,
94-
});
95-
return openShorts.map((openShort) => ({
96-
...openShort,
97-
hyperdrive,
98-
}));
100+
try {
101+
const readHyperdrive = await getHyperdrive({
102+
address: hyperdrive.address,
103+
drift: getDrift({ chainId: hyperdrive.chainId }),
104+
earliestBlock: hyperdrive.initializationBlock,
105+
debugName: hyperdrive.name,
106+
zapContractAddress:
107+
appConfigForConnectedChain.zaps[hyperdrive.chainId]
108+
?.address,
109+
});
110+
const openShorts = await readHyperdrive.getOpenShorts({
111+
account,
112+
options: {
113+
block:
114+
LATEST_POSITION_BLOCKS_BY_CHAIN_ID[hyperdrive.chainId]
115+
.short,
116+
},
117+
});
118+
return openShorts.map((openShort) => ({
119+
...openShort,
120+
hyperdrive,
121+
}));
122+
} catch (e) {
123+
console.error(
124+
`Error fetching shorts data for ${hyperdrive.address}:`,
125+
e,
126+
);
127+
return [];
128+
}
99129
}),
100130
);
101131
return results.flat();

0 commit comments

Comments
 (0)