-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.ts
85 lines (75 loc) · 2.41 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ethers } from 'ethers'
import { explorerUrls } from './src/constants'
export * from './src/constants'
export * from './src/metadata'
export * from './src/pricing'
export * from './src/server'
export * from './src/swap'
export * from './src/swap-erc20'
export * from './src/tokenlists'
export * from './src/types'
export function getReceiptUrl(chainId: number, hash: string): string {
return `${explorerUrls[chainId]}/tx/${hash}`
}
export function getAccountUrl(chainId: number, address: string): string {
return `${explorerUrls[chainId]}/address/${address}`
}
export function parseCheckResult(errors: Array<string>) {
const res: Array<string> = []
for (let idx = 0; idx < errors.length; idx++) {
const error = ethers.utils.parseBytes32String(errors[idx])
if (error) {
res.push(error)
}
}
return res
}
export function getInterfaceId(functions: string[]): string {
const _interface = new ethers.utils.Interface(functions)
const interfaceId = ethers.utils.arrayify(
_interface.getSighash(_interface.fragments[0])
)
for (let i = 1; i < _interface.fragments.length; i++) {
const hash = ethers.utils.arrayify(
_interface.getSighash(_interface.fragments[i])
)
for (let j = 0; j < hash.length; j++) {
interfaceId[j] = interfaceId[j] ^ hash[j]
}
}
return ethers.utils.hexlify(interfaceId)
}
export function stringifyEIP712Type(
types: { [key: string]: { type: string; name: string }[] },
primaryType: string
): string {
return types[primaryType].reduce((str, value, index, values) => {
const isEnd = index !== values.length - 1
return `${str}${value.type} ${value.name}${isEnd ? ',' : ')'}`
}, `${primaryType}(`)
}
export function parseUrl(locator: string): URL {
if (!/(http|ws)s?:\/\//.test(locator)) {
return new URL(`https://${locator}`)
}
return new URL(locator)
}
export function lowerCaseAddresses(obj: any): any {
for (const key in obj) {
if (typeof obj[key] === 'object') {
lowerCaseAddresses(obj[key])
} else if (typeof obj[key] === 'string' && obj[key].indexOf('0x') === 0) {
obj[key] = obj[key]?.toLowerCase()
} else {
obj[key] = obj[key]?.toString()
}
}
return obj
}
export function getTimestamp(): string {
return Math.round(Date.now() / 1000).toString()
}
export function numberToBytes32(number: number): string {
const hexString = number.toString(16)
return `0x${hexString.padStart(64, '0')}`
}