-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
index.ts
56 lines (47 loc) · 2.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)}`
}
// account is not optional
function getSigner(library: Web3Provider, account: string): JsonRpcSigner {
return library.getSigner(account).connectUnchecked()
}
// account is optional
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
}