Skip to content

Commit 4a5382c

Browse files
Darya KavianiDarya Kaviani
authored andcommitted
close position hook
1 parent 011a88b commit 4a5382c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

packages/frontend/src/state/lp/hooks.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../contro
1616
/*** CONSTANTS ***/
1717
const TICK_SPACE = 60
1818
const COLLAT_RATIO = 1.5
19+
const POOL_FEE = 3000
1920

2021
/*** ACTIONS ***/
2122

@@ -68,6 +69,61 @@ export const useOpenPositionDeposit = () => {
6869
return openPositionDeposit
6970
}
7071

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+
71127
/*** GETTERS ***/
72128

73129
export const useGetPosition = () => {

0 commit comments

Comments
 (0)