pyth.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { parse } from "superjson";
  2. import { z } from "zod";
  3. import { PUBLIC_URL, VERCEL_AUTOMATION_BYPASS_SECRET } from "../config/server";
  4. import { Cluster, ClusterToName, priceFeedsSchema } from "../services/pyth";
  5. import { DEFAULT_CACHE_TTL } from "../utils/cache";
  6. export async function getPublishersForFeedRequest(
  7. cluster: Cluster,
  8. symbol: string,
  9. ) {
  10. const data = await fetch(
  11. `${PUBLIC_URL}/api/pyth/get-publishers/${encodeURIComponent(symbol)}?cluster=${ClusterToName[cluster]}`,
  12. {
  13. next: {
  14. revalidate: DEFAULT_CACHE_TTL,
  15. },
  16. headers: {
  17. // this is a way to bypass vercel protection for the internal api route
  18. "x-vercel-protection-bypass": VERCEL_AUTOMATION_BYPASS_SECRET,
  19. },
  20. },
  21. );
  22. const parsedData: unknown = await data.json();
  23. return z.array(z.string()).parse(parsedData);
  24. }
  25. export async function getFeedsForPublisherRequest(
  26. cluster: Cluster,
  27. publisher: string,
  28. ) {
  29. const data = await fetch(
  30. `${PUBLIC_URL}/api/pyth/get-feeds-for-publisher/${encodeURIComponent(publisher)}?cluster=${ClusterToName[cluster]}`,
  31. {
  32. next: {
  33. revalidate: DEFAULT_CACHE_TTL,
  34. },
  35. headers: {
  36. // this is a way to bypass vercel protection for the internal api route
  37. "x-vercel-protection-bypass": VERCEL_AUTOMATION_BYPASS_SECRET,
  38. },
  39. },
  40. );
  41. const rawData = await data.text();
  42. const parsedData = parse(rawData);
  43. return priceFeedsSchema.parse(parsedData);
  44. }
  45. export const getFeedsRequest = async (cluster: Cluster) => {
  46. const data = await fetch(
  47. `${PUBLIC_URL}/api/pyth/get-feeds?cluster=${ClusterToName[cluster]}&excludePriceComponents=true`,
  48. {
  49. next: {
  50. revalidate: DEFAULT_CACHE_TTL,
  51. },
  52. headers: {
  53. // this is a way to bypass vercel protection for the internal api route
  54. "x-vercel-protection-bypass": VERCEL_AUTOMATION_BYPASS_SECRET,
  55. },
  56. },
  57. );
  58. const rawData = await data.text();
  59. const parsedData = parse(rawData);
  60. return priceFeedsSchema.element
  61. .omit({ price: true })
  62. .array()
  63. .parse(parsedData);
  64. };
  65. export const getFeedForSymbolRequest = async ({
  66. symbol,
  67. cluster = Cluster.Pythnet,
  68. }: {
  69. symbol: string;
  70. cluster?: Cluster;
  71. }): Promise<z.infer<typeof priceFeedsSchema>[0] | undefined> => {
  72. const data = await fetch(
  73. `${PUBLIC_URL}/api/pyth/get-feeds/${encodeURIComponent(symbol)}?cluster=${ClusterToName[cluster]}`,
  74. {
  75. next: {
  76. revalidate: DEFAULT_CACHE_TTL,
  77. },
  78. headers: {
  79. // this is a way to bypass vercel protection for the internal api route
  80. "x-vercel-protection-bypass": VERCEL_AUTOMATION_BYPASS_SECRET,
  81. },
  82. },
  83. );
  84. if (!data.ok) {
  85. return undefined;
  86. }
  87. const rawData = await data.text();
  88. const parsedData = parse(rawData);
  89. return priceFeedsSchema.element.parse(parsedData);
  90. };