Skip to content

Commit

Permalink
chore: update oracle price cache timing
Browse files Browse the repository at this point in the history
  • Loading branch information
borcherd committed Nov 20, 2024
1 parent 2fa7e5c commit 3717195
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions apps/marginfi-v2-trading/src/pages/api/oracle/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import config from "~/config/marginfi";
const SWITCHBOARD_CROSSSBAR_API = process.env.SWITCHBOARD_CROSSSBAR_API || "https://crossbar.switchboard.xyz";
const IS_SWB_STAGE = SWITCHBOARD_CROSSSBAR_API === "https://staging.crossbar.switchboard.xyz";

const S_MAXAGE_TIME = 10;
const STALE_WHILE_REVALIDATE_TIME = 15;

interface OracleData {
oracleKey: string;
oracleSetup: OracleSetup;
Expand Down Expand Up @@ -135,7 +138,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const currentTime = Math.round(Date.now() / 1000);
const oracleTime = oraclePrice.timestamp.toNumber();
const isStale = currentTime - oracleTime > oracleData.maxAge;
const maxAge = oracleData.maxAge + S_MAXAGE_TIME; // add some buffer to maxAge to account for api route cache
const isStale = currentTime - oracleTime > maxAge;

// If on-chain data is recent enough, use it even for SwitchboardPull oracles
if (oracleData.oracleSetup === OracleSetup.SwitchboardPull && isStale) {
Expand Down Expand Up @@ -190,7 +194,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const updatedOraclePricesSorted = requestedOraclesData.map((value) => updatedOraclePrices.get(value.oracleKey)!);

res.setHeader("Cache-Control", "s-maxage=10, stale-while-revalidate=59");
res.setHeader("Cache-Control", `s-maxage=${S_MAXAGE_TIME}, stale-while-revalidate=${STALE_WHILE_REVALIDATE_TIME}`);
return res.status(200).json(updatedOraclePricesSorted.map(stringifyOraclePrice));
} catch (error) {
console.error("Error:", error);
Expand Down
8 changes: 6 additions & 2 deletions apps/marginfi-v2-ui/src/pages/api/oracle/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import config from "~/config/marginfi";

const SWITCHBOARD_CROSSSBAR_API = "https://crossbar.switchboard.xyz";

const S_MAXAGE_TIME = 10;
const STALE_WHILE_REVALIDATE_TIME = 15;

interface OracleData {
oracleKey: string;
oracleSetup: OracleSetup;
Expand Down Expand Up @@ -132,7 +135,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const currentTime = Math.round(Date.now() / 1000);
const oracleTime = oraclePrice.timestamp.toNumber();
const isStale = currentTime - oracleTime > oracleData.maxAge;
const maxAge = oracleData.maxAge + S_MAXAGE_TIME; // add some buffer to maxAge to account for api route cache
const isStale = currentTime - oracleTime > maxAge;

// If on-chain data is recent enough, use it even for SwitchboardPull oracles
if (oracleData.oracleSetup === OracleSetup.SwitchboardPull && isStale) {
Expand Down Expand Up @@ -187,7 +191,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const updatedOraclePricesSorted = requestedOraclesData.map((value) => updatedOraclePrices.get(value.oracleKey)!);

res.setHeader("Cache-Control", "s-maxage=10, stale-while-revalidate=59");
res.setHeader("Cache-Control", `s-maxage=${S_MAXAGE_TIME}, stale-while-revalidate=${STALE_WHILE_REVALIDATE_TIME}`);
return res.status(200).json(updatedOraclePricesSorted.map(stringifyOraclePrice));
} catch (error) {
console.error("Error:", error);
Expand Down

0 comments on commit 3717195

Please sign in to comment.