Skip to content

Commit d805c19

Browse files
authored
Fix total values to include closed (#1175)
* check if closed longs * fix total values for closed * remove console log * remove typecasting * move total variable * split into two hooks * moved is loading * adds enabled param
1 parent 020f7bf commit d805c19

File tree

6 files changed

+200
-61
lines changed

6 files changed

+200
-61
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { ClosedLong } from "@delvtech/hyperdrive-viem";
2+
import { HyperdriveConfig } from "@hyperdrive/appconfig";
3+
import { useQuery } from "@tanstack/react-query";
4+
import { makeQueryKey } from "src/base/makeQueryKey";
5+
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
6+
import { useOpenOrClosedSearchParam } from "src/ui/markets/hooks/useOpenOrClosedSearchParam";
7+
import { Address } from "viem";
8+
9+
export function useTotalClosedLongsValue({
10+
hyperdrive,
11+
account,
12+
closedLongs,
13+
enabled,
14+
}: {
15+
hyperdrive: HyperdriveConfig;
16+
account: Address | undefined;
17+
closedLongs: ClosedLong[] | undefined;
18+
enabled: boolean;
19+
}): { totalClosedLongsValue: bigint | undefined; isLoading: boolean } {
20+
const readHyperdrive = useReadHyperdrive(hyperdrive.address);
21+
const activeOpenOrClosedTab = useOpenOrClosedSearchParam();
22+
23+
const queryEnabled =
24+
!!account && !!closedLongs && !!readHyperdrive && enabled;
25+
26+
const { data: totalClosedLongsValue, isLoading } = useQuery({
27+
queryKey: makeQueryKey("totalClosedLongsValue", {
28+
hyperdriveAddress: hyperdrive.address,
29+
account,
30+
activeOpenOrClosedTab,
31+
}),
32+
enabled: queryEnabled,
33+
queryFn: queryEnabled
34+
? async () => {
35+
let totalClosedLongsValue = 0n;
36+
closedLongs.forEach((long) => {
37+
totalClosedLongsValue += long.baseAmount;
38+
});
39+
return totalClosedLongsValue;
40+
}
41+
: undefined,
42+
});
43+
44+
return { totalClosedLongsValue, isLoading };
45+
}

apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useTotalLongsValue.ts renamed to apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useTotalOpenLongsValue.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,38 @@ import { makeQueryKey } from "src/base/makeQueryKey";
55
import { convertSharesToBase } from "src/hyperdrive/convertSharesToBase";
66
import { usePoolInfo } from "src/ui/hyperdrive/hooks/usePoolInfo";
77
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
8+
import { useOpenOrClosedSearchParam } from "src/ui/markets/hooks/useOpenOrClosedSearchParam";
89
import { Address } from "viem";
910

10-
export function useTotalLongsValue({
11+
export function useTotalOpenLongsValue({
1112
hyperdrive,
1213
account,
13-
openLongs,
14+
longs,
15+
enabled,
1416
}: {
1517
hyperdrive: HyperdriveConfig;
1618
account: Address | undefined;
17-
openLongs: Long[] | undefined;
18-
}): { totalLongsValue: bigint | undefined; isLoading: boolean } {
19+
longs: Long[] | undefined;
20+
enabled: boolean;
21+
}): { totalOpenLongsValue: bigint | undefined; isLoading: boolean } {
1922
const readHyperdrive = useReadHyperdrive(hyperdrive.address);
2023
const { poolInfo } = usePoolInfo({ hyperdriveAddress: hyperdrive.address });
21-
24+
const activeOpenOrClosedTab = useOpenOrClosedSearchParam();
2225
const queryEnabled =
23-
!!account && !!openLongs && !!readHyperdrive && !!poolInfo;
26+
!!account && !!longs && !!readHyperdrive && !!poolInfo && enabled;
2427

25-
const { data: totalLongsValue, isLoading } = useQuery({
28+
const { data: totalOpenLongsValue, isLoading } = useQuery({
2629
queryKey: makeQueryKey("totalLongsValue", {
2730
hyperdriveAddress: hyperdrive.address,
2831
account,
32+
activeOpenOrClosedTab,
2933
}),
3034
enabled: queryEnabled,
3135
queryFn: queryEnabled
3236
? async () => {
33-
const promises = openLongs.map((long) =>
37+
let totalOpenLongsValue = 0n;
38+
39+
const promises = longs.map((long) =>
3440
readHyperdrive.previewCloseLong({
3541
maturityTime: long.maturity,
3642
bondAmountIn: long.bondAmount,
@@ -40,20 +46,19 @@ export function useTotalLongsValue({
4046

4147
const results = await Promise.all(promises);
4248

43-
let totalLongsValue = 0n;
4449
results.forEach((result) => {
4550
const amountOutInBase = convertSharesToBase({
4651
decimals: hyperdrive.decimals,
4752
sharesAmount: result.amountOut,
4853
vaultSharePrice: poolInfo?.vaultSharePrice,
4954
});
50-
totalLongsValue += amountOutInBase || 0n;
55+
totalOpenLongsValue += amountOutInBase || 0n;
5156
});
5257

53-
return totalLongsValue;
58+
return totalOpenLongsValue;
5459
}
5560
: undefined,
5661
});
5762

58-
return { totalLongsValue, isLoading };
63+
return { totalOpenLongsValue, isLoading };
5964
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { ClosedShort } from "@delvtech/hyperdrive-viem";
2+
import { HyperdriveConfig } from "@hyperdrive/appconfig";
3+
import { useQuery } from "@tanstack/react-query";
4+
import { makeQueryKey } from "src/base/makeQueryKey";
5+
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
6+
import { useOpenOrClosedSearchParam } from "src/ui/markets/hooks/useOpenOrClosedSearchParam";
7+
import { Address } from "viem";
8+
9+
export function useTotalClosedShortsValue({
10+
hyperdrive,
11+
account,
12+
closedShorts,
13+
enabled,
14+
}: {
15+
hyperdrive: HyperdriveConfig;
16+
account: Address | undefined;
17+
closedShorts: ClosedShort[] | undefined;
18+
enabled: boolean;
19+
}): { totalClosedShortsValue: bigint | undefined; isLoading: boolean } {
20+
const readHyperdrive = useReadHyperdrive(hyperdrive.address);
21+
const activeOpenOrClosedTab = useOpenOrClosedSearchParam();
22+
23+
const queryEnabled =
24+
!!account && !!closedShorts && !!readHyperdrive && enabled;
25+
26+
const { data: totalClosedShortsValue, isLoading } = useQuery({
27+
queryKey: makeQueryKey("totalClosedShortsValue", {
28+
hyperdriveAddress: hyperdrive.address,
29+
account,
30+
activeOpenOrClosedTab,
31+
}),
32+
enabled: queryEnabled,
33+
queryFn: queryEnabled
34+
? async () => {
35+
let totalClosedShortsValue = 0n;
36+
closedShorts.forEach((short) => {
37+
totalClosedShortsValue += short.baseAmountReceived;
38+
});
39+
return totalClosedShortsValue;
40+
}
41+
: undefined,
42+
});
43+
44+
return { totalClosedShortsValue, isLoading };
45+
}

apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalShortsValue.ts renamed to apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalOpenShortsValue.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@ import { usePoolInfo } from "src/ui/hyperdrive/hooks/usePoolInfo";
77
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
88
import { Address } from "viem";
99

10-
export function useTotalShortsValue({
10+
export function useTotalOpenShortsValue({
1111
hyperdrive,
1212
account,
13-
openShorts,
13+
shorts,
14+
enabled,
1415
}: {
1516
hyperdrive: HyperdriveConfig;
1617
account: Address | undefined;
17-
openShorts: Short[] | undefined;
18-
}): { totalShortsValue: bigint | undefined; isLoading: boolean } {
18+
shorts: Short[] | undefined;
19+
enabled: boolean;
20+
}): { totalOpenShortsValue: bigint | undefined; isLoading: boolean } {
1921
const readHyperdrive = useReadHyperdrive(hyperdrive.address);
2022
const { poolInfo } = usePoolInfo({ hyperdriveAddress: hyperdrive.address });
2123

2224
const queryEnabled =
23-
!!account && !!openShorts && !!readHyperdrive && !!poolInfo;
25+
!!account && !!shorts && !!readHyperdrive && !!poolInfo && enabled;
2426

25-
const { data: totalShortsValue, isLoading } = useQuery({
26-
queryKey: makeQueryKey("totalShortsValue", {
27+
const { data: totalOpenShortsValue, isLoading } = useQuery({
28+
queryKey: makeQueryKey("totalOpenShortsValue", {
2729
hyperdriveAddress: hyperdrive.address,
2830
account,
2931
}),
3032
enabled: queryEnabled,
3133
queryFn: queryEnabled
3234
? async () => {
33-
const promises = openShorts.map((short) =>
35+
let totalOpenShortsValue = 0n;
36+
37+
const promises = shorts.map((short) =>
3438
readHyperdrive.previewCloseShort({
3539
maturityTime: short.maturity,
3640
shortAmountIn: short.bondAmount,
@@ -40,20 +44,19 @@ export function useTotalShortsValue({
4044

4145
const results = await Promise.all(promises);
4246

43-
let totalShortsValue = 0n;
4447
results.forEach((result) => {
4548
const amountOutInBase = convertSharesToBase({
4649
decimals: hyperdrive.decimals,
4750
sharesAmount: result.amountOut,
4851
vaultSharePrice: poolInfo?.vaultSharePrice,
4952
});
50-
totalShortsValue += amountOutInBase || 0n;
53+
totalOpenShortsValue += amountOutInBase || 0n;
5154
});
5255

53-
return totalShortsValue;
56+
return totalOpenShortsValue;
5457
}
5558
: undefined,
5659
});
5760

58-
return { totalShortsValue, isLoading };
61+
return { totalOpenShortsValue, isLoading };
5962
}

apps/hyperdrive-trading/src/ui/markets/LongsTab/LongsTab.tsx

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import { formatBalance } from "src/ui/base/formatting/formatBalance";
66
import { ClosedLongsTable } from "src/ui/hyperdrive/longs/ClosedLongsTable/ClosedLongsTable";
77
import { OpenLongModalButton } from "src/ui/hyperdrive/longs/OpenLongModalButton/OpenLongModalButton";
88
import { OpenLongsTable } from "src/ui/hyperdrive/longs/OpenLongsTable/OpenLongsTable";
9+
import { useClosedLongs } from "src/ui/hyperdrive/longs/hooks/useClosedLongs";
910
import { useOpenLongs } from "src/ui/hyperdrive/longs/hooks/useOpenLongs";
10-
import { useTotalLongsValue } from "src/ui/hyperdrive/longs/hooks/useTotalLongsValue";
11+
import { useTotalClosedLongsValue } from "src/ui/hyperdrive/longs/hooks/useTotalClosedLongsValue";
12+
import { useTotalOpenLongsValue } from "src/ui/hyperdrive/longs/hooks/useTotalOpenLongsValue";
1113
import { MarketDetailsTab } from "src/ui/markets/MarketDetailsTab/MarketDetailsTab";
1214
import { OpenClosedFilter } from "src/ui/markets/OpenClosedFilter/OpenClosedFilter";
1315
import { useOpenOrClosedSearchParam } from "src/ui/markets/hooks/useOpenOrClosedSearchParam";
1416
import { useAccount } from "wagmi";
17+
1518
export function LongsTab({
1619
hyperdrive,
1720
}: {
@@ -24,36 +27,54 @@ export function LongsTab({
2427
account,
2528
hyperdriveAddress: hyperdrive.address,
2629
});
27-
const { totalLongsValue, isLoading } = useTotalLongsValue({
28-
hyperdrive,
30+
const { closedLongs } = useClosedLongs({
2931
account,
30-
openLongs,
32+
hyperdriveAddress: hyperdrive.address,
3133
});
34+
const { totalOpenLongsValue, isLoading: isTotalOpenValueLoading } =
35+
useTotalOpenLongsValue({
36+
hyperdrive,
37+
account,
38+
longs: openLongs,
39+
enabled: activeOpenOrClosedTab === "Open",
40+
});
41+
const { totalClosedLongsValue, isLoading: isTotalClosedLongsValueLoading } =
42+
useTotalClosedLongsValue({
43+
hyperdrive,
44+
account,
45+
closedLongs,
46+
enabled: activeOpenOrClosedTab === "Closed",
47+
});
3248
const baseToken = findBaseToken({
3349
baseTokenAddress: hyperdrive.baseToken,
3450
tokens: appConfig.tokens,
3551
});
52+
53+
const totalValue =
54+
activeOpenOrClosedTab === "Open"
55+
? totalOpenLongsValue
56+
: totalClosedLongsValue;
57+
const longs = activeOpenOrClosedTab === "Open" ? openLongs : closedLongs;
58+
3659
return (
3760
<MarketDetailsTab
3861
positions={
3962
<div className="flex flex-col">
4063
<div className="flex flex-wrap items-center justify-between gap-4 p-8">
4164
<div className="flex flex-col items-start gap-2">
4265
<h5 className="font-medium">Long Positions</h5>
43-
{!isLoading ? (
44-
<>
45-
{openLongs?.length ? (
46-
<p className="text-sm text-neutral-content">
47-
Total Value:{" "}
48-
{formatBalance({
49-
balance: totalLongsValue || 0n,
50-
decimals: baseToken.decimals,
51-
places: baseToken.places,
52-
})}{" "}
53-
{baseToken.symbol}
54-
</p>
55-
) : undefined}
56-
</>
66+
{!isTotalOpenValueLoading || !isTotalClosedLongsValueLoading ? (
67+
longs?.length ? (
68+
<p className="text-sm text-neutral-content">
69+
Total Value:{" "}
70+
{formatBalance({
71+
balance: totalValue || 0n,
72+
decimals: baseToken.decimals,
73+
places: baseToken.places,
74+
})}{" "}
75+
{baseToken.symbol}
76+
</p>
77+
) : undefined
5778
) : (
5879
<Skeleton width={100} />
5980
)}

0 commit comments

Comments
 (0)