import { getAddress } from '@ethersproject/address' ; import { BigNumber } from '@ethersproject/bignumber' ; import { AddressZero } from '@ethersproject/constants' ; import { Contract } from '@ethersproject/contracts' ; import { JsonRpcSigner , Web3Provider } from '@ethersproject/providers' ; import { Currency , CurrencyAmount , Fraction , Percent , Token } from '@uniswap/sdk-core' ; import { abi as IUniswapV2Router02ABI } from '@uniswap/v2-periphery/build/IUniswapV2Router02.json' ; import { SupportedChainId } from 'constants/chains' ; import { ArbitrumNetworkInfo , NetworkInfo , PolygonNetworkInfo } from 'constants/networks' ; import JSBI from 'jsbi' ; import { ROUTER_ADDRESS } from '../constants' ; import { TokenAddressMap } from '../state/lists/hooks' ; import { OptimismNetworkInfo } from './../constants/networks' ; export function isAddress ( value ) { try { return getAddress ( value ) ; } catch { return false ; } } const ETHERSCAN_PREFIXES = { [ SupportedChainId . MAINNET ] : '' , [ SupportedChainId . ROPSTEN ] : 'ropsten.' , [ SupportedChainId . RINKEBY ] : 'rinkeby.' , [ SupportedChainId . GOERLI ] : 'goerli.' , [ SupportedChainId . KOVAN ] : 'kovan.' , [ SupportedChainId . OPTIMISM ] : 'optimistic.' , [ SupportedChainId . OPTIMISTIC_KOVAN ] : 'kovan-optimistic.' , } ; export function getEtherscanLink ( chainId , data , type , networkVersion ) { const prefix = networkVersion === PolygonNetworkInfo ? 'https://polygonscan.com/' : networkVersion === ArbitrumNetworkInfo ? 'https://arbiscan.io/' : networkVersion === OptimismNetworkInfo ? 'https://optimistic.etherscan.io' : `https://${ ETHERSCAN_PREFIXES [ chainId ] || ETHERSCAN_PREFIXES [ 1 ] }etherscan.io` ; if ( networkVersion === OptimismNetworkInfo ) { switch ( type ) { case 'transaction' : { return `${ prefix }/tx/${ data }` ; } case 'token' : { return `${ prefix }/address/${ data }` ; } case 'block' : { return `https://optimistic.etherscan.io` ; } case 'address' : default : { return `${ prefix }/address/${ data }` ; } } } if ( networkVersion === ArbitrumNetworkInfo ) { switch ( type ) { case 'transaction' : { return `${ prefix }/tx/${ data }` ; } case 'token' : { return `${ prefix }/address/${ data }` ; } case 'block' : { return 'https://arbiscan.io/' ; } case 'address' : default : { return `${ prefix }/address/${ data }` ; } } } switch ( type ) { case 'transaction' : { return `${ prefix }/tx/${ data }` ; } case 'token' : { return `${ prefix }/token/${ data }` ; } case 'block' : { return `${ prefix }/block/${ data }` ; } case 'address' : default : { return `${ prefix }/address/${ data }` ; } } } export const currentTimestamp = ( ) => new Date ( ) . getTime ( ) ; export function shortenAddress ( address , chars = 4 ) { const parsed = isAddress ( address ) ; if ( ! parsed ) { throw Error ( `Invalid 'address' parameter '${ address }'.` ) ; } return `${ parsed . substring ( 0 , chars + 2 ) }...${ parsed . substring ( 42 - chars ) }` ; } export function calculateGasMargin ( value ) { return value . mul ( BigNumber . from ( 10000 ) . add ( BigNumber . from ( 1000 ) ) ) . div ( BigNumber . from ( 10000 ) ) ; } export function basisPointsToPercent ( num ) { return new Percent ( JSBI . BigInt ( num ) , JSBI . BigInt ( 10000 ) ) ; } const ONE = new Fraction ( 1 , 1 ) ; export function calculateSlippageAmount ( value , slippage ) { if ( slippage . lessThan ( 0 ) || slippage . greaterThan ( ONE ) ) throw new Error ( 'Unexpected slippage' ) ; return [ value . multiply ( ONE . subtract ( slippage ) ) . quotient , value . multiply ( ONE . add ( slippage ) ) . quotient ] ; } export function getSigner ( library , account ) { return library . getSigner ( account ) . connectUnchecked ( ) ; } export function getProviderOrSigner ( library , account ? ) { return account ? getSigner ( library , account ) : library ; } export function getContract ( address , ABI , library , account ? ) { if ( ! isAddress ( address ) || address === AddressZero ) { throw Error ( `Invalid 'address' parameter '${ address }'.` ) ; } return new Contract ( address , ABI , getProviderOrSigner ( library , account ) as any ) ; } export function getRouterContract ( _ , library , account ? ) { return getContract ( ROUTER_ADDRESS , IUniswapV2Router02ABI , library , account ) ; } export function escapeRegExp ( string ) { return string . replace ( /[.*+?^${}()|[\]\\]/g , '\\$&' ) ; } export function isTokenOnList ( tokenAddressMap , token ? ) { return Boolean ( token ?. isToken && tokenAddressMap [ token . chainId ] ?. [ token . address ] ) ; } export function feeTierPercent ( fee ) { return ( fee / 10000 ) . toPrecision ( 1 ) + '%' ; } export function notEmpty < TValue > ( value ) { return value !== null && value !== undefined ; }