-
Notifications
You must be signed in to change notification settings - Fork 69
/
config.ts
265 lines (232 loc) · 8.67 KB
/
config.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import * as yargs from 'yargs';
import { config } from 'dotenv';
import { BigNumber } from 'ethers';
import { LogTypes } from '@requestnetwork/types';
import { LogMode } from './logger';
const argv = yargs.option('help', { alias: 'h', type: 'boolean' }).parseSync();
// Load environment variables from .env file (without overriding variables already set)
config();
/**
* This contains default values used for the server and storage initialization
* when environment variable is not specified
*/
const defaultValues = {
storage: {
ethereum: {
networkId: 0,
web3ProviderUrl: 'http://localhost:8545',
gasPriceMin: '1000000000', // 1 gwei per gas
gasPriceMax: '10000000000000', // 10,000 gwei per gas
// multiply by 2 the estimated max fee per gas to accomadate for volatility
gasPriceMultiplier: 200,
blockConfirmations: 2,
},
ipfs: {
url: 'http://localhost:5001',
timeout: 30000,
},
thegraph: {
nodeUrl: 'http://localhost:8000/subgraphs/name/RequestNetwork/request-storage',
},
persistTransactionTimeout: 600,
},
log: {
level: LogTypes.LogLevel.INFO,
mode: LogMode.human,
},
server: {
headers: '{}',
port: 3000,
},
wallet: {
mnemonic: 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat',
},
};
const getOption = <T extends string | number>(
argName: string,
envName: string,
defaultValue?: T,
): T => {
const val = argv[argName] || process.env[envName] || defaultValue;
if (typeof defaultValue === 'number') return Number(val) as T;
return String(val) as T;
};
const makeOption =
<T extends string | number>(...params: Parameters<typeof getOption<T>>) =>
() =>
getOption<T>(...params);
export const isHelp = (): boolean => argv.help || false;
/**
* Get the port from command line argument, environment variables or default values to allow user to connect to the server
*/
export const getServerPort = makeOption('port', 'PORT', defaultValues.server.port);
/**
* Get network id of the Ethereum network from command line argument, environment variables or default values
*/
export const getStorageNetworkId = makeOption(
'networkId',
'ETHEREUM_NETWORK_ID',
defaultValues.storage.ethereum.networkId,
);
/**
* Get Web3 provider url from command line argument, environment variables or default values
*/
export const getStorageWeb3ProviderUrl = makeOption(
'providerUrl',
'WEB3_PROVIDER_URL',
defaultValues.storage.ethereum.web3ProviderUrl,
);
/** Get the Graph node URL */
export const getGraphNodeUrl = makeOption(
'graphNodeUrl',
'GRAPH_NODE_URL',
defaultValues.storage.thegraph.nodeUrl,
);
export function getGasPriceMin(): BigNumber | undefined {
const gasPriceMin = getOption(
'gasPriceMin',
'GAS_PRICE_MIN',
defaultValues.storage.ethereum.gasPriceMin,
);
return gasPriceMin ? BigNumber.from(gasPriceMin) : undefined;
}
export function getGasPriceMax(): BigNumber | undefined {
const gasPriceMax = getOption(
'gasPriceMax',
'GAS_PRICE_MAX',
defaultValues.storage.ethereum.gasPriceMax,
);
return gasPriceMax ? BigNumber.from(gasPriceMax) : undefined;
}
export const getGasPriceMultiplier = makeOption(
'gasPriceMultiplier',
'GAS_PRICE_MULTIPLIER',
defaultValues.storage.ethereum.gasPriceMultiplier,
);
/**
* Get the number of block confirmations to wait before considering a transaction successful
*/
export const getBlockConfirmations = makeOption(
'blockConfirmations',
'BLOCK_CONFIRMATIONS',
defaultValues.storage.ethereum.blockConfirmations,
);
/**
* Get IPFS url from command line argument, environment variables or default values to connect to IPFS gateway
* @returns the url of the IPFS gateway
*/
export const getIpfsUrl = makeOption('ipfsUrl', 'IPFS_URL', defaultValues.storage.ipfs.url);
/**
* Get the timeout threshold from command line argument, environment variables or default values for IPFS gateway connection
* If the connection delay for IPFS gateway reachs this value, the connection fails
* @returns the timeout threshold for IPFS gateway connection
*/
export const getIpfsTimeout = makeOption(
'ipfsTimeout',
'IPFS_TIMEOUT',
defaultValues.storage.ipfs.timeout,
);
/**
* Get the mnemonic from environment variables or default values to generate the private key for the wallet
* The default value must only be used for test purposes
* For production, mnemonic should always be provided as environment variable
* @returns the mnemonic for HDWallet
*/
export function getMnemonic(): string {
if (!process.env.MNEMONIC) {
if (getStorageNetworkId() !== 0) {
throw new Error(
'the environment variable MNEMONIC must be set up. The default mnemonic is only for private network.',
);
}
return defaultValues.wallet.mnemonic;
}
return process.env.MNEMONIC;
}
/** logLevel is the maximum level of messages we will log */
export const getLogLevel = (): LogTypes.LogLevel => {
const logLevelStr = getOption<keyof typeof LogTypes.LogLevel>('logLevel', 'LOG_LEVEL');
return LogTypes.LogLevel[logLevelStr] || defaultValues.log.level;
};
/** logMode defines the log format to display: `human` is a more readable log, `machine` is better for parsing */
export const getLogMode = makeOption('logMode', 'LOG_MODE', defaultValues.log.mode);
/**
* Get the delay to wait before sending a timeout when performing a persistTransaction request
* persistTransaction is called when a request is created or updated and need to wait for Ethereum to commit the transaction
* PROT-300: This configuration value can be removed once batching is implenented
* because there will be no more need to wait for the smart contract
* @returns THe delay to wait for the timeout
*/
export const getPersistTransactionTimeout = makeOption(
'persistTransactionTimeout',
'PERSIST_TRANSACTION_TIMEOUT',
defaultValues.storage.persistTransactionTimeout,
);
/**
* Get the mnemonic from command line argument, environment variables or default values to generate the private key for the wallet
* The default value must only be used for test purposes
* For production, mnemonic should always be provided as environment variable
* @returns the mnemonic for HDWallet
*/
export function getHelpMessage(): string {
const message = `USAGE
request-node - Node for request protocol v2
yarn start <options>
OPTIONS
SERVER OPTIONS
port (${defaultValues.server.port})\t\t\t\tPort for the server to listen for API requests
headers (${
defaultValues.server.headers
})\t\t\t\tCustom headers to send with the API responses
THE GRAPH OPTIONS
graphNodeUrl (${defaultValues.storage.thegraph.nodeUrl})\t\t\t\tURL of the Graph node
ETHEREUM OPTIONS
networkId (${
defaultValues.storage.ethereum.networkId
})\t\t\t\tId of the Ethereum network used
providerUrl (${
defaultValues.storage.ethereum.web3ProviderUrl
})\tUrl of the web3 provider for Ethereum
gasPriceMin (${
defaultValues.storage.ethereum.gasPriceMin
})\t\t\t\tMinimum value for maxPriorityFeePerGas and maxFeePerGas
gasPriceMax (${
defaultValues.storage.ethereum.gasPriceMax
})\t\t\t\tMaximum value for maxFeePerGas
gasPriceMultiplier (${
defaultValues.storage.ethereum.gasPriceMultiplier
})\t\t\t\tMultiplier for the computed maxFeePerGas
blockConfirmations (${
defaultValues.storage.ethereum.blockConfirmations
})\t\t\t\tNumber of block confirmations to wait before considering a transaction successful
IPFS OPTIONS
ipfsUrl (${defaultValues.storage.ipfs.url})\t\t\tURL of the IPFS gateway
ipfsTimeout (${
defaultValues.storage.ipfs.timeout
})\t\t\tTimeout threshold to connect to the IPFS gateway
OTHER OPTIONS
logLevel (${
LogTypes.LogLevel[defaultValues.log.level]
})\t\t\tThe node log level (ERROR, WARN, INFO or DEBUG)
logMode (${defaultValues.log.mode})\t\t\tThe node log mode (human or machine)
EXAMPLE
yarn start --port 5000 --networkId 1
All options are optional, not specified options are read from environment variables
If the environment variable is not specified, default value is used
Default mnemonic is:
${defaultValues.wallet.mnemonic}
`;
return message;
}
export const getConfigDisplay = (): string => {
return `Using config:
Ethereum network id: ${getStorageNetworkId()}
Log Level: ${LogTypes.LogLevel[getLogLevel()]}
Log Mode: ${getLogMode()}
Web3 provider url: ${getStorageWeb3ProviderUrl()}
TheGraph url: ${getGraphNodeUrl()}
IPFS url: ${getIpfsUrl()}
IPFS timeout: ${getIpfsTimeout()}
Storage block confirmations: ${getBlockConfirmations()}
`;
};