From 94f9f296d9789423d5ed1067414fabc3ac695b44 Mon Sep 17 00:00:00 2001 From: Diogo Soares <32431609+DiogoSoaress@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:16:56 +0100 Subject: [PATCH] refactor: move the Dune data fetching functions to their own files --- src/hooks/useSafeStats.ts | 57 ------------------------------ src/lib/fetchMonthlyActiveUsers.ts | 17 +++++++++ src/lib/fetchTotalBalanceUsd.ts | 12 +++++++ src/lib/fetchTotalSafesDeployed.ts | 17 +++++++++ src/lib/fetchTotalTransactions.ts | 17 +++++++++ src/pages/index.tsx | 4 ++- src/pages/press.tsx | 2 +- src/pages/wallet.tsx | 3 +- 8 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 src/lib/fetchMonthlyActiveUsers.ts create mode 100644 src/lib/fetchTotalBalanceUsd.ts create mode 100644 src/lib/fetchTotalSafesDeployed.ts create mode 100644 src/lib/fetchTotalTransactions.ts diff --git a/src/hooks/useSafeStats.ts b/src/hooks/useSafeStats.ts index b532f90e..56c431cc 100644 --- a/src/hooks/useSafeStats.ts +++ b/src/hooks/useSafeStats.ts @@ -1,63 +1,6 @@ import { useContext } from 'react' import { formatValue } from '@/lib/formatValue' import SafeStatsContext from '@/contexts/SafeStatsContext' -import { DUNE_API_KEY } from '@/config/constants' -import { duneQueryUrlBuilder } from '@/lib/duneQueryUrlBuilder' - -const QUERY_ID_TOTAL_TRANSACTIONS = 2093960 -const QUERY_ID_TOTAL_ASSETS_BY_CHAIN = 3609251 -const QUERY_ID_TOTAL_SAFES_DEPLOYED = 2459401 -const QUERY_ID_MONTHLY_ACTIVE_USERS = 4151164 - -export const fetchTotalTransactions = async (): Promise => { - return fetch(duneQueryUrlBuilder(QUERY_ID_TOTAL_TRANSACTIONS, DUNE_API_KEY)) - .then((res) => { - if (!res.ok) { - throw new Error(res.statusText) - } - - return res.json() - }) - .then((data) => data.result.rows[0].num_txs) - .catch((err) => console.error(`Error fetching total number of transactions: ${err.message}`)) -} - -export const fetchTotalBalanceUsd = async (): Promise => { - // TODO: Uncomment this code after agreeing with the narrative team on the data source - return null - // return fetch(duneQueryUrlBuilder(QUERY_ID_TOTAL_ASSETS_BY_CHAIN, DUNE_API_KEY)) - // .then((res) => res.json()) - // .then((data) => { - // return data.result.rows[0].total_balance_usd - // }) - // .catch(() => null) -} - -export const fetchTotalSafesDeployed = async (): Promise => { - return fetch(duneQueryUrlBuilder(QUERY_ID_TOTAL_SAFES_DEPLOYED, DUNE_API_KEY)) - .then((res) => { - if (!res.ok) { - throw new Error(res.statusText) - } - - return res.json() - }) - .then((data) => data.result.rows[0].num_safes) - .catch((err) => console.error(`Error fetching total safes deployed: ${err.message}`)) -} - -export const fetchMonthlyActiveUsers = async (): Promise => { - return fetch(duneQueryUrlBuilder(QUERY_ID_MONTHLY_ACTIVE_USERS, DUNE_API_KEY)) - .then((res) => { - if (!res.ok) { - throw new Error(res.statusText) - } - - return res.json() - }) - .then((data) => data.result.rows[data.result.rows.length - 1].active_users) - .catch((err) => console.error(`Error fetching monthly active users: ${err.message}`)) -} type SafeStats = { formattedTotalTransactions: string | null diff --git a/src/lib/fetchMonthlyActiveUsers.ts b/src/lib/fetchMonthlyActiveUsers.ts new file mode 100644 index 00000000..05da20c1 --- /dev/null +++ b/src/lib/fetchMonthlyActiveUsers.ts @@ -0,0 +1,17 @@ +import { DUNE_API_KEY } from '@/config/constants' +import { duneQueryUrlBuilder } from '@/lib/duneQueryUrlBuilder' + +const QUERY_ID_MONTHLY_ACTIVE_USERS = 4151164 + +export const fetchMonthlyActiveUsers = async (): Promise => { + return fetch(duneQueryUrlBuilder(QUERY_ID_MONTHLY_ACTIVE_USERS, DUNE_API_KEY)) + .then((res) => { + if (!res.ok) { + throw new Error(res.statusText) + } + + return res.json() + }) + .then((data) => data.result.rows[data.result.rows.length - 1].active_users) + .catch((err) => console.error(`Error fetching monthly active users: ${err.message}`)) +} diff --git a/src/lib/fetchTotalBalanceUsd.ts b/src/lib/fetchTotalBalanceUsd.ts new file mode 100644 index 00000000..7eb8b39f --- /dev/null +++ b/src/lib/fetchTotalBalanceUsd.ts @@ -0,0 +1,12 @@ +const QUERY_ID_TOTAL_ASSETS_BY_CHAIN = 3609251 + +export const fetchTotalBalanceUsd = async (): Promise => { + // TODO: Uncomment this code after agreeing with the narrative team on the data source + return null + // return fetch(duneQueryUrlBuilder(QUERY_ID_TOTAL_ASSETS_BY_CHAIN, DUNE_API_KEY)) + // .then((res) => res.json()) + // .then((data) => { + // return data.result.rows[0].total_balance_usd + // }) + // .catch(() => null) +} diff --git a/src/lib/fetchTotalSafesDeployed.ts b/src/lib/fetchTotalSafesDeployed.ts new file mode 100644 index 00000000..e667c6ee --- /dev/null +++ b/src/lib/fetchTotalSafesDeployed.ts @@ -0,0 +1,17 @@ +import { DUNE_API_KEY } from '@/config/constants' +import { duneQueryUrlBuilder } from '@/lib/duneQueryUrlBuilder' + +const QUERY_ID_TOTAL_SAFES_DEPLOYED = 2459401 + +export const fetchTotalSafesDeployed = async (): Promise => { + return fetch(duneQueryUrlBuilder(QUERY_ID_TOTAL_SAFES_DEPLOYED, DUNE_API_KEY)) + .then((res) => { + if (!res.ok) { + throw new Error(res.statusText) + } + + return res.json() + }) + .then((data) => data.result.rows[0].num_safes) + .catch((err) => console.error(`Error fetching total safes deployed: ${err.message}`)) +} diff --git a/src/lib/fetchTotalTransactions.ts b/src/lib/fetchTotalTransactions.ts new file mode 100644 index 00000000..0bc23883 --- /dev/null +++ b/src/lib/fetchTotalTransactions.ts @@ -0,0 +1,17 @@ +import { DUNE_API_KEY } from '@/config/constants' +import { duneQueryUrlBuilder } from '@/lib/duneQueryUrlBuilder' + +const QUERY_ID_TOTAL_TRANSACTIONS = 2093960 + +export const fetchTotalTransactions = async (): Promise => { + return fetch(duneQueryUrlBuilder(QUERY_ID_TOTAL_TRANSACTIONS, DUNE_API_KEY)) + .then((res) => { + if (!res.ok) { + throw new Error(res.statusText) + } + + return res.json() + }) + .then((data) => data.result.rows[0].num_txs) + .catch((err) => console.error(`Error fetching total number of transactions: ${err.message}`)) +} diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 9879fef1..b7ca66c6 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,7 +1,9 @@ import type { InferGetStaticPropsType, NextPage } from 'next' import { loadChainsData } from '@/lib/loadChainsData' import { Home } from '@/components/Home' -import { fetchTotalBalanceUsd, fetchTotalSafesDeployed, fetchTotalTransactions } from '@/hooks/useSafeStats' +import { fetchTotalTransactions } from '@/lib/fetchTotalTransactions' +import { fetchTotalBalanceUsd } from '@/lib/fetchTotalBalanceUsd' +import { fetchTotalSafesDeployed } from '@/lib/fetchTotalSafesDeployed' const IndexPage: NextPage> = (props) => { return diff --git a/src/pages/press.tsx b/src/pages/press.tsx index a48c1958..836d9b25 100644 --- a/src/pages/press.tsx +++ b/src/pages/press.tsx @@ -1,7 +1,7 @@ import client from '@/lib/contentful' import PressRoom, { type PressRoomProps } from '@/components/Pressroom' import type { TypePressRoomSkeleton, TypePostSkeleton } from '@/contentful/types' -import { fetchTotalBalanceUsd } from '@/hooks/useSafeStats' +import { fetchTotalBalanceUsd } from '@/lib/fetchTotalBalanceUsd' import type { GetStaticProps, InferGetStaticPropsType, NextPage } from 'next' const PressroomPage: NextPage> = (props) => { diff --git a/src/pages/wallet.tsx b/src/pages/wallet.tsx index e58467f2..c396e78d 100644 --- a/src/pages/wallet.tsx +++ b/src/pages/wallet.tsx @@ -3,7 +3,8 @@ import client from '@/lib/contentful' import { Wallet } from '@/components/Wallet' import type { TypeBaseBlockSkeleton } from '@/contentful/types' import { fetchDataRoomStats } from '@/hooks/useSafeDataRoomStats' -import { fetchMonthlyActiveUsers, fetchTotalBalanceUsd } from '@/hooks/useSafeStats' +import { fetchTotalBalanceUsd } from '@/lib/fetchTotalBalanceUsd' +import { fetchMonthlyActiveUsers } from '@/lib/fetchMonthlyActiveUsers' const FAQ_CONTENT_TYPE_ID = '1jCIVFDUzFO1okK8b6TTxS'