This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathconfig.ts
More file actions
183 lines (165 loc) · 7.34 KB
/
config.ts
File metadata and controls
183 lines (165 loc) · 7.34 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
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
import { TUrl, TChainId, TAddress, TIntegerString, AllowedSwapSchema } from "@connext/vector-types";
import { Static, Type } from "@sinclair/typebox";
import Ajv from "ajv";
import { getAddress } from "@ethersproject/address";
import { BigNumber } from "@ethersproject/bignumber";
import { readFileSync } from "fs";
import { AddressZero } from "@ethersproject/constants";
const ajv = new Ajv();
const RebalanceProfileSchema = Type.Object({
chainId: TChainId,
assetId: TAddress,
reclaimThreshold: TIntegerString,
target: TIntegerString,
collateralizeThreshold: TIntegerString,
});
export type RebalanceProfile = Static<typeof RebalanceProfileSchema>;
const VectorRouterConfigSchema = Type.Object({
adminToken: Type.String(),
allowedSwaps: Type.Array(AllowedSwapSchema),
chainProviders: Type.Dict(Type.String()),
dbUrl: Type.Optional(TUrl),
nodeUrl: TUrl,
routerUrl: TUrl,
logLevel: Type.Optional(
Type.Union([
Type.Literal("fatal"),
Type.Literal("error"),
Type.Literal("warn"),
Type.Literal("info"),
Type.Literal("debug"),
Type.Literal("trace"),
Type.Literal("silent"),
]),
),
messagingUrl: Type.Optional(TUrl),
natsUrl: Type.Optional(Type.String()),
authUrl: Type.Optional(TUrl),
rebalanceProfiles: Type.Array(RebalanceProfileSchema),
mnemonic: Type.Optional(Type.String()),
stableAmmChainId: Type.Optional(TChainId),
stableAmmAddress: Type.Optional(TAddress),
routerMaxSafePriceImpact: Type.Optional(TIntegerString),
autoRebalanceInterval: Type.Optional(Type.Number({ minimum: 1_800_000 })),
basePercentageFee: Type.Optional(Type.Number({ minimum: 0, maximum: 100 })),
baseFlatFee: Type.Optional(TIntegerString),
baseGasSubsidyPercentage: Type.Optional(Type.Number({ minimum: 0, maximum: 100 })),
feeQuoteExpiry: Type.Optional(Type.Number({ minimum: 15_000 })),
});
export type VectorRouterConfig = Static<typeof VectorRouterConfigSchema>;
export const getEnvConfig = (): VectorRouterConfig => {
let configFile: any = {};
try {
let json;
if (process.env.VECTOR_CONFIG_FILE) {
console.log("process.env.VECTOR_CONFIG_FILE: ", process.env.VECTOR_CONFIG_FILE);
json = readFileSync(process.env.VECTOR_CONFIG_FILE, "utf-8");
} else {
json = readFileSync("config.json", "utf-8");
}
if (json) {
configFile = JSON.parse(json);
console.log("configFile: ", configFile);
console.log("Found configFile");
}
} catch (e) {
console.warn("No config file available...");
}
let configJson: Record<string, any> = {};
if (process.env.VECTOR_CONFIG) {
try {
configJson = JSON.parse(process.env.VECTOR_CONFIG);
console.log("Found process.env.VECTOR_CONFIG");
} catch (e) {
console.warn("No VECTOR_CONFIG exists...");
}
}
const vectorConfig: VectorRouterConfig = {
mnemonic: process.env.VECTOR_MNEMONIC || configJson.mnemonic || configFile.mnemonic,
dbUrl: process.env.VECTOR_DATABASE_URL || configJson.dbUrl || configFile.dbUrl,
messagingUrl: process.env.VECTOR_MESSAGING_URL || configJson.messagingUrl || configFile.messagingUrl,
authUrl: process.env.VECTOR_AUTH_URL || configJson.authUrl || configFile.authUrl,
natsUrl: process.env.VECTOR_NATS_URL || configJson.natsUrl || configFile.natsUrl,
adminToken: process.env.VECTOR_ADMIN_TOKEN || configJson.adminToken || configFile.adminToken,
baseGasSubsidyPercentage: process.env.VECTOR_BASE_GAS_SUBSIDY_PERCENTAGE
? process.env.VECTOR_BASE_GAS_SUBSIDY_PERCENTAGE
: configJson.baseGasSubsidyPercentage
? configJson.baseGasSubsidyPercentage
: configFile.baseGasSubsidyPercentage
? configFile.baseGasSubsidyPercentage
: 100,
chainProviders: process.env.VECTOR_CHAIN_PROVIDERS
? JSON.parse(process.env.VECTOR_CHAIN_PROVIDERS)
: configJson.chainProviders
? configJson.chainProviders
: configFile.chainProviders,
allowedSwaps: process.env.VECTOR_ALLOWED_SWAPS
? JSON.parse(process.env.VECTOR_ALLOWED_SWAPS)
: configJson.allowedSwaps
? configJson.allowedSwaps
: configFile.allowedSwaps,
stableAmmChainId:
process.env.VECTOR_STABLE_AMM_CHAIN_ID || configJson.stableAmmChainId || configFile.stableAmmChainId,
routerMaxSafePriceImpact:
process.env.ROUTER_MAX_SAFE_PRICE_IMPACT ||
configJson.routerMaxSafePriceImpact ||
configFile.routerMaxSafePriceImpact,
stableAmmAddress:
process.env.VECTOR_STABLE_AMM_ADDRESS || configJson.stableAmmAddress || configFile.stableAmmAddress,
nodeUrl: process.env.VECTOR_NODE_URL || configJson.nodeUrl || configFile.nodeUrl || "http://node:8000",
routerUrl: process.env.VECTOR_ROUTER_URL || configJson.routerUrl || configFile.routerUrl || "http://router:8000",
rebalanceProfiles:
process.env.VECTOR_REBALANCE_PROFILES || configJson.rebalanceProfiles || configFile.rebalanceProfiles,
autoRebalanceInterval:
process.env.VECTOR_AUTOREBALANCE_INTERVAL || configJson.autoRebalanceInterval || configFile.autoRebalanceInterval,
baseFlatFee: process.env.VECTOR_BASE_FLAT_FEE || configJson.baseFlatFee || configFile.baseFlatFee,
basePercentageFee:
process.env.VECTOR_BASE_PERCENTAGE_FEE || configJson.basePercentageFee || configFile.basePercentageFee,
feeQuoteExpiry: process.env.VECTOR_FEE_QUOTE_EXPIRY || configJson.feeQuoteExpiry || configFile.feeQuoteExpiry,
logLevel: process.env.VECTOR_FEE_LOG_LEVEL || configJson.logLevel || configFile.logLevel,
};
return vectorConfig;
};
const vectorConfig = getEnvConfig();
const mnemonic = vectorConfig.mnemonic;
// Set defaults
if (!vectorConfig.authUrl && !vectorConfig.messagingUrl && !vectorConfig.natsUrl) {
vectorConfig.messagingUrl = "http://messaging";
}
const validate = ajv.compile(VectorRouterConfigSchema);
const valid = validate(vectorConfig);
if (!valid) {
console.error(`Invalid config: ${JSON.stringify(vectorConfig, null, 2)}`);
throw new Error(validate.errors?.map((err) => err.message).join(","));
}
// checksum allowed swaps + rebalance profiles
vectorConfig.allowedSwaps = vectorConfig.allowedSwaps.map((s) => {
// sanity check:
// dynamicGasFees can only be assessed if `toChainId` or `fromChainId`
// is 1
if (s.toChainId !== 1 && s.fromChainId !== 1 && typeof s.gasSubsidyPercentage !== "undefined") {
throw new Error(`Cannot dynamically assess gas fees for non-mainnet swaps`);
}
return { ...s, fromAssetId: getAddress(s.fromAssetId), toAssetId: getAddress(s.toAssetId) };
});
vectorConfig.rebalanceProfiles = vectorConfig.rebalanceProfiles.map((profile) => {
// sanity checks
const target = BigNumber.from(profile.target);
if (target.gt(profile.reclaimThreshold)) {
throw new Error("Rebalance target must be less than reclaim threshold");
}
if (target.lt(profile.collateralizeThreshold) && !target.isZero()) {
throw new Error("Rebalance target must be larger than collateralizeThreshold or 0");
}
// checksum
return {
...profile,
assetId: getAddress(profile.assetId),
};
});
// check stableAmm params
if (vectorConfig.stableAmmChainId && !vectorConfig.chainProviders[vectorConfig.stableAmmChainId!]) {
throw new Error(`Config requires chain provider for stableAmmChainId ${vectorConfig.stableAmmChainId}`);
}
const config = vectorConfig as Omit<VectorRouterConfig, "mnemonic"> & { mnemonic: string };
export const getConfig = (): Omit<VectorRouterConfig, "mnemonic"> & { mnemonic: string } => config;