Skip to content

Commit

Permalink
feat(staking): estimated next apy (#1917)
Browse files Browse the repository at this point in the history
* feat(staking): extimated next apy

* fix

* fix

* fix type
  • Loading branch information
keyvankhademi authored Sep 13, 2024
1 parent b3fe3d4 commit 122d340
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
4 changes: 4 additions & 0 deletions apps/staking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ type Data = {
publicKey: PublicKey;
stakeAccount: PublicKey | undefined;
selfStake: bigint;
selfStakeDelta: bigint;
poolCapacity: bigint;
poolUtilization: bigint;
poolUtilizationDelta: bigint;
numFeeds: number;
qualityRanking: number;
apyHistory: { date: Date; apy: number }[];
Expand Down Expand Up @@ -301,9 +303,11 @@ const loadPublisherData = async (
numFeeds: publisherRanking?.numSymbols ?? 0,
poolCapacity: getPublisherCap(publisherCaps, publisher.pubkey),
poolUtilization: publisher.totalDelegation,
poolUtilizationDelta: publisher.totalDelegationDelta,
publicKey: publisher.pubkey,
qualityRanking: publisherRanking?.rank ?? 0,
selfStake: publisher.selfDelegation,
selfStakeDelta: publisher.selfDelegationDelta,
stakeAccount: publisher.stakeAccount ?? undefined,
};
});
Expand Down
31 changes: 21 additions & 10 deletions apps/staking/src/components/OracleIntegrityStaking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export const OracleIntegrityStaking = ({
<thead className="bg-pythpurple-400/30 font-light">
<tr>
<PublisherTableHeader>Pool</PublisherTableHeader>
<PublisherTableHeader>Last epoch APY</PublisherTableHeader>
<PublisherTableHeader>
Estimated next APY
</PublisherTableHeader>
<PublisherTableHeader>Historical APY</PublisherTableHeader>
<PublisherTableHeader>Number of feeds</PublisherTableHeader>
<PublisherTableHeader>Quality ranking</PublisherTableHeader>
Expand Down Expand Up @@ -570,7 +572,7 @@ const PublisherList = ({
sort={sort}
setSort={updateSort}
>
APY
Estimated next APY
</SortablePublisherTableHeader>
<PublisherTableHeader>Historical APY</PublisherTableHeader>
<SortablePublisherTableHeader
Expand Down Expand Up @@ -657,16 +659,16 @@ const doSort = (
return (
calculateApy({
isSelf: false,
selfStake: a.selfStake,
selfStake: a.selfStake + a.selfStakeDelta,
poolCapacity: a.poolCapacity,
poolUtilization: a.poolUtilization,
poolUtilization: a.poolUtilization + a.poolUtilizationDelta,
yieldRate,
}) -
calculateApy({
isSelf: false,
selfStake: b.selfStake,
selfStake: b.selfStake + b.selfStakeDelta,
poolCapacity: b.poolCapacity,
poolUtilization: b.poolUtilization,
poolUtilization: b.poolUtilization + b.poolUtilizationDelta,
yieldRate,
})
);
Expand Down Expand Up @@ -755,8 +757,10 @@ type PublisherProps = {
publicKey: PublicKey;
stakeAccount: PublicKey | undefined;
selfStake: bigint;
selfStakeDelta: bigint;
poolCapacity: bigint;
poolUtilization: bigint;
poolUtilizationDelta: bigint;
numFeeds: number;
qualityRanking: number;
apyHistory: { date: Date; apy: number }[];
Expand Down Expand Up @@ -870,9 +874,10 @@ const Publisher = ({
<div>
{calculateApy({
isSelf: isSelf ?? false,
selfStake: publisher.selfStake,
selfStake: publisher.selfStake + publisher.selfStakeDelta,
poolCapacity: publisher.poolCapacity,
poolUtilization: publisher.poolUtilization,
poolUtilization:
publisher.poolUtilization + publisher.poolUtilizationDelta,
yieldRate,
})}
%
Expand Down Expand Up @@ -1074,20 +1079,26 @@ const NewApy = ({
...(isSelf
? {
isSelf: true,
selfStake: publisher.selfStake + children,
selfStake:
publisher.selfStake + publisher.selfStakeDelta + children,
}
: {
isSelf: false,
selfStake: publisher.selfStake,
poolUtilization: publisher.poolUtilization + children,
poolUtilization:
publisher.poolUtilization +
publisher.poolUtilizationDelta +
children,
}),
}),
[
publisher.poolCapacity,
yieldRate,
isSelf,
publisher.selfStake,
publisher.selfStakeDelta,
publisher.poolUtilization,
publisher.poolUtilizationDelta,
children,
],
);
Expand Down
17 changes: 9 additions & 8 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ export class PythStakingClient {
stakeAccountPositions: PublicKey,
publisher: PublicKey,
) {
const delegationRecord =
await this.integrityPoolProgram.account.delegationRecord.fetch(
getDelegationRecordAddress(stakeAccountPositions, publisher),
);
return convertBNToBigInt(delegationRecord);
return this.integrityPoolProgram.account.delegationRecord
.fetch(getDelegationRecordAddress(stakeAccountPositions, publisher))
.then((record) => convertBNToBigInt(record));
}

public async getStakeAccountCustody(
Expand Down Expand Up @@ -624,16 +622,19 @@ export class PythStakingClient {
totalRewards += BigInt("0x" + buffer.toString("hex"));
}

const delegationRecords = await Promise.all(
const delegationRecords = await Promise.allSettled(
instructions.publishers.map(({ pubkey }) =>
this.getDelegationRecord(stakeAccountPositions, pubkey),
),
);

let lowestEpoch: bigint | undefined;
for (const record of delegationRecords) {
if (lowestEpoch === undefined || record.lastEpoch < lowestEpoch) {
lowestEpoch = record.lastEpoch;
if (record.status === "fulfilled") {
const { lastEpoch } = record.value;
if (lowestEpoch === undefined || lastEpoch < lowestEpoch) {
lowestEpoch = lastEpoch;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions governance/pyth_staking_sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export type PublisherData = {
pubkey: PublicKey;
stakeAccount: PublicKey | null;
totalDelegation: bigint;
totalDelegationDelta: bigint;
selfDelegation: bigint;
selfDelegationDelta: bigint;
apyHistory: { epoch: bigint; apy: number; selfApy: number }[];
}[];

Expand Down
4 changes: 4 additions & 0 deletions governance/pyth_staking_sdk/src/utils/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export const extractPublisherData = (
totalDelegation:
(poolData.delState[index]?.totalDelegation ?? 0n) +
(poolData.selfDelState[index]?.totalDelegation ?? 0n),
totalDelegationDelta:
(poolData.delState[index]?.deltaDelegation ?? 0n) +
(poolData.selfDelState[index]?.deltaDelegation ?? 0n),
selfDelegation: poolData.selfDelState[index]?.totalDelegation ?? 0n,
selfDelegationDelta: poolData.selfDelState[index]?.deltaDelegation ?? 0n,
apyHistory: poolData.events
.filter((event) => event.epoch > 0n)
.map((event) => ({
Expand Down

0 comments on commit 122d340

Please sign in to comment.