Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions apps/insights/src/components/Root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
GOOGLE_ANALYTICS_ID,
} from "../../config/server";
import { getPublishersWithRankings } from "../../get-publishers-with-rankings";
import { LivePriceDataProvider } from "../../hooks/use-live-price-data";
import { Cluster } from "../../services/pyth";
import { getFeeds } from "../../services/pyth/get-feeds";
import { PriceFeedIcon } from "../PriceFeedIcon";
Expand All @@ -32,7 +31,7 @@ export const Root = ({ children }: Props) => (
amplitudeApiKey={AMPLITUDE_API_KEY}
googleAnalyticsId={GOOGLE_ANALYTICS_ID}
enableAccessibilityReporting={ENABLE_ACCESSIBILITY_REPORTING}
providers={[NuqsAdapter, LivePriceDataProvider]}
providers={[NuqsAdapter]}
tabs={TABS}
extraCta={<SearchButton />}
>
Expand Down
180 changes: 17 additions & 163 deletions apps/insights/src/hooks/use-live-price-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,34 @@
import type { PriceData } from "@pythnetwork/client";
import { useLogger } from "@pythnetwork/component-library/useLogger";
import { PublicKey } from "@solana/web3.js";
import type { ComponentProps } from "react";
import {
use,
createContext,
useEffect,
useCallback,
useState,
useMemo,
useRef,
} from "react";
import { useEffect, useState, useMemo } from "react";

import {
Cluster,
subscribe,
getAssetPricesFromAccounts,
} from "../services/pyth";

const LivePriceDataContext = createContext<
ReturnType<typeof usePriceData> | undefined
>(undefined);

type LivePriceDataProviderProps = Omit<
ComponentProps<typeof LivePriceDataContext>,
"value"
>;

export const LivePriceDataProvider = (props: LivePriceDataProviderProps) => {
const priceData = usePriceData();

return <LivePriceDataContext value={priceData} {...props} />;
};
import { Cluster, subscribe, unsubscribe } from "../services/pyth";

export const useLivePriceData = (cluster: Cluster, feedKey: string) => {
const { addSubscription, removeSubscription } =
useLivePriceDataContext()[cluster];

const logger = useLogger();
const [data, setData] = useState<{
current: PriceData | undefined;
prev: PriceData | undefined;
}>({ current: undefined, prev: undefined });

useEffect(() => {
addSubscription(feedKey, setData);
const subscriptionId = subscribe(
cluster,
new PublicKey(feedKey),
({ data }) => {
setData((prev) => ({ current: data, prev: prev.current }));
},
);
return () => {
removeSubscription(feedKey, setData);
unsubscribe(cluster, subscriptionId).catch((error: unknown) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

q: should we consider a failure to unsubscrive as a more critical failure and alert the user to reload their page (or similar)?

Copy link
Collaborator Author

@cprussin cprussin Oct 28, 2025

Choose a reason for hiding this comment

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

No, at the worst case it means that the websocket will be continuing to send some data that we ignore. The way the web3.js Connection object works is that it updates all subscriptions when creating a new subscription or deleting a subscription, so the websocket unsubscribe message will be retried by the next time any change in the subscribed feeds happens anyways. So even the problem with continuing to receive unnecessary ws messages would resolve itself by just navigating around the app.

logger.error(
`Failed to remove subscription for price feed ${feedKey}`,
error,
);
});
};
}, [addSubscription, removeSubscription, feedKey]);
}, [cluster, feedKey, logger]);

return data;
};
Expand All @@ -75,130 +56,3 @@ export const useLivePriceComponent = (
exponent: current?.exponent,
};
};

const usePriceData = () => {
const pythnetPriceData = usePriceDataForCluster(Cluster.Pythnet);
const pythtestPriceData = usePriceDataForCluster(Cluster.PythtestConformance);

return {
[Cluster.Pythnet]: pythnetPriceData,
[Cluster.PythtestConformance]: pythtestPriceData,
};
};

type Subscription = (value: {
current: PriceData;
prev: PriceData | undefined;
}) => void;

