Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimisation of the balance/debt increase math #707

Open
wants to merge 1 commit into
base: feat/3.0.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions contracts/protocol/libraries/logic/ReserveLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,6 @@ library ReserveLogic {
);
}

struct AccrueToTreasuryLocalVars {
uint256 prevTotalStableDebt;
uint256 prevTotalVariableDebt;
uint256 currTotalVariableDebt;
uint256 cumulatedStableInterest;
uint256 totalDebtAccrued;
uint256 amountToMint;
}

/**
* @notice Mints part of the repaid interest to the reserve treasury as a function of the reserve factor for the
* specific asset.
Expand All @@ -230,47 +221,35 @@ library ReserveLogic {
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
AccrueToTreasuryLocalVars memory vars;

if (reserveCache.reserveFactor == 0) {
return;
}

//calculate the total variable debt at moment of the last interaction
vars.prevTotalVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
reserveCache.currVariableBorrowIndex
);

//calculate the new total variable debt after accumulation of the interest on the index
vars.currTotalVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
reserveCache.nextVariableBorrowIndex
// calculate the total variable debt increase after the moment of the last interaction
uint256 variableDebtIncrease = reserveCache.currScaledVariableDebt.rayMul(
reserveCache.nextVariableBorrowIndex - reserveCache.currVariableBorrowIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

);

//calculate the stable debt until the last timestamp update
vars.cumulatedStableInterest = MathUtils.calculateCompoundedInterest(
uint256 cumulatedStableInterest = MathUtils.calculateCompoundedInterest(
reserveCache.currAvgStableBorrowRate,
reserveCache.stableDebtLastUpdateTimestamp,
reserveCache.reserveLastUpdateTimestamp
);

vars.prevTotalStableDebt = reserveCache.currPrincipalStableDebt.rayMul(
vars.cumulatedStableInterest
uint256 prevTotalStableDebt = reserveCache.currPrincipalStableDebt.rayMul(
cumulatedStableInterest
);

//debt accrued is the sum of the current debt minus the sum of the debt at the last update
vars.totalDebtAccrued =
vars.currTotalVariableDebt +
uint256 totalDebtAccrued = variableDebtIncrease +
reserveCache.currTotalStableDebt -
vars.prevTotalVariableDebt -
vars.prevTotalStableDebt;
prevTotalStableDebt;

vars.amountToMint = vars.totalDebtAccrued.percentMul(reserveCache.reserveFactor);
uint256 amountToMint = totalDebtAccrued.percentMul(reserveCache.reserveFactor);

if (vars.amountToMint != 0) {
reserve.accruedToTreasury += vars
.amountToMint
.rayDiv(reserveCache.nextLiquidityIndex)
.toUint128();
if (amountToMint != 0) {
reserve.accruedToTreasury += amountToMint.rayDiv(reserveCache.nextLiquidityIndex).toUint128();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ abstract contract ScaledBalanceTokenBase is MintableIncentivizedERC20, IScaledBa
require(amountScaled != 0, Errors.INVALID_MINT_AMOUNT);

uint256 scaledBalance = super.balanceOf(onBehalfOf);
uint256 balanceIncrease = scaledBalance.rayMul(index) -
scaledBalance.rayMul(_userState[onBehalfOf].additionalData);

uint256 balanceIncrease = scaledBalance.rayMul(index - _userState[onBehalfOf].additionalData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is not exactly equivalent to the other because of math rounding and precision. We should stick to the original approach

_userState[onBehalfOf].additionalData = index.toUint128();

_mint(onBehalfOf, amountScaled.toUint128());
Expand Down Expand Up @@ -109,8 +107,7 @@ abstract contract ScaledBalanceTokenBase is MintableIncentivizedERC20, IScaledBa
require(amountScaled != 0, Errors.INVALID_BURN_AMOUNT);

uint256 scaledBalance = super.balanceOf(user);
uint256 balanceIncrease = scaledBalance.rayMul(index) -
scaledBalance.rayMul(_userState[user].additionalData);
uint256 balanceIncrease = scaledBalance.rayMul(index - _userState[user].additionalData);

_userState[user].additionalData = index.toUint128();

Expand Down