@@ -18,6 +18,7 @@ import {
18
18
DEFAULT_ALLOWANCE ,
19
19
ADDRESS_ZERO ,
20
20
} from '@/constants/index'
21
+ import useTokenBalance from '@/hooks/useTokenBalance'
21
22
22
23
import { FACTORY_ADDRESS } from '@sushiswap/sdk'
23
24
import {
@@ -29,6 +30,7 @@ import {
29
30
Option ,
30
31
createOptionEntityWithAddress ,
31
32
SUSHI_FACTORY_ADDRESS ,
33
+ getBalance ,
32
34
} from '@primitivefi/sdk'
33
35
import { parseEther , formatEther } from 'ethers/lib/utils'
34
36
import {
@@ -76,7 +78,7 @@ export const useUpdateItem = (): ((
76
78
lpPair ?: Pair | SushiSwapSDK . Pair ,
77
79
reset ?: boolean
78
80
) => void ) => {
79
- const { chainId } = useWeb3React ( )
81
+ const { library , account , chainId } = useWeb3React ( )
80
82
const dispatch = useDispatch < AppDispatch > ( )
81
83
const getAllowance = useGetTokenAllowance ( )
82
84
const clear = useClearSwap ( )
@@ -96,6 +98,7 @@ export const useUpdateItem = (): ((
96
98
approved : [ false , false ] ,
97
99
} )
98
100
)
101
+
99
102
let manage = false
100
103
switch ( orderType ) {
101
104
case Operation . MINT :
@@ -146,9 +149,16 @@ export const useUpdateItem = (): ((
146
149
item . entity . underlying . address ,
147
150
spender
148
151
)
152
+ const tokenBalance = await getBalance (
153
+ library ,
154
+ item . entity . underlying . address ,
155
+ account
156
+ )
157
+
149
158
const approved =
150
- parseEther ( tokenAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ||
151
- item . entity . underlying . address === WETH9 [ chainId ] . address
159
+ parseEther ( tokenAllowance . toString ( ) ) . gt (
160
+ parseEther ( tokenBalance . toString ( ) )
161
+ ) || item . entity . underlying . address === WETH9 [ chainId ] . address
152
162
dispatch (
153
163
updateItem ( {
154
164
item,
@@ -169,14 +179,29 @@ export const useUpdateItem = (): ((
169
179
spender
170
180
)
171
181
const lpAllowance = await getAllowance ( lpToken , spender )
182
+ const longBalance = await getBalance (
183
+ library ,
184
+ item . entity . address ,
185
+ account
186
+ )
187
+
188
+ const lpBalance = await getBalance (
189
+ library ,
190
+ item . market . liquidityToken . address ,
191
+ account
192
+ )
172
193
dispatch (
173
194
updateItem ( {
174
195
item,
175
196
orderType,
176
197
loading : false ,
177
198
approved : [
178
- parseEther ( optionAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ,
179
- parseEther ( lpAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ,
199
+ parseEther ( optionAllowance . toString ( ) ) . gt (
200
+ parseEther ( longBalance . toString ( ) )
201
+ ) ,
202
+ parseEther ( lpAllowance . toString ( ) ) . gt (
203
+ parseEther ( lpBalance . toString ( ) )
204
+ ) ,
180
205
] ,
181
206
} )
182
207
)
@@ -194,14 +219,29 @@ export const useUpdateItem = (): ((
194
219
spender
195
220
)
196
221
const lpAllowance = await getAllowance ( lpToken , spender )
222
+ const longBalance = await getBalance (
223
+ library ,
224
+ item . entity . address ,
225
+ account
226
+ )
227
+
228
+ const lpBalance = await getBalance (
229
+ library ,
230
+ item . market . liquidityToken . address ,
231
+ account
232
+ )
197
233
dispatch (
198
234
updateItem ( {
199
235
item,
200
236
orderType,
201
237
loading : false ,
202
238
approved : [
203
- parseEther ( optionAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ,
204
- parseEther ( lpAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ,
239
+ parseEther ( optionAllowance . toString ( ) ) . gt (
240
+ parseEther ( longBalance . toString ( ) )
241
+ ) ,
242
+ parseEther ( lpAllowance . toString ( ) ) . gt (
243
+ parseEther ( lpBalance . toString ( ) )
244
+ ) ,
205
245
] ,
206
246
} )
207
247
)
@@ -234,14 +274,28 @@ export const useUpdateItem = (): ((
234
274
if ( secondaryAddress ) {
235
275
secondaryAllowance = await getAllowance ( secondaryAddress , spender )
236
276
}
277
+ const secondaryBal = await getBalance (
278
+ library ,
279
+ secondaryAddress ,
280
+ account
281
+ )
282
+ const tokenBalance = await getBalance (
283
+ library ,
284
+ item . entity . underlying . address ,
285
+ account
286
+ )
237
287
dispatch (
238
288
updateItem ( {
239
289
item,
240
290
orderType,
241
291
loading : false ,
242
292
approved : [
243
- parseEther ( tokenAllowance ) . gt ( parseEther ( '0' ) ) ,
244
- parseEther ( secondaryAllowance ) . gt ( parseEther ( '0' ) ) ,
293
+ parseEther ( tokenAllowance ) . gt (
294
+ parseEther ( tokenBalance . toString ( ) )
295
+ ) ,
296
+ parseEther ( secondaryAllowance ) . gt (
297
+ parseEther ( secondaryBal . toString ( ) )
298
+ ) ,
245
299
] ,
246
300
} )
247
301
)
@@ -283,14 +337,26 @@ export const useUpdateItem = (): ((
283
337
} else {
284
338
secondaryAllowance = '0'
285
339
}
340
+
341
+ const primaryBal = await getBalance ( library , tokenAddress , account )
342
+ const secondaryBal = await getBalance (
343
+ library ,
344
+ secondaryAddress ,
345
+ account
346
+ )
347
+
286
348
dispatch (
287
349
updateItem ( {
288
350
item,
289
351
orderType,
290
352
loading : false ,
291
353
approved : [
292
- parseEther ( tokenAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ,
293
- parseEther ( secondaryAllowance . toString ( ) ) . gt ( parseEther ( '0' ) ) ,
354
+ parseEther ( tokenAllowance . toString ( ) ) . gt (
355
+ parseEther ( primaryBal . toString ( ) )
356
+ ) ,
357
+ parseEther ( secondaryAllowance . toString ( ) ) . gt (
358
+ parseEther ( secondaryBal . toString ( ) )
359
+ ) ,
294
360
] ,
295
361
} )
296
362
)
0 commit comments