Mint debt token

Overview

  • If stable interest was selected -> mint stable debt tokens

  • Else -> mint variable debt tokens

mint stableDebtTokens

rate: reserve.currentStableBorrowRate

_calculateBalanceIncrease

  • For a user, calculates the increase in stable debt, due to interest accrued from timestamp of their last update, to current time.

  • Serves to update user's prior stable debt

balanceIncrease reflects interest accrued from lastUpdate till current block.timestamp.

Does not relate to incoming borrow action.

totalSupply

    vars.previousSupply = totalSupply();
    vars.currentAvgStableRate = _avgStableRate;
    vars.nextSupply = _totalSupply = vars.previousSupply + amount;

vars.previousSupply = totalSupply();

totalSupply returns the updated supply value, by accounting for recently accrued interest, from _totalSupplyTimestamp till now, based on the _avgStableRate.

  • _avgStableRate: internal storage variable on StableDebtToken.sol

  • previousSupply: updated supply value

  • principalSupply => super.totalSupply calls totalSupply on IncentivizedERC20 which returns _totalSupply

previousSupply is set to totalSupply() instead of _totalSupply directly, so that the updated supply value can be obtained.

With recent interest accounted for, the storage variable _totalSupply is updated with the incoming stable debt, amount.

vars.nextSupply = _totalSupply = 
    vars.previousSupply + amount;

Calculate new stable rate

  • nextStableRate is new stable rate, specific to the user

nextStableRate = 
(_avgStableRate * currentBalance) + (amount * rate / currentBalance + amount)
  • nextStableRate stored in the user's UserState.additionalData struct. userState mapping collects all users' UserState structs.

  • Store update time in _timestamps._timestamps mapping collects the last update timestamp for all users.

  • Update the timestamp of the last update of _totalSupply.

rate: reserve.currentStableBorrowRate

Update average Stable rate

  • _avgStableRate: internal uint128 on StableDebtToken.sol

  • vars.currentAvgStableRate was set to _avgStableRate at the start of mint

newAvgStableRate = 
(_avgStableRate * previousSupply) + [(rate * amountInRay) / nextSupply]
  • previousSupply: updatedtotalSupply

mint tokens

Notice that we do not just mint as per the incoming stable debt amount; but also mint for balanceIncrease. This serves to update user's prior stable debt based on interest accrued over time elapsed since last update.

  • Since we going to mint, mint to update the user's entire position

mint calls the internal function _mint:

mint variableDebtTokens

  • amountScaled: scale the borrow amount against variableBorrowIndex

  • See variableDebtToken mint

Last updated