From a48861f8d126b641ce2c20cd65952d8de889d274 Mon Sep 17 00:00:00 2001 From: chambaz Date: Wed, 11 Dec 2024 20:00:30 -0500 Subject: [PATCH 1/4] chore: use edge function instead of be directly in price endpoint --- .../src/pages/api/oracle/price.ts | 19 +++++++++---------- turbo.json | 3 ++- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts index 8321af886..891ee23b1 100644 --- a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts +++ b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts @@ -412,22 +412,21 @@ async function fetchMultiPrice(tokens: string[]): Promise throw new Error("No tokens provided"); } - // use abort controller to restrict fetch to 10 seconds const controller = new AbortController(); const timeoutId = setTimeout(() => { controller.abort(); }, 5000); - // Fetch from API and update cache try { - const response = await fetch(`${BIRDEYE_API}/defi/multi_price?list_address=${tokens.join("%2C")}`, { - headers: { - Accept: "application/json", - "x-chain": "solana", - "X-Api-Key": process.env.BIRDEYE_API_KEY || "", - }, - signal: controller.signal, - }); + // Use our cached endpoint instead of hitting Birdeye directly + const response = await fetch( + `${ + process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : "http://localhost:3004" + }/api/tokens/multi?mintList=${tokens.join(",")}`, + { + signal: controller.signal, + } + ); clearTimeout(timeoutId); const data = (await response.json()) as BirdeyePriceResponse; diff --git a/turbo.json b/turbo.json index 73ee2d22d..343e0e6a7 100644 --- a/turbo.json +++ b/turbo.json @@ -104,6 +104,7 @@ "NEXT_PUBLIC_RPC_PROXY_KEY", "NEXT_PUBLIC_TX_SINGLE_BROADCAST_TYPE", "NEXT_PUBLIC_TX_MULTI_BROADCAST_TYPE", - "NEXT_PUBLIC_TX_FALLBACK_SEQUENCE" + "NEXT_PUBLIC_TX_FALLBACK_SEQUENCE", + "VERCEL_URL" ] } From 926df767d6ccc1f3a68955f0fe350b434133a0c6 Mon Sep 17 00:00:00 2001 From: chambaz Date: Wed, 11 Dec 2024 20:41:23 -0500 Subject: [PATCH 2/4] chore: log vercel url --- apps/marginfi-v2-ui/src/pages/api/oracle/price.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts index 891ee23b1..5563ced68 100644 --- a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts +++ b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts @@ -214,6 +214,8 @@ async function handleFetchCrossbarPrices( payload.push(...mainPayload); + console.log(process.env.VERCEL_URL); + if (!mainBrokenFeeds.length) { return crossbarPayloadToOraclePricePerFeedHash(payload); } From e7b180aa0ba39de2309febe0e2a9cfe4319190f5 Mon Sep 17 00:00:00 2001 From: chambaz Date: Wed, 11 Dec 2024 21:06:03 -0500 Subject: [PATCH 3/4] chore: use vercel env vars for be route --- .../src/pages/api/oracle/price.ts | 21 ++++++++++--------- turbo.json | 4 +++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts index 5563ced68..f7c582d08 100644 --- a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts +++ b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts @@ -214,7 +214,7 @@ async function handleFetchCrossbarPrices( payload.push(...mainPayload); - console.log(process.env.VERCEL_URL); + console.log(process.env.VERCEL_ENV, process.env.VERCEL_PROJECT_PRODUCTION_URL, process.env.VERCEL_BRANCH_URL); if (!mainBrokenFeeds.length) { return crossbarPayloadToOraclePricePerFeedHash(payload); @@ -420,15 +420,16 @@ async function fetchMultiPrice(tokens: string[]): Promise }, 5000); try { - // Use our cached endpoint instead of hitting Birdeye directly - const response = await fetch( - `${ - process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : "http://localhost:3004" - }/api/tokens/multi?mintList=${tokens.join(",")}`, - { - signal: controller.signal, - } - ); + const baseUrl = + process.env.VERCEL_ENV === "production" && process.env.VERCEL_PROJECT_PRODUCTION_URL + ? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}` + : process.env.VERCEL_BRANCH_URL + ? `https://${process.env.VERCEL_BRANCH_URL}` + : "localhost:3004"; + + const response = await fetch(`${baseUrl}/api/tokens/multi?mintList=${tokens.join(",")}`, { + signal: controller.signal, + }); clearTimeout(timeoutId); const data = (await response.json()) as BirdeyePriceResponse; diff --git a/turbo.json b/turbo.json index 343e0e6a7..d898ee470 100644 --- a/turbo.json +++ b/turbo.json @@ -105,6 +105,8 @@ "NEXT_PUBLIC_TX_SINGLE_BROADCAST_TYPE", "NEXT_PUBLIC_TX_MULTI_BROADCAST_TYPE", "NEXT_PUBLIC_TX_FALLBACK_SEQUENCE", - "VERCEL_URL" + "VERCEL_ENV", + "VERCEL_BRANCH_URL", + "VERCEL_PROJECT_PRODUCTION_URL" ] } From 777f6e94844b4d0b5ebd3bd011cd671c674283e4 Mon Sep 17 00:00:00 2001 From: chambaz Date: Wed, 11 Dec 2024 21:25:36 -0500 Subject: [PATCH 4/4] chore: remove vercel env var log --- apps/marginfi-v2-ui/src/pages/api/oracle/price.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts index f7c582d08..da00492d5 100644 --- a/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts +++ b/apps/marginfi-v2-ui/src/pages/api/oracle/price.ts @@ -214,8 +214,6 @@ async function handleFetchCrossbarPrices( payload.push(...mainPayload); - console.log(process.env.VERCEL_ENV, process.env.VERCEL_PROJECT_PRODUCTION_URL, process.env.VERCEL_BRANCH_URL); - if (!mainBrokenFeeds.length) { return crossbarPayloadToOraclePricePerFeedHash(payload); }