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

Switch to linear trailing APRs #30

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Switch to linear trailing APRs
kexleyBeefy committed Jun 20, 2024

Verified

This commit was signed with the committer’s verified signature.
henrikvtcodes Henrik VT
commit 83315e638db33d5efa82ef0761dad3ed06cc8978
9 changes: 6 additions & 3 deletions src/utils/apr.ts
Original file line number Diff line number Diff line change
@@ -121,14 +121,17 @@ export function calculateLastApr(
}

let durationSum = ZERO_BD;
let timeWeight = ZERO_BD;
let weighedAPRSum = ZERO_BD;

// linearly weight the APRs, most recent APRs have full impact
for (let i = 0; i < APRs.length; i++) {
durationSum = durationSum.plus(durations[i]);
weighedAPRSum = weighedAPRSum.plus(APRs[i].times(durations[i]));
durationSum = durationSum.plus(durations[i].dividedBy(periodMs));
timeWeight = timeWeight.plus(durations[i].times(durationSum));
weighedAPRSum = weighedAPRSum.plus(APRs[i].times(timeWeight));
}

const apr = weighedAPRSum.div(durationSum);
const apr = weighedAPRSum.div(timeWeight);
// we compound the APR to get the APY
const annualPeriods = ONE_YEAR / periodMs;
const annualCompounds = annualPeriods * compoundCount;