Skip to content

Commit

Permalink
fix: improvement to switchboard oracles
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Jan 7, 2025
1 parent c97ad75 commit 6f8f816
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions apps/marginfi-v2-trading/src/pages/api/oracle/priceV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
feedHash,
} of swbPullOraclesStale) {
let crossbarPrice = crossbarPrices.get(feedHash);
if (!crossbarPrice) {
throw new Error(`Crossbar didn't return data for ${feedHash}`);
}
if (crossbarPrice.priceRealtime.price.isNaN()) {
if (!crossbarPrice || crossbarPrice.priceRealtime.price.isNaN()) {
crossbarPrice = {
...crossbarPrice,
timestamp: crossbarPrice?.timestamp ?? timestamp,
priceRealtime: {
price: new BigNumber(0),
confidence: new BigNumber(0),
Expand Down Expand Up @@ -232,11 +229,13 @@ async function handleFetchCrossbarPrices(
try {
// main crossbar
const payload: CrossbarSimulatePayload = [];
let brokenFeeds: string[] = [];

const { payload: mainPayload, brokenFeeds: mainBrokenFeeds } = await fetchCrossbarPrices(
feedHashes,
SWITCHBOARD_CROSSSBAR_API
);
brokenFeeds = mainBrokenFeeds;

payload.push(...mainPayload);

Expand All @@ -247,9 +246,10 @@ async function handleFetchCrossbarPrices(
if (process.env.SWITCHBOARD_CROSSSBAR_API_FALLBACK) {
// fallback crossbar
const { payload: fallbackPayload, brokenFeeds: fallbackBrokenFeeds } = await fetchCrossbarPrices(
mainBrokenFeeds,
brokenFeeds,
process.env.SWITCHBOARD_CROSSSBAR_API_FALLBACK
);
brokenFeeds = fallbackBrokenFeeds;
payload.push(...fallbackPayload);

if (!fallbackBrokenFeeds.length) {
Expand All @@ -258,7 +258,7 @@ async function handleFetchCrossbarPrices(
}

// birdeye as last resort
const { payload: birdeyePayload, brokenFeeds: birdeyeBrokenFeeds } = await fetchBirdeyePrices(feedHashes, mintMap);
const { payload: birdeyePayload, brokenFeeds: birdeyeBrokenFeeds } = await fetchBirdeyePrices(brokenFeeds, mintMap);

payload.push(...birdeyePayload);

Expand Down Expand Up @@ -309,7 +309,7 @@ async function fetchBirdeyePrices(

return { payload: finalPayload, brokenFeeds };
} catch (error) {
console.error("Error:", error);
console.log("Error:", "fetch from birdeye failed");
return { payload: [], brokenFeeds: feedHashes };
}
}
Expand All @@ -327,6 +327,8 @@ async function fetchCrossbarPrices(

const isAuth = username && bearer;

const isCrossbarMain = endpoint.includes("switchboard.xyz");

const basicAuth = isAuth ? Buffer.from(`${username}:${bearer}`).toString("base64") : undefined;

try {
Expand Down Expand Up @@ -358,7 +360,8 @@ async function fetchCrossbarPrices(

return { payload: finalPayload, brokenFeeds: brokenFeeds };
} catch (error) {
console.error("Error:", error);
const errorMessage = isCrossbarMain ? "Couldn't fetch from crossbar" : "Couldn't fetch from fallback crossbar";
console.log("Error:", errorMessage);
return { payload: [], brokenFeeds: feedHashes };
}
}
Expand Down Expand Up @@ -464,7 +467,6 @@ async function fetchMultiPrice(tokens: string[]): Promise<BirdeyePriceResponse>
const data = (await response.json()) as BirdeyePriceResponse;
return data;
} catch (error) {
console.error("Error:", error);
throw new Error("Error fetching birdey prices");
}
}

0 comments on commit 6f8f816

Please sign in to comment.