1
1
import { useQuery } from "@tanstack/react-query" ;
2
2
import { makeQueryKey } from "src/base/makeQueryKey" ;
3
3
import { QueryStatusWithIdle , getStatus } from "src/base/queryStatus" ;
4
+ import { useAppConfig } from "src/ui/appconfig/useAppConfig" ;
5
+ import { prepareSharesOut } from "src/ui/hyperdrive/hooks/usePrepareSharesOut" ;
4
6
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive" ;
5
7
import { Address } from "viem" ;
6
8
@@ -28,6 +30,7 @@ export function usePreviewCloseShort({
28
30
enabled = true ,
29
31
} : UsePreviewCloseShortOptions ) : UsePreviewCloseShortResult {
30
32
const readHyperdrive = useReadHyperdrive ( hyperdriveAddress ) ;
33
+ const appConfig = useAppConfig ( ) ;
31
34
const queryEnabled =
32
35
! ! readHyperdrive && ! ! maturityTime && ! ! shortAmountIn && enabled ;
33
36
@@ -41,12 +44,49 @@ export function usePreviewCloseShort({
41
44
42
45
enabled : queryEnabled ,
43
46
queryFn : queryEnabled
44
- ? async ( ) =>
45
- readHyperdrive . previewCloseShort ( {
47
+ ? async ( ) => {
48
+ const result = await readHyperdrive . previewCloseShort ( {
46
49
maturityTime,
47
50
shortAmountIn,
48
51
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
+ }
50
90
: undefined ,
51
91
} ) ;
52
92
0 commit comments