-
Notifications
You must be signed in to change notification settings - Fork 54
/
environment.ts
154 lines (141 loc) · 4.65 KB
/
environment.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
import { Cardano } from '@cardano-sdk/core';
import { Schema, Validator as SchemaValidator } from 'jsonschema';
import { ValidatorSpec, bool, cleanEnv, makeValidator, num, str } from 'envalid';
export interface KeyManagementParams {
accountIndex: number;
mnemonic: string;
chainId: Cardano.ChainId;
password: string;
cryptoProvider: string;
}
export interface ProviderParams {
baseUrl: string;
}
const baseValidator = <T>(value: string, schema: Schema, ...dependencySchemas: Schema[]) => {
const parsed = JSON.parse(value) as T;
const v = new SchemaValidator();
for (const dependencySchema of dependencySchemas) {
v.addSchema(dependencySchema);
}
const res = v.validate(parsed, schema, { required: true });
if (!res.valid) throw new Error(res.errors[0].stack.replace(/^instance\./, ''));
return parsed;
};
const keyManagementParams = makeValidator((value) =>
baseValidator<KeyManagementParams>(
value,
{
properties: {
accountIndex: { minimum: 0, type: 'integer' },
chainId: { $ref: '/ChainId' },
cryptoProvider: { type: 'string' },
mnemonic: { type: 'string' },
password: { type: 'string' }
},
required: ['accountIndex', 'mnemonic', 'chainId', 'password', 'cryptoProvider'],
type: 'object'
},
{
id: '/ChainId',
properties: {
networkId: { minimum: 0, type: 'integer' },
networkMagic: { minimum: 0, type: 'integer' }
},
required: ['networkId', 'networkMagic'],
type: 'object'
}
)
);
const providerParams = makeValidator((value) => {
const validated = baseValidator<ProviderParams>(value, {
properties: { baseUrl: { type: 'string' } },
required: ['baseUrl'],
type: 'object'
});
try {
// eslint-disable-next-line no-new
new URL(validated.baseUrl);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
throw new Error(`baseUrl: ${error.message}`);
}
return validated;
});
/**
* Shared across all tests
*/
const validators = {
ARRIVAL_PHASE_DURATION_IN_SECS: num(),
ASSET_PROVIDER: str(),
ASSET_PROVIDER_PARAMS: providerParams(),
CHAIN_HISTORY_PROVIDER: str(),
CHAIN_HISTORY_PROVIDER_PARAMS: providerParams(),
DB_SYNC_CONNECTION_STRING: str(),
KEY_MANAGEMENT_PARAMS: keyManagementParams(),
KEY_MANAGEMENT_PROVIDER: str(),
LOGGER_MIN_SEVERITY: str({ default: 'info' }),
NETWORK_INFO_PROVIDER: str(),
NETWORK_INFO_PROVIDER_PARAMS: providerParams(),
OGMIOS_SERVER_URL: str(),
OGMIOS_URL: str(),
REWARDS_PROVIDER: str(),
REWARDS_PROVIDER_PARAMS: providerParams(),
STAKE_POOL_PROVIDER: str(),
STAKE_POOL_PROVIDER_PARAMS: providerParams(),
START_LOCAL_HTTP_SERVER: bool(),
TRANSACTIONS_NUMBER: num(),
TX_SUBMIT_HTTP_URL: str(),
TX_SUBMIT_PROVIDER: str(),
TX_SUBMIT_PROVIDER_PARAMS: providerParams(),
UTXO_PROVIDER: str(),
UTXO_PROVIDER_PARAMS: providerParams(),
VIRTUAL_USERS_COUNT: num(),
VIRTUAL_USERS_GENERATE_DURATION: num(),
WORKER_PARALLEL_TRANSACTION: num()
} as const;
type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T][];
type Validators = typeof validators;
type Validator = keyof Validators;
/**
* Reads the environment variables from `process.env` and performs the checks against
* the shared constraints to ensure the required configuration is provided through
* the environment variables.
*
* @param variables Array of the names of the required variables
* @param options Options to customize the behavior
* @param options.default Object of default values
* @param options.override Object of override values
* @returns A `NodeJS.ProcessEnv` like object which respects the shared constraints
*/
export const getEnv = <V extends readonly Validator[]>(
variables: V,
options: { default?: NodeJS.ProcessEnv; override?: NodeJS.ProcessEnv } = {}
) =>
cleanEnv(
{ ...options.default, ...process.env, ...options.override },
Object.fromEntries((Object.entries(validators) as Entries<Validators>).filter(([name]) => variables.includes(name)))
) as unknown as {
[v in V[number]]: Validators[v] extends ValidatorSpec<infer T> ? T : never;
};
/**
* Collection of all the configuration variables required by `getWallet`
*/
export const walletVariables = [
'ASSET_PROVIDER',
'ASSET_PROVIDER_PARAMS',
'CHAIN_HISTORY_PROVIDER',
'CHAIN_HISTORY_PROVIDER_PARAMS',
'KEY_MANAGEMENT_PARAMS',
'KEY_MANAGEMENT_PROVIDER',
'LOGGER_MIN_SEVERITY',
'NETWORK_INFO_PROVIDER',
'NETWORK_INFO_PROVIDER_PARAMS',
'REWARDS_PROVIDER',
'REWARDS_PROVIDER_PARAMS',
'STAKE_POOL_PROVIDER',
'STAKE_POOL_PROVIDER_PARAMS',
'TX_SUBMIT_PROVIDER',
'TX_SUBMIT_PROVIDER_PARAMS',
'UTXO_PROVIDER',
'UTXO_PROVIDER_PARAMS'
] as const;