-
Notifications
You must be signed in to change notification settings - Fork 302
feat(staking): add more real data #1880
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
Changes from 5 commits
be70475
3c138bc
6479b88
d023080
31d6da4
2681953
ce0cc0d
e1000ea
c69d80d
401600f
eba95e2
63a7b7b
13c300d
b75165d
9f602c4
6439c6a
e022d8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| export async function GET() { | ||
| const publisherRankingsResponse = await fetch( | ||
| "https://www.pyth.network/api/publishers-ranking?cluster=pythnet", | ||
keyvankhademi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| const publisherRankings = (await publisherRankingsResponse.json()) as JSON; | ||
| return NextResponse.json(publisherRankings); | ||
| } | ||
keyvankhademi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| "use client"; | ||
|
|
||
| import { HermesClient } from "@pythnetwork/hermes-client"; | ||
| import type { StakeAccountPositions } from "@pythnetwork/staking-sdk"; | ||
| import { PythStakingClient } from "@pythnetwork/staking-sdk"; | ||
| import { useConnection, useWallet } from "@solana/wallet-adapter-react"; | ||
|
|
@@ -38,12 +39,14 @@ const State = { | |
|
|
||
| Loaded: ( | ||
| client: PythStakingClient, | ||
| hermesClient: HermesClient, | ||
| account: StakeAccountPositions, | ||
| allAccounts: [StakeAccountPositions, ...StakeAccountPositions[]], | ||
| selectAccount: (account: StakeAccountPositions) => void, | ||
| ) => ({ | ||
| type: StateType.Loaded as const, | ||
| client, | ||
| hermesClient, | ||
| account, | ||
| allAccounts, | ||
| selectAccount, | ||
|
|
@@ -83,7 +86,13 @@ const useStakeAccountState = () => { | |
| (account: StakeAccountPositions) => { | ||
| setState((cur) => | ||
| cur.type === StateType.Loaded | ||
| ? State.Loaded(cur.client, account, cur.allAccounts, setAccount) | ||
| ? State.Loaded( | ||
| cur.client, | ||
| cur.hermesClient, | ||
| account, | ||
| cur.allAccounts, | ||
| setAccount, | ||
| ) | ||
| : cur, | ||
| ); | ||
| }, | ||
|
|
@@ -109,13 +118,16 @@ const useStakeAccountState = () => { | |
| signTransaction: wallet.signTransaction, | ||
| }, | ||
| }); | ||
| // TODO: use env var to support mainnet | ||
| const hermesClient = new HermesClient("https://hermes-beta.pyth.network"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this should be in a separate react context, it doesn't really make sense to be in this context as it's not associated to the wallet state (I should rename this hook as it's no longer just about staking accounts, it's now really a hook for the wallet state, where the list of stake accounts & the selected stake account is part of the wallet state -- and the Pyth Staking Client is here because it is only valid in certain wallet states). |
||
| getStakeAccounts(client) | ||
| .then((accounts) => { | ||
| const [firstAccount, ...otherAccounts] = accounts; | ||
| if (firstAccount) { | ||
| setState( | ||
| State.Loaded( | ||
| client, | ||
| hermesClient, | ||
| firstAccount, | ||
| [firstAccount, ...otherAccounts], | ||
| setAccount, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from "./pyth-staking-client"; | ||
| export * from "./utils/clock"; | ||
| export * from "./utils/position"; | ||
| export * from "./utils/pool"; | ||
| export * from "./types"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { PublicKey } from "@solana/web3.js"; | ||
|
|
||
| import type { PoolDataAccount, PublisherData } from "../types"; | ||
|
|
||
| export const extractPublisherData = ( | ||
| poolData: PoolDataAccount, | ||
| ): PublisherData => { | ||
| return poolData.publishers | ||
| .filter((publisher) => !publisher.equals(PublicKey.default)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels wrong if there were gaps in the publishers (which can't happen in the program though) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes there are a lot of assumptions based on that. |
||
| .map((publisher, index) => ({ | ||
| pubkey: publisher, | ||
| stakeAccount: | ||
| poolData.publisherStakeAccounts[index] === undefined || | ||
| poolData.publisherStakeAccounts[index].equals(PublicKey.default) | ||
| ? null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor preference but I suggest just using If you need a true ternary state, I will always recommend using the fake sum types pattern I shared with you earlier, it's far more explicit, easier to understand the intent, and less error-prone. |
||
| : poolData.publisherStakeAccounts[index], | ||
| totalDelegation: | ||
| (poolData.delState[index]?.totalDelegation ?? 0n) + | ||
| (poolData.selfDelState[index]?.totalDelegation ?? 0n), | ||
| selfDelegation: poolData.selfDelState[index]?.totalDelegation ?? 0n, | ||
| apyHistory: poolData.events | ||
| .filter((event) => event.epoch > 0n) | ||
| .map((event) => ({ | ||
| epoch: event.epoch, | ||
| apy: | ||
| (event.y * (event.eventData[index]?.otherRewardRatio ?? 0n)) / | ||
| 1_000_000n, | ||
| selfApy: | ||
| (event.y * (event.eventData[index]?.selfRewardRatio ?? 0n)) / | ||
| 1_000_000n, | ||
| })) | ||
| .sort((a, b) => Number(a.epoch) - Number(b.epoch)), | ||
| })); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.