-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use total shorts value added (#1042)
- Loading branch information
1 parent
16ac1e2
commit a345a21
Showing
3 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/useTotalShortsValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Short } from "@delvtech/hyperdrive-viem"; | ||
import { HyperdriveConfig } from "@hyperdrive/appconfig"; | ||
import { useQueries } from "@tanstack/react-query"; | ||
import { makeQueryKey } from "src/base/makeQueryKey"; | ||
import { parseUnits } from "src/base/parseUnits"; | ||
import { convertSharesToBase } from "src/hyperdrive/convertSharesToBase"; | ||
import { usePoolInfo } from "src/ui/hyperdrive/hooks/usePoolInfo"; | ||
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive"; | ||
import { Address } from "viem"; | ||
|
||
export function useTotalShortsValue({ | ||
hyperdrive, | ||
account, | ||
openShorts, | ||
}: { | ||
hyperdrive: HyperdriveConfig; | ||
account: Address | undefined; | ||
openShorts: Short[] | undefined; | ||
}): { totalShortsValue: bigint | undefined; isLoading: boolean } { | ||
const readHyperdrive = useReadHyperdrive(hyperdrive.address); | ||
const { poolInfo } = usePoolInfo({ hyperdriveAddress: hyperdrive.address }); | ||
|
||
let isLoading = true; | ||
const queryEnabled = | ||
!!account && !!openShorts && !!readHyperdrive && !!poolInfo; | ||
const previewCloseShortQueries = useQueries({ | ||
queries: | ||
openShorts?.map((short) => ({ | ||
queryKey: makeQueryKey("previewCloseShort", { | ||
hyperdriveAddress: hyperdrive.address, | ||
maturityTime: short.maturity?.toString(), | ||
shortAmountIn: short.bondAmount?.toString(), | ||
minAmountOut: parseUnits("0", 18).toString(), | ||
destination: account, | ||
asBase: false, | ||
}), | ||
enabled: queryEnabled, | ||
queryFn: queryEnabled | ||
? () => | ||
readHyperdrive?.previewCloseShort({ | ||
maturityTime: short.maturity, | ||
shortAmountIn: short.bondAmount, | ||
destination: account, | ||
asBase: false, | ||
minAmountOut: parseUnits("0", 18), | ||
options: { | ||
from: account, | ||
}, | ||
}) | ||
: undefined, | ||
})) ?? [], | ||
}); | ||
const allQueriesSucceeded = previewCloseShortQueries.every( | ||
(query) => query.status === "success", | ||
); | ||
let totalShortsValue = 0n; | ||
if (allQueriesSucceeded) { | ||
previewCloseShortQueries.forEach((query) => { | ||
const amountOutInBase = convertSharesToBase({ | ||
decimals: hyperdrive.decimals, | ||
sharesAmount: query.data, | ||
vaultSharePrice: poolInfo?.vaultSharePrice, | ||
}); | ||
totalShortsValue += amountOutInBase || 0n; | ||
}); | ||
} | ||
|
||
isLoading = previewCloseShortQueries.some( | ||
(query) => query.status === "loading", | ||
); | ||
|
||
return { totalShortsValue, isLoading }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 38 additions & 5 deletions
43
apps/hyperdrive-trading/src/ui/markets/ShortsTab/ShortsTab.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters