From d77c7eb896f9513f21e2bb0971b903d91e3c0b58 Mon Sep 17 00:00:00 2001 From: BoThe1K Date: Thu, 16 Nov 2023 17:55:01 +0100 Subject: [PATCH] feat/qb-2301: Add apis for coingecko/marketcap --- .../src/pages/api/circulating-supply.tsx | 16 ++++++++++++++++ apps/web-stratos/src/pages/api/total-supply.tsx | 16 ++++++++++++++++ .../home/components/charts/TokenomicChart.tsx | 2 -- .../screens/home/components/tokenomics/index.tsx | 1 - .../screens/home/components/tokenomics/utils.ts | 7 ++++++- 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 apps/web-stratos/src/pages/api/circulating-supply.tsx create mode 100644 apps/web-stratos/src/pages/api/total-supply.tsx diff --git a/apps/web-stratos/src/pages/api/circulating-supply.tsx b/apps/web-stratos/src/pages/api/circulating-supply.tsx new file mode 100644 index 0000000000..3491820611 --- /dev/null +++ b/apps/web-stratos/src/pages/api/circulating-supply.tsx @@ -0,0 +1,16 @@ +import { getMetrics } from '@/screens/home/components/tokenomics/queries'; +import { bigToNativeDecimals } from '@/screens/home/components/tokenomics/utils'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +type MetricsData = { + circulation_supply: string; +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const result: MetricsData = await getMetrics(); + res.status(200).send(bigToNativeDecimals(result.circulation_supply)); + } catch (err) { + res.status(400).json({ error: 'failed to load data' }); + } +} diff --git a/apps/web-stratos/src/pages/api/total-supply.tsx b/apps/web-stratos/src/pages/api/total-supply.tsx new file mode 100644 index 0000000000..dd83b2030f --- /dev/null +++ b/apps/web-stratos/src/pages/api/total-supply.tsx @@ -0,0 +1,16 @@ +import { getMetrics } from '@/screens/home/components/tokenomics/queries'; +import { bigToNativeDecimals } from '@/screens/home/components/tokenomics/utils'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +type MetricsData = { + total_supply: string; +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const result: MetricsData = await getMetrics(); + res.status(200).send(bigToNativeDecimals(result.total_supply)); + } catch (err) { + res.status(400).json({ error: 'failed to load data' }); + } +} diff --git a/apps/web-stratos/src/screens/home/components/charts/TokenomicChart.tsx b/apps/web-stratos/src/screens/home/components/charts/TokenomicChart.tsx index 9397a77aa2..50c137a30e 100644 --- a/apps/web-stratos/src/screens/home/components/charts/TokenomicChart.tsx +++ b/apps/web-stratos/src/screens/home/components/charts/TokenomicChart.tsx @@ -1,7 +1,5 @@ import { useId, FC } from 'react'; -import { BigNumber } from 'ethers'; - import { Tooltip, BarChart, CartesianGrid, XAxis, YAxis, Bar } from 'recharts'; interface ITokenomicChart { diff --git a/apps/web-stratos/src/screens/home/components/tokenomics/index.tsx b/apps/web-stratos/src/screens/home/components/tokenomics/index.tsx index 1fcc4f2009..84fd4507f4 100644 --- a/apps/web-stratos/src/screens/home/components/tokenomics/index.tsx +++ b/apps/web-stratos/src/screens/home/components/tokenomics/index.tsx @@ -1,5 +1,4 @@ import { FC } from 'react'; -import numeral from 'numeral'; import dynamic from 'next/dynamic'; import chainConfig from '@/chainConfig'; diff --git a/apps/web-stratos/src/screens/home/components/tokenomics/utils.ts b/apps/web-stratos/src/screens/home/components/tokenomics/utils.ts index 3867fd6ffc..d51834f7d7 100644 --- a/apps/web-stratos/src/screens/home/components/tokenomics/utils.ts +++ b/apps/web-stratos/src/screens/home/components/tokenomics/utils.ts @@ -1,6 +1,11 @@ -import { BigNumberish } from 'ethers'; +import { BigNumberish, FixedNumber } from 'ethers'; import { formatUnits } from 'ethers/lib/utils'; export function prettyFormat(value: BigNumberish, unitName?: number, points = 8): number { return +(+formatUnits(value, unitName)).toFixed(points); } + +export const bigToNativeDecimals = (bn: string, decimals?: number): string => + FixedNumber.fromString(bn) + .divUnsafe(FixedNumber.from((10 ** (decimals || 18)).toString())) + .toString();