-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.ts
More file actions
81 lines (70 loc) · 2.42 KB
/
config.ts
File metadata and controls
81 lines (70 loc) · 2.42 KB
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
// Copyright (c) 2025 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { z } from 'zod';
import { config } from 'dotenv';
const HEX_REGEX = /^0x[0-9a-fA-F]+$/;
const envSchema = z.record(
z.object({
L1: z.object({
networkName: z.string(),
rpcUrl: z.string().url(),
faucetUrl: z.string().url().optional(),
chainId: z.string(),
packageId: z.string(),
}),
L2: z.object({
chainName: z.string(),
chainCurrency: z.string(),
rpcUrl: z.string().url(),
chainId: z.preprocess(
(val) => (val !== undefined ? Number(val) : undefined),
z.number().int().positive(),
),
chainDecimals: z.preprocess(
(val) => (val !== undefined ? Number(val) : undefined),
z.number().int().positive(),
),
chainExplorerName: z.string(),
chainExplorerUrl: z.string(),
wagmiAppName: z.string(),
walletConnectProjectId: z.string(),
iscContractAddress: z
.string()
.regex(
HEX_REGEX,
'Must be a valid hex string starting with 0x',
) as z.ZodType<`0x${string}`>,
evmRpcUrl: z.string().url(),
}),
}),
);
type Config = z.infer<typeof envSchema>;
function loadConfig(): Config {
config();
const rawEvmBridgeConfig = process.env.VITE_EVM_BRIDGE_CONFIG as string;
let evmBridgeConfig: Record<string, unknown> = {};
try {
evmBridgeConfig = JSON.parse(rawEvmBridgeConfig);
} catch (error) {
throw new Error(
`Failed to parse IOTA EVM Bridge config JSON env var! ${(error as Error)?.message}`,
);
}
try {
return envSchema.parse(evmBridgeConfig);
} catch (error) {
if (error instanceof z.ZodError) {
const missingVars = error.issues
.map((issue) => `${issue.path.join('.')}: ${issue.message}`)
.join('\n');
throw new Error(`Missing required configuration:\n${missingVars}`);
}
throw error;
}
}
export const CONFIG = getDefaultNetwork();
function getDefaultNetwork() {
const config = loadConfig();
const evmBridgeDefaultNetwork = process.env.VITE_EVM_BRIDGE_DEFAULT_NETWORK as string;
return config[evmBridgeDefaultNetwork];
}