From a7c2169b3dd3f496a95b1d6c8cf2fc0b5a04cdf8 Mon Sep 17 00:00:00 2001 From: David Mihal Date: Mon, 18 Jul 2022 00:47:58 +0100 Subject: [PATCH] Average fee chart --- components/AverageFeeChart.tsx | 80 ++++++++++++++++++++++++++++++++++ data/sdk.ts | 11 +++-- pages/index.tsx | 49 ++++++++++++++++----- 3 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 components/AverageFeeChart.tsx diff --git a/components/AverageFeeChart.tsx b/components/AverageFeeChart.tsx new file mode 100644 index 0000000..6e474c3 --- /dev/null +++ b/components/AverageFeeChart.tsx @@ -0,0 +1,80 @@ +import React from 'react' +import { + BarChart, + Bar, + XAxis, + YAxis, + CartesianGrid, + Tooltip, + Legend, + ResponsiveContainer +} from "recharts" + +interface AverageFeeChartProps { + data: { + date: number + [protocol: string]: number + }[] +} + +const toNiceDate = (timestamp: number, year?: boolean) => { + return (new Date(timestamp * 1000)).toLocaleString([], { + month: 'long', + day: 'numeric', + year: year ? 'numeric' : undefined, + timeZone: 'UTC', + }); +} + +function getColorFromStr(stringInput: string) { + const stringUniqueHash = Array.from(stringInput).reduce((acc, char) => { + return char.charCodeAt(0) + ((acc << 5) - acc); + }, 0); + return `hsl(${stringUniqueHash % 360}, 95%, 35%)`; +} + +const AverageFeeChart: React.FC = ({ data }) => { + return ( +
+ + + + toNiceDate(timestamp)} + /> + + toNiceDate(timestamp)} + formatter={(val: number) => '$' + val.toFixed(2)} + /> + + {Object.keys(data[data.length - 1]).map(key => key !== 'date' && ( + + ))} + + + + +
+ ) +} + +export default AverageFeeChart; diff --git a/data/sdk.ts b/data/sdk.ts index 581f42f..feaecfe 100644 --- a/data/sdk.ts +++ b/data/sdk.ts @@ -6,11 +6,16 @@ const sdk = new CryptoStatsSDK({ redisConnectionString: process.env.REDIS_URL, }); +const cacheQueryByParam = (cacheQuery: string) => + (_id: string, query: string, params: string[]) => query === cacheQuery ? params[0] : null + sdk .getCollection('rollup-l1-fees') - .setCacheKeyResolver((_id: string, query: string, params: string[]) => - query === 'oneDayFeesPaidUSD' ? params[0] : null - ); + .setCacheKeyResolver(cacheQueryByParam('oneDayFeesPaidUSD')); + +sdk + .getCollection('historic-l2-fees') + .setCacheKeyResolver(cacheQueryByParam('oneDayAverageFeeSwap')); sdk.ethers.addProvider('arbitrum-one', 'https://arb1.arbitrum.io/rpc'); sdk.ethers.addProvider('optimism', 'https://mainnet.optimism.io'); diff --git a/pages/index.tsx b/pages/index.tsx index 6d38e6b..044da3c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -5,12 +5,14 @@ import List from 'components/List'; import SocialTags from 'components/SocialTags'; import ToggleNavBar from 'components/ToggleNavBar'; import ToggleBar from 'components/ToggleBar'; +import AverageFeeChart from 'components/AverageFeeChart'; interface HomeProps { data: any[]; + historicData: any[]; } -export const Home: NextPage = ({ data }) => { +export const Home: NextPage = ({ data, historicData }) => { const [mode, setMode] = useState('l2s'); let _data = mode === 'rollups' ? data.filter((item) => item.id !== 'metisnetwork') : data; @@ -56,6 +58,8 @@ export const Home: NextPage = ({ data }) => { + +