This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathindex.ts
188 lines (147 loc) · 5.28 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import memoize from 'lodash.memoize'
import networks from 'src/config/networks'
import {
EnvironmentSettings,
ETHEREUM_NETWORK,
FEATURES,
GasPriceOracle,
NetworkSettings,
SafeFeatures,
Wallets,
} from 'src/config/networks/network.d'
import { APP_ENV, ETHERSCAN_API_KEY, GOOGLE_ANALYTICS_ID, INFURA_TOKEN, NETWORK, NODE_ENV } from 'src/utils/constants'
import { ensureOnce } from 'src/utils/singleton'
export const getNetworkId = (): ETHEREUM_NETWORK => ETHEREUM_NETWORK[NETWORK]
export const getNetworkName = (): string => ETHEREUM_NETWORK[getNetworkId()]
const getCurrentEnvironment = (): string => {
switch (NODE_ENV) {
case 'test': {
return 'test'
}
case 'production': {
return APP_ENV === 'production' ? 'production' : 'staging'
}
default: {
return 'dev'
}
}
}
type NetworkSpecificConfiguration = EnvironmentSettings & {
network: NetworkSettings
disabledFeatures?: SafeFeatures
disabledWallets?: Wallets
}
const configuration = (): NetworkSpecificConfiguration => {
const currentEnvironment = getCurrentEnvironment()
// special case for test environment
if (currentEnvironment === 'test') {
const configFile = networks.local
return {
...configFile.environment.production,
network: configFile.network,
disabledFeatures: configFile.disabledFeatures,
}
}
// lookup the config file based on the network specified in the NETWORK variable
const configFile = networks[getNetworkName().toLowerCase()]
// defaults to 'production' as it's the only environment that is required for the network configs
const networkBaseConfig = configFile.environment[currentEnvironment] ?? configFile.environment.production
return {
...networkBaseConfig,
network: configFile.network,
disabledFeatures: configFile.disabledFeatures,
disabledWallets: configFile.disabledWallets,
}
}
const getConfig: () => NetworkSpecificConfiguration = ensureOnce(configuration)
export const getTxServiceUrl = (): string => getConfig().txServiceUrl
export const getRelayUrl = (): string | undefined => getConfig().relayApiUrl
export const getGnosisSafeAppsUrl = (): string => getConfig().safeAppsUrl
export const getGasPrice = (): number | undefined => getConfig()?.gasPrice
export const getGasPriceOracle = (): GasPriceOracle | undefined => getConfig()?.gasPriceOracle
export const getRpcServiceUrl = (): string => {
const usesInfuraRPC = [ETHEREUM_NETWORK.MAINNET, ETHEREUM_NETWORK.RINKEBY].includes(getNetworkId())
if (usesInfuraRPC) {
return `${getConfig().rpcServiceUrl}/${INFURA_TOKEN}`
}
return getConfig().rpcServiceUrl
}
export const getSafeServiceBaseUrl = (safeAddress: string) => `${getTxServiceUrl()}/safes/${safeAddress}`
export const getTokensServiceBaseUrl = () => `${getTxServiceUrl()}/tokens`
export const getNetworkExplorerInfo = (): { name: string; url: string; apiUrl: string } => ({
name: getConfig().networkExplorerName,
url: getConfig().networkExplorerUrl,
apiUrl: getConfig().networkExplorerApiUrl,
})
export const getNetworkConfigDisabledFeatures = (): SafeFeatures => getConfig().disabledFeatures || []
/**
* Checks if a particular feature is enabled in the current network configuration
* @params {FEATURES} feature
* @returns boolean
*/
export const isFeatureEnabled = memoize((feature: FEATURES): boolean => {
const disabledFeatures = getNetworkConfigDisabledFeatures()
return !disabledFeatures.some((disabledFeature) => disabledFeature === feature)
})
export const getNetworkConfigDisabledWallets = (): Wallets => getConfig()?.disabledWallets || []
export const getNetworkInfo = (): NetworkSettings => getConfig().network
export const getGoogleAnalyticsTrackingID = (): string => GOOGLE_ANALYTICS_ID
const fetchContractABI = memoize(
async (url: string, contractAddress: string, apiKey?: string) => {
let params: Record<string, string> = {
module: 'contract',
action: 'getAbi',
address: contractAddress,
}
if (apiKey) {
params = { ...params, apiKey }
}
const response = await fetch(`${url}?${new URLSearchParams(params)}`)
if (!response.ok) {
return { status: 0, result: [] }
}
return response.json()
},
(url, contractAddress) => `${url}_${contractAddress}`,
)
const getNetworkExplorerApiKey = (networkExplorerName: string): string | undefined => {
switch (networkExplorerName.toLowerCase()) {
case 'etherscan': {
return ETHERSCAN_API_KEY
}
default: {
return undefined
}
}
}
export const getContractABI = async (contractAddress: string) => {
const { apiUrl, name } = getNetworkExplorerInfo()
const apiKey = getNetworkExplorerApiKey(name)
try {
const { result, status } = await fetchContractABI(apiUrl, contractAddress, apiKey)
if (status === '0') {
return []
}
return result
} catch (e) {
console.error('Failed to retrieve ABI', e)
return undefined
}
}
export type BlockScanInfo = () => {
alt: string
url: string
}
export const getExplorerInfo = (hash: string): BlockScanInfo => {
const { name, url } = getNetworkExplorerInfo()
const networkInfo = getNetworkInfo()
switch (networkInfo.id) {
default: {
const type = hash.length > 42 ? 'tx' : 'address'
return () => ({
url: `${url}/${type}/${hash}`,
alt: name || '',
})
}
}
}