Skip to content

Commit d98081a

Browse files
committed
Update usePreviewCloseShort to prepare shares out
1 parent c4f5671 commit d98081a

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

apps/hyperdrive-trading/src/ui/hyperdrive/shorts/hooks/usePreviewCloseShort.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useQuery } from "@tanstack/react-query";
22
import { makeQueryKey } from "src/base/makeQueryKey";
33
import { QueryStatusWithIdle, getStatus } from "src/base/queryStatus";
4+
import { useAppConfig } from "src/ui/appconfig/useAppConfig";
5+
import { prepareSharesOut } from "src/ui/hyperdrive/hooks/usePrepareSharesOut";
46
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
57
import { Address } from "viem";
68

@@ -28,6 +30,7 @@ export function usePreviewCloseShort({
2830
enabled = true,
2931
}: UsePreviewCloseShortOptions): UsePreviewCloseShortResult {
3032
const readHyperdrive = useReadHyperdrive(hyperdriveAddress);
33+
const appConfig = useAppConfig();
3134
const queryEnabled =
3235
!!readHyperdrive && !!maturityTime && !!shortAmountIn && enabled;
3336

@@ -41,12 +44,49 @@ export function usePreviewCloseShort({
4144

4245
enabled: queryEnabled,
4346
queryFn: queryEnabled
44-
? async () =>
45-
readHyperdrive.previewCloseShort({
47+
? async () => {
48+
const result = await readHyperdrive.previewCloseShort({
4649
maturityTime,
4750
shortAmountIn,
4851
asBase,
49-
})
52+
});
53+
54+
// All shares from the sdk need to be prepared for the UI
55+
const [finalAmountOut, finalMarketEstimate, finalFlatPlusCurveFee] =
56+
asBase
57+
? [
58+
result.amountOut,
59+
result.marketEstimate,
60+
result.flatPlusCurveFee,
61+
]
62+
: await Promise.all([
63+
prepareSharesOut({
64+
appConfig,
65+
hyperdriveAddress,
66+
readHyperdrive,
67+
sharesAmount: result.amountOut,
68+
}),
69+
prepareSharesOut({
70+
appConfig,
71+
hyperdriveAddress,
72+
readHyperdrive,
73+
sharesAmount: result.marketEstimate,
74+
}),
75+
prepareSharesOut({
76+
appConfig,
77+
hyperdriveAddress,
78+
readHyperdrive,
79+
sharesAmount: result.flatPlusCurveFee,
80+
}),
81+
]);
82+
83+
return {
84+
...result,
85+
amountOut: finalAmountOut,
86+
marketEstimate: finalMarketEstimate,
87+
flatPlusCurveFee: finalFlatPlusCurveFee,
88+
};
89+
}
5090
: undefined,
5191
});
5292

0 commit comments

Comments
 (0)