5

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.

3 Answers 3

1

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.

3
  • 1
    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, 2019 at 7:26
  • 1
    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. Apr 16, 2019 at 16:00
  • 2
    I think the key here is that the interest index is global (for all users) and is updated on every transaction that affects the rate. Aug 13, 2021 at 15:43
1

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!

2
  • When you say "let's say he repays the full 50 DAI plus 1 DAI of interest that has accrued" -- well, how do we know 1 DAI has accrued? The accrued interest actually depends on the history of interest rates. As you say "the real rate depends on system parameters". These change in various ways over time. As mentioned in Rob's comment to his answer, the CToken tracks an "interest index", which is global and adjusted on every transaction affecting cash, reserves, etc. Aug 13, 2021 at 15:54
  • for v3. Interest accrues every second using the block timestamp. what would be the changes with the model? Dec 30, 2023 at 7:33
1

Another answerer (Rob Hitchens) has basically hit upon the mechanism in his answer + comments, but I think it's beneficial here to dig into the Compound whitepaper. Although the details of the interest rate models have changed, the high-level concepts still apply to how the CToken functions.

When system parameters change, this will adjust the interest rate, and since it applies to all borrowers, the history of interest rates must be "tracked" somehow.

The whitepaper explains:

The history of each interest rate, for each money market, is captured by an Interest Rate Index , which is calculated each time an interest rate changes, resulting from a user minting, redeeming, borrowing, repaying or liquidating the asset.

In particular, on each transaction, we find that the index is updated, but the accrued interest is also accumulated onto the total borrows and reserves:

Each time a transaction occurs, the Interest Rate Index for the asset is updated to compound the interest since the prior index, using the interest for the period, denominated by r * t, calculated using a per-block interest rate [...]

The market’s total borrowing outstanding is updated to include interest accrued since the last index [...]

And a portion of the accrued interest is retained (set aside) as reserves[...]

Then it becomes simple to track how much a borrower owes (including the accrued interest) by taking snapshots of (balance, interest index) whenever the borrower takes an action.

Note that the exchange rate between a CToken and its underlyer token does depend on quantities involving accrued interest.

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.