Skip to content

Commit

Permalink
refactor: pass approval amount from info (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Dec 5, 2022
1 parent 0a19ebc commit 221ae44
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/hooks/swap/useSwapApproval.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Percent } from '@uniswap/sdk-core'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core'
import { SWAP_ROUTER_ADDRESSES } from 'constants/addresses'
import { ErrorCode } from 'constants/eip1193'
Expand All @@ -7,7 +7,6 @@ import { PermitState, SignatureData, usePermit } from 'hooks/usePermit'
import useTransactionDeadline from 'hooks/useTransactionDeadline'
import { useAtomValue } from 'jotai/utils'
import { useMemo } from 'react'
import { InterfaceTrade } from 'state/routing/types'
import { swapEventHandlersAtom } from 'state/swap'

import { ApprovalState, useApproval } from '../useApproval'
Expand All @@ -31,8 +30,7 @@ export interface SwapApproval {
* Returns all relevant statuses and callback functions for approvals.
* Considers both standard approval and ERC20 permit.
*/
export function useSwapApproval(trade: InterfaceTrade | undefined, allowedSlippage: Percent): SwapApproval {
const amount = useMemo(() => trade?.maximumAmountIn(allowedSlippage), [allowedSlippage, trade])
export function useSwapApproval(amount?: CurrencyAmount<Currency>): SwapApproval {
const { chainId } = useWeb3React()
const deadline = useTransactionDeadline()
const spender = chainId ? SWAP_ROUTER_ADDRESSES[chainId] : undefined
Expand Down
7 changes: 6 additions & 1 deletion src/hooks/swap/useSwapInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ function useComputeSwapInfo(routerUrl?: string): SwapInfo {
// Compute slippage and impact off of the trade so that it refreshes with the trade.
// Wait until the trade is valid to avoid displaying incorrect intermediate values.
const slippage = useSlippage(trade)
const approval = useSwapApproval(trade.trade, slippage.allowed)
const impact = usePriceImpact(trade.trade)

const maximumAmountIn = useMemo(() => {
const maximumAmountIn = trade.trade?.maximumAmountIn(slippage.allowed)
return maximumAmountIn?.currency.isToken ? (maximumAmountIn as CurrencyAmount<Token>) : undefined
}, [slippage.allowed, trade.trade])
const approval = useSwapApproval(maximumAmountIn)

return useMemo(() => {
return {
[Field.INPUT]: {
Expand Down

0 comments on commit 221ae44

Please sign in to comment.