-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathserver.ts
92 lines (81 loc) · 3.24 KB
/
server.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
// Disable the following rule because this file is the intended place to declare
// and load all env variables.
/* eslint-disable n/no-process-env */
// Disable the following rule because variables in this file are only loaded at
// runtime and do not influence the build outputs, thus they need not be
// declared to turbo for it to be able to cache build outputs correctly.
/* eslint-disable turbo/no-undeclared-env-vars */
import "server-only";
/**
* Throw if the env var `key` is not set (at either runtime or build time).
*/
const demand = (key: string): string => {
const value = process.env[key];
if (value === undefined || value === "") {
throw new MissingEnvironmentError(key);
} else {
return value;
}
};
const fromCsv = (value: string): string[] =>
value.split(",").map((entry) => entry.toLowerCase().trim());
const transform = <T>(key: string, fn: (value: string | undefined) => T): T => {
const value = process.env[key];
return fn(value === "" ? undefined : value);
};
const transformOr = <T>(
key: string,
fn: (value: string) => T,
defaultValue: T,
): T => transform(key, (value) => (value ? fn(value) : defaultValue));
const getOr = (key: string, defaultValue: string): string =>
transform(key, (value) => value ?? defaultValue);
/**
* Indicates that this server is the live customer-facing production server.
*/
export const IS_PRODUCTION_SERVER = process.env.VERCEL_ENV === "production";
/**
* Throw if the env var `key` is not set in the live customer-facing production
* server, but allow it to be unset in any other environment.
*/
const demandInProduction = IS_PRODUCTION_SERVER
? demand
: (key: string) => process.env[key];
export const GOOGLE_ANALYTICS_ID = demandInProduction("GOOGLE_ANALYTICS_ID");
export const AMPLITUDE_API_KEY = demandInProduction("AMPLITUDE_API_KEY");
export const WALLETCONNECT_PROJECT_ID = demandInProduction(
"WALLETCONNECT_PROJECT_ID",
);
export const MAINNET_RPC = process.env.MAINNET_RPC;
export const MAINNET_API_RPC =
process.env.MAINNET_API_RPC ?? process.env.MAINNET_RPC;
export const PYTHNET_RPC = getOr("PYTHNET_RPC", "https://pythnet.rpcpool.com");
export const HERMES_URL = getOr("HERMES_URL", "https://hermes.pyth.network");
export const BLOCKED_REGIONS = transformOr("BLOCKED_REGIONS", fromCsv, []);
export const IP_ALLOWLIST = transformOr("IP_ALLOWLIST", fromCsv, []);
export const VPN_ORGANIZATION_ALLOWLIST = transformOr(
"VPN_ORGANIZATION_ALLOWLIST",
fromCsv,
["iCloud Private Relay"],
);
export const GOVERNANCE_ONLY_REGIONS = transformOr(
"GOVERNANCE_ONLY_REGIONS",
fromCsv,
[],
);
export const PROXYCHECK_API_KEY = demandInProduction("PROXYCHECK_API_KEY");
// This needs to be a public key that has SOL in it all the time, it will be used as a payer in the transaction simulation to compute the claimable rewards
// such simulation fails when the payer has no funds.
export const SIMULATION_PAYER_ADDRESS = getOr(
"SIMULATION_PAYER_ADDRESS",
"E5KR7yfb9UyVB6ZhmhQki1rM1eBcxHvyGKFZakAC5uc",
);
export const AMOUNT_STAKED_PER_ACCOUNT_SECRET = demandInProduction(
"AMOUNT_STAKED_PER_ACCOUNT_SECRET",
);
class MissingEnvironmentError extends Error {
constructor(name: string) {
super(`Missing environment variable: ${name}!`);
this.name = "MissingEnvironmentError";
}
}