@@ -5,7 +5,6 @@ import { addressesAtom, isWethToken0Atom } from '../positions/atoms'
5
5
import BigNumber from 'bignumber.js'
6
6
import { INDEX_SCALE , OSQUEETH_DECIMALS , WETH_DECIMALS } from '@constants/index'
7
7
import {
8
- controllerContractAtom ,
9
8
controllerHelperHelperContractAtom ,
10
9
nftManagerContractAtom ,
11
10
quoterContractAtom ,
@@ -17,7 +16,7 @@ import { addressAtom } from '../wallet/atoms'
17
16
import { Contract } from 'web3-eth-contract'
18
17
import { useHandleTransaction } from '../wallet/hooks'
19
18
import { useCallback } from 'react'
20
- import { useGetDebtAmount , useGetVault } from '../controller/hooks'
19
+ import { useGetVault } from '../controller/hooks'
21
20
import { indexAtom , normFactorAtom } from '../controller/atoms'
22
21
23
22
/*** CONSTANTS ***/
@@ -46,16 +45,19 @@ export const useGetDepositAmounts = () => {
46
45
withdrawAmount : number ,
47
46
) => {
48
47
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
+ }
51
51
52
52
const mintWSqueethAmount = fromTokenAmount ( squeethToMint , OSQUEETH_DECIMALS )
53
53
const { tick, tickSpacing } = await getPoolState ( squeethPoolContract )
54
54
const lowerTick = nearestUsableTick ( lowerTickInput , Number ( tickSpacing ) )
55
55
const upperTick = nearestUsableTick ( upperTickInput , Number ( tickSpacing ) )
56
56
57
57
const collateralToLp = await getCollateralToLP ( mintWSqueethAmount , lowerTick , upperTick , tick )
58
- if ( ! collateralToLp ) return
58
+ if ( ! collateralToLp ) {
59
+ return null
60
+ }
59
61
60
62
const collateralToWithdraw = fromTokenAmount ( withdrawAmount , OSQUEETH_DECIMALS )
61
63
const ethIndexPrice = toTokenAmount ( index , 18 ) . sqrt ( )
@@ -66,14 +68,21 @@ export const useGetDepositAmounts = () => {
66
68
const collateralToMint = new BigNumber ( collatRatio )
67
69
. times ( vaultShortAmt . plus ( mintWSqueethAmount ) . times ( normFactor ) . times ( ethIndexPrice ) . div ( INDEX_SCALE ) )
68
70
. 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 )
69
77
70
78
const collateralToMintPos = BigNumber . max ( collateralToMint , 0 )
71
- const total = collateralToLp . plus ( collateralToMintPos ) . minus ( collateralToWithdraw )
79
+ const flashLoanAmountPos = BigNumber . max ( flashLoanAmount , 0 )
72
80
73
81
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 ) ,
77
86
}
78
87
} ,
79
88
[ index , normFactor , squeethPoolContract , getCollateralToLP , getVault ] ,
@@ -88,13 +97,9 @@ export const useOpenPositionDeposit = () => {
88
97
const address = useAtomValue ( addressAtom )
89
98
const contract = useAtomValue ( controllerHelperHelperContractAtom )
90
99
const handleTransaction = useHandleTransaction ( )
91
- const getDebtAmount = useGetDebtAmount ( )
92
100
const squeethPoolContract = useAtomValue ( squeethPoolContractAtom )
93
101
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 ( )
98
103
99
104
const openPositionDeposit = useAppCallback (
100
105
async (
@@ -107,83 +112,54 @@ export const useOpenPositionDeposit = () => {
107
112
withdrawAmount : number ,
108
113
onTxConfirmed ?: ( ) => void ,
109
114
) => {
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 ,
118
124
)
119
- return null
125
+ if ( ! deposits ) return null
120
126
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
125
128
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
131
132
const amount0Min = amount0New . times ( new BigNumber ( 1 ) . minus ( slippage ) ) . toFixed ( 0 )
132
133
const amount1Min = amount1New . times ( new BigNumber ( 1 ) . minus ( slippage ) ) . toFixed ( 0 )
133
134
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 ) )
152
138
153
139
const flashloanWMintDepositNftParams = {
154
140
wPowerPerpPool : squeethPool ,
155
141
vaultId : vaultId ,
156
142
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 ) ,
161
147
amount0Min,
162
148
amount1Min,
163
149
lowerTick : lowerTick ,
164
150
upperTick : upperTick ,
165
151
}
166
152
167
- return handleTransaction (
153
+ const txHash = handleTransaction (
168
154
contract . methods . flashloanWMintLpDepositNft ( flashloanWMintDepositNftParams ) . send ( {
169
155
from : address ,
170
- value : collateralToLp . plus ( collateralToMintPos ) . minus ( collateralToWithdraw ) . toFixed ( 0 ) ,
156
+ value : lpAmount . plus ( mintAmount ) . minus ( withdrawAmount ) . toFixed ( 0 ) ,
171
157
} ) ,
172
158
onTxConfirmed ,
173
159
)
160
+ return txHash
174
161
} ,
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 ] ,
187
163
)
188
164
189
165
return openPositionDeposit
0 commit comments