Skip to content

Commit

Permalink
Include redemption fees in the APY calc
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyDelott committed Feb 7, 2023
1 parent 52305a0 commit 86b18ff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
32 changes: 32 additions & 0 deletions apps/liquity-ui/src/ui/redeem/useRedemptionInfos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useQuery } from "@tanstack/react-query";
import { alchemy } from "src/provider";
import {
fetchRedemptionInfos,
RedemptionInfosFile,
REDEMPTION_INFOS_URL,
} from "liquity";
import { ONE_DAY_IN_MILLISECONDS } from "src/base/time";

export function useRedemptionInfos() {
return useQuery({
queryKey: ["redemption-infos"],
queryFn: async () => {
// Move fetch to own hook w/ staleTime 2 hours
const response = await fetch(REDEMPTION_INFOS_URL);
const redemptionInfos = (await response.json()) as RedemptionInfosFile;

const provider = await alchemy.config.getProvider();
const latestRedemptionInfos = await fetchRedemptionInfos(
redemptionInfos.data[redemptionInfos.data.length - 1].block + 1,
provider
);

return {
...redemptionInfos,
data: [...redemptionInfos.data, ...latestRedemptionInfos],
};
},
// This is scraped fresh once a day
staleTime: ONE_DAY_IN_MILLISECONDS,
});
}
49 changes: 33 additions & 16 deletions apps/liquity-ui/src/ui/stake/useStakingAPY.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { BorrowInfo } from "liquity";
import { BorrowInfo, RedemptionInfo } from "liquity";
import meanBy from "lodash.meanby";
import sumBy from "lodash.sumby";
import { ONE_DAY_IN_SECONDS, ONE_WEEK_IN_SECONDS } from "src/base/time";
import { useBorrowInfos } from "src/ui/borrow/useBorrowInfos";
import { useRedemptionInfos } from "src/ui/redeem/useRedemptionInfos";

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

// TODO: Add Redemption infos for Eth
// const { data: redemptionInfos, status: redemptionInfosStatus } =
// useRedemptionInfos();

if (!borrowInfos) {
if (!borrowInfos || !redemptionInfos) {
return {
status: borrowInfosStatus,
apy: undefined,
Expand All @@ -21,34 +20,52 @@ export function useStakingAPY(numDays: number) {

const now = Date.now() / 1000;
const numDaysAgo = now - numDays * ONE_DAY_IN_SECONDS;

const borrowInfosFromLastNumDays = borrowInfos.data.filter(
({ timestamp }) => timestamp >= numDaysAgo
);
const redemptionInfosFromLastNumDays = redemptionInfos.data.filter(
({ timestamp }) => timestamp >= numDaysAgo
);

const totalInterestLastNumDays = getTotalInterestUSD(
const totalInterestFromBorrowInfos = getTotalInterestFromBorrowInfos(
borrowInfosFromLastNumDays
);
const meanLQTYStakedLastNumDays = getMeanLQTYStaked(
borrowInfosFromLastNumDays
const totalInterestFromRedemptionInfos = getTotalInterestFromRedemptionInfos(
redemptionInfosFromLastNumDays
);
const meanLQTYPriceLastNumDays = getMeanLQTYPrice(borrowInfosFromLastNumDays);

const combinedInfos = [
...borrowInfosFromLastNumDays,
...redemptionInfosFromLastNumDays,
];
const totalInterest =
totalInterestFromBorrowInfos + totalInterestFromRedemptionInfos;
const meanLQTYStakedLastNumDays = getMeanLQTYStaked(combinedInfos);
const meanLQTYPriceLastNumDays = getMeanLQTYPrice(combinedInfos);
const principalValue = meanLQTYStakedLastNumDays * meanLQTYPriceLastNumDays;

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

return {
status: borrowInfosStatus,
apy,
};
}

function getMeanLQTYPrice(borrowInfos: BorrowInfo[]) {
return meanBy(borrowInfos, ({ lqtyPrice }) => +lqtyPrice);
function getMeanLQTYPrice(infos: (BorrowInfo | RedemptionInfo)[]) {
return meanBy(infos, ({ lqtyPrice }) => +lqtyPrice);
}
function getMeanLQTYStaked(borrowInfos: BorrowInfo[]) {
return meanBy(borrowInfos, ({ totalLqtyStaked }) => +totalLqtyStaked);
function getMeanLQTYStaked(infos: (BorrowInfo | RedemptionInfo)[]) {
return meanBy(infos, ({ totalLqtyStaked }) => +totalLqtyStaked);
}

function getTotalInterestUSD(borrowInfos: BorrowInfo[]) {
function getTotalInterestFromBorrowInfos(borrowInfos: BorrowInfo[]) {
return sumBy(borrowInfos, ({ lusdFee, lusdPrice }) => +lusdFee * +lusdPrice);
}

function getTotalInterestFromRedemptionInfos(
redemptionInfos: RedemptionInfo[]
) {
return sumBy(redemptionInfos, ({ ethFee, ethPrice }) => +ethFee * +ethPrice);
}

1 comment on commit 86b18ff

@vercel
Copy link

@vercel vercel bot commented on 86b18ff Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.