Skip to content

Commit

Permalink
feat: added get positions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Dec 13, 2024
1 parent 0f21521 commit 3b467c6
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions apps/marginfi-v2-trading/src/pages/api/pool/positions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NextApiRequest, NextApiResponse } from "next";
import { PoolApiResponse } from "~/types/api.types";

// Cache times in seconds
const S_MAXAGE_TIME = 60 * 4; // 4 minutes
const STALE_WHILE_REVALIDATE_TIME = 60 * 10; // 10 minutes

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (!process.env.MARGINFI_API_URL) {
return res.status(500).json({ error: "API URL is not set" });
}

const { address } = req.query;

console.log({ address });

try {
const response = await fetch(`${process.env.MARGINFI_API_URL}/arena/positions/${address}`, {
headers: {
Accept: "application/json",
},
});

if (!response.ok) {
throw new Error(`API responded with status: ${response.status}`);
}

const data = await response.json();

// Set cache headers
res.setHeader("Cache-Control", `s-maxage=${S_MAXAGE_TIME}, stale-while-revalidate=${STALE_WHILE_REVALIDATE_TIME}`);

return res.status(200).json(data);
} catch (error) {
console.error("Error fetching pool data:", error);
return res.status(500).json({
error: "Internal server error",
message: process.env.NODE_ENV === "development" ? error : undefined,
});
}
}

0 comments on commit 3b467c6

Please sign in to comment.