From be36171b0cffcdbaadc58dfb6923c4bc5bd7e7c6 Mon Sep 17 00:00:00 2001 From: Joe Le <55604805+nguyenlejoe@users.noreply.github.com> Date: Mon, 15 Jan 2024 16:30:54 -0800 Subject: [PATCH] feat: XYK pool timeseries (#46) --- .../XYKPoolTimeSeriesView.stories.tsx | 19 +++ .../XYKPoolTimeSeriesView.tsx | 153 ++++++++++++++++++ src/utils/functions/capitalize.ts | 3 + src/utils/types/molecules.types.ts | 9 +- 4 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.stories.tsx create mode 100644 src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.tsx create mode 100644 src/utils/functions/capitalize.ts diff --git a/src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.stories.tsx b/src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.stories.tsx new file mode 100644 index 00000000..c202a8e7 --- /dev/null +++ b/src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.stories.tsx @@ -0,0 +1,19 @@ +import { type Meta, type StoryObj } from "@storybook/react"; +import { XYKPoolTimeSeriesView } from "./XYKPoolTimeSeriesView"; + +type Story = StoryObj; + +const meta: Meta = { + title: "Molecules/XYK", + component: XYKPoolTimeSeriesView, +}; + +export default meta; + +export const XYKPoolTimeSeries: Story = { + args: { + chain_name: "eth-mainnet", + dex_name: "uniswap_v2", + pool_address: "0x02af166a28393809f55bb5befbcc27ec15908241", + }, +}; diff --git a/src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.tsx b/src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.tsx new file mode 100644 index 00000000..f55560e4 --- /dev/null +++ b/src/components/Molecules/XYK/XYKPoolTimeSeriesView/XYKPoolTimeSeriesView.tsx @@ -0,0 +1,153 @@ +import { type Option, None, Some } from "@/utils/option"; +import { useEffect, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { BarChart } from "@tremor/react"; +import { rootColor, timestampParser } from "@/utils/functions"; +import { TypographyH4 } from "@/components/ui/typography"; +import { Skeleton } from "@/components/ui/skeleton"; +import { GRK_SIZES, PERIOD } from "@/utils/constants/shared.constants"; +import { CHART_COLORS } from "@/utils/constants/shared.constants"; +import { useCovalent } from "@/utils/store/Covalent"; +import { type XYKPoolTimeSeriesViewProps } from "@/utils/types/molecules.types"; +import { prettifyCurrency } from "@covalenthq/client-sdk"; +import { capitalizeFirstLetter } from "@/utils/functions/capitalize"; + +export const XYKPoolTimeSeriesView: React.FC = ({ + chain_name, + dex_name, + pool_address, + pool_data, +}) => { + const [maybeResult, setResult] = useState>(None); + const [chartData, setChartData] = useState>(None); + const [period, setPeriod] = useState(PERIOD.DAYS_7); + const [timeSeries, setTimeSerious] = useState("liquidity"); + const [chartColor, setColor] = useState(""); + const { covalentClient } = useCovalent(); + + const handleChartData = () => { + maybeResult.match({ + None: () => null, + Some: (response) => { + const chart_key = `${timeSeries}_timeseries_${period}d`; + const value_key = + timeSeries === "price" + ? "price_of_token0_in_token1" + : `${timeSeries}_quote`; + + const result = response[chart_key].map((x: any) => { + const dt = timestampParser(x.dt, "DD MMM YY"); + return { + date: dt, + [`${capitalizeFirstLetter(timeSeries)} (USD)`]: + x[value_key], + }; + }); + setChartData(new Some(result)); + }, + }); + }; + + useEffect(() => { + (async () => { + setResult(None); + const response = await covalentClient.XykService.getPoolByAddress( + chain_name, + dex_name, + pool_address + ); + setColor(rootColor()); + setResult(new Some(response.data.items[0])); + })(); + }, [pool_data, dex_name, chain_name]); + + useEffect(() => { + handleChartData(); + }, [maybeResult, period, timeSeries]); + + const body = chartData.match({ + None: () => { + return ( +
+ +
+ ); + }, + Some: (result) => { + return ( +
+ +
+ ); + }, + }); + + return ( +
+
+ {`${capitalizeFirstLetter( + timeSeries + )} (USD)`} +
+ +
+
+ + + {/* */} +
+
+ + +
+
+ + {body} +
+ ); +}; diff --git a/src/utils/functions/capitalize.ts b/src/utils/functions/capitalize.ts new file mode 100644 index 00000000..152191d4 --- /dev/null +++ b/src/utils/functions/capitalize.ts @@ -0,0 +1,3 @@ +export function capitalizeFirstLetter(str: string) { + return str.charAt(0).toUpperCase() + str.slice(1); +} diff --git a/src/utils/types/molecules.types.ts b/src/utils/types/molecules.types.ts index a79ee6e9..2a5aa65f 100644 --- a/src/utils/types/molecules.types.ts +++ b/src/utils/types/molecules.types.ts @@ -1,4 +1,4 @@ -import { type Chain } from "@covalenthq/client-sdk"; +import { type Chain, type PoolWithTimeseries } from "@covalenthq/client-sdk"; export interface AccountCardViewProps { name?: string; @@ -23,6 +23,13 @@ export interface NFTSalesCountViewProps { token_id?: string; } +export interface XYKPoolTimeSeriesViewProps { + chain_name: Chain; + dex_name: string; + pool_address: string; + pool_data?: PoolWithTimeseries; +} + export interface NFTVolumeViewProps { chain_name: Chain; collection_address: string;