Skip to content

Commit 47a2ac3

Browse files
committed
refactor: data fetching
1 parent 87f043e commit 47a2ac3

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/pages/stablecoins.tsx

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
3636
import { runOnlyOnce } from "@/lib/utils/runOnlyOnce"
3737
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
3838

39-
import {
40-
BASE_TIME_UNIT,
41-
COINGECKO_API_BASE_URL,
42-
COINGECKO_API_URL_PARAMS,
43-
} from "@/lib/constants"
39+
import { BASE_TIME_UNIT } from "@/lib/constants"
4440

41+
import {
42+
fetchEthereumEcosystemData,
43+
fetchEthereumStablecoinsData,
44+
} from "@/lib/api/stablecoinsData"
4545
import dogeComputerImg from "@/public/doge-computer.png"
4646
// -- daps
4747
import aaveImg from "@/public/stablecoins/aave.png"
@@ -80,17 +80,9 @@ type Props = SSRConfig & {
8080
lastDeployDate: string
8181
}
8282

83-
const ethereumDataFetch = runOnlyOnce(() =>
84-
fetch(
85-
`${COINGECKO_API_BASE_URL}ethereum-ecosystem${COINGECKO_API_URL_PARAMS}`
86-
).then((res) => res.json())
87-
)
88-
89-
const stablecoinDataFetch = runOnlyOnce(() =>
90-
fetch(`${COINGECKO_API_BASE_URL}stablecoins${COINGECKO_API_URL_PARAMS}`).then(
91-
(res) => res.json()
92-
)
93-
)
83+
// Fetch external API data once to avoid hitting rate limit
84+
const ethereumEcosystemDataFetch = runOnlyOnce(fetchEthereumEcosystemData)
85+
const ethereumStablecoinsDataFetch = runOnlyOnce(fetchEthereumStablecoinsData)
9486

9587
export const getStaticProps = (async (context) => {
9688
const { locale } = context
@@ -133,15 +125,16 @@ export const getStaticProps = (async (context) => {
133125

134126
try {
135127
// Fetch token data in the Ethereum ecosystem
136-
const ethereumData: EthereumDataResponse = await ethereumDataFetch()
137-
128+
const ethereumEcosystemData: EthereumDataResponse =
129+
await ethereumEcosystemDataFetch()
138130
// Fetch token data for stablecoins
139-
const stablecoinData: StablecoinDataResponse = await stablecoinDataFetch()
131+
const stablecoinsData: StablecoinDataResponse =
132+
await ethereumStablecoinsDataFetch()
140133

141134
// Get the intersection of stablecoins and Ethereum tokens to only have a list of data for stablecoins in the Ethereum ecosystem
142-
const ethereumStablecoinData = stablecoinData.filter(
135+
const ethereumStablecoinData = stablecoinsData.filter(
143136
(stablecoin) =>
144-
ethereumData.findIndex(
137+
ethereumEcosystemData.findIndex(
145138
// eslint-disable-next-line
146139
(etherToken) => stablecoin.id == etherToken.id
147140
) > -1

0 commit comments

Comments
 (0)