Skip to content

Commit b47e9f8

Browse files
committed
refactor: use getDepositAmounts directly
1 parent ceef774 commit b47e9f8

File tree

1 file changed

+44
-68
lines changed

1 file changed

+44
-68
lines changed

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

+44-68
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { addressesAtom, isWethToken0Atom } from '../positions/atoms'
55
import BigNumber from 'bignumber.js'
66
import { INDEX_SCALE, OSQUEETH_DECIMALS, WETH_DECIMALS } from '@constants/index'
77
import {
8-
controllerContractAtom,
98
controllerHelperHelperContractAtom,
109
nftManagerContractAtom,
1110
quoterContractAtom,
@@ -17,7 +16,7 @@ import { addressAtom } from '../wallet/atoms'
1716
import { Contract } from 'web3-eth-contract'
1817
import { useHandleTransaction } from '../wallet/hooks'
1918
import { useCallback } from 'react'
20-
import { useGetDebtAmount, useGetVault } from '../controller/hooks'
19+
import { useGetVault } from '../controller/hooks'
2120
import { indexAtom, normFactorAtom } from '../controller/atoms'
2221

2322
/*** CONSTANTS ***/
@@ -46,16 +45,19 @@ export const useGetDepositAmounts = () => {
4645
withdrawAmount: number,
4746
) => {
4847
const vaultBefore = await getVault(vaultId)
49-
50-
if (!squeethPoolContract || !vaultBefore || !vaultBefore.shortAmount || !vaultBefore.collateralAmount) return null
48+
if (!squeethPoolContract || !vaultBefore || !vaultBefore.shortAmount || !vaultBefore.collateralAmount) {
49+
return null
50+
}
5151

5252
const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS)
5353
const { tick, tickSpacing } = await getPoolState(squeethPoolContract)
5454
const lowerTick = nearestUsableTick(lowerTickInput, Number(tickSpacing))
5555
const upperTick = nearestUsableTick(upperTickInput, Number(tickSpacing))
5656

5757
const collateralToLp = await getCollateralToLP(mintWSqueethAmount, lowerTick, upperTick, tick)
58-
if (!collateralToLp) return
58+
if (!collateralToLp) {
59+
return null
60+
}
5961

6062
const collateralToWithdraw = fromTokenAmount(withdrawAmount, OSQUEETH_DECIMALS)
6163
const ethIndexPrice = toTokenAmount(index, 18).sqrt()
@@ -66,14 +68,21 @@ export const useGetDepositAmounts = () => {
6668
const collateralToMint = new BigNumber(collatRatio)
6769
.times(vaultShortAmt.plus(mintWSqueethAmount).times(normFactor).times(ethIndexPrice).div(INDEX_SCALE))
6870
.minus(vaultCollateralAmt.minus(collateralToWithdraw).plus(collateralToLp).plus(oSQTHInETH))
71+
const flashLoanAmount = new BigNumber(COLLAT_RATIO_FLASHLOAN + FLASHLOAN_BUFFER)
72+
.times(vaultShortAmt.plus(mintWSqueethAmount))
73+
.times(normFactor)
74+
.times(ethIndexPrice)
75+
.div(INDEX_SCALE)
76+
.minus(vaultCollateralAmt)
6977

7078
const collateralToMintPos = BigNumber.max(collateralToMint, 0)
71-
const total = collateralToLp.plus(collateralToMintPos).minus(collateralToWithdraw)
79+
const flashLoanAmountPos = BigNumber.max(flashLoanAmount, 0)
7280

7381
return {
74-
vault: collateralToMintPos.toFixed(0),
75-
lp: collateralToLp.toFixed(0),
76-
total: total.toFixed(0),
82+
mintAmount: collateralToMintPos,
83+
lpAmount: collateralToLp,
84+
flashloanAmount: flashLoanAmountPos,
85+
totalAmount: collateralToLp.plus(collateralToMintPos).minus(collateralToWithdraw),
7786
}
7887
},
7988
[index, normFactor, squeethPoolContract, getCollateralToLP, getVault],
@@ -88,13 +97,9 @@ export const useOpenPositionDeposit = () => {
8897
const address = useAtomValue(addressAtom)
8998
const contract = useAtomValue(controllerHelperHelperContractAtom)
9099
const handleTransaction = useHandleTransaction()
91-
const getDebtAmount = useGetDebtAmount()
92100
const squeethPoolContract = useAtomValue(squeethPoolContractAtom)
93101
const isWethToken0 = useAtomValue(isWethToken0Atom)
94-
const index = useAtomValue(indexAtom)
95-
const normFactor = useAtomValue(normFactorAtom)
96-
const getVault = useGetVault()
97-
const getCollateralToLP = useGetCollateralToLP()
102+
const getDepositAmounts = useGetDepositAmounts()
98103

99104
const openPositionDeposit = useAppCallback(
100105
async (
@@ -107,83 +112,54 @@ export const useOpenPositionDeposit = () => {
107112
withdrawAmount: number,
108113
onTxConfirmed?: () => void,
109114
) => {
110-
const vaultBefore = await getVault(vaultId)
111-
if (
112-
!contract ||
113-
!address ||
114-
!squeethPoolContract ||
115-
!vaultBefore ||
116-
!vaultBefore.shortAmount ||
117-
!vaultBefore.collateralAmount
115+
if (!squeethPoolContract || !contract || !address) return null
116+
117+
const deposits = await getDepositAmounts(
118+
squeethToMint,
119+
lowerTickInput,
120+
upperTickInput,
121+
vaultId,
122+
collatRatio,
123+
withdrawAmount,
118124
)
119-
return null
125+
if (!deposits) return null
120126

121-
const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS)
122-
const { tick, tickSpacing } = await getPoolState(squeethPoolContract)
123-
const lowerTick = nearestUsableTick(lowerTickInput, Number(tickSpacing))
124-
const upperTick = nearestUsableTick(upperTickInput, Number(tickSpacing))
127+
const { lpAmount, mintAmount, flashloanAmount } = deposits
125128

126-
const collateralToLp = await getCollateralToLP(mintWSqueethAmount, lowerTick, upperTick, tick)
127-
if (!collateralToLp) return
128-
129-
const amount0New = isWethToken0 ? collateralToLp : mintWSqueethAmount
130-
const amount1New = isWethToken0 ? mintWSqueethAmount : collateralToLp
129+
const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS)
130+
const amount0New = isWethToken0 ? lpAmount : mintWSqueethAmount
131+
const amount1New = isWethToken0 ? mintWSqueethAmount : lpAmount
131132
const amount0Min = amount0New.times(new BigNumber(1).minus(slippage)).toFixed(0)
132133
const amount1Min = amount1New.times(new BigNumber(1).minus(slippage)).toFixed(0)
133134

134-
const collateralToWithdraw = fromTokenAmount(withdrawAmount, OSQUEETH_DECIMALS)
135-
const ethIndexPrice = toTokenAmount(index, 18).sqrt()
136-
const vaultShortAmt = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS)
137-
const vaultCollateralAmt = fromTokenAmount(vaultBefore.collateralAmount, WETH_DECIMALS)
138-
139-
// Calculate collateralToMint
140-
const oSQTHInETH = mintWSqueethAmount.times(ethIndexPrice.div(INDEX_SCALE)).times(normFactor)
141-
const collateralToMint = new BigNumber(collatRatio)
142-
.times(vaultShortAmt.plus(mintWSqueethAmount).times(normFactor).times(ethIndexPrice).div(INDEX_SCALE))
143-
.minus(vaultCollateralAmt.minus(collateralToWithdraw).plus(collateralToLp).plus(oSQTHInETH))
144-
const flashLoanAmount = new BigNumber(COLLAT_RATIO_FLASHLOAN + FLASHLOAN_BUFFER)
145-
.times(vaultShortAmt.plus(mintWSqueethAmount))
146-
.times(normFactor)
147-
.times(ethIndexPrice)
148-
.div(INDEX_SCALE)
149-
.minus(vaultCollateralAmt)
150-
const collateralToMintPos = BigNumber.max(collateralToMint, 0)
151-
const flashLoanAmountPos = BigNumber.max(flashLoanAmount, 0)
135+
const { tickSpacing } = await getPoolState(squeethPoolContract)
136+
const lowerTick = nearestUsableTick(lowerTickInput, Number(tickSpacing))
137+
const upperTick = nearestUsableTick(upperTickInput, Number(tickSpacing))
152138

153139
const flashloanWMintDepositNftParams = {
154140
wPowerPerpPool: squeethPool,
155141
vaultId: vaultId,
156142
wPowerPerpAmount: mintWSqueethAmount.toFixed(0),
157-
collateralToDeposit: collateralToMintPos.plus(flashLoanAmountPos).toFixed(0),
158-
collateralToFlashloan: flashLoanAmountPos.toFixed(0),
159-
collateralToLp: collateralToLp.toFixed(0),
160-
collateralToWithdraw: collateralToWithdraw.toFixed(0),
143+
collateralToDeposit: mintAmount.plus(flashloanAmount).toFixed(0),
144+
collateralToFlashloan: flashloanAmount.toFixed(0),
145+
collateralToLp: lpAmount.toFixed(0),
146+
collateralToWithdraw: withdrawAmount.toFixed(0),
161147
amount0Min,
162148
amount1Min,
163149
lowerTick: lowerTick,
164150
upperTick: upperTick,
165151
}
166152

167-
return handleTransaction(
153+
const txHash = handleTransaction(
168154
contract.methods.flashloanWMintLpDepositNft(flashloanWMintDepositNftParams).send({
169155
from: address,
170-
value: collateralToLp.plus(collateralToMintPos).minus(collateralToWithdraw).toFixed(0),
156+
value: lpAmount.plus(mintAmount).minus(withdrawAmount).toFixed(0),
171157
}),
172158
onTxConfirmed,
173159
)
160+
return txHash
174161
},
175-
[
176-
address,
177-
squeethPool,
178-
contract,
179-
handleTransaction,
180-
getDebtAmount,
181-
squeethPoolContract,
182-
isWethToken0,
183-
index,
184-
normFactor,
185-
getVault,
186-
],
162+
[address, squeethPool, contract, handleTransaction, squeethPoolContract, isWethToken0, getDepositAmounts],
187163
)
188164

189165
return openPositionDeposit

0 commit comments

Comments
 (0)