|
4 | 4 | OpenLongPositionReceived, |
5 | 5 | } from "@delvtech/hyperdrive-js"; |
6 | 6 | import { useQuery } from "@tanstack/react-query"; |
| 7 | +import { LATEST_POSITION_BLOCKS_BY_CHAIN_ID } from "src/base/latestBlocks"; |
7 | 8 | import { makeQueryKey, makeQueryKey2 } from "src/base/makeQueryKey"; |
8 | 9 | import { getDrift } from "src/drift/getDrift"; |
9 | 10 | import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain"; |
@@ -32,32 +33,54 @@ export function usePortfolioLongsData({ |
32 | 33 | ? async () => |
33 | 34 | await Promise.all( |
34 | 35 | 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 | + }); |
43 | 45 |
|
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 | + ); |
56 | 69 |
|
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 | + } |
61 | 84 | }), |
62 | 85 | ) |
63 | 86 | : undefined, |
@@ -95,30 +118,52 @@ export function usePortfolioLongsDataFromHyperdrives({ |
95 | 118 | ? async () => { |
96 | 119 | const results = await Promise.all( |
97 | 120 | 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 | + }); |
109 | 138 |
|
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 | + ); |
120 | 155 |
|
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 | + } |
122 | 167 | }), |
123 | 168 | ); |
124 | 169 | return results.flat(); |
|
0 commit comments