Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
7 changes: 5 additions & 2 deletions apps/staking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ type Data = {
poolUtilizationDelta: bigint;
numFeeds: number;
qualityRanking: number;
apyHistory: { date: Date; apy: number }[];
delegationFee: bigint;
apyHistory: { date: Date; apy: number; selfApy: number }[];
Copy link
Collaborator

@cprussin cprussin Oct 3, 2024

Choose a reason for hiding this comment

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

Hmm in my opinion it's better to pass the current stake account to the sdk when fetching the apy, rather than having all the raw data and calculating it all in the frontend, thoughts? never mind I see how you're threading this into calculateApy

positions?:
| {
warmup?: bigint | undefined;
Expand Down Expand Up @@ -226,9 +227,10 @@ const loadPublisherData = async (
const publisherRanking = publisherRankings.find(
(ranking) => ranking.publisher === publisherPubkeyString,
);
const apyHistory = publisher.apyHistory.map(({ epoch, apy }) => ({
const apyHistory = publisher.apyHistory.map(({ epoch, apy, selfApy }) => ({
date: epochToDate(epoch + 1n),
apy,
selfApy,
}));

return {
Expand All @@ -243,6 +245,7 @@ const loadPublisherData = async (
selfStake: publisher.selfDelegation,
selfStakeDelta: publisher.selfDelegationDelta,
stakeAccount: publisher.stakeAccount ?? undefined,
delegationFee: publisher.delegationFee,
};
});
};
Expand Down
11 changes: 8 additions & 3 deletions apps/staking/src/components/OracleIntegrityStaking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -935,13 +935,15 @@ const doSort = (
poolCapacity: a.poolCapacity,
poolUtilization: a.poolUtilization + a.poolUtilizationDelta,
yieldRate,
delegationFee: a.delegationFee,
}) -
calculateApy({
isSelf: false,
selfStake: b.selfStake + b.selfStakeDelta,
poolCapacity: b.poolCapacity,
poolUtilization: b.poolUtilization + b.poolUtilizationDelta,
yieldRate,
delegationFee: b.delegationFee,
});
return sort === SortOption.ApyDescending ? -1 * value : value;
}
Expand Down Expand Up @@ -1063,7 +1065,8 @@ type PublisherProps = {
poolUtilizationDelta: bigint;
numFeeds: number;
qualityRanking: number;
apyHistory: { date: Date; apy: number }[];
delegationFee: bigint;
apyHistory: { date: Date; apy: number; selfApy: number }[];
positions?:
| {
warmup?: bigint | undefined;
Expand Down Expand Up @@ -1124,6 +1127,7 @@ const Publisher = ({
poolUtilization:
publisher.poolUtilization + publisher.poolUtilizationDelta,
yieldRate,
delegationFee: publisher.delegationFee,
}).toFixed(2),
[
isSelf,
Expand Down Expand Up @@ -1262,9 +1266,9 @@ const Publisher = ({
<PublisherTableCell>
<div className="mx-auto h-14 w-28">
<SparkChart
data={publisher.apyHistory.map(({ date, apy }) => ({
data={publisher.apyHistory.map(({ date, apy, selfApy }) => ({
date,
value: apy,
value: isSelf ? selfApy : apy,
}))}
/>
</div>
Expand Down Expand Up @@ -1548,6 +1552,7 @@ const NewApy = ({
calculateApy({
poolCapacity: publisher.poolCapacity,
yieldRate,
delegationFee: publisher.delegationFee,
...(isSelf
? {
isSelf: true,
Expand Down
1 change: 1 addition & 0 deletions governance/pyth_staking_sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type PublisherData = {
totalDelegationDelta: bigint;
selfDelegation: bigint;
selfDelegationDelta: bigint;
delegationFee: bigint;
apyHistory: { epoch: bigint; apy: number; selfApy: number }[];
}[];

Expand Down
12 changes: 10 additions & 2 deletions governance/pyth_staking_sdk/src/utils/apy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ export const convertEpochYieldToApy = (epochYield: bigint) => {
return (Number(epochYield) * 52 * 100) / FRACTION_PRECISION;
};

export const computeDelegatorRewardPercentage = (
delegationFee : bigint,
) => {
return 1 - Number(delegationFee) / FRACTION_PRECISION;
}

export const calculateApy = (
options: {
selfStake: bigint;
poolCapacity: bigint;
yieldRate: bigint;
delegationFee : bigint;
} & ({ isSelf: true } | { isSelf: false; poolUtilization: bigint }),
) => {
const { selfStake, poolCapacity, yieldRate, isSelf } = options;
Expand All @@ -24,6 +31,7 @@ export const calculateApy = (
}

const { poolUtilization } = options;
const delegatorPercentage = computeDelegatorRewardPercentage(options.delegationFee);

const delegatorPoolUtilization = poolUtilization - selfStake;
const delegatorPoolCapacity = poolCapacity - eligibleSelfStake;
Expand All @@ -33,10 +41,10 @@ export const calculateApy = (
: delegatorPoolUtilization;

if (poolUtilization === selfStake) {
return apyPercentage;
return (selfStake >= poolCapacity ? 0 : apyPercentage) * delegatorPercentage;
}

return (
(apyPercentage * Number(eligibleStake)) / Number(delegatorPoolUtilization)
(apyPercentage * delegatorPercentage * Number(eligibleStake)) / Number(delegatorPoolUtilization)
);
};
5 changes: 3 additions & 2 deletions governance/pyth_staking_sdk/src/utils/pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PublicKey } from "@solana/web3.js";

import { convertEpochYieldToApy } from "./apy";
import { computeDelegatorRewardPercentage, convertEpochYieldToApy } from "./apy";
import { FRACTION_PRECISION_N } from "../constants";
import type { PoolDataAccount, PublisherData } from "../types";

Expand All @@ -24,14 +24,15 @@ export const extractPublisherData = (
(poolData.selfDelState[index]?.deltaDelegation ?? 0n),
selfDelegation: poolData.selfDelState[index]?.totalDelegation ?? 0n,
selfDelegationDelta: poolData.selfDelState[index]?.deltaDelegation ?? 0n,
delegationFee: poolData.delegationFees[index] ?? 0n,
apyHistory: poolData.events
.filter((event) => event.epoch > 0n)
.map((event) => ({
epoch: event.epoch,
apy: convertEpochYieldToApy(
(event.y * (event.eventData[index]?.otherRewardRatio ?? 0n)) /
FRACTION_PRECISION_N,
),
) * computeDelegatorRewardPercentage(poolData.delegationFees[index] ?? 0n),
selfApy: convertEpochYieldToApy(
(event.y * (event.eventData[index]?.selfRewardRatio ?? 0n)) /
FRACTION_PRECISION_N,
Expand Down
Loading