2

On every action from LendingPool (deposit, withdraw, repay, borrow, etc), Aave updates its entire state for certain variables, including the aToken balances of each user holding aTokens.

How can Aave pay the gas when writing to storage when updating the balances with the interest accrued of millions of users in a constant basis?

I read somewhere that it's the user who pays for this, but I don't see a single user paying the state modifications of millions of other people in just one transaction. And the balances update is a constant process that happens even when you're not interacting with the protocol.

From ReserveLogic.sol:

function updateState(DataTypes.ReserveData storage reserve) internal {
    uint256 scaledVariableDebt =
      IVariableDebtToken(reserve.variableDebtTokenAddress).scaledTotalSupply();
    uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex;
    uint256 previousLiquidityIndex = reserve.liquidityIndex;
    uint40 lastUpdatedTimestamp = reserve.lastUpdateTimestamp;

    (uint256 newLiquidityIndex, uint256 newVariableBorrowIndex) =
      _updateIndexes( //---> writes to storage
        reserve,
        scaledVariableDebt,
        previousLiquidityIndex,
        previousVariableBorrowIndex,
        lastUpdatedTimestamp
      );

    _mintToTreasury( //---> mints the accrued interest
      reserve,
      scaledVariableDebt,
      previousVariableBorrowIndex,
      newLiquidityIndex,
      newVariableBorrowIndex,
      lastUpdatedTimestamp
    );
  }

Line derived from each interaction with LendingPool:

reserve.updateState();

1 Answer 1

2

Aave employs the concept of an "index" - it's not that the protocol writes to the storage of all user accounts, but that the index gets updated and this is paid for by users. Later on, whoever queries the "balanceOf" function (which is free because is constant) will use the latest index to calculate the given user's balance.

This is actually not unique to Aave. Many DeFi protocols employ similar strategies. Take a look at Maker for example, whose Rates Module docs should be a good read for you.

1
  • Thanks for the explanation and the article! The combination was perfect.
    – dNyrM
    Oct 19, 2021 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.