Share Dialog

Compounding interest is the interest on a loan calculated based on the borrowed amount and the accumulated interest since the loan was taken. What does this concretely mean?
If I borrow 100 with a 2% yearly interest rate, after a year my debt should be 102, but after 2 years it should be 102 * (1.02) = 104.04.
Here the .04 correspond to the interest paid on my debt increase because of interest.
The exact formula to compound an interest rate r over a period of time n on a loan of 100 is:

In Solidity, the unit of time is seconds, and so interest rates stored in smart contracts are generally not per year interest rates but rather per second interest rates.
This means that if the “per” second interest rate is r, my compounded debt after a year should be:

While in stablecoin protocols like Angle and Maker interest rates are constant, in lending protocols it varies almost every time someone takes an action on the protocol (by like lending/borrowing and moving the utilization curve). So the formula looks more like:

For both stablecoin and lending protocols, the tricky and complex thing to compute is this (1+r)^n. This is expensive to get on-chain since power/exponentiation is something which is not well handled by Solidity as it requires a lot of operations.
How do protocols cope with that?
Maker sticks as much as possible to the formula. They use a repeated squaring algorithm written in Assembly.
Pros: it’s super accurate
Cons: It involves a loop and as such it’s gas inefficient

Aave on its end relies on a binomial approximation.
They leverage the fact that when r is small:

For more info: https://en.wikipedia.org/wiki/Binomial_approximation
Aave limits itself to the third term, which is enough to be accurate. It in fact slightly undercharges borrowers, but at the benefit of a great gas cost reduction.

When working on Angle Borrowing module, we put up some simulations for this.
On a $1m borrow with a 2% interest rate, the difference with what you’d get with the real formula is after 1 year just $0.06 and after 2 years $0.1.
For full details, you can check: https://docs.google.com/spreadsheets/d/1AOHsr0ZyOKBsq78tthDE4VhDdWAu4n3iKqiRQ9H-SWA/edit?usp=sharing
Actually, we pushed the simulation to the 4th order to see the gain in accuracy, and it gets really negligible at this point: so Aave did a great job optimizing for the gas vs. accuracy trade-off.
Practically speaking, in terms of gas, Aave solution costs 5k gas less than Maker (a 1$ price diff when gas price is 100 Gwei), which makes Aave’s solution both accurate and also cheap to compute. It effectively relies on an approximation but who cares when parameters can be set accordingly by governance?
Compound uses the same approximation as Aave, but only goes to the first order. This means that for them:

This is of course super cheap to compute (2k gas less than Aave, $0.5 tx cost difference when gas price is 100 Gwei), but interest are not compounded at all, which is kind of odd for a protocol called Compound…

Consequence is that borrowers are more undercharged than on Aave, which means among other things less money for lenders.
On a $1m loan with a 2% interest, after 1 year borrowers spend $197 less than what they should if interest were well compounded. After 2 years this makes a $794 diff.
Aave’s solution is definitely the one I prefer, it optimizes between accuracy and efficiency. It is for this reason that we adopted it for Angle Borrowing module.
Main takeaways for me are that there is no “good” or “bad” way to compound interest rates (even if Compound could have worked a bit more), and that the interest rates you see are never exact, especially for long periods of time.
It’s interesting to see that these 3 major protocols use different approaches. And what matters overall in the end is that people are well informed to take the right decisions.
If my protocol slightly undercharges borrowers, then I should probably vote for higher borrowing rates than what I’ll naturally set.
If I lend on Compound, then I should know that lenders will receive less from borrowers than on Aave for similar borrowing APY + reserve factors.
No comments yet