2020-07-13 08:54:08 +00:00
|
|
|
import {LendingPool} from '../../../types/LendingPool';
|
|
|
|
import {ReserveData, UserReserveData} from './interfaces';
|
2020-06-12 20:12:53 +00:00
|
|
|
import {
|
|
|
|
getLendingRateOracle,
|
|
|
|
getIErc20Detailed,
|
|
|
|
getMintableErc20,
|
2020-10-12 18:07:17 +00:00
|
|
|
getAToken,
|
|
|
|
getStableDebtToken,
|
|
|
|
getVariableDebtToken,
|
2020-07-13 08:54:08 +00:00
|
|
|
} from '../../../helpers/contracts-helpers';
|
|
|
|
import {tEthereumAddress} from '../../../helpers/types';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import {getDb, BRE} from '../../../helpers/misc-utils';
|
2020-10-12 18:07:17 +00:00
|
|
|
import {AaveProtocolTestHelpers} from '../../../types/AaveProtocolTestHelpers';
|
2020-06-12 20:12:53 +00:00
|
|
|
|
|
|
|
export const getReserveData = async (
|
2020-10-12 18:07:17 +00:00
|
|
|
helper: AaveProtocolTestHelpers,
|
2020-06-12 20:12:53 +00:00
|
|
|
reserve: tEthereumAddress
|
|
|
|
): Promise<ReserveData> => {
|
2020-09-14 17:25:45 +00:00
|
|
|
const [reserveData, tokenAddresses, rateOracle, token] = await Promise.all([
|
2020-10-12 18:07:17 +00:00
|
|
|
helper.getReserveData(reserve),
|
|
|
|
helper.getReserveTokensAddresses(reserve),
|
2020-09-14 17:25:45 +00:00
|
|
|
getLendingRateOracle(),
|
|
|
|
getIErc20Detailed(reserve),
|
|
|
|
]);
|
2020-06-12 20:12:53 +00:00
|
|
|
|
2020-09-14 17:25:45 +00:00
|
|
|
const stableDebtToken = await getStableDebtToken(tokenAddresses.stableDebtTokenAddress);
|
|
|
|
const variableDebtToken = await getVariableDebtToken(tokenAddresses.variableDebtTokenAddress);
|
|
|
|
|
2020-09-17 14:37:51 +00:00
|
|
|
const [principalStableDebt] = await stableDebtToken.getSupplyData();
|
2020-09-21 13:27:53 +00:00
|
|
|
const totalStableDebtLastUpdated = await stableDebtToken.getTotalSupplyLastUpdated();
|
|
|
|
|
2020-09-14 17:25:45 +00:00
|
|
|
const scaledVariableDebt = await variableDebtToken.scaledTotalSupply();
|
|
|
|
|
|
|
|
const rate = (await rateOracle.getMarketBorrowRate(reserve)).toString();
|
2020-08-13 11:06:23 +00:00
|
|
|
const symbol = await token.symbol();
|
|
|
|
const decimals = new BigNumber(await token.decimals());
|
2020-06-26 14:58:52 +00:00
|
|
|
|
2020-09-14 17:25:45 +00:00
|
|
|
const totalLiquidity = new BigNumber(reserveData.availableLiquidity.toString())
|
|
|
|
.plus(reserveData.totalStableDebt.toString())
|
|
|
|
.plus(reserveData.totalVariableDebt.toString());
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
const utilizationRate = new BigNumber(
|
|
|
|
totalLiquidity.eq(0)
|
|
|
|
? 0
|
2020-09-14 17:25:45 +00:00
|
|
|
: new BigNumber(reserveData.totalStableDebt.toString())
|
|
|
|
.plus(reserveData.totalVariableDebt.toString())
|
2020-07-13 08:54:08 +00:00
|
|
|
.rayDiv(totalLiquidity)
|
|
|
|
);
|
2020-06-26 14:58:52 +00:00
|
|
|
|
2020-06-12 20:12:53 +00:00
|
|
|
return {
|
2020-06-26 14:58:52 +00:00
|
|
|
totalLiquidity,
|
|
|
|
utilizationRate,
|
2020-09-14 17:25:45 +00:00
|
|
|
availableLiquidity: new BigNumber(reserveData.availableLiquidity.toString()),
|
|
|
|
totalStableDebt: new BigNumber(reserveData.totalStableDebt.toString()),
|
|
|
|
totalVariableDebt: new BigNumber(reserveData.totalVariableDebt.toString()),
|
|
|
|
liquidityRate: new BigNumber(reserveData.liquidityRate.toString()),
|
|
|
|
variableBorrowRate: new BigNumber(reserveData.variableBorrowRate.toString()),
|
|
|
|
stableBorrowRate: new BigNumber(reserveData.stableBorrowRate.toString()),
|
|
|
|
averageStableBorrowRate: new BigNumber(reserveData.averageStableBorrowRate.toString()),
|
|
|
|
liquidityIndex: new BigNumber(reserveData.liquidityIndex.toString()),
|
|
|
|
variableBorrowIndex: new BigNumber(reserveData.variableBorrowIndex.toString()),
|
|
|
|
lastUpdateTimestamp: new BigNumber(reserveData.lastUpdateTimestamp),
|
2020-09-21 13:27:53 +00:00
|
|
|
totalStableDebtLastUpdated: new BigNumber(totalStableDebtLastUpdated),
|
2020-09-14 17:25:45 +00:00
|
|
|
principalStableDebt: new BigNumber(principalStableDebt.toString()),
|
|
|
|
scaledVariableDebt: new BigNumber(scaledVariableDebt.toString()),
|
2020-06-12 20:12:53 +00:00
|
|
|
address: reserve,
|
2020-07-09 14:27:19 +00:00
|
|
|
aTokenAddress: tokenAddresses.aTokenAddress,
|
2020-06-12 20:12:53 +00:00
|
|
|
symbol,
|
|
|
|
decimals,
|
|
|
|
marketStableRate: new BigNumber(rate),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getUserData = async (
|
|
|
|
pool: LendingPool,
|
2020-10-12 18:07:17 +00:00
|
|
|
helper: AaveProtocolTestHelpers,
|
2020-06-12 20:12:53 +00:00
|
|
|
reserve: string,
|
2020-09-09 10:47:27 +00:00
|
|
|
user: tEthereumAddress,
|
|
|
|
sender?: tEthereumAddress
|
2020-06-12 20:12:53 +00:00
|
|
|
): Promise<UserReserveData> => {
|
2020-09-09 19:16:39 +00:00
|
|
|
const [userData, scaledATokenBalance] = await Promise.all([
|
2020-10-12 18:07:17 +00:00
|
|
|
helper.getUserReserveData(reserve, user),
|
|
|
|
getATokenUserData(reserve, user, helper),
|
2020-06-12 20:12:53 +00:00
|
|
|
]);
|
|
|
|
|
2020-08-13 11:06:23 +00:00
|
|
|
const token = await getMintableErc20(reserve);
|
2020-09-09 10:47:27 +00:00
|
|
|
const walletBalance = new BigNumber((await token.balanceOf(sender || user)).toString());
|
2020-06-12 20:12:53 +00:00
|
|
|
|
|
|
|
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()),
|
2020-07-07 15:14:44 +00:00
|
|
|
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()),
|
2020-06-12 20:12:53 +00:00
|
|
|
usageAsCollateralEnabled: userData.usageAsCollateralEnabled,
|
2020-07-03 21:20:02 +00:00
|
|
|
stableRateLastUpdated: new BigNumber(userData.stableRateLastUpdated.toString()),
|
2020-06-12 20:12:53 +00:00
|
|
|
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-10-12 18:07:17 +00:00
|
|
|
const getATokenUserData = async (
|
|
|
|
reserve: string,
|
|
|
|
user: string,
|
|
|
|
helpersContract: AaveProtocolTestHelpers
|
|
|
|
) => {
|
|
|
|
const aTokenAddress: string = (await helpersContract.getReserveTokensAddresses(reserve))
|
|
|
|
.aTokenAddress;
|
2020-06-12 20:12:53 +00:00
|
|
|
|
|
|
|
const aToken = await getAToken(aTokenAddress);
|
|
|
|
|
2020-09-09 19:16:39 +00:00
|
|
|
const scaledBalance = await aToken.scaledBalanceOf(user);
|
|
|
|
return scaledBalance.toString();
|
2020-06-12 20:12:53 +00:00
|
|
|
};
|