Skip to content

Commit 23e637d

Browse files
authored
Replace portfolio event queries with harcoded positions snapshot (#1852)
* Add positions snapshot * Replace portfolio event query hooks with snapshot hooks * Update tables to show current balance * Update close forms to use current balance * Remove unnecessary `ReadHyperdrive` instantiation * Fix close short modal * nit * Delete dead code * Normalize addresses before compare
1 parent 209f697 commit 23e637d

File tree

13 files changed

+1152
-47
lines changed

13 files changed

+1152
-47
lines changed

apps/hyperdrive-trading/src/hyperdrive/positionsSnapshot.ts

Lines changed: 598 additions & 0 deletions
Large diffs are not rendered by default.

apps/hyperdrive-trading/src/ui/hyperdrive/longs/StatusCell.tsx renamed to apps/hyperdrive-trading/src/ui/hyperdrive/StatusCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function StatusCell({
2929
className={classNames("flex w-28 items-center gap-2 font-inter", {
3030
"rounded-md border border-accent/20 bg-accent/20 px-[6px] py-[2px] text-accent":
3131
isTermComplete,
32-
[statusCellClassName || "text-neutral-content"]: !isTermComplete,
32+
[statusCellClassName || ""]: !isTermComplete,
3333
})}
3434
>
3535
{isTermComplete ? <CheckCircleIcon className="size-4" /> : null}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { hyperdriveAbi } from "@delvtech/hyperdrive-js";
2+
import { QueryStatus, useQuery } from "@tanstack/react-query";
3+
import { makeQueryKey2 } from "src/base/makeQueryKey";
4+
import { useDrift } from "src/ui/drift/useDrift";
5+
import { Address } from "viem";
6+
7+
export function useAssetBalance({
8+
account,
9+
assetId,
10+
hyperdriveAddress,
11+
chainId,
12+
}: {
13+
account: Address | undefined;
14+
assetId: bigint;
15+
hyperdriveAddress: Address;
16+
chainId: number;
17+
}): {
18+
assetBalance: bigint | undefined;
19+
assetBalanceStatus: QueryStatus;
20+
} {
21+
const drift = useDrift({ chainId });
22+
const queryEnabled = !!account && !!drift;
23+
const { data, status } = useQuery({
24+
queryKey: makeQueryKey2({
25+
namespace: "hyperdrive",
26+
queryId: "assetBalance",
27+
params: {
28+
account,
29+
assetId,
30+
chainId,
31+
hyperdriveAddress,
32+
},
33+
}),
34+
queryFn: queryEnabled
35+
? async () =>
36+
drift.read({
37+
address: hyperdriveAddress,
38+
abi: hyperdriveAbi,
39+
fn: "balanceOf",
40+
args: {
41+
owner: account,
42+
tokenId: assetId,
43+
},
44+
})
45+
: undefined,
46+
enabled: queryEnabled,
47+
});
48+
49+
return {
50+
assetBalance: data,
51+
assetBalanceStatus: status,
52+
};
53+
}

apps/hyperdrive-trading/src/ui/hyperdrive/longs/CloseLongForm/CloseLongForm.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import { formatBalance } from "src/ui/base/formatting/formatBalance";
2121
import { useActiveItem } from "src/ui/base/hooks/useActiveItem";
2222
import { useNumericInput } from "src/ui/base/hooks/useNumericInput";
2323
import { SwitchNetworksButton } from "src/ui/chains/SwitchChainButton/SwitchChainButton";
24+
import { useAssetBalance } from "src/ui/hyperdrive/hooks/useAssetBalance";
2425
import { InvalidTransactionButton } from "src/ui/hyperdrive/InvalidTransactionButton";
2526
import { useCloseLong } from "src/ui/hyperdrive/longs/hooks/useCloseLong";
2627
import { usePreviewCloseLong } from "src/ui/hyperdrive/longs/hooks/usePreviewCloseLong";
27-
import { StatusCell } from "src/ui/hyperdrive/longs/StatusCell";
28+
import { StatusCell } from "src/ui/hyperdrive/StatusCell";
2829
import { TransactionView } from "src/ui/hyperdrive/TransactionView";
2930
import { useCloseLongZap } from "src/ui/hyperdrive/zaps/hooks/useCloseLongZap";
3031
import { ApproveTokenChoices } from "src/ui/token/ApproveTokenChoices";
@@ -61,6 +62,12 @@ export function CloseLongForm({
6162
hyperdriveChainId: hyperdrive.chainId,
6263
appConfig,
6364
});
65+
const { assetBalance = 0n } = useAssetBalance({
66+
account,
67+
assetId: long.assetId,
68+
hyperdriveAddress: hyperdrive.address,
69+
chainId: hyperdrive.chainId,
70+
});
6471

6572
const { isFlagEnabled: isZapsEnabled } = useFeatureFlag("zaps");
6673

@@ -183,7 +190,7 @@ export function CloseLongForm({
183190
}
184191
// You can't close an amount that's larger than the position size
185192
const isAmountLargerThanPositionSize = !!(
186-
bondAmountAsBigInt && bondAmountAsBigInt > long.bondAmount
193+
bondAmountAsBigInt && bondAmountAsBigInt > assetBalance
187194
);
188195

189196
const { closeLongZap } = useCloseLongZap({
@@ -232,7 +239,7 @@ export function CloseLongForm({
232239
token={`hy${baseToken.symbol}`}
233240
value={bondAmount ?? ""}
234241
maxValue={
235-
long ? formatUnits(long.bondAmount, hyperdrive.decimals) : ""
242+
long ? formatUnits(assetBalance, hyperdrive.decimals) : ""
236243
}
237244
onChange={(newAmount) => {
238245
window.plausible("formChange", {
@@ -250,9 +257,9 @@ export function CloseLongForm({
250257
bottomRightElement={
251258
<div className="flex flex-col gap-1 text-xs text-neutral-content">
252259
{`Balance: ${formatBalance({
253-
balance: long.bondAmount,
260+
balance: assetBalance,
254261
decimals: hyperdrive.decimals,
255-
places: baseToken.places,
262+
places: 4,
256263
})}`}
257264
</div>
258265
}

apps/hyperdrive-trading/src/ui/hyperdrive/queryKeys.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import "src/base/makeQueryKey";
22
import { Address } from "viem";
33
interface HyperdriveQueryKeys {
4+
assetBalance: {
5+
account: Address | undefined;
6+
assetId: bigint;
7+
hyperdriveAddress: Address;
8+
chainId: number;
9+
};
410
unpausedPools: {
511
chainId: number;
612
filterPoolsWithoutRewards?: boolean;

apps/hyperdrive-trading/src/ui/hyperdrive/shorts/CloseShortForm/CloseShortForm.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import { formatBalance } from "src/ui/base/formatting/formatBalance";
1515
import { useActiveItem } from "src/ui/base/hooks/useActiveItem";
1616
import { useNumericInput } from "src/ui/base/hooks/useNumericInput";
1717
import { SwitchNetworksButton } from "src/ui/chains/SwitchChainButton/SwitchChainButton";
18+
import { useAssetBalance } from "src/ui/hyperdrive/hooks/useAssetBalance";
1819
import { InvalidTransactionButton } from "src/ui/hyperdrive/InvalidTransactionButton";
19-
import { StatusCell } from "src/ui/hyperdrive/longs/StatusCell";
2020
import { useCloseShort } from "src/ui/hyperdrive/shorts/hooks/useCloseShort";
2121
import { usePreviewCloseShort } from "src/ui/hyperdrive/shorts/hooks/usePreviewCloseShort";
22+
import { StatusCell } from "src/ui/hyperdrive/StatusCell";
2223
import { TransactionView } from "src/ui/hyperdrive/TransactionView";
2324
import { useTokenBalance } from "src/ui/token/hooks/useTokenBalance";
2425
import { useTokenFiatPrice } from "src/ui/token/hooks/useTokenFiatPrice";
@@ -60,6 +61,13 @@ export function CloseShortForm({
6061
defaultItems.push(sharesToken);
6162
}
6263

64+
const { assetBalance = 0n } = useAssetBalance({
65+
account,
66+
assetId: short.assetId,
67+
hyperdriveAddress: hyperdrive.address,
68+
chainId: hyperdrive.chainId,
69+
});
70+
6371
const { balance: baseTokenBalance } = useTokenBalance({
6472
account,
6573
tokenAddress: baseToken.address,
@@ -88,7 +96,7 @@ export function CloseShortForm({
8896

8997
// You can't close an amount that's larger than the position size
9098
const isAmountLargerThanPositionSize = !!(
91-
amountAsBigInt && amountAsBigInt > short.bondAmount
99+
amountAsBigInt && amountAsBigInt > assetBalance
92100
);
93101
const { amountOut, flatPlusCurveFee, previewCloseShortStatus } =
94102
usePreviewCloseShort({
@@ -158,7 +166,7 @@ export function CloseShortForm({
158166
token={`hy${baseToken.symbol}`}
159167
value={amount ?? ""}
160168
maxValue={
161-
short ? formatUnits(short.bondAmount, hyperdrive.decimals) : ""
169+
short ? formatUnits(assetBalance, hyperdrive.decimals) : ""
162170
}
163171
onChange={(newAmount) => {
164172
window.plausible("formChange", {
@@ -177,9 +185,9 @@ export function CloseShortForm({
177185
<div className="flex flex-col text-xs text-neutral-content">
178186
{short
179187
? `Balance: ${formatBalance({
180-
balance: short.bondAmount,
188+
balance: assetBalance,
181189
decimals: hyperdrive.decimals,
182-
places: baseToken?.places,
190+
places: 4,
183191
})}`
184192
: undefined}
185193
</div>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { getBaseToken, HyperdriveConfig } from "@delvtech/hyperdrive-appconfig";
2+
import {
3+
type OpenLongPositionReceived,
4+
type OpenShort,
5+
} from "@delvtech/hyperdrive-js";
6+
import { ReactElement } from "react";
7+
import Skeleton from "react-loading-skeleton";
8+
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
9+
import { formatBalance } from "src/ui/base/formatting/formatBalance";
10+
import { useAssetBalance } from "src/ui/hyperdrive/hooks/useAssetBalance";
11+
import type { Address } from "viem";
12+
13+
export function CurrentBalanceCell({
14+
row,
15+
account,
16+
hyperdrive,
17+
}: {
18+
row:
19+
| OpenLongPositionReceived
20+
| (OpenShort & { hyperdrive: HyperdriveConfig });
21+
account: Address;
22+
hyperdrive: HyperdriveConfig;
23+
}): ReactElement {
24+
const { assetBalance, assetBalanceStatus } = useAssetBalance({
25+
account,
26+
assetId: row.assetId,
27+
hyperdriveAddress: hyperdrive.address,
28+
chainId: hyperdrive.chainId,
29+
});
30+
const appConfig = useAppConfigForConnectedChain();
31+
const baseToken = getBaseToken({
32+
hyperdriveChainId: hyperdrive.chainId,
33+
hyperdriveAddress: hyperdrive.address,
34+
appConfig,
35+
});
36+
37+
if (assetBalanceStatus === "loading") {
38+
return (
39+
<div className={"flex"}>
40+
<Skeleton width={100} />
41+
</div>
42+
);
43+
}
44+
45+
return (
46+
<div className="text-neutral-content">
47+
{formatBalance({
48+
balance: assetBalance || 0n,
49+
decimals: baseToken.decimals,
50+
places: baseToken.places,
51+
})}{" "}
52+
hy{baseToken.symbol}
53+
</div>
54+
);
55+
}

apps/hyperdrive-trading/src/ui/portfolio/longs/LongsContainer.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ExternalLink } from "src/ui/analytics/ExternalLink";
55
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
66
import LoadingState from "src/ui/base/components/LoadingState";
77
import { NonIdealState } from "src/ui/base/components/NonIdealState";
8-
import { usePortfolioLongsData } from "src/ui/portfolio/longs/usePortfolioLongsData";
8+
import { usePortfolioLongsSnapshotData } from "src/ui/portfolio/longs/usePortfolioLongsSnapshotData";
99
import { NoWalletConnected } from "src/ui/portfolio/NoWalletConnected";
1010
import { PositionContainer } from "src/ui/portfolio/PositionContainer";
1111
import { Address } from "viem";
@@ -17,9 +17,10 @@ export function OpenLongsContainer({
1717
account: Address | undefined;
1818
}): ReactElement {
1919
const appConfig = useAppConfigForConnectedChain();
20-
const { openLongPositions, openLongPositionsStatus } = usePortfolioLongsData({
21-
account,
22-
});
20+
const { openLongPositions, openLongPositionsStatus } =
21+
usePortfolioLongsSnapshotData({
22+
account,
23+
});
2324

2425
const hyperdrivesByChainAndYieldSource = groupBy(
2526
appConfig.hyperdrives,
@@ -75,7 +76,7 @@ export function OpenLongsContainer({
7576
return (
7677
<PositionContainer className="mt-10">
7778
{Object.entries(hyperdrivesByChainAndYieldSource).map(
78-
([key, hyperdrives], i) => (
79+
([, hyperdrives], i) => (
7980
<OpenLongsTableDesktop
8081
key={i}
8182
hyperdrives={hyperdrives}

apps/hyperdrive-trading/src/ui/portfolio/longs/OpenLongsTable/OpenLongsTableDesktop.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import { NonIdealState } from "src/ui/base/components/NonIdealState";
2525
import { Pagination } from "src/ui/base/components/Pagination";
2626
import { formatBalance } from "src/ui/base/formatting/formatBalance";
2727
import { CloseLongModalButton } from "src/ui/hyperdrive/longs/CloseLongModalButton/CloseLongModalButton";
28-
import { StatusCell } from "src/ui/hyperdrive/longs/StatusCell";
2928
import { MaturesOnCell } from "src/ui/hyperdrive/MaturesOnCell/MaturesOnCell";
29+
import { StatusCell } from "src/ui/hyperdrive/StatusCell";
30+
import { CurrentBalanceCell } from "src/ui/portfolio/CurrentBalanceCell";
3031
import { CurrentValueCell } from "src/ui/portfolio/longs/OpenLongsTable/CurrentValueCell";
3132
import { ManageLongsButton } from "src/ui/portfolio/longs/OpenLongsTable/ManageLongsButton";
3233
import { TotalOpenLongsValue } from "src/ui/portfolio/longs/TotalOpenLongsValue/TotalOpenLongsValue";
33-
import { usePortfolioLongsDataFromHyperdrives } from "src/ui/portfolio/longs/usePortfolioLongsData";
34+
import { usePortfolioLongsSnapshotDataFromHyperdrives } from "src/ui/portfolio/longs/usePortfolioLongsSnapshotData";
3435
import { PositionTableHeading } from "src/ui/portfolio/PositionTableHeading";
3536
import { Address } from "viem";
3637

@@ -41,7 +42,7 @@ export function OpenLongsTableDesktop({
4142
hyperdrives: HyperdriveConfig[];
4243
account: Address | undefined;
4344
}): ReactElement | null {
44-
const { openLongPositions } = usePortfolioLongsDataFromHyperdrives({
45+
const { openLongPositions } = usePortfolioLongsSnapshotDataFromHyperdrives({
4546
hyperdrives,
4647
account,
4748
});
@@ -322,13 +323,20 @@ function getColumns({
322323
}),
323324
columnHelper.accessor("maturity", {
324325
id: "value",
325-
header: `Status`,
326+
header: "Status / Current Balance",
326327
cell: ({ row, getValue }) => {
327328
return (
328-
<StatusCell
329-
maturity={getValue()}
330-
chainId={row.original.hyperdrive.chainId}
331-
/>
329+
<div>
330+
<StatusCell
331+
maturity={getValue()}
332+
chainId={row.original.hyperdrive.chainId}
333+
/>
334+
<CurrentBalanceCell
335+
account={account!}
336+
hyperdrive={row.original.hyperdrive}
337+
row={row.original}
338+
/>
339+
</div>
332340
);
333341
},
334342
}),

0 commit comments

Comments
 (0)