Skip to content

Commit

Permalink
fix: baseUrl logic in price endpoint, improved price formatting in as…
Browse files Browse the repository at this point in the history
…set list
  • Loading branch information
chambaz committed Jan 7, 2025
1 parent 7ba0bc9 commit 2c4038c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import React from "react";

import Image from "next/image";
import Link from "next/link";
import { aprToApy, numeralFormatter, percentFormatter, usdFormatter } from "@mrgnlabs/mrgn-common";
import {
aprToApy,
dynamicNumeralFormatter,
numeralFormatter,
percentFormatter,
usdFormatter,
} from "@mrgnlabs/mrgn-common";
import { IconAlertTriangle, IconExternalLink } from "@tabler/icons-react";

import {
Expand Down Expand Up @@ -40,7 +46,16 @@ export const getAssetPriceCell = ({
}: AssetPriceData) => (
<div className="relative flex items-center justify-end gap-1.5">
<div className="relative">
{assetPrice >= 0.01 ? usdFormatter.format(assetPrice) : `$${assetPrice.toExponential(2)}`}
{/* {assetPrice >= 0.01 ? usdFormatter.format(assetPrice) : `$${assetPrice.toExponential(2)}`} */}
{assetPrice >= 1
? usdFormatter.format(assetPrice)
: assetPrice >= 0.0000001
? `$${dynamicNumeralFormatter(assetPrice, {
minDisplay: 0.0000001,
tokenPrice: assetPrice,
forceDecimals: true,
})}`
: `$${assetPrice.toExponential(2)}`}
{assetPriceOffset > assetPrice * 0.1 && (
<div className="absolute top-[-8px] right-2">
<TooltipProvider>
Expand Down
9 changes: 7 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 @@ -432,16 +432,21 @@ async function fetchMultiPrice(tokens: string[]): Promise<BirdeyePriceResponse>
? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`
: process.env.VERCEL_BRANCH_URL
? `https://${process.env.VERCEL_BRANCH_URL}`
: "localhost:3004";
: "http://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;

if (!data || !data.success) {
throw new Error("Error fetching birdeye prices");
}

return data;
} catch (error) {
throw new Error("Error fetching birdey prices");
throw new Error("Error fetching birdeye prices");
}
}
9 changes: 8 additions & 1 deletion apps/marginfi-v2-ui/src/pages/api/tokens/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
const data = await response.json();

// cache for 20 minutes
if (!data || !data.data) {
console.log("data not found");
return res.status(404).json({
error: "Birdeye API error: data not found",
});
}

// cache for 20 minutes (1200 seconds)
res.setHeader("Cache-Control", "s-maxage=1200, stale-while-revalidate=300");
res.status(200).json(data);
} catch (error) {
Expand Down
7 changes: 4 additions & 3 deletions packages/mrgn-common/src/utils/formatters.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const numeralFormatter = (value: number) => {
interface dynamicNumeralFormatterOptions {
minDisplay?: number;
tokenPrice?: number;
forceDecimals?: boolean;
}

export const dynamicNumeralFormatter = (value: number, options: dynamicNumeralFormatterOptions = {}) => {
const { minDisplay = 0.00001, tokenPrice } = options;
const { minDisplay = 0.00001, tokenPrice, forceDecimals } = options;

if (value === 0) return "0";

Expand All @@ -43,11 +44,11 @@ export const dynamicNumeralFormatter = (value: number, options: dynamicNumeralFo
}

if (Math.abs(value) > 10000) {
return numeral(value).format("0,0.[00]a");
return numeral(value).format(forceDecimals ? "0,0.00a" : "0,0.[00]a");
}

if (Math.abs(value) >= 0.01) {
return numeral(value).format("0,0.[0000]a");
return numeral(value).format(forceDecimals ? "0,0.00a" : "0,0.[0000]a");
}

if (tokenPrice) {
Expand Down

0 comments on commit 2c4038c

Please sign in to comment.