Skip to content

Commit c1123db

Browse files
committed
feat(Apporvals): check current allowance against balance
1 parent 6263c16 commit c1123db

File tree

1 file changed

+77
-11
lines changed

1 file changed

+77
-11
lines changed

src/state/order/hooks.tsx

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
DEFAULT_ALLOWANCE,
1919
ADDRESS_ZERO,
2020
} from '@/constants/index'
21+
import useTokenBalance from '@/hooks/useTokenBalance'
2122

2223
import { FACTORY_ADDRESS } from '@sushiswap/sdk'
2324
import {
@@ -29,6 +30,7 @@ import {
2930
Option,
3031
createOptionEntityWithAddress,
3132
SUSHI_FACTORY_ADDRESS,
33+
getBalance,
3234
} from '@primitivefi/sdk'
3335
import { parseEther, formatEther } from 'ethers/lib/utils'
3436
import {
@@ -76,7 +78,7 @@ export const useUpdateItem = (): ((
7678
lpPair?: Pair | SushiSwapSDK.Pair,
7779
reset?: boolean
7880
) => void) => {
79-
const { chainId } = useWeb3React()
81+
const { library, account, chainId } = useWeb3React()
8082
const dispatch = useDispatch<AppDispatch>()
8183
const getAllowance = useGetTokenAllowance()
8284
const clear = useClearSwap()
@@ -96,6 +98,7 @@ export const useUpdateItem = (): ((
9698
approved: [false, false],
9799
})
98100
)
101+
99102
let manage = false
100103
switch (orderType) {
101104
case Operation.MINT:
@@ -146,9 +149,16 @@ export const useUpdateItem = (): ((
146149
item.entity.underlying.address,
147150
spender
148151
)
152+
const tokenBalance = await getBalance(
153+
library,
154+
item.entity.underlying.address,
155+
account
156+
)
157+
149158
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
152162
dispatch(
153163
updateItem({
154164
item,
@@ -169,14 +179,29 @@ export const useUpdateItem = (): ((
169179
spender
170180
)
171181
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+
)
172193
dispatch(
173194
updateItem({
174195
item,
175196
orderType,
176197
loading: false,
177198
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+
),
180205
],
181206
})
182207
)
@@ -194,14 +219,29 @@ export const useUpdateItem = (): ((
194219
spender
195220
)
196221
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+
)
197233
dispatch(
198234
updateItem({
199235
item,
200236
orderType,
201237
loading: false,
202238
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+
),
205245
],
206246
})
207247
)
@@ -234,14 +274,28 @@ export const useUpdateItem = (): ((
234274
if (secondaryAddress) {
235275
secondaryAllowance = await getAllowance(secondaryAddress, spender)
236276
}
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+
)
237287
dispatch(
238288
updateItem({
239289
item,
240290
orderType,
241291
loading: false,
242292
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+
),
245299
],
246300
})
247301
)
@@ -283,14 +337,26 @@ export const useUpdateItem = (): ((
283337
} else {
284338
secondaryAllowance = '0'
285339
}
340+
341+
const primaryBal = await getBalance(library, tokenAddress, account)
342+
const secondaryBal = await getBalance(
343+
library,
344+
secondaryAddress,
345+
account
346+
)
347+
286348
dispatch(
287349
updateItem({
288350
item,
289351
orderType,
290352
loading: false,
291353
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+
),
294360
],
295361
})
296362
)

0 commit comments

Comments
 (0)