-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
474a044
commit 0065bd4
Showing
5 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import useSWR from 'swr' | ||
import { formatValue } from '@/lib/formatValue' | ||
import { DUNE_API_KEY } from '@/config/constants' | ||
|
||
const QUERY_ID_TOTAL_TRANSACTIONS = 2093960 | ||
const QUERY_ID_TOTAL_ASSETS = 2771785 | ||
const QUERY_ID_TOTAL_SAFES_DEPOLOYED = 2459401 | ||
|
||
const fallbackStats = ['16M', '$56B', '4.3M'] | ||
|
||
const fetcher = (url: string) => | ||
fetch(url) | ||
.then((res) => res.json()) | ||
.then((data) => data.result.rows[0]) | ||
|
||
function totalAssetsEndpoint(queryId: number): string { | ||
return `https://api.dune.com/api/v1/query/${queryId}/results?api_key=${DUNE_API_KEY}` | ||
} | ||
|
||
export const useSafeNumbers = (): Array<string> => { | ||
const { data: totalTransactions, isLoading: isLoadingTransactions } = useSWR( | ||
totalAssetsEndpoint(QUERY_ID_TOTAL_TRANSACTIONS), | ||
fetcher, | ||
) | ||
const { data: totalAssets, isLoading: isLoadingAssets } = useSWR(totalAssetsEndpoint(QUERY_ID_TOTAL_ASSETS), fetcher) | ||
const { data: totalSafesDeployed, isLoading: isLoadingSafesDeployed } = useSWR( | ||
totalAssetsEndpoint(QUERY_ID_TOTAL_SAFES_DEPOLOYED), | ||
fetcher, | ||
) | ||
|
||
if (isLoadingTransactions || isLoadingAssets || isLoadingSafesDeployed) { | ||
return fallbackStats | ||
} | ||
|
||
const formattedTotalAssets = '$' + formatValue(totalAssets.usd_value) | ||
const formattedTotalSafesDeployed = formatValue(totalSafesDeployed.num_safes) | ||
const formattedTotalTransactions = formatValue(totalTransactions.num_txs) | ||
|
||
return [formattedTotalTransactions, formattedTotalAssets, formattedTotalSafesDeployed] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const hasSingleIntegerDigit = (value: number): boolean => value < 10 | ||
|
||
/** | ||
* Formats a numeric value into its thousands order of magnitude. | ||
* If it rounds to a single integer digit, keep one decimal place. | ||
* | ||
* @param {number} value - The numeric value to format. | ||
* @returns {string} The formatted value with the order of magnitude suffix. | ||
*/ | ||
export function formatValue(value: number): string { | ||
if (value >= 1e12) { | ||
const valueInTrillions = value / 1e12 | ||
const isSingleDigitInteger = hasSingleIntegerDigit(valueInTrillions) | ||
const formattedValue = isSingleDigitInteger ? valueInTrillions.toFixed(1) : Math.floor(valueInTrillions).toString() | ||
return formattedValue + 'T' | ||
} else if (value >= 1e9) { | ||
const valueInBillions = value / 1e9 | ||
const isSingleDigitInteger = hasSingleIntegerDigit(valueInBillions) | ||
const formattedValue = isSingleDigitInteger ? valueInBillions.toFixed(1) : Math.floor(valueInBillions).toString() | ||
return formattedValue + 'B' | ||
} else if (value >= 1e6) { | ||
const valueInMillions = value / 1e6 | ||
const isSingleDigitInteger = hasSingleIntegerDigit(valueInMillions) | ||
const formattedValue = isSingleDigitInteger ? valueInMillions.toFixed(1) : Math.floor(valueInMillions).toString() | ||
return formattedValue + 'M' | ||
} else if (value >= 1e3) { | ||
const valueInThousands = value / 1e3 | ||
const isSingleDigitInteger = hasSingleIntegerDigit(valueInThousands) | ||
const formattedValue = isSingleDigitInteger ? valueInThousands.toFixed(1) : Math.floor(valueInThousands).toString() | ||
return formattedValue + 'K' | ||
} else { | ||
return `${value}` | ||
} | ||
} |