Skip to content

Commit 51cdac4

Browse files
authored
Singular Loading State for Shorts (#1454)
* open shorts working on multichain * shorts loading state completed * Move app config mapping logic into hook * move open shorts into own variable * return promise all * map over chains, not registries
1 parent 4b2eeaf commit 51cdac4

File tree

4 files changed

+64
-64
lines changed

4 files changed

+64
-64
lines changed

apps/hyperdrive-trading/src/ui/hyperdrive/hooks/usePublicClients.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AppConfig } from "@hyperdrive/appconfig";
12
import { getPublicClient } from "@wagmi/core";
23
import { wagmiConfig } from "src/network/wagmiClient";
34
import { PublicClient } from "viem";
@@ -7,14 +8,13 @@ type ClientObject = { [chainId: number]: { publicClient: PublicClient } };
78
/**
89
* Initializes and returns public clients for specified chain IDs.
910
*
10-
* @param {number[]} chainIds - An array of chain IDs to create public clients for.
11-
* @returns {ClientObject} An object where keys are chain IDs and values are objects
12-
* containing the corresponding public client.
11+
* @param {AppConfig} appConfig
12+
* @returns {ClientObject} An object where keys are chain IDs and values are objects containing the corresponding public client.
1313
*/
14-
export function usePublicClients(chainIds: number[]): ClientObject {
14+
export function usePublicClients(chains: number[]): ClientObject {
1515
const clients: ClientObject = {};
1616

17-
for (const chainId of chainIds) {
17+
for (const chainId of chains) {
1818
const publicClient = getPublicClient(wagmiConfig as any, {
1919
chainId,
2020
}) as PublicClient;

apps/hyperdrive-trading/src/ui/hyperdrive/shorts/OpenShortsTable/OpenShortsTableDesktopTwo.tsx

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,29 @@ import { NonIdealState } from "src/ui/base/components/NonIdealState";
2424
import { Pagination } from "src/ui/base/components/Pagination";
2525
import { formatBalance } from "src/ui/base/formatting/formatBalance";
2626
import { MaturesOnCellTwo } from "src/ui/hyperdrive/MaturesOnCell/MaturesOnCell";
27-
import { useMarketState } from "src/ui/hyperdrive/hooks/useMarketState";
2827
import { StatusCell } from "src/ui/hyperdrive/longs/OpenLongsTable/StatusCell";
2928
import { CloseShortModalButton } from "src/ui/hyperdrive/shorts/CloseShortModalButton/CloseShortModalButton";
3029
import { CurrentShortsValueCell } from "src/ui/hyperdrive/shorts/OpenShortsTable/CurrentShortsValueCell";
3130
import { ShortRateAndSizeCell } from "src/ui/hyperdrive/shorts/OpenShortsTable/ShortRateAndSizeCell";
3231
import { TotalOpenShortValue } from "src/ui/hyperdrive/shorts/OpenShortsTable/TotalOpenShortsValue";
33-
import { useOpenShorts } from "src/ui/hyperdrive/shorts/hooks/useOpenShorts";
3432
import { usePortfolioShortsData } from "src/ui/portfolio/usePortfolioShortsData";
3533
import { useAccount } from "wagmi";
3634

3735
export function OpenShortsContainer(): ReactElement {
3836
const { openShortPositions, openShortPositionsStatus } =
3937
usePortfolioShortsData();
4038
const appConfig = useAppConfig();
39+
40+
if (openShortPositionsStatus === "loading") {
41+
return (
42+
<div className="mt-10 flex w-[1036px] flex-col gap-10">
43+
<LoadingState
44+
heading="Loading your Shorts..."
45+
text="Searching for Shorts events, calculating current value and PnL..."
46+
/>
47+
</div>
48+
);
49+
}
4150
return (
4251
<div className="mt-10 flex w-[1036px] flex-col gap-10">
4352
{appConfig.hyperdrives.map((hyperdrive) => {
@@ -51,11 +60,13 @@ export function OpenShortsContainer(): ReactElement {
5160
tokens: appConfig.tokens,
5261
tokenAddress: hyperdrive.poolConfig.vaultSharesToken,
5362
});
63+
const openShorts = openShortPositions?.find(
64+
(position) =>
65+
position.hyperdrive.address === hyperdrive.address &&
66+
position.hyperdrive.chainId === hyperdrive.chainId,
67+
)?.openShorts;
5468
// Ensure this hyperdrive pool has open positions before rendering.
55-
if (
56-
openShortPositionsStatus === "success" &&
57-
!openShortPositions?.[hyperdrive.address]?.length
58-
) {
69+
if (openShortPositionsStatus === "success" && !openShorts?.length) {
5970
return null;
6071
}
6172
return (
@@ -87,7 +98,7 @@ export function OpenShortsContainer(): ReactElement {
8798
</div>
8899
<OpenShortsTableDesktopTwo
89100
hyperdrive={hyperdrive}
90-
openShortPositionsStatus={openShortPositionsStatus}
101+
openShorts={openShorts}
91102
/>
92103
</div>
93104
);
@@ -98,22 +109,14 @@ export function OpenShortsContainer(): ReactElement {
98109

99110
export function OpenShortsTableDesktopTwo({
100111
hyperdrive,
101-
openShortPositionsStatus,
112+
openShorts,
102113
}: {
103114
hyperdrive: HyperdriveConfig;
104-
openShortPositionsStatus?: "loading" | "success" | "error";
115+
openShorts: OpenShort[] | undefined;
105116
}): ReactElement {
106117
const { address: account } = useAccount();
107118
const appConfig = useAppConfig();
108-
const { marketState } = useMarketState({
109-
hyperdriveAddress: hyperdrive.address,
110-
chainId: hyperdrive.chainId,
111-
});
112-
const { openShorts, openShortsStatus } = useOpenShorts({
113-
account,
114-
chainId: hyperdrive.chainId,
115-
hyperdriveAddress: hyperdrive.address,
116-
});
119+
117120
const tableInstance = useReactTable({
118121
columns: getColumns({ hyperdrive, appConfig }),
119122
data: openShorts || [],
@@ -141,17 +144,6 @@ export function OpenShortsTableDesktopTwo({
141144
</div>
142145
);
143146
}
144-
if (
145-
openShortsStatus === "loading" ||
146-
openShortPositionsStatus === "loading"
147-
) {
148-
return (
149-
<LoadingState
150-
heading="Loading your Shorts..."
151-
text="Searching for Shorts events, calculating current value and PnL..."
152-
/>
153-
);
154-
}
155147

156148
return (
157149
<div className="daisy-card overflow-x-clip rounded-box bg-gray-750 pt-3">

apps/hyperdrive-trading/src/ui/portfolio/usePortfolioLpData.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
import { HyperdriveConfig } from "@hyperdrive/appconfig";
12
import { useQuery } from "@tanstack/react-query";
23
import { makeQueryKey } from "src/base/makeQueryKey";
34
import { getReadHyperdrive } from "src/hyperdrive/getReadHyperdrive";
45
import { useAppConfig } from "src/ui/appconfig/useAppConfig";
56
import { usePublicClients } from "src/ui/hyperdrive/hooks/usePublicClients";
6-
import { Address } from "viem";
77
import { useAccount } from "wagmi";
88

99
type LpPosition = {
10-
hyperdrive: {
11-
address: Address;
12-
chainId: number;
13-
};
10+
hyperdrive: HyperdriveConfig;
1411
lpShares: bigint;
1512
withdrawalShares: bigint;
1613
};
@@ -22,9 +19,7 @@ export function usePortfolioLpData(): {
2219
const appConfig = useAppConfig();
2320
const { address: account } = useAccount();
2421
const queryEnabled = !!account && !!appConfig.hyperdrives.length;
25-
const clients = usePublicClients(
26-
Object.keys(appConfig.registries).map(Number),
27-
);
22+
const clients = usePublicClients(Object.keys(appConfig.chains).map(Number));
2823
const { data, status } = useQuery({
2924
queryKey: makeQueryKey("portfolioLp", { account }),
3025
queryFn: queryEnabled

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1-
import { OpenShort, ReadHyperdrive } from "@delvtech/hyperdrive-viem";
1+
import { OpenShort } from "@delvtech/hyperdrive-viem";
2+
import { HyperdriveConfig } from "@hyperdrive/appconfig";
23
import { useQuery } from "@tanstack/react-query";
34
import { makeQueryKey } from "src/base/makeQueryKey";
4-
import { sdkCache } from "src/sdk/sdkCache";
5+
import { getReadHyperdrive } from "src/hyperdrive/getReadHyperdrive";
56
import { useAppConfig } from "src/ui/appconfig/useAppConfig";
6-
import { Address } from "viem";
7-
import { useAccount, usePublicClient } from "wagmi";
7+
import { usePublicClients } from "src/ui/hyperdrive/hooks/usePublicClients";
8+
import { useAccount } from "wagmi";
89

9-
type OpenShortPositionsData = Record<Address, OpenShort[]>;
10+
type OpenShortPositionsData = {
11+
hyperdrive: HyperdriveConfig;
12+
openShorts: OpenShort[];
13+
}[];
1014

1115
export function usePortfolioShortsData(): {
1216
openShortPositions: OpenShortPositionsData | undefined;
1317
openShortPositionsStatus: "error" | "success" | "loading";
1418
} {
1519
const { address: account } = useAccount();
16-
// TODO: We should be getting a specific public client for the chain the
17-
// hyperdrive is on
18-
const publicClient = usePublicClient();
20+
1921
const appConfig = useAppConfig();
20-
const queryEnabled = !!account && !!appConfig && !!publicClient;
22+
const clients = usePublicClients(Object.keys(appConfig.chains).map(Number));
23+
const queryEnabled = !!account && !!appConfig && !!clients;
2124

2225
const { data: openShortPositions, status: openShortPositionsStatus } =
2326
useQuery({
2427
queryKey: makeQueryKey("portfolioShorts", { account }),
2528
enabled: queryEnabled,
2629
queryFn: queryEnabled
27-
? async () => {
28-
const result: OpenShortPositionsData = {};
30+
? async () =>
2931
await Promise.all(
3032
appConfig.hyperdrives.map(async (hyperdrive) => {
31-
const readHyperdrive = new ReadHyperdrive({
32-
address: hyperdrive.address,
33+
const publicClient = clients[hyperdrive.chainId]?.publicClient;
34+
35+
if (!publicClient) {
36+
console.error(
37+
`No public client found for chainId ${hyperdrive.chainId}`,
38+
);
39+
return {
40+
hyperdrive,
41+
openShorts: [],
42+
};
43+
}
44+
45+
const readHyperdrive = await getReadHyperdrive({
46+
appConfig,
47+
hyperdriveAddress: hyperdrive.address,
3348
publicClient,
34-
cache: sdkCache,
3549
});
3650

37-
result[hyperdrive.address] = await readHyperdrive.getOpenShorts(
38-
{
39-
account: account,
40-
},
41-
);
51+
return {
52+
hyperdrive,
53+
openShorts: await readHyperdrive.getOpenShorts({
54+
account,
55+
}),
56+
};
4257
}),
43-
);
44-
return result;
45-
}
58+
)
4659
: undefined,
4760
});
4861
return {

0 commit comments

Comments
 (0)