Fixes borrow, repay, swap rate mode, rebalance tests

This commit is contained in:
The3D 2020-09-09 11:43:11 +02:00
parent 6454f040e8
commit b0084aaf33
4 changed files with 47 additions and 65 deletions

View File

@ -120,8 +120,6 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
uint256 currentBalance = balanceOf(user); uint256 currentBalance = balanceOf(user);
console.log("Amount is %s, balance is %s", amount, currentBalance);
require(amount <= currentBalance, Errors.INVALID_ATOKEN_BALANCE); require(amount <= currentBalance, Errors.INVALID_ATOKEN_BALANCE);
uint256 index = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS); uint256 index = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);

View File

@ -60,7 +60,7 @@ const almostEqualOrEqual = function (
this.assert(actual[key] != undefined, `Property ${key} is undefined in the actual data`); this.assert(actual[key] != undefined, `Property ${key} is undefined in the actual data`);
expect(expected[key] != undefined, `Property ${key} is undefined in the expected data`); expect(expected[key] != undefined, `Property ${key} is undefined in the expected data`);
if (!expected[key] || !actual[key]) { if (expected[key] == null || !actual[key] == null) {
console.log('Found a undefined value for Key ', key, ' value ', expected[key], actual[key]); console.log('Found a undefined value for Key ', key, ' value ', expected[key], actual[key]);
} }

View File

@ -49,32 +49,33 @@ export const calcExpectedUserDataAfterDeposit = (
expectedUserData.liquidityRate = reserveDataAfterAction.liquidityRate; expectedUserData.liquidityRate = reserveDataAfterAction.liquidityRate;
expectedUserData.scaledATokenBalance = calcExpectedScaledATokenBalance(
if (userDataBeforeAction.currentATokenBalance.eq(0)) { reserveDataAfterAction,
expectedUserData.usageAsCollateralEnabled = true; userDataBeforeAction,
} else { new BigNumber(amountDeposited),
//if the user is withdrawing everything, usageAsCollateralEnabled must be false new BigNumber(0)
if (expectedUserData.currentATokenBalance.eq(0)) { );
expectedUserData.usageAsCollateralEnabled = false;
} else {
expectedUserData.usageAsCollateralEnabled = userDataBeforeAction.usageAsCollateralEnabled;
}
}
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited);
expectedUserData.scaledATokenBalance = calcExpectedScaledATokenBalance(reserveDataAfterAction, userDataBeforeAction, new BigNumber(amountDeposited), new BigNumber(0));
expectedUserData.currentATokenBalance = calcExpectedATokenBalance( expectedUserData.currentATokenBalance = calcExpectedATokenBalance(
reserveDataBeforeAction, reserveDataBeforeAction,
userDataBeforeAction, userDataBeforeAction,
txTimestamp txTimestamp
).plus(amountDeposited); ).plus(amountDeposited);
if (userDataBeforeAction.currentATokenBalance.eq(0)) {
expectedUserData.usageAsCollateralEnabled = true;
} else {
expectedUserData.usageAsCollateralEnabled = userDataBeforeAction.usageAsCollateralEnabled;
}
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited);
expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance; expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance;
expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress; expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress;
expectedUserData.interestRedirectionIndex = userDataBeforeAction.interestRedirectionAddress == ZERO_ADDRESS ? new BigNumber(0) : reserveDataAfterAction.liquidityIndex; expectedUserData.interestRedirectionIndex =
userDataBeforeAction.interestRedirectionAddress == ZERO_ADDRESS
? new BigNumber(0)
: reserveDataAfterAction.liquidityIndex;
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance( expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction, userDataBeforeAction,
@ -119,14 +120,15 @@ export const calcExpectedUserDataAfterWithdraw = (
amountWithdrawn = aTokenBalance.toFixed(0); amountWithdrawn = aTokenBalance.toFixed(0);
} }
expectedUserData.scaledATokenBalance = calcExpectedScaledATokenBalance(reserveDataAfterAction, userDataBeforeAction, new BigNumber(0), new BigNumber(amountWithdrawn)); expectedUserData.scaledATokenBalance = calcExpectedScaledATokenBalance(
reserveDataAfterAction,
console.log("Scaled balance is ", expectedUserData.scaledATokenBalance.toFixed()); userDataBeforeAction,
new BigNumber(0),
expectedUserData.currentATokenBalance = aTokenBalance.minus( new BigNumber(amountWithdrawn)
amountWithdrawn
); );
expectedUserData.currentATokenBalance = aTokenBalance.minus(amountWithdrawn);
expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt; expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt;
expectedUserData.principalVariableDebt = userDataBeforeAction.principalVariableDebt; expectedUserData.principalVariableDebt = userDataBeforeAction.principalVariableDebt;
@ -166,7 +168,10 @@ export const calcExpectedUserDataAfterWithdraw = (
expectedUserData.interestRedirectionIndex = new BigNumber(0); expectedUserData.interestRedirectionIndex = new BigNumber(0);
} else { } else {
expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress; expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress;
expectedUserData.interestRedirectionIndex = userDataBeforeAction.interestRedirectionAddress == ZERO_ADDRESS ? new BigNumber(0) : reserveDataAfterAction.liquidityIndex; expectedUserData.interestRedirectionIndex =
userDataBeforeAction.interestRedirectionAddress == ZERO_ADDRESS
? new BigNumber(0)
: reserveDataAfterAction.liquidityIndex;
} }
expectedUserData.redirectionAddressRedirectedBalance = calcExpectedRedirectedBalance( expectedUserData.redirectionAddressRedirectedBalance = calcExpectedRedirectedBalance(
@ -176,7 +181,7 @@ export const calcExpectedUserDataAfterWithdraw = (
new BigNumber(0), new BigNumber(0),
new BigNumber(amountWithdrawn) new BigNumber(amountWithdrawn)
); );
return expectedUserData; return expectedUserData;
}; };
@ -658,9 +663,10 @@ export const calcExpectedUserDataAfterRepay = (
userDataBeforeAction, userDataBeforeAction,
txTimestamp txTimestamp
); );
expectedUserData.principalATokenBalance = userDataBeforeAction.principalATokenBalance; expectedUserData.scaledATokenBalance = userDataBeforeAction.scaledATokenBalance;
expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance; expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance;
expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress; expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress;
expectedUserData.interestRedirectionIndex = userDataBeforeAction.interestRedirectionIndex;
expectedUserData.redirectionAddressRedirectedBalance = expectedUserData.redirectionAddressRedirectedBalance =
userDataBeforeAction.redirectionAddressRedirectedBalance; userDataBeforeAction.redirectionAddressRedirectedBalance;
expectedUserData.currentATokenUserIndex = userDataBeforeAction.currentATokenUserIndex; expectedUserData.currentATokenUserIndex = userDataBeforeAction.currentATokenUserIndex;
@ -803,12 +809,6 @@ export const calcExpectedUserDataAfterSwapRateMode = (
txTimestamp txTimestamp
); );
expectedUserData.principalATokenBalance = userDataBeforeAction.principalATokenBalance;
expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance;
expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress;
expectedUserData.redirectionAddressRedirectedBalance =
userDataBeforeAction.redirectionAddressRedirectedBalance;
if (rateMode === RateMode.Stable) { if (rateMode === RateMode.Stable) {
// swap to variable // swap to variable
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = new BigNumber(0); expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = new BigNumber(0);
@ -954,12 +954,6 @@ export const calcExpectedUserDataAfterStableRateRebalance = (
userDataBeforeAction, userDataBeforeAction,
txTimestamp txTimestamp
); );
expectedUserData.scaledATokenBalance = userDataBeforeAction.scaledATokenBalance;
expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance;
expectedUserData.interestRedirectionAddress = userDataBeforeAction.interestRedirectionAddress;
expectedUserData.redirectionAddressRedirectedBalance =
userDataBeforeAction.redirectionAddressRedirectedBalance;
return expectedUserData; return expectedUserData;
}; };
@ -994,9 +988,7 @@ export const calcExpectedUsersDataAfterRedirectInterest = (
expectedFromData.stableBorrowRate = fromDataBeforeAction.stableBorrowRate; expectedFromData.stableBorrowRate = fromDataBeforeAction.stableBorrowRate;
expectedToData.stableBorrowRate = toDataBeforeAction.stableBorrowRate; expectedToData.stableBorrowRate = toDataBeforeAction.stableBorrowRate;
expectedFromData.scaledATokenBalance = expectedFromData.scaledATokenBalance = expectedFromData.currentATokenBalance = calcExpectedATokenBalance(
expectedFromData.currentATokenBalance = calcExpectedATokenBalance(
reserveDataBeforeAction, reserveDataBeforeAction,
fromDataBeforeAction, fromDataBeforeAction,
txTimestamp txTimestamp
@ -1034,16 +1026,16 @@ export const calcExpectedUsersDataAfterRedirectInterest = (
return [expectedFromData, expectedToData]; return [expectedFromData, expectedToData];
}; };
const calcExpectedScaledATokenBalance = ( const calcExpectedScaledATokenBalance = (
reserveDataAfterAction: ReserveData, reserveDataAfterAction: ReserveData,
userDataBeforeAction: UserReserveData, userDataBeforeAction: UserReserveData,
amountAdded: BigNumber, amountAdded: BigNumber,
amountTaken: BigNumber amountTaken: BigNumber
) => { ) => {
return userDataBeforeAction.scaledATokenBalance.plus(amountAdded.rayDiv(reserveDataAfterAction.liquidityIndex)).minus(amountTaken.rayDiv(reserveDataAfterAction.liquidityIndex)); return userDataBeforeAction.scaledATokenBalance
} .plus(amountAdded.rayDiv(reserveDataAfterAction.liquidityIndex))
.minus(amountTaken.rayDiv(reserveDataAfterAction.liquidityIndex));
};
const calcExpectedATokenBalance = ( const calcExpectedATokenBalance = (
reserveDataBeforeAction: ReserveData, reserveDataBeforeAction: ReserveData,
@ -1062,21 +1054,14 @@ const calcExpectedATokenBalance = (
if (scaledBalanceBeforeAction.eq(0) && redirectedBalance.eq(0)) { if (scaledBalanceBeforeAction.eq(0) && redirectedBalance.eq(0)) {
return new BigNumber(0); return new BigNumber(0);
} }
if (interestRedirectionAddress === ZERO_ADDRESS) {
return scaledBalanceBeforeAction
.plus(redirectedBalance)
.rayMul(index)
.minus(redirectedBalance);
}
const lastRedirectedBalance = scaledBalanceBeforeAction.rayDiv(redirectionIndexBeforeAction); if (interestRedirectionAddress === ZERO_ADDRESS) {
return scaledBalanceBeforeAction.plus(redirectedBalance).rayMul(index).minus(redirectedBalance);
return lastRedirectedBalance.plus( }
redirectedBalance
.rayMul(index) const lastRedirectedBalance = scaledBalanceBeforeAction.rayDiv(redirectionIndexBeforeAction);
.minus(redirectedBalance)
); return lastRedirectedBalance.plus(redirectedBalance.rayMul(index).minus(redirectedBalance));
}; };
const calcExpectedRedirectedBalance = ( const calcExpectedRedirectedBalance = (
@ -1086,7 +1071,6 @@ const calcExpectedRedirectedBalance = (
amountToAdd: BigNumber, amountToAdd: BigNumber,
amountToSubstract: BigNumber amountToSubstract: BigNumber
): BigNumber => { ): BigNumber => {
return expectedUserDataAfterAction.interestRedirectionAddress !== ZERO_ADDRESS return expectedUserDataAfterAction.interestRedirectionAddress !== ZERO_ADDRESS
? redirectedBalanceBefore.plus(amountToAdd).minus(amountToSubstract) ? redirectedBalanceBefore.plus(amountToAdd).minus(amountToSubstract)
: new BigNumber('0'); : new BigNumber('0');

View File

@ -12,7 +12,7 @@ BigNumber.config({DECIMAL_PLACES: 0, ROUNDING_MODE: BigNumber.ROUND_DOWN});
const scenarioFolder = './test/helpers/scenarios/'; const scenarioFolder = './test/helpers/scenarios/';
const selectedScenarios: string[] = []; const selectedScenarios: string[] = [''];
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;