Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize compound interest calculation #973

Open
daniel-savu opened this issue Mar 20, 2023 · 2 comments
Open

Optimize compound interest calculation #973

daniel-savu opened this issue Mar 20, 2023 · 2 comments
Labels
enhancement New feature or request prio:low
Milestone

Comments

@daniel-savu
Copy link
Contributor

daniel-savu commented Mar 20, 2023

Is your feature request related to a problem? Please describe.
The compound interest formula uses saturating_pow, which can iterate 32 times in the worst case. Lending interest is accrued by external pallets such as the vault-registry or oracle, so it's very important that the implementation is efficient.

Describe the solution you'd like
Aave uses a Taylor series to approximate the exponential and only computes the first three terms. We could use a similar approach, to reduce the function weight. See #758.

Context
Since the exponent is the seconds elapsed since the last accrual, one re-accrual per day takes 16 steps (log2(24*60*60)). One interaction every 10 days is 20 steps. So changing this is not very high priority. Re-accruing every 50 years will still not exceed the 32 exponent bits.

@daniel-savu daniel-savu added the enhancement New feature or request label Mar 20, 2023
@daniel-savu daniel-savu added this to the Lend / Borrow milestone Mar 20, 2023
@nud3l nud3l added this to Backlog Mar 20, 2023
@github-project-automation github-project-automation bot moved this to New 🆕 in Backlog Mar 20, 2023
@sander2
Copy link
Member

sander2 commented Mar 20, 2023

If compound is using simple interest, why don't we do the same?

@daniel-savu
Copy link
Contributor Author

Because the simple interest formula relies on interest being re-accrued very often, whereas the compound interest one will correctly compute interest no matter how often it is called. We switched to the latter based on the OpenZeppelin audit report of Compound v2: #901

@nud3l nud3l moved this from New 🆕 to Todo ⏳ in Backlog Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request prio:low
Projects
Status: Todo
Development

No branches or pull requests

2 participants