From 467e80a42f610bc01d8dbe0e993b0591ba031d7a Mon Sep 17 00:00:00 2001 From: Moody Salem Date: Wed, 26 Aug 2020 09:19:59 -0500 Subject: [PATCH] improvement(#1043): do not allow swapping to bad addresses --- src/hooks/useENSAddress.ts | 7 ++++++- src/state/swap/hooks.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/hooks/useENSAddress.ts b/src/hooks/useENSAddress.ts index 08b4b41a98..37bab9e628 100644 --- a/src/hooks/useENSAddress.ts +++ b/src/hooks/useENSAddress.ts @@ -1,6 +1,7 @@ import { namehash } from 'ethers/lib/utils' import { useMemo } from 'react' import { useSingleCallResult } from '../state/multicall/hooks' +import isZero from '../utils/isZero' import { useENSRegistrarContract, useENSResolverContract } from './useContract' import useDebounce from './useDebounce' @@ -19,7 +20,11 @@ export default function useENSAddress(ensName?: string | null): { loading: boole }, [debouncedName]) const registrarContract = useENSRegistrarContract(false) const resolverAddress = useSingleCallResult(registrarContract, 'resolver', ensNodeArgument) - const resolverContract = useENSResolverContract(resolverAddress.result?.[0], false) + const resolverAddressResult = resolverAddress.result?.[0] + const resolverContract = useENSResolverContract( + resolverAddressResult && !isZero(resolverAddressResult) ? resolverAddressResult : undefined, + false + ) const addr = useSingleCallResult(resolverContract, 'addr', ensNodeArgument) const changed = debouncedName !== ensName diff --git a/src/state/swap/hooks.ts b/src/state/swap/hooks.ts index 491d5e3e39..b7975dd385 100644 --- a/src/state/swap/hooks.ts +++ b/src/state/swap/hooks.ts @@ -88,6 +88,24 @@ export function tryParseAmount(value?: string, currency?: Currency): CurrencyAmo return } +const BAD_RECIPIENT_ADDRESSES: string[] = [ + '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f', // v2 factory + '0xf164fC0Ec4E93095b804a4795bBe1e041497b92a', // v2 router 01 + '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D' // v2 router 02 +] + +/** + * Returns true if any of the pairs or tokens in a trade have the given checksummed address + * @param trade to check for the given address + * @param checksummedAddress address to check in the pairs and tokens + */ +function involvesAddress(trade: Trade, checksummedAddress: string): boolean { + return ( + trade.route.path.some(token => token.address === checksummedAddress) || + trade.route.pairs.some(pair => pair.liquidityToken.address === checksummedAddress) + ) +} + // from the current swap inputs, compute the best trade and return it. export function useDerivedSwapInfo(): { currencies: { [field in Field]?: Currency } @@ -153,8 +171,17 @@ export function useDerivedSwapInfo(): { inputError = inputError ?? 'Select a token' } - if (!to) { + const formattedTo = isAddress(to) + if (!to || !formattedTo) { inputError = inputError ?? 'Enter a recipient' + } else { + if ( + BAD_RECIPIENT_ADDRESSES.indexOf(formattedTo) !== -1 || + (bestTradeExactIn && involvesAddress(bestTradeExactIn, formattedTo)) || + (bestTradeExactOut && involvesAddress(bestTradeExactOut, formattedTo)) + ) { + inputError = inputError ?? 'Invalid recipient' + } } const [allowedSlippage] = useUserSlippageTolerance()