import { Contract } from '@ethersproject/contracts' ; import { WETH9 } from '@uniswap/sdk-core' ; import { abi as IUniswapV2PairABI } from '@uniswap/v2-core/build/IUniswapV2Pair.json' ; import { SupportedChainId } from 'constants/chains' ; import { useMemo } from 'react' ; import { ARGENT_WALLET_DETECTOR_ABI , ARGENT_WALLET_DETECTOR_MAINNET_ADDRESS , } from '../constants/abis/argent-wallet-detector' ; import ENS_PUBLIC_RESOLVER_ABI from '../constants/abis/ens-public-resolver.json' ; import ENS_ABI from '../constants/abis/ens-registrar.json' ; import { ERC20_BYTES32_ABI } from '../constants/abis/erc20' ; import ERC20_ABI from '../constants/abis/erc20.json' ; import { MIGRATOR_ABI , MIGRATOR_ADDRESS } from '../constants/abis/migrator' ; import UNISOCKS_ABI from '../constants/abis/unisocks.json' ; import WETH_ABI from '../constants/abis/weth.json' ; import { MULTICALL_ABI , MULTICALL_NETWORKS } from '../constants/multicall' ; import { getContract } from '../utils' ; import { useActiveWeb3React } from './index' ; function useContract ( address , ABI , withSignerIfPossible = true ) { const { library , account } = useActiveWeb3React ( ) ; return useMemo ( ( ) => { if ( ! address || ! ABI || ! library ) return null ; try { return getContract ( address , ABI , library , withSignerIfPossible && account ? account : undefined ) ; } catch ( error ) { console . error ( 'Failed to get contract' , error ) ; return null ; } } , [ address , ABI , library , withSignerIfPossible , account ] ) ; } export function useV2MigratorContract ( ) { return useContract ( MIGRATOR_ADDRESS , MIGRATOR_ABI , true ) ; } export function useTokenContract ( tokenAddress ? , withSignerIfPossible ? ) { return useContract ( tokenAddress , ERC20_ABI , withSignerIfPossible ) ; } export function useWETHContract ( withSignerIfPossible ? ) { const { chainId } = useActiveWeb3React ( ) ; return useContract ( chainId ? WETH9 [ chainId ] . address : undefined , WETH_ABI , withSignerIfPossible ) ; } export function useArgentWalletDetectorContract ( ) { const { chainId } = useActiveWeb3React ( ) ; return useContract ( chainId === SupportedChainId . MAINNET ? ARGENT_WALLET_DETECTOR_MAINNET_ADDRESS : undefined , ARGENT_WALLET_DETECTOR_ABI , false ) ; } export function useENSRegistrarContract ( withSignerIfPossible ? ) { const { chainId } = useActiveWeb3React ( ) ; let address ; if ( chainId ) { switch ( chainId ) { case SupportedChainId . MAINNET : case SupportedChainId . ROPSTEN : case SupportedChainId . RINKEBY : address = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' ; break ; } } return useContract ( address , ENS_ABI , withSignerIfPossible ) ; } export function useENSResolverContract ( address , withSignerIfPossible ? ) { return useContract ( address , ENS_PUBLIC_RESOLVER_ABI , withSignerIfPossible ) ; } export function useBytes32TokenContract ( tokenAddress ? , withSignerIfPossible ? ) { return useContract ( tokenAddress , ERC20_BYTES32_ABI , withSignerIfPossible ) ; } export function usePairContract ( pairAddress ? , withSignerIfPossible ? ) { return useContract ( pairAddress , IUniswapV2PairABI , withSignerIfPossible ) ; } export function useMulticallContract ( ) { const { chainId } = useActiveWeb3React ( ) ; return useContract ( chainId ? MULTICALL_NETWORKS [ chainId ] : undefined , MULTICALL_ABI , false ) ; } export function useSocksController ( ) { const { chainId } = useActiveWeb3React ( ) ; return useContract ( chainId === SupportedChainId . MAINNET ? '0x65770b5283117639760beA3F867b69b3697a91dd' : undefined , UNISOCKS_ABI , false ) ; }