Skip to content

Commit

Permalink
Refactor useStakingAPY to accept dynamic numDays
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyDelott committed Feb 2, 2023
1 parent 10dc703 commit 84f90da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/liquity-ui/src/ui/stake/StakeCard/StakeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function StakeCard({ account }: StakeCardProps): ReactElement {
const { data: blockNumber } = useBlockNumber();
const { data: lqtyStaked } = useLQTYStakedForAccount(account);
const { data: totalLQTYStaked } = useTotalLQTYStaked();
const { apy } = useStakingAPY();
const { apy } = useStakingAPY(7);
const { data: currentPendingETHGain, status: currentPendingETHGainStatus } =
usePendingETHGain(account);
const {
Expand Down
24 changes: 14 additions & 10 deletions apps/liquity-ui/src/ui/stake/useStakingAPY.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BorrowInfo } from "liquity";
import meanBy from "lodash.meanby";
import sumBy from "lodash.sumby";
import { ONE_WEEK_IN_SECONDS } from "src/base/time";
import { ONE_DAY_IN_SECONDS, ONE_WEEK_IN_SECONDS } from "src/base/time";
import { useBorrowInfos } from "src/ui/borrow/useBorrowInfos";

// See: https://money.stackexchange.com/questions/154267/how-to-calculate-the-apr-on-an-investment
export function useStakingAPY() {
export function useStakingAPY(numDays: number) {
const { data: borrowInfos, status: borrowInfosStatus } = useBorrowInfos();

// TODO: Add Redemption infos for Eth
Expand All @@ -20,17 +20,21 @@ export function useStakingAPY() {
}

const now = Date.now() / 1000;
const oneWeekAgo = now - ONE_WEEK_IN_SECONDS;
const borrowInfosFromLast7Days = borrowInfos.data.filter(
({ timestamp }) => timestamp >= oneWeekAgo
const numDaysAgo = now - numDays * ONE_DAY_IN_SECONDS;
const borrowInfosFromLastNumDays = borrowInfos.data.filter(
({ timestamp }) => timestamp >= numDaysAgo
);

const totalInterestLast7Days = getTotalInterestUSD(borrowInfosFromLast7Days);
const meanLQTYStakedLast7Days = getMeanLQTYStaked(borrowInfosFromLast7Days);
const meanLQTYPriceLast7Days = getMeanLQTYPrice(borrowInfosFromLast7Days);
const principalValue = meanLQTYStakedLast7Days * meanLQTYPriceLast7Days;
const totalInterestLastNumDays = getTotalInterestUSD(
borrowInfosFromLastNumDays
);
const meanLQTYStakedLastNumDays = getMeanLQTYStaked(
borrowInfosFromLastNumDays
);
const meanLQTYPriceLastNumDays = getMeanLQTYPrice(borrowInfosFromLastNumDays);
const principalValue = meanLQTYStakedLastNumDays * meanLQTYPriceLastNumDays;

const apy = totalInterestLast7Days / (principalValue * (7 / 365));
const apy = totalInterestLastNumDays / (principalValue * (numDays / 365));

return {
status: borrowInfosStatus,
Expand Down

0 comments on commit 84f90da

Please sign in to comment.