# How could Compound's interest accruing every block? How's that implemented?

Here I'm referring to https://compound.finance, which is quite an awesome product.

One thing inspires me most is that the interests accrued in Compound are updated per block. How's that even possible?

From its FAQ (https://medium.com/compound-finance/faq-1a2636713b69):

How often is interest calculated?

The interest rates you see in the Interface and Market Overview are quoted as annual interest rates. Interest accrues each Ethereum block; every ~15 seconds, your balance will increase by (1/2102400) of the quoted interest rate. Really!

I still couldn't figure out how to do that, as I'm seeing exactly it works even without frontend UI - by setting `requestedAmount` to be 0xff...ff.

Can someone please educate me on this? Thanks.

In Compound V2, borrow interest is paid back to the cToken contract such that all cToken holders have proportional claims to the underlying collateral at the current time. It's somewhat confusing based on the UI that Compound has provided, but basically the history of interest rates is irrelevant.

Lenders "mint" cTokens (e.g. cDAI) by locking up the underlying tokens (e.g. DAI). Each cToken can, at any time, theoretically claim 1/N of the total underlying collateral, where N is the total number of cTokens minted.

As an example, consider a [very simple] system where Alice is the only lender and the cDAI rate is 1:1. (NOTE: These are not paramters you would encounter in practice and are just for example). Here is the lifecycle of a loan:

1. Alice mints 100 cDAI with 100 DAI1. At this point, each cDAI is worth 1 DAI.

2. Bob borrows 50 DAI at some interest rate. This must be collateralized with some other compound token, e.g. cETH. Even though he has taken 50 DAI from the system, his debt is still counted in the total assets under management, so each cDAI should still be worth 1 DAI. Note that there is an obvious liquidity constraint here, which is that Alice right now may only redeem 50 cDAI (and receive 50 DAI in return). If she wants to redeem more cDAI, she must wait for Bob to repay his DAI debt.

3. Bob eventually repays some or all of his loan. For simplicity's sake, let's say he repays the full 50 DAI plus 1 DAI of interest that has accrued. Thus, there is now 101 DAI in the system, which means that now each cDAI is worth 1.01 DAI. At this point, Alice may redeem her full 100 cDAI and receive 101 DAI of collateral.

As you can see, we are not interested in the historical interest rates, only in the amount of underlying collateral we can claim with our cTokens, which were minted at a time when they were less valuable. The general thesis of this sytem is that borrow interest causes the cTokens to become more valuable over time, regardless of the historical interest rates (which are really just relative measurements of how much demand there is for a particular asset).

1Again, the 1:1 rate is not realistic. The real rate depends on system parameters, which I believe remain constant until an administrator decides to change them. IIRC they are determined based on the liqudity and volatility of the token, which are meant to safeguard against big liquidation events -- outside the scope of this post!

It seems daunting bordering on impossible if you imagine a balances database updated every fifteen seconds.

That isn't necessary. If one has a principle amount, a rate, and elapsed time periods, one can calculate interest and present balance.

Hope it helps.

• Hi Rob, you're right. But the thing is that if someone deposit or withdrawal some assest, then the interest rate for that particular asset changes, and the changed interest rate would affect all ppl who supplied / borrowed. - It seems Compound still works for all who are affected, I'm wondering how's that handled – nrek Apr 16 '19 at 7:26
• Contract source code: etherscan.io/address/… There is a lot to wade through, but you might start around 921 `function calculateInterestIndex(`. From eyeballing it superficially, they are using the principle described. – Rob Hitchens Apr 16 '19 at 16:00