aave-protocol-v2/helpers/init-helpers.ts

94 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-08-21 11:07:32 +00:00
import {iMultiPoolsAssets, IReserveParams, tEthereumAddress} from './types';
import {LendingPool} from '../types/LendingPool';
import {LendingPoolConfigurator} from '../types/LendingPoolConfigurator';
2020-10-13 11:41:57 +00:00
import {AaveProtocolTestHelpers} from '../types/AaveProtocolTestHelpers';
2020-10-21 09:42:27 +00:00
import {waitForTx} from './misc-utils';
2020-08-21 11:07:32 +00:00
export const enableReservesToBorrow = async (
reservesParams: iMultiPoolsAssets<IReserveParams>,
tokenAddresses: {[symbol: string]: tEthereumAddress},
2020-10-13 11:41:57 +00:00
helpers: AaveProtocolTestHelpers,
2020-08-21 11:07:32 +00:00
lendingPoolConfigurator: LendingPoolConfigurator
) => {
for (const [assetSymbol, {borrowingEnabled, stableBorrowRateEnabled}] of Object.entries(
reservesParams
) as [string, IReserveParams][]) {
if (!borrowingEnabled) continue;
try {
const assetAddressIndex = Object.keys(tokenAddresses).findIndex(
(value) => value === assetSymbol
);
const [, tokenAddress] = (Object.entries(tokenAddresses) as [string, string][])[
assetAddressIndex
];
2020-10-13 11:41:57 +00:00
const {borrowingEnabled: borrowingAlreadyEnabled} = await helpers.getReserveConfigurationData(
tokenAddress
);
2020-08-21 11:07:32 +00:00
if (borrowingAlreadyEnabled) {
console.log(`Reserve ${assetSymbol} is already enabled for borrowing, skipping`);
continue;
}
2020-10-21 11:35:52 +00:00
console.log('Enabling borrowing on reserve ', assetSymbol);
2020-10-21 09:42:27 +00:00
await waitForTx(
await lendingPoolConfigurator.enableBorrowingOnReserve(
tokenAddress,
stableBorrowRateEnabled
)
);
2020-08-21 11:07:32 +00:00
} catch (e) {
console.log(
`Enabling reserve for borrowings for ${assetSymbol} failed with error ${e}. Skipped.`
);
}
}
};
export const enableReservesAsCollateral = async (
reservesParams: iMultiPoolsAssets<IReserveParams>,
tokenAddresses: {[symbol: string]: tEthereumAddress},
2020-10-13 11:41:57 +00:00
helpers: AaveProtocolTestHelpers,
2020-08-21 11:07:32 +00:00
lendingPoolConfigurator: LendingPoolConfigurator
) => {
for (const [
assetSymbol,
{baseLTVAsCollateral, liquidationBonus, liquidationThreshold},
] of Object.entries(reservesParams) as [string, IReserveParams][]) {
if (baseLTVAsCollateral === '-1') continue;
const assetAddressIndex = Object.keys(tokenAddresses).findIndex(
(value) => value === assetSymbol
);
const [, tokenAddress] = (Object.entries(tokenAddresses) as [string, string][])[
assetAddressIndex
];
2020-10-13 11:41:57 +00:00
const {usageAsCollateralEnabled: alreadyEnabled} = await helpers.getReserveConfigurationData(
tokenAddress
);
2020-08-21 11:07:32 +00:00
if (alreadyEnabled) {
console.log(`Reserve ${assetSymbol} is already enabled as collateral, skipping`);
continue;
}
try {
2020-10-21 11:35:52 +00:00
console.log(`Enabling reserve ${assetSymbol} as collateral`);
2020-10-21 09:42:27 +00:00
await waitForTx(
await lendingPoolConfigurator.enableReserveAsCollateral(
tokenAddress,
baseLTVAsCollateral,
liquidationThreshold,
liquidationBonus
)
2020-08-21 11:07:32 +00:00
);
} catch (e) {
console.log(
`Enabling reserve as collateral for ${assetSymbol} failed with error ${e}. Skipped.`
);
}
}
};