import { getAddress } from '@ethersproject/address' import { AddressZero } from '@ethersproject/constants' import { Contract } from '@ethersproject/contracts' import { JsonRpcSigner, Web3Provider } from '@ethersproject/providers' import { Token } from '@uniswap/sdk-core' import { FeeAmount } from '@uniswap/v3-sdk/dist/' import { TokenAddressMap } from '../state/lists/hooks' // returns the checksummed address if the address is valid, otherwise returns false export function isAddress(value: any): string | false { try { return getAddress(value) } catch { return false } } // shorten the checksummed version of the input address to have 0x + 4 characters at start and end export function shortenAddress(address: string, chars = 4): string { const parsed = isAddress(address) if (!parsed) { throw Error(`Invalid 'address' parameter '${address}'.`) } return `${parsed.substring(0, chars + 2)}...${parsed.substring(42 - chars)}` } // shorten the checksummed version of the input address to have 0x + 4 characters at start and end export function shortenTx(hash: string, chars = 6): string { const parsed = isAddress(hash) if (parsed) { throw Error(`Invalid 'address' parameter '${hash}'.`) } return `${hash.substring(0, chars + 2)}...${hash.substring(66 - chars)}` } export function shortenURL(url: string, chars = 6): string { return `${url.substring(0, chars + 22)}...${url.substring(url.length - chars)}` } // account is not optional export function getSigner(library: Web3Provider, account: string): JsonRpcSigner { return library.getSigner(account).connectUnchecked() } // account is optional export function getProviderOrSigner(library: Web3Provider, account?: string): Web3Provider | JsonRpcSigner { return account ? getSigner(library, account) : library } // account is optional export function getContract(address: string, ABI: any, library: Web3Provider, account?: string): Contract { if (!isAddress(address) || address === AddressZero) { throw Error(`Invalid 'address' parameter '${address}'.`) } return new Contract(address, ABI, getProviderOrSigner(library, account) as any) } export function escapeRegExp(string: string): string { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string } export function isTokenOnList(tokenAddressMap: TokenAddressMap, token?: Token): boolean { return Boolean(token?.isToken && tokenAddressMap[token.chainId]?.[token.address]) } export function formattedFeeAmount(feeAmount: FeeAmount): number { return feeAmount / 10000 } export function notEmpty(value: TValue | null | undefined): value is TValue { return value !== null && value !== undefined } export function ascendingAPR(value: TValue | null | undefined): value is TValue { return value !== null && value !== undefined } export const currentTimestamp = () => new Date().getTime()