Skip to content

Commit

Permalink
refactor: move the Dune data fetching functions to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoSoaress committed Dec 6, 2024
1 parent d9e4992 commit 94f9f29
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 60 deletions.
57 changes: 0 additions & 57 deletions src/hooks/useSafeStats.ts
Original file line number Diff line number Diff line change
@@ -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<number | null> => {
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<number | null> => {
// 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<number | null> => {
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<number | null> => {
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
Expand Down
17 changes: 17 additions & 0 deletions src/lib/fetchMonthlyActiveUsers.ts
Original file line number Diff line number Diff line change
@@ -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<number | null> => {
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}`))
}
12 changes: 12 additions & 0 deletions src/lib/fetchTotalBalanceUsd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const QUERY_ID_TOTAL_ASSETS_BY_CHAIN = 3609251

export const fetchTotalBalanceUsd = async (): Promise<number | null> => {
// 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)
}
17 changes: 17 additions & 0 deletions src/lib/fetchTotalSafesDeployed.ts
Original file line number Diff line number Diff line change
@@ -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<number | null> => {
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}`))
}
17 changes: 17 additions & 0 deletions src/lib/fetchTotalTransactions.ts
Original file line number Diff line number Diff line change
@@ -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<number | null> => {
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}`))
}
4 changes: 3 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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<InferGetStaticPropsType<typeof getStaticProps>> = (props) => {
return <Home {...props} />
Expand Down
2 changes: 1 addition & 1 deletion src/pages/press.tsx
Original file line number Diff line number Diff line change
@@ -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<InferGetStaticPropsType<typeof getStaticProps>> = (props) => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 94f9f29

Please sign in to comment.