diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongPreview/OpenLongPreview.tsx b/apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongPreview/OpenLongPreview.tsx index b7b3f3328..4a26bc885 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongPreview/OpenLongPreview.tsx +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongPreview/OpenLongPreview.tsx @@ -49,7 +49,6 @@ export function OpenLongPreview({ tokens: appConfig.tokens, }); const { fixedAPR } = useCurrentFixedAPR(hyperdrive.address); - const termLengthMS = Number(hyperdrive.poolConfig.positionDuration * 1000n); const numDays = convertMillisecondsToDays(termLengthMS); return ( @@ -68,7 +67,7 @@ export function OpenLongPreview({ ) : ( {`${formatBalance({ @@ -82,7 +81,7 @@ export function OpenLongPreview({ ) : ( ) : ( ) : ( ) : ( ) : (
} disclaimer={(() => { - if (traderDeposit && !!amountOfBondsToShortAsBigInt) { + if (!amountOfBondsToShortAsBigInt) { + return null; + } else if (!!amountOfBondsToShortAsBigInt && !hasEnoughLiquidity) { + return ( +

+ Pool limit exceeded. Max short size is{" "} + {formatBalance({ + balance: maxBondsOut || 0n, + decimals: hyperdrive.decimals, + })}{" "} + hy{baseToken.symbol} +

+ ); + } else { return (
- {!hasEnoughBalance ? ( + {!hasEnoughBalance && openShortPreviewStatus !== "loading" ? (

Insufficient balance

@@ -250,22 +265,34 @@ export function OpenShortForm({

You pay{" "} - {formatBalance({ - balance: traderDeposit || 0n, - decimals: activeToken.decimals, - includeCommas: true, - places: activeToken.places, - })}{" "} - {activeToken.symbol} + {openShortPreviewStatus === "loading" ? ( + // make this an inline skeleton + + + + ) : ( + formatBalance({ + balance: traderDeposit || 0n, + decimals: activeToken.decimals, + includeCommas: true, + places: activeToken.places, + }) + )}{" "} {" "} in exchange for the yield on{" "} - {formatBalance({ - balance: amountOfBondsToShortAsBigInt, - decimals: activeToken.decimals, - includeCommas: true, - places: activeToken.places, - })}{" "} + {openShortPreviewStatus === "loading" ? ( + + + + ) : ( + formatBalance({ + balance: amountOfBondsToShortAsBigInt, + decimals: activeToken.decimals, + includeCommas: true, + places: activeToken.places, + }) + )}{" "} {baseToken.symbol} {" "} deposited into{" "} @@ -285,18 +312,6 @@ export function OpenShortForm({

); } - if (!!amountOfBondsToShortAsBigInt && !hasEnoughLiquidity) { - return ( -

- Pool limit exceeded. Max short size is{" "} - {formatBalance({ - balance: maxBondsOut || 0n, - decimals: hyperdrive.decimals, - })}{" "} - hy{baseToken.symbol} -

- ); - } })()} actionButton={(() => { if (!account) { diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/OpenShortPreview/OpenShortPreview.tsx b/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/OpenShortPreview/OpenShortPreview.tsx index da46e593c..a450bd9db 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/OpenShortPreview/OpenShortPreview.tsx +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/OpenShortPreview/OpenShortPreview.tsx @@ -6,6 +6,7 @@ import { import classNames from "classnames"; import * as dnum from "dnum"; import { ReactElement } from "react"; +import Skeleton from "react-loading-skeleton"; import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays"; import { formatRate } from "src/base/formatRate"; import { useAppConfig } from "src/ui/appconfig/useAppConfig"; @@ -20,6 +21,7 @@ interface OpenShortPreviewProps { shortSize: bigint | undefined; spotRateAfterOpen: bigint | undefined; curveFee: bigint | undefined; + openShortPreviewStatus: "error" | "idle" | "loading" | "success"; } export function OpenShortPreview({ @@ -29,6 +31,7 @@ export function OpenShortPreview({ shortSize, spotRateAfterOpen, curveFee, + openShortPreviewStatus, }: OpenShortPreviewProps): ReactElement { const appConfig = useAppConfig(); const baseToken = findBaseToken({ @@ -36,7 +39,6 @@ export function OpenShortPreview({ tokens: appConfig.tokens, }); const { fixedAPR } = useCurrentFixedAPR(hyperdrive.address); - const termLengthMS = Number(hyperdrive.poolConfig.positionDuration * 1000n); return (
@@ -60,35 +62,43 @@ export function OpenShortPreview({ - {curveFee - ? `${formatBalance({ - balance: curveFee, - decimals: tokenIn.decimals, - places: tokenIn.places, - })} ${tokenIn.symbol}` - : `0 ${tokenIn.symbol}`} - + openShortPreviewStatus === "loading" && shortSize ? ( + + ) : ( + + {curveFee + ? `${formatBalance({ + balance: curveFee, + decimals: tokenIn.decimals, + places: tokenIn.places, + })} ${tokenIn.symbol}` + : `0 ${tokenIn.symbol}`} + + ) } /> - {costBasis - ? `${formatBalance({ - balance: costBasis, - decimals: tokenIn.decimals, - places: tokenIn.places, - })} ${tokenIn.symbol}` - : `0 ${tokenIn.symbol}`} - + openShortPreviewStatus === "loading" && shortSize ? ( + + ) : ( + + {costBasis + ? `${formatBalance({ + balance: costBasis, + decimals: tokenIn.decimals, + places: tokenIn.places, + })} ${tokenIn.symbol}` + : `0 ${tokenIn.symbol}`} + + ) } />
@@ -96,34 +106,44 @@ export function OpenShortPreview({ - {spotRateAfterOpen - ? `${formatRate(spotRateAfterOpen)}% APR` - : "-"} - + openShortPreviewStatus === "loading" && shortSize ? ( + + ) : ( + + {spotRateAfterOpen + ? `${formatRate(spotRateAfterOpen)}% APR` + : "-"} + + ) } /> - {getMarketImpactLabel(fixedAPR?.apr, spotRateAfterOpen)} - + openShortPreviewStatus === "loading" && shortSize ? ( + + ) : ( + + {getMarketImpactLabel(fixedAPR?.apr, spotRateAfterOpen)} + + ) } />
diff --git a/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/usePreviewOpenShort.ts b/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/usePreviewOpenShort.ts index 71dedfda3..9b5f95159 100644 --- a/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/usePreviewOpenShort.ts +++ b/apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/usePreviewOpenShort.ts @@ -2,6 +2,7 @@ import { MutationStatus, useQuery } from "@tanstack/react-query"; import { makeQueryKey } from "src/base/makeQueryKey"; import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive"; import { Address } from "viem"; +import { useBlockNumber } from "wagmi"; interface UsePreviewOpenShortOptions { hyperdriveAddress: Address; @@ -23,6 +24,8 @@ export function usePreviewOpenShort({ asBase, }: UsePreviewOpenShortOptions): UsePreviewOpenShortResult { const readHyperdrive = useReadHyperdrive(hyperdriveAddress); + + const { data: blockNumber } = useBlockNumber({ watch: true }); const queryEnabled = !!readHyperdrive && !!amountOfBondsToShort; const { data, status } = useQuery({ @@ -30,6 +33,7 @@ export function usePreviewOpenShort({ hyperdrive: hyperdriveAddress, amountBondShorts: amountOfBondsToShort?.toString(), asBase, + blockNumber: blockNumber?.toString(), }), enabled: queryEnabled, queryFn: queryEnabled