uniswap-interface-uncensored/src/hooks/useApproveCallback.ts
Vignesh Mohankumar 585d67c44a
refactor: always enable permit2 (#6068)
* refactor: always enable permit2

* rm from modal

* one more

* rm

* more

* more

* more

* rm useswapcallback

* rm useapprovecallback

* unused hooks

* unused hooks

* rm

* lint
2023-03-06 18:07:19 -05:00

29 lines
1.2 KiB
TypeScript

import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import { ApprovalState, useApproval } from 'lib/hooks/useApproval'
import { useCallback } from 'react'
import { useHasPendingApproval, useTransactionAdder } from '../state/transactions/hooks'
import { TransactionType } from '../state/transactions/types'
export { ApprovalState } from 'lib/hooks/useApproval'
function useGetAndTrackApproval(getApproval: ReturnType<typeof useApproval>[1]) {
const addTransaction = useTransactionAdder()
return useCallback(() => {
return getApproval().then((pending) => {
if (pending) {
const { response, tokenAddress, spenderAddress: spender } = pending
addTransaction(response, { type: TransactionType.APPROVAL, tokenAddress, spender })
}
})
}, [addTransaction, getApproval])
}
// returns a variable indicating the state of the approval and a function which approves if necessary or early returns
export function useApproveCallback(
amountToApprove?: CurrencyAmount<Currency>,
spender?: string
): [ApprovalState, () => Promise<void>] {
const [approval, getApproval] = useApproval(amountToApprove, spender, useHasPendingApproval)
return [approval, useGetAndTrackApproval(getApproval)]
}