aTokens balance gets updated for every block - implementation Aave error


So in the contract logic itself, I read that the aTokens balance gets updated for every block. How is this being implemented?

Answers 1

aToken balances are updated when the index (accrued interest for the entire pool of this reserve) is updated. The index update occurs whenever any user in the pool performs an action on this reserve: supply, borrow, etc..

When you call balanceOf on the aToken contract, it calls a function on the Pool contract to calculate the initial balance + income: AToken -> LendingPool -> ReserveLogic

Whenever you perform an action with aTokens such as transfer, withdraw, borrow, liquidate, etc. It checks the balance in this way and allows you to access the aTokens which you've accumulated.

By doing the update in this way, the aToken balance is increasing continuously, and the aTokens are allocated, not transferred to the users wallet;

The calculation of accrued interest is based on the liquidityIndex. Since the income is distributed across all depositors proportional to their balance, a single index is used to track the accumulated interest for everyone in the pool. The index and rate are updated on the pool contract for every deposit, borrow, withdrawal, repay of ANY Aave user. The update works like this:

Utilization Rate (borrows/deposits) -> Liquidity Rate -> Liquidity Index