@@ -16,6 +16,7 @@ import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../contro
16
16
/*** CONSTANTS ***/
17
17
const TICK_SPACE = 60
18
18
const COLLAT_RATIO = 1.5
19
+ const POOL_FEE = 3000
19
20
20
21
/*** ACTIONS ***/
21
22
@@ -68,6 +69,61 @@ export const useOpenPositionDeposit = () => {
68
69
return openPositionDeposit
69
70
}
70
71
72
+ // Close position with flashloan
73
+ export const useClosePosition = ( ) => {
74
+ const address = useAtomValue ( addressAtom )
75
+ const controllerHelperContract = useAtomValue ( controllerHelperHelperContractAtom )
76
+ const controllerContract = useAtomValue ( controllerContractAtom )
77
+ const handleTransaction = useHandleTransaction ( )
78
+ const getDebtAmount = useGetDebtAmount ( )
79
+ const getVault = useGetVault ( )
80
+ const getPosition = useGetPosition ( )
81
+ const getQuote = useGetQuote ( )
82
+ const closePosition = useAppCallback ( async ( vaultId : number , onTxConfirmed ?: ( ) => void ) => {
83
+ const vaultBefore = await getVault ( vaultId )
84
+ const uniTokenId = vaultBefore ?. NFTCollateralId
85
+ const position = await getPosition ( uniTokenId )
86
+
87
+ if (
88
+ ! controllerContract ||
89
+ ! controllerHelperContract ||
90
+ ! address ||
91
+ ! position ||
92
+ ! vaultBefore ||
93
+ ! vaultBefore . shortAmount
94
+ )
95
+ return
96
+
97
+ const shortAmount = fromTokenAmount ( vaultBefore . shortAmount , OSQUEETH_DECIMALS )
98
+ const debtInEth = await getDebtAmount ( shortAmount )
99
+ const collateralToFlashloan = debtInEth . multipliedBy ( COLLAT_RATIO )
100
+ const limitEth = await getQuote ( shortAmount , true )
101
+
102
+ const flashloanCloseVaultLpNftParam = {
103
+ vaultId : vaultId ,
104
+ tokenId : uniTokenId ,
105
+ liquidity : position . liquidity ,
106
+ liquidityPercentage : fromTokenAmount ( 1 , 18 ) . toFixed ( 0 ) ,
107
+ wPowerPerpAmountToBurn : shortAmount . toFixed ( 0 ) ,
108
+ collateralToFlashloan : collateralToFlashloan . toFixed ( 0 ) ,
109
+ collateralToWithdraw : 0 ,
110
+ limitPriceEthPerPowerPerp : limitEth ,
111
+ amount0Min : 0 ,
112
+ amount1Min : 0 ,
113
+ poolFee : POOL_FEE ,
114
+ burnExactRemoved : true ,
115
+ }
116
+
117
+ return handleTransaction (
118
+ await controllerHelperContract . methods . flashloanCloseVaultLpNft ( flashloanCloseVaultLpNftParam ) . send ( {
119
+ from : address ,
120
+ } ) ,
121
+ onTxConfirmed ,
122
+ )
123
+ } , [ address , controllerHelperContract , controllerContract , handleTransaction , getDebtAmount , getVault , getPosition , getQuote ] )
124
+ return closePosition
125
+ }
126
+
71
127
/*** GETTERS ***/
72
128
73
129
export const useGetPosition = ( ) => {
0 commit comments