Skip to content

Commit

Permalink
fix: added lst apy endpoint again
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Nov 12, 2024
1 parent 0d3a545 commit 5f71140
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
75 changes: 75 additions & 0 deletions apps/marginfi-v2-ui/src/pages/api/stakingApy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { NextApiRequest, NextApiResponse } from "next";
import NodeCache from "node-cache";
import { Readable } from "stream";
import { parse } from "csv-parse";

type PriceRecord = {
timestamp: number;
epoch: number;
price: number;
};

const parsePriceRecordsFromCSV = async (csv: Readable): Promise<PriceRecord[]> => {
const csvParser = parse({ delimiter: ",", columns: true });
const records: PriceRecord[] = [];
for await (const row of csv.pipe(csvParser)) {
const { timestamp, epoch, price } = row;
if (!timestamp || !epoch || !price) {
throw new Error('Columns "timestamp", "epoch", "price" must be present!');
}
const record = {
timestamp: Math.round(new Date(timestamp).getTime() / 1e3),
epoch: Number(epoch),
price: Number(price),
};
if (isNaN(record.timestamp)) {
throw new Error("Timestamp must be a... timestamp!");
}
if (isNaN(record.epoch)) {
throw new Error("Epoch must be a number!");
}
if (isNaN(record.price)) {
throw new Error("Price must be a number!");
}

records.push(record);
}
return records;
};

const fetchAndParsePricesCsv = async (url: string) => {
const csvResponse = await fetch(url);
const csvContents = await csvResponse.text();
const prices = await parsePriceRecordsFromCSV(Readable.from([csvContents]));

return prices;
};

const myCache = new NodeCache({ stdTTL: 1800 }); // Cache for 1 hour
const SOLANA_COMPASS_BASE_URL = "https://raw.githubusercontent.com/glitchful-dev/sol-stake-pool-apy/master/db/";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { solanaCompassKey } = req.query;

if (!solanaCompassKey) {
return res.status(400).json({ error: "solanaCompassKey is required" });
}

const cacheKey = `key_${solanaCompassKey.toString()}`;

const cachedData = myCache.get(cacheKey);

if (cachedData) {
res.status(200).json(cachedData);
return;
}

try {
const solanaCompassPrices = await fetchAndParsePricesCsv(`${SOLANA_COMPASS_BASE_URL}${solanaCompassKey}.csv`);
myCache.set(cacheKey, solanaCompassPrices);
res.status(200).json(solanaCompassPrices);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Error fetching data" });
}
}
2 changes: 1 addition & 1 deletion packages/mrgn-utils/src/lst-apy.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const calculateLstYield = async (bank: ExtendedBankInfo) => {
const solanaCompassKey = LSTS_SOLANA_COMPASS_MAP[bank.meta.tokenSymbol];
if (!solanaCompassKey) return 0;

const response = await fetch(`/api/lst?solanaCompassKey=${solanaCompassKey}`);
const response = await fetch(`/api/stakingApy?solanaCompassKey=${solanaCompassKey}`);
if (!response.ok) return 0;

const solanaCompassPrices = await response.json();
Expand Down

0 comments on commit 5f71140

Please sign in to comment.