const usePriceDataForCluster = (cluster: Cluster) => {
const [feedKeys, setFeedKeys] = useState<string[]>([]);
const feedSubscriptions = useRef<Map<string, Set<Subscription>>>(new Map());
const priceData = useRef<Map<string, PriceData>>(new Map());
const prevPriceData = useRef<Map<string, PriceData>>(new Map());
const logger = useLogger();

useEffect(() => {
// First, we initialize prices with the last available price. This way, if
// there's any symbol that isn't currently publishing prices (e.g. the
// markets are closed), we will still display the last published price for
// that symbol.
const uninitializedFeedKeys = feedKeys.filter(
(key) => !priceData.current.has(key),
);
if (uninitializedFeedKeys.length > 0) {
getAssetPricesFromAccounts(
cluster,
uninitializedFeedKeys.map((key) => new PublicKey(key)),
)
.then((initialPrices) => {
for (const [i, price] of initialPrices.entries()) {
const key = uninitializedFeedKeys[i];
if (key && !priceData.current.has(key)) {
priceData.current.set(key, price);
}
}
})
.catch((error: unknown) => {
logger.error("Failed to fetch initial prices", error);
});
}

// Then, we create a subscription to update prices live.
const connection = subscribe(
cluster,
feedKeys.map((key) => new PublicKey(key)),
({ price_account }, data) => {
if (price_account) {
const prevData = priceData.current.get(price_account);
if (prevData) {
prevPriceData.current.set(price_account, prevData);
}
priceData.current.set(price_account, data);
for (const subscription of feedSubscriptions.current.get(
price_account,
) ?? []) {
subscription({ current: data, prev: prevData });
}
}
},
);

connection.start().catch((error: unknown) => {
logger.error("Failed to subscribe to prices", error);
});
return () => {
connection.stop().catch((error: unknown) => {
logger.error("Failed to unsubscribe from price updates", error);
});
};
}, [feedKeys, logger, cluster]);

const addSubscription = useCallback(
(key: string, subscription: Subscription) => {
const current = feedSubscriptions.current.get(key);
if (current === undefined) {
feedSubscriptions.current.set(key, new Set([subscription]));
setFeedKeys((prev) => [...new Set([...prev, key])]);
} else {
current.add(subscription);
}
},
[feedSubscriptions],
);

const removeSubscription = useCallback(
(key: string, subscription: Subscription) => {
const current = feedSubscriptions.current.get(key);
if (current) {
if (current.size === 0) {
feedSubscriptions.current.delete(key);
setFeedKeys((prev) => prev.filter((elem) => elem !== key));
} else {
current.delete(subscription);
}
}
},
[feedSubscriptions],
);

return {
addSubscription,
removeSubscription,
};
};

const useLivePriceDataContext = () => {
const prices = use(LivePriceDataContext);
if (prices === undefined) {
throw new LivePriceDataProviderNotInitializedError();
}
return prices;
};

class LivePriceDataProviderNotInitializedError extends Error {
constructor() {
super("This component must be a child of <LivePriceDataProvider>");
this.name = "LivePriceDataProviderNotInitializedError";
}
}
33 changes: 20 additions & 13 deletions apps/insights/src/services/pyth/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { PriceData } from "@pythnetwork/client";
import {
PythHttpClient,
PythConnection,
getPythProgramKeyForCluster,
parsePriceData,
} from "@pythnetwork/client";
import type { PythPriceCallback } from "@pythnetwork/client/lib/PythConnection";
import type { AccountInfo } from "@solana/web3.js";
import { Connection, PublicKey } from "@solana/web3.js";

import { PYTHNET_RPC, PYTHTEST_CONFORMANCE_RPC } from "../../config/isomorphic";
Expand Down Expand Up @@ -67,15 +68,21 @@ export const getAssetPricesFromAccounts = (

export const subscribe = (
cluster: Cluster,
feeds: PublicKey[],
cb: PythPriceCallback,
) => {
const pythConn = new PythConnection(
connections[cluster],
getPythProgramKeyForCluster(ClusterToName[cluster]),
"confirmed",
feeds,
feed: PublicKey,
cb: (values: { accountInfo: AccountInfo<Buffer>; data: PriceData }) => void,
) =>
connections[cluster].onAccountChange(
feed,
(accountInfo, context) => {
cb({
accountInfo,
data: parsePriceData(accountInfo.data, context.slot),
});
},
{
commitment: "confirmed",
},
);
pythConn.onPriceChange(cb);
return pythConn;
};

export const unsubscribe = (cluster: Cluster, subscriptionId: number) =>
connections[cluster].removeAccountChangeListener(subscriptionId);
Loading