-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmarket-config-helpers.ts
385 lines (336 loc) · 10.8 KB
/
market-config-helpers.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { getAddress } from "ethers/lib/utils";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import Bluebird from "bluebird";
import {
iParamsPerNetwork,
eNetwork,
PoolConfiguration,
IBaseConfiguration,
ITokenAddress,
tEthereumAddress,
ICommonConfiguration,
SubTokenOutput,
AssetType,
} from "./types";
import AaveMarket from "../markets/aave";
import AaveTestMarket from "../markets/test";
import HarmonyMarket from "../markets/harmony";
import AvalancheMarket from "../markets/avalanche";
import FantomMarket from "../markets/fantom";
import PolygonMarket from "../markets/polygon";
import OptimisticConfig from "../markets/optimistic";
import ArbitrumConfig from "../markets/arbitrum";
import { isValidAddress } from "./utilities/utils";
import { AaveProtocolDataProvider } from "../typechain";
import {
ATOKEN_PREFIX,
STABLE_DEBT_PREFIX,
TESTNET_PRICE_AGGR_PREFIX,
TESTNET_REWARD_TOKEN_PREFIX,
TESTNET_TOKEN_PREFIX,
TREASURY_PROXY_ID,
VARIABLE_DEBT_PREFIX,
} from "./deploy-ids";
import { ZERO_ADDRESS } from "./constants";
import { getTestnetReserveAddressFromSymbol, POOL_DATA_PROVIDER } from ".";
import { ENABLE_REWARDS } from "./env";
declare var hre: HardhatRuntimeEnvironment;
export enum ConfigNames {
Commons = "Commons",
Aave = "Aave",
Test = "Test",
Harmony = "Harmony",
Avalanche = "Avalanche",
Fantom = "Fantom",
Polygon = "Polygon",
Optimistic = "Optimistic",
Arbitrum = "Arbitrum",
}
export const getParamPerNetwork = <T>(
param: iParamsPerNetwork<T> | undefined,
network: eNetwork
): T | undefined => {
if (!param) return undefined;
return param[network];
};
export const getRequiredParamPerNetwork = <T>(
poolConfig: PoolConfiguration,
key: keyof PoolConfiguration,
network: eNetwork
): T => {
const mapNetworkToValue = poolConfig[key] as iParamsPerNetwork<T>;
if (!mapNetworkToValue)
throw `[config] missing required parameter ${key} at market config`;
const value = mapNetworkToValue[network];
if (!value) throw `[config] missing required value at ${key}.${network}`;
return value;
};
export const getAddressFromConfig = (
param: iParamsPerNetwork<string | undefined>,
network: eNetwork,
key?: string
): tEthereumAddress => {
const value = getParamPerNetwork<tEthereumAddress | undefined>(
param,
network
);
if (!value || !isValidAddress(value)) {
throw Error(
`[aave-v3-deploy] Input parameter ${
key ? `"${key}"` : ""
} is missing or is not an address.`
);
}
return value;
};
export const loadPoolConfig = (configName: ConfigNames): PoolConfiguration => {
switch (configName) {
case ConfigNames.Aave:
return AaveMarket;
case ConfigNames.Test:
return AaveTestMarket;
case ConfigNames.Harmony:
return HarmonyMarket;
case ConfigNames.Avalanche:
return AvalancheMarket;
case ConfigNames.Fantom:
return FantomMarket;
case ConfigNames.Polygon:
return PolygonMarket;
case ConfigNames.Optimistic:
return OptimisticConfig;
case ConfigNames.Arbitrum:
return ArbitrumConfig;
default:
throw new Error(
`Unsupported pool configuration: ${configName} is not one of the supported configs ${Object.values(
ConfigNames
)}`
);
}
};
export const checkRequiredEnvironment = () => {
if (!process.env.MARKET_NAME) {
console.error(
`Skipping Market deployment due missing "MARKET_NAME" environment variable.`
);
return true;
}
return false;
};
export const savePoolTokens = async (
reservesConfig: ITokenAddress,
dataProviderAddress: tEthereumAddress
) => {
const dataProviderArtifact = await hre.deployments.get(POOL_DATA_PROVIDER);
const dataProvider = (await hre.ethers.getContractAt(
dataProviderArtifact.abi,
dataProviderAddress
)) as AaveProtocolDataProvider;
const aTokenArtifact = await hre.deployments.getExtendedArtifact("AToken");
const variableDebtTokenArtifact = await hre.deployments.getExtendedArtifact(
"VariableDebtToken"
);
const stableDebtTokenArtifact = await hre.deployments.getExtendedArtifact(
"StableDebtToken"
);
return Bluebird.each(Object.keys(reservesConfig), async (tokenSymbol) => {
const { aTokenAddress, variableDebtTokenAddress, stableDebtTokenAddress } =
await dataProvider.getReserveTokensAddresses(reservesConfig[tokenSymbol]);
await hre.deployments.save(`${tokenSymbol}${ATOKEN_PREFIX}`, {
address: aTokenAddress,
...aTokenArtifact,
});
await hre.deployments.save(`${tokenSymbol}${VARIABLE_DEBT_PREFIX}`, {
address: variableDebtTokenAddress,
...variableDebtTokenArtifact,
});
await hre.deployments.save(`${tokenSymbol}${STABLE_DEBT_PREFIX}`, {
address: stableDebtTokenAddress,
...stableDebtTokenArtifact,
});
});
};
export const getReserveAddresses = async (
poolConfig: IBaseConfiguration,
network: eNetwork
) => {
const isLive = hre.config.networks[network].live;
if (isLive && !poolConfig.TestnetMarket) {
console.log("[NOTICE] Using ReserveAssets from configuration file");
return getRequiredParamPerNetwork<ITokenAddress>(
poolConfig,
"ReserveAssets",
network
);
}
console.log(
"[WARNING] Using deployed Testnet tokens instead of ReserveAssets from configuration file"
);
const reservesKeys = Object.keys(poolConfig.ReservesConfig);
const allDeployments = await hre.deployments.all();
const testnetTokenKeys = Object.keys(allDeployments).filter(
(key) =>
key.includes(TESTNET_TOKEN_PREFIX) &&
reservesKeys.includes(key.replace(TESTNET_TOKEN_PREFIX, ""))
);
return testnetTokenKeys.reduce<ITokenAddress>((acc, key) => {
const symbol = key.replace(TESTNET_TOKEN_PREFIX, "");
acc[symbol] = allDeployments[key].address;
return acc;
}, {});
};
export const getSubTokensByPrefix = async (
prefix: string
): Promise<SubTokenOutput[]> => {
const allDeployments = await hre.deployments.all();
const tokenKeys = Object.keys(allDeployments).filter((key) =>
key.includes(prefix)
);
if (!tokenKeys.length) {
return [];
}
return tokenKeys.reduce<SubTokenOutput[]>((acc, key) => {
acc.push({
symbol: key.replace(prefix, ""),
artifact: allDeployments[key],
});
return acc;
}, []);
};
export const getSymbolsByPrefix = async (prefix: string): Promise<string[]> => {
const allDeployments = await hre.deployments.all();
const tokenKeys = Object.keys(allDeployments).filter((key) =>
key.includes(prefix)
);
if (!tokenKeys.length) {
return [];
}
return tokenKeys.reduce<string[]>((acc, key) => {
acc.push(key.replace(prefix, ""));
return acc;
}, []);
};
export const getChainlinkOracles = async (
poolConfig: IBaseConfiguration,
network: eNetwork
) => {
const isLive = hre.config.networks[network].live;
if (isLive) {
console.log("[NOTICE] Using ChainlinkAggregator from configuration file");
return getRequiredParamPerNetwork<ITokenAddress>(
poolConfig,
"ChainlinkAggregator",
network
);
}
console.log(
"[WARNING] Using deployed Mock Price Aggregators instead of ChainlinkAggregator from configuration file"
);
let rewardKeys: string[] = [];
if (isIncentivesEnabled(poolConfig)) {
rewardKeys = await getSymbolsByPrefix(TESTNET_REWARD_TOKEN_PREFIX);
}
const reservesKeys = Object.keys(poolConfig.ReservesConfig);
const allDeployments = await hre.deployments.all();
const testnetKeys = Object.keys(allDeployments).filter(
(key) =>
key.includes(TESTNET_PRICE_AGGR_PREFIX) &&
(reservesKeys.includes(key.replace(TESTNET_PRICE_AGGR_PREFIX, "")) ||
rewardKeys.includes(key.replace(TESTNET_PRICE_AGGR_PREFIX, "")))
);
return testnetKeys.reduce<ITokenAddress>((acc, key) => {
const symbol = key.replace(TESTNET_PRICE_AGGR_PREFIX, "");
acc[symbol] = allDeployments[key].address;
return acc;
}, {});
};
export const getTreasuryAddress = async (
poolConfig: IBaseConfiguration,
network: eNetwork
): Promise<tEthereumAddress> => {
const treasuryConfigAddress = getParamPerNetwork<string>(
poolConfig.ReserveFactorTreasuryAddress,
network
);
if (
treasuryConfigAddress &&
getAddress(treasuryConfigAddress) !== getAddress(ZERO_ADDRESS)
) {
return treasuryConfigAddress;
}
console.log(
"[WARNING] Using latest deployed Treasury proxy instead of ReserveFactorTreasuryAddress from configuration file"
);
const deployedTreasury = await hre.deployments.get(TREASURY_PROXY_ID);
return deployedTreasury.address;
};
export const isProductionMarket = (
poolConfig: ICommonConfiguration
): boolean => {
const network = (
process.env.FORK ? process.env.FORK : hre.network.name
) as eNetwork;
return hre.config.networks[network]?.live && !poolConfig.TestnetMarket;
};
export const isTestnetMarket = (poolConfig: ICommonConfiguration): boolean =>
!isProductionMarket(poolConfig);
export const getReserveAddress = async (
poolConfig: ICommonConfiguration,
symbol: string
) => {
const network = (
process.env.FORK ? process.env.FORK : hre.network.name
) as eNetwork;
let assetAddress = poolConfig.ReserveAssets?.[network]?.[symbol];
const isZeroOrNull = !assetAddress || assetAddress === ZERO_ADDRESS;
if (isZeroOrNull && isTestnetMarket(poolConfig)) {
return await getTestnetReserveAddressFromSymbol(symbol);
}
if (!assetAddress || isZeroOrNull) {
throw `Missing asset address for asset ${symbol}`;
}
return assetAddress;
};
export const getOracleByAsset = async (
poolConfig: ICommonConfiguration,
symbol: string
) => {
const network = (
process.env.FORK ? process.env.FORK : hre.network.name
) as eNetwork;
if (isTestnetMarket(poolConfig)) {
return (await hre.deployments.get(`${symbol}${TESTNET_PRICE_AGGR_PREFIX}`))
.address;
}
const oracleAddress = poolConfig.ChainlinkAggregator[network]?.[symbol];
if (!oracleAddress) {
throw `Missing oracle address for ${symbol}`;
}
return oracleAddress;
};
export const isL2PoolSupported = (poolConfig: ICommonConfiguration) => {
const network = (
process.env.FORK ? process.env.FORK : hre.network.name
) as eNetwork;
return !!getParamPerNetwork<boolean>(poolConfig.L2PoolEnabled, network);
};
export const getPrefixByAssetType = (assetType: AssetType) => {
switch (assetType) {
case AssetType.AToken:
return ATOKEN_PREFIX;
case AssetType.VariableDebtToken:
return VARIABLE_DEBT_PREFIX;
case AssetType.StableDebtToken:
return STABLE_DEBT_PREFIX;
}
};
export const isIncentivesEnabled = (poolConfig: ICommonConfiguration) => {
const network = (
process.env.FORK ? process.env.FORK : hre.network.name
) as eNetwork;
if (ENABLE_REWARDS !== undefined) {
return !!ENABLE_REWARDS;
}
return !!getParamPerNetwork(poolConfig.IncentivesConfig.enabled, network);
};