diff --git a/apps/insights/src/components/Root/index.tsx b/apps/insights/src/components/Root/index.tsx
index 35954de1a0..e3b708356a 100644
--- a/apps/insights/src/components/Root/index.tsx
+++ b/apps/insights/src/components/Root/index.tsx
@@ -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";
@@ -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={}
>
diff --git a/apps/insights/src/hooks/use-live-price-data.tsx b/apps/insights/src/hooks/use-live-price-data.tsx
index 2ebbb2169f..b47391defe 100644
--- a/apps/insights/src/hooks/use-live-price-data.tsx
+++ b/apps/insights/src/hooks/use-live-price-data.tsx
@@ -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 | undefined
->(undefined);
-
-type LivePriceDataProviderProps = Omit<
- ComponentProps,
- "value"
->;
-
-export const LivePriceDataProvider = (props: LivePriceDataProviderProps) => {
- const priceData = usePriceData();
-
- return ;
-};
+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) => {
+ logger.error(
+ `Failed to remove subscription for price feed ${feedKey}`,
+ error,
+ );
+ });
};
- }, [addSubscription, removeSubscription, feedKey]);
+ }, [cluster, feedKey, logger]);
return data;
};
@@ -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([]);
- const feedSubscriptions = useRef