-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
53 lines (44 loc) · 1.23 KB
/
index.js
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
import { isTest, serviceUrl } from './config'
export const url = (path) => serviceUrl + path
export const fetcher = (url) => fetch(url).then((res) => res.json())
export const post = async (url, data) => {
try {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
return await response.json()
} catch (error) {
return error.message
}
}
export function chainLabel(chainId) {
const names = {
eth: 'Ethereum',
bsc: 'Binance Smart Chain',
}
let chain = chainId
if (!['eth', 'bsc'].includes(chainId)) {
chain = isChainBsc(chainId) ? 'bsc' : 'eth'
}
return names[chain]
}
export function isChainValid(chainId) {
return isTest
? [3, 97, '0x3', '0x61'].includes(chainId)
: [1, 56, '0x1', '0x38'].includes(chainId)
}
export const isChainBsc = (chainId) =>
(typeof chainId == 'string' ? ['0x38', '0x61'] : [56, 97]).includes(chainId)
let contracts
export async function getTokenAddress(chainId) {
if (!contracts) {
contracts = await (await fetch(url('/contracts'))).json()
}
return contracts[isChainBsc(chainId) ? 'bsc' : 'eth']
}