mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Updated borrow calculations on scenarios
This commit is contained in:
parent
7986a4704b
commit
5b38bb144b
|
@ -185,7 +185,6 @@ export const calcExpectedReserveDataAfterDeposit = (
|
||||||
expectedReserveData.variableBorrowIndex
|
expectedReserveData.variableBorrowIndex
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
|
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
|
||||||
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
|
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
|
||||||
|
|
||||||
|
@ -246,8 +245,14 @@ export const calcExpectedReserveDataAfterWithdraw = (
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(reserveDataBeforeAction, txTimestamp);
|
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
|
||||||
expectedReserveData.totalVariableDebt = calcExpectedTotalVariableDebt(reserveDataBeforeAction, expectedReserveData.variableBorrowIndex);
|
reserveDataBeforeAction,
|
||||||
|
txTimestamp
|
||||||
|
);
|
||||||
|
expectedReserveData.totalVariableDebt = calcExpectedTotalVariableDebt(
|
||||||
|
reserveDataBeforeAction,
|
||||||
|
expectedReserveData.variableBorrowIndex
|
||||||
|
);
|
||||||
|
|
||||||
expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate;
|
expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate;
|
||||||
|
|
||||||
|
@ -295,60 +300,85 @@ export const calcExpectedReserveDataAfterBorrow = (
|
||||||
);
|
);
|
||||||
|
|
||||||
if (borrowRateMode == RateMode.Stable) {
|
if (borrowRateMode == RateMode.Stable) {
|
||||||
|
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
|
||||||
|
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.scaledVariableDebt.rayMul(
|
||||||
|
expectedReserveData.variableBorrowIndex
|
||||||
|
);
|
||||||
|
|
||||||
//if the borrow rate mode
|
const totalStableDebtUntilTx = calcExpectedTotalStableDebt(
|
||||||
|
reserveDataBeforeAction,
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
txTimestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
|
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
|
||||||
reserveDataBeforeAction.averageStableBorrowRate,
|
reserveDataBeforeAction.averageStableBorrowRate,
|
||||||
reserveDataBeforeAction.totalStableDebt.plus(debtAccrued),
|
totalStableDebtUntilTx,
|
||||||
amountBorrowedBN,
|
amountBorrowedBN,
|
||||||
reserveDataBeforeAction.stableBorrowRate
|
reserveDataBeforeAction.stableBorrowRate
|
||||||
);
|
);
|
||||||
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt;
|
|
||||||
} else {
|
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
|
||||||
const variableDebtBefore = userDataBeforeAction.scaledVariableDebt.rayMul(
|
{
|
||||||
reserveDataBeforeAction.variableBorrowIndex
|
...reserveDataBeforeAction,
|
||||||
|
principalStableDebt: totalStableDebtUntilTx.plus(amountBorrowedBN),
|
||||||
|
averageStableBorrowRate: expectedReserveData.averageStableBorrowRate,
|
||||||
|
},
|
||||||
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
const debtAccrued = userVariableDebt.minus(variableDebtBefore);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.availableLiquidity
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
.minus(amountBorrowedBN)
|
||||||
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt
|
.plus(expectedReserveData.totalVariableDebt)
|
||||||
.plus(amountBorrowedBN)
|
.plus(expectedReserveData.totalStableDebt);
|
||||||
.plus(debtAccrued);
|
} else {
|
||||||
expectedReserveData.totalStableDebt = reserveDataBeforeAction.totalStableDebt;
|
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
|
||||||
expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate;
|
expectedReserveData.totalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
|
userDataBeforeAction,
|
||||||
|
currentTimestamp
|
||||||
|
);
|
||||||
|
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus(
|
||||||
|
amountBorrowedBN.rayDiv(expectedReserveData.variableBorrowIndex)
|
||||||
|
);
|
||||||
|
const totalVariableDebtAfterTx = reserveDataBeforeAction.scaledVariableDebt.rayMul(
|
||||||
|
expectedReserveData.variableBorrowIndex
|
||||||
|
);
|
||||||
|
|
||||||
|
const rates = calcExpectedInterestRates(
|
||||||
|
reserveDataBeforeAction.symbol,
|
||||||
|
reserveDataBeforeAction.marketStableRate,
|
||||||
|
expectedReserveData.utilizationRate,
|
||||||
|
expectedReserveData.totalStableDebt,
|
||||||
|
totalVariableDebtAfterTx,
|
||||||
|
expectedReserveData.averageStableBorrowRate
|
||||||
|
);
|
||||||
|
|
||||||
|
expectedReserveData.liquidityRate = rates[0];
|
||||||
|
|
||||||
|
expectedReserveData.stableBorrowRate = rates[1];
|
||||||
|
|
||||||
|
expectedReserveData.variableBorrowRate = rates[2];
|
||||||
|
|
||||||
|
expectedReserveData.lastUpdateTimestamp = txTimestamp;
|
||||||
|
|
||||||
|
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
|
||||||
|
calcExpectedReserveNormalizedDebt(expectedReserveData, currentTimestamp)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus(
|
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus(
|
||||||
amountBorrowedBN
|
amountBorrowedBN
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity
|
||||||
|
.plus(expectedReserveData.totalStableDebt)
|
||||||
|
.plus(expectedReserveData.totalVariableDebt);
|
||||||
|
|
||||||
expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
|
expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
|
||||||
expectedReserveData.totalStableDebt,
|
expectedReserveData.totalStableDebt,
|
||||||
expectedReserveData.totalVariableDebt,
|
expectedReserveData.totalVariableDebt,
|
||||||
expectedReserveData.totalLiquidity
|
expectedReserveData.totalLiquidity
|
||||||
);
|
);
|
||||||
|
|
||||||
const rates = calcExpectedInterestRates(
|
|
||||||
reserveDataBeforeAction.symbol,
|
|
||||||
reserveDataBeforeAction.marketStableRate,
|
|
||||||
expectedReserveData.utilizationRate,
|
|
||||||
expectedReserveData.totalStableDebt,
|
|
||||||
expectedReserveData.totalVariableDebt,
|
|
||||||
expectedReserveData.averageStableBorrowRate
|
|
||||||
);
|
|
||||||
expectedReserveData.liquidityRate = rates[0];
|
|
||||||
|
|
||||||
expectedReserveData.stableBorrowRate = rates[1];
|
|
||||||
|
|
||||||
expectedReserveData.variableBorrowRate = rates[2];
|
|
||||||
|
|
||||||
expectedReserveData.lastUpdateTimestamp = txTimestamp;
|
|
||||||
|
|
||||||
return expectedReserveData;
|
return expectedReserveData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1229,7 +1259,6 @@ const calcExpectedVariableBorrowIndex = (reserveData: ReserveData, timestamp: Bi
|
||||||
};
|
};
|
||||||
|
|
||||||
const calcExpectedTotalStableDebt = (reserveData: ReserveData, timestamp: BigNumber) => {
|
const calcExpectedTotalStableDebt = (reserveData: ReserveData, timestamp: BigNumber) => {
|
||||||
|
|
||||||
const cumulatedInterest = calcCompoundedInterest(
|
const cumulatedInterest = calcCompoundedInterest(
|
||||||
reserveData.averageStableBorrowRate,
|
reserveData.averageStableBorrowRate,
|
||||||
timestamp,
|
timestamp,
|
||||||
|
@ -1237,9 +1266,11 @@ const calcExpectedTotalStableDebt = (reserveData: ReserveData, timestamp: BigNum
|
||||||
);
|
);
|
||||||
|
|
||||||
return cumulatedInterest.rayMul(reserveData.principalStableDebt);
|
return cumulatedInterest.rayMul(reserveData.principalStableDebt);
|
||||||
}
|
};
|
||||||
|
|
||||||
const calcExpectedTotalVariableDebt = (reserveData: ReserveData, expectedVariableDebtIndex: BigNumber) => {
|
|
||||||
|
|
||||||
|
const calcExpectedTotalVariableDebt = (
|
||||||
|
reserveData: ReserveData,
|
||||||
|
expectedVariableDebtIndex: BigNumber
|
||||||
|
) => {
|
||||||
return reserveData.scaledVariableDebt.rayMul(expectedVariableDebtIndex);
|
return reserveData.scaledVariableDebt.rayMul(expectedVariableDebtIndex);
|
||||||
}
|
};
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine';
|
||||||
|
|
||||||
const scenarioFolder = './test/helpers/scenarios/';
|
const scenarioFolder = './test/helpers/scenarios/';
|
||||||
|
|
||||||
const selectedScenarios: string[] = ['borrow-repay-variable.json'];
|
const selectedScenarios: string[] = ['deposit.json'];
|
||||||
|
|
||||||
fs.readdirSync(scenarioFolder).forEach((file) => {
|
fs.readdirSync(scenarioFolder).forEach((file) => {
|
||||||
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;
|
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user