Skip to content

Commit df69951

Browse files
committed
enable StakingStatsBox data fetching
client-side, no api key required
1 parent ddc6392 commit df69951

File tree

1 file changed

+59
-50
lines changed

1 file changed

+59
-50
lines changed

src/components/Staking/StakingStatsBox.tsx

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -108,56 +108,65 @@ const StakingStatsBox = () => {
108108
const [totalValidators, setTotalValidators] = useState<string | null>(ZERO)
109109
const [currentApr, setCurrentApr] = useState<string | null>(ZERO)
110110

111-
// TODO! Fix data fetching
112-
// useEffect(() => {
113-
// const localeForStatsBoxNumbers = getLocaleForNumberFormat(locale! as Lang)
114-
115-
// // Helper functions
116-
// const formatInteger = (amount: number): string =>
117-
// new Intl.NumberFormat(localeForStatsBoxNumbers).format(amount)
118-
119-
// const formatPercentage = (amount: number): string =>
120-
// new Intl.NumberFormat(localeForStatsBoxNumbers, {
121-
// style: "percent",
122-
// minimumSignificantDigits: 2,
123-
// maximumSignificantDigits: 2,
124-
// }).format(amount)
125-
126-
// // API calls, data formatting, and state setting
127-
// const base = "https://beaconcha.in"
128-
// const { href: ethstore } = new URL("api/v1/ethstore/latest", base)
129-
// const { href: epoch } = new URL("api/v1/epoch/latest", base)
130-
// // Get total ETH staked and current APR from ethstore endpoint
131-
// ;(async () => {
132-
// try {
133-
// const ethStoreResponse = await getData<EthStoreResponse>(ethstore)
134-
// const {
135-
// data: { apr, effective_balances_sum_wei },
136-
// } = ethStoreResponse
137-
// const totalEffectiveBalance: number = effective_balances_sum_wei * 1e-18
138-
// const valueTotalEth = formatInteger(Math.floor(totalEffectiveBalance))
139-
// const valueCurrentApr = formatPercentage(apr)
140-
// setTotalEth(valueTotalEth)
141-
// setCurrentApr(valueCurrentApr)
142-
// } catch (error) {
143-
// setTotalEth(null)
144-
// setCurrentApr(null)
145-
// }
146-
// })()
147-
// // Get total active validators from latest epoch endpoint
148-
// ;(async () => {
149-
// try {
150-
// const epochResponse = await getData<EpochResponse>(epoch)
151-
// const {
152-
// data: { validatorscount },
153-
// } = epochResponse
154-
// const valueTotalValidators = formatInteger(validatorscount)
155-
// setTotalValidators(valueTotalValidators)
156-
// } catch (error) {
157-
// setTotalValidators(null)
158-
// }
159-
// })()
160-
// }, [locale])
111+
// TODO: Confirm data fetch approach/frequency
112+
useEffect(() => {
113+
const localeForStatsBoxNumbers = getLocaleForNumberFormat(locale! as Lang)
114+
115+
// Helper functions
116+
const formatInteger = (amount: number): string =>
117+
new Intl.NumberFormat(localeForStatsBoxNumbers).format(amount)
118+
119+
const formatPercentage = (amount: number): string =>
120+
new Intl.NumberFormat(localeForStatsBoxNumbers, {
121+
style: "percent",
122+
minimumSignificantDigits: 2,
123+
maximumSignificantDigits: 2,
124+
}).format(amount)
125+
126+
// API calls, data formatting, and state setting
127+
const base = "https://beaconcha.in"
128+
const { href: ethstore } = new URL("api/v1/ethstore/latest", base)
129+
const { href: epoch } = new URL("api/v1/epoch/latest", base)
130+
// Get total ETH staked and current APR from ethstore endpoint
131+
;(async () => {
132+
try {
133+
const ethStoreResponse = await fetch(ethstore)
134+
if (!ethStoreResponse.ok)
135+
throw new Error(
136+
"Network response from Beaconcha.in ETHSTORE was not ok"
137+
)
138+
const ethStoreResponseJson: EthStoreResponse =
139+
await ethStoreResponse.json()
140+
const {
141+
data: { apr, effective_balances_sum_wei },
142+
} = ethStoreResponseJson
143+
const totalEffectiveBalance: number = effective_balances_sum_wei * 1e-18
144+
const valueTotalEth = formatInteger(Math.floor(totalEffectiveBalance))
145+
const valueCurrentApr = formatPercentage(apr)
146+
setTotalEth(valueTotalEth)
147+
setCurrentApr(valueCurrentApr)
148+
} catch (error) {
149+
setTotalEth(null)
150+
setCurrentApr(null)
151+
}
152+
})()
153+
// Get total active validators from latest epoch endpoint
154+
;(async () => {
155+
try {
156+
const epochResponse = await fetch(epoch)
157+
if (!epochResponse.ok)
158+
throw new Error("Network response from Beaconcha.in EPOCH was not ok")
159+
const epochResponseJson: EpochResponse = await epochResponse.json()
160+
const {
161+
data: { validatorscount },
162+
} = epochResponseJson
163+
const valueTotalValidators = formatInteger(validatorscount)
164+
setTotalValidators(valueTotalValidators)
165+
} catch (error) {
166+
setTotalValidators(null)
167+
}
168+
})()
169+
}, [locale])
161170

162171
return (
163172
<Flex direction={{ base: "column", md: "row" }}>

0 commit comments

Comments
 (0)