aave-protocol-v2/test/helpers/utils/helpers.ts

111 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-07-13 08:54:08 +00:00
import {LendingPool} from '../../../types/LendingPool';
import {ReserveData, UserReserveData} from './interfaces';
import {
getLendingRateOracle,
getIErc20Detailed,
getMintableErc20,
getAToken,
2020-07-13 08:54:08 +00:00
} from '../../../helpers/contracts-helpers';
import {ZERO_ADDRESS} from '../../../helpers/constants';
2020-07-13 08:54:08 +00:00
import {tEthereumAddress} from '../../../helpers/types';
import BigNumber from 'bignumber.js';
import {getDb, BRE} from '../../../helpers/misc-utils';
export const getReserveData = async (
pool: LendingPool,
reserve: tEthereumAddress
): Promise<ReserveData> => {
2020-08-19 12:23:41 +00:00
const data = await pool.getReserveData(reserve);
const tokenAddresses = await pool.getReserveTokensAddresses(reserve);
const rateOracle = await getLendingRateOracle();
const rate = (await rateOracle.getMarketBorrowRate(reserve)).toString();
const token = await getIErc20Detailed(reserve);
const symbol = await token.symbol();
const decimals = new BigNumber(await token.decimals());
2020-06-26 14:58:52 +00:00
2020-08-19 12:23:41 +00:00
const totalLiquidity = new BigNumber(data.availableLiquidity.toString())
.plus(data.totalStableDebt.toString())
.plus(data.totalVariableDebt.toString());
2020-07-13 08:54:08 +00:00
const utilizationRate = new BigNumber(
totalLiquidity.eq(0)
? 0
: new BigNumber(data.totalStableDebt.toString())
.plus(data.totalVariableDebt.toString())
2020-07-13 08:54:08 +00:00
.rayDiv(totalLiquidity)
);
2020-06-26 14:58:52 +00:00
return {
2020-06-26 14:58:52 +00:00
totalLiquidity,
utilizationRate,
2020-08-19 12:23:41 +00:00
availableLiquidity: new BigNumber(data.availableLiquidity.toString()),
totalStableDebt: new BigNumber(data.totalStableDebt.toString()),
totalVariableDebt: new BigNumber(data.totalVariableDebt.toString()),
2020-08-19 12:23:41 +00:00
liquidityRate: new BigNumber(data.liquidityRate.toString()),
variableBorrowRate: new BigNumber(data.variableBorrowRate.toString()),
stableBorrowRate: new BigNumber(data.stableBorrowRate.toString()),
averageStableBorrowRate: new BigNumber(data.averageStableBorrowRate.toString()),
liquidityIndex: new BigNumber(data.liquidityIndex.toString()),
variableBorrowIndex: new BigNumber(data.variableBorrowIndex.toString()),
lastUpdateTimestamp: new BigNumber(data.lastUpdateTimestamp),
address: reserve,
aTokenAddress: tokenAddresses.aTokenAddress,
symbol,
decimals,
marketStableRate: new BigNumber(rate),
};
};
export const getUserData = async (
pool: LendingPool,
reserve: string,
2020-09-09 10:47:27 +00:00
user: tEthereumAddress,
sender?: tEthereumAddress
): Promise<UserReserveData> => {
const [userData, scaledATokenBalance] = await Promise.all([
pool.getUserReserveData(reserve, user),
2020-06-20 23:40:03 +00:00
getATokenUserData(reserve, user, pool),
]);
const token = await getMintableErc20(reserve);
2020-09-09 10:47:27 +00:00
const walletBalance = new BigNumber((await token.balanceOf(sender || user)).toString());
return {
2020-09-08 11:45:24 +00:00
scaledATokenBalance: new BigNumber(scaledATokenBalance),
2020-06-30 12:09:28 +00:00
currentATokenBalance: new BigNumber(userData.currentATokenBalance.toString()),
currentStableDebt: new BigNumber(userData.currentStableDebt.toString()),
currentVariableDebt: new BigNumber(userData.currentVariableDebt.toString()),
principalStableDebt: new BigNumber(userData.principalStableDebt.toString()),
2020-09-10 11:05:02 +00:00
scaledVariableDebt: new BigNumber(userData.scaledVariableDebt.toString()),
2020-06-30 12:09:28 +00:00
stableBorrowRate: new BigNumber(userData.stableBorrowRate.toString()),
liquidityRate: new BigNumber(userData.liquidityRate.toString()),
usageAsCollateralEnabled: userData.usageAsCollateralEnabled,
2020-07-03 21:20:02 +00:00
stableRateLastUpdated: new BigNumber(userData.stableRateLastUpdated.toString()),
walletBalance,
};
};
export const getReserveAddressFromSymbol = async (symbol: string) => {
const token = await getMintableErc20(
(await getDb().get(`${symbol}.${BRE.network.name}`).value()).address
);
if (!token) {
throw `Could not find instance for contract ${symbol}`;
}
return token.address;
};
2020-07-13 08:54:08 +00:00
const getATokenUserData = async (reserve: string, user: string, pool: LendingPool) => {
const aTokenAddress: string = (await pool.getReserveTokensAddresses(reserve)).aTokenAddress;
const aToken = await getAToken(aTokenAddress);
const scaledBalance = await aToken.scaledBalanceOf(user);
return scaledBalance.toString();
};