From 221ae44df4a73bad2e81c3adb3d53317c41bf91e Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Mon, 5 Dec 2022 11:41:32 -0800 Subject: [PATCH] refactor: pass approval amount from info (#319) --- src/hooks/swap/useSwapApproval.ts | 6 ++---- src/hooks/swap/useSwapInfo.tsx | 7 ++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/hooks/swap/useSwapApproval.ts b/src/hooks/swap/useSwapApproval.ts index 41c68569e..571dd0482 100644 --- a/src/hooks/swap/useSwapApproval.ts +++ b/src/hooks/swap/useSwapApproval.ts @@ -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' @@ -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' @@ -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): SwapApproval { const { chainId } = useWeb3React() const deadline = useTransactionDeadline() const spender = chainId ? SWAP_ROUTER_ADDRESSES[chainId] : undefined diff --git a/src/hooks/swap/useSwapInfo.tsx b/src/hooks/swap/useSwapInfo.tsx index 1361edd0f..ff0ff2f89 100644 --- a/src/hooks/swap/useSwapInfo.tsx +++ b/src/hooks/swap/useSwapInfo.tsx @@ -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) : undefined + }, [slippage.allowed, trade.trade]) + const approval = useSwapApproval(maximumAmountIn) + return useMemo(() => { return { [Field.INPUT]: {