Skip to content

Commit 0efa5a2

Browse files
committed
removes extraneous API call
Uses new endpoint for all needed data, with direct access to total eth staked, and can easily calculate from there total validators. Adds clarifier for ETH.STORE being used for APR calculation, keeping Beaconcha.in link until eth.store subpage available for direct link
1 parent 7a066c6 commit 0efa5a2

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/components/Staking/StakingStatsBox.tsx

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getLocaleForNumberFormat } from "../../utils/translations"
1616
// Constants
1717
const NA_ERROR = "n/a"
1818
const ZERO = "0"
19+
const MAX_EFFECTIVE_BALANCE = 32
1920

2021
// Styled components
2122
const Container = styled.div`
@@ -73,11 +74,13 @@ const StyledIcon = styled(Icon)`
7374
}
7475
`
7576

76-
const BeaconchainTooltip = () => (
77+
// BeaconchainTooltip component
78+
const BeaconchainTooltip = ({ isEthStore }: { isEthStore?: boolean }) => (
7779
<Tooltip
7880
content={
7981
<div>
8082
<Translation id="data-provided-by" />{" "}
83+
{isEthStore && <span>ETH.STORE </span>}
8184
<Link to="https://beaconcha.in">Beaconcha.in</Link>
8285
</div>
8386
}
@@ -106,6 +109,7 @@ const StakingStatsBox: React.FC<IProps> = () => {
106109
intl.locale as Lang
107110
)
108111

112+
// Helper functions
109113
const formatInteger = (amount: number): string =>
110114
new Intl.NumberFormat(localeForStatsBoxNumbers).format(amount)
111115

@@ -116,35 +120,27 @@ const StakingStatsBox: React.FC<IProps> = () => {
116120
maximumSignificantDigits: 2,
117121
}).format(amount)
118122

123+
// API call, data formatting, and state setting
119124
;(async () => {
120125
try {
121126
const {
122-
data: { totalvalidatorbalance, validatorscount },
127+
data: { apr, effective_balances_sum_wei },
123128
} = await getData<{
124-
data: { totalvalidatorbalance: number; validatorscount: number }
125-
}>("https://mainnet.beaconcha.in/api/v1/epoch/latest")
126-
const valueTotalEth = formatInteger(
127-
Number((totalvalidatorbalance * 1e-9).toFixed(0))
129+
data: { apr: number; effective_balances_sum_wei: number }
130+
}>("https://beaconcha.in/api/v1/ethstore/latest")
131+
const totalEffectiveBalance: number = effective_balances_sum_wei * 1e-18
132+
const valueTotalEth = formatInteger(Math.floor(totalEffectiveBalance))
133+
const valueTotalValidators = formatInteger(
134+
totalEffectiveBalance / MAX_EFFECTIVE_BALANCE
128135
)
129-
const valueTotalValidators = formatInteger(validatorscount)
136+
const valueCurrentApr = formatPercentage(apr)
130137
setTotalEth(valueTotalEth)
131138
setTotalValidators(valueTotalValidators)
132-
} catch (error) {
133-
setTotalEth(null)
134-
setTotalValidators(null)
135-
}
136-
})()
137-
;(async () => {
138-
try {
139-
const {
140-
data: { apr },
141-
} = await getData<{
142-
data: { apr: number }
143-
}>("https://beaconcha.in/api/v1/ethstore/latest")
144-
const valueCurrentApr = formatPercentage(apr)
145139
setCurrentApr(valueCurrentApr)
146140
} catch (error) {
141+
setTotalEth(null)
147142
setCurrentApr(null)
143+
setTotalValidators(null)
148144
}
149145
})()
150146
}, [intl.locale])
@@ -185,7 +181,7 @@ const StakingStatsBox: React.FC<IProps> = () => {
185181
)}
186182
<Label>
187183
<Translation id="page-staking-stats-box-metric-3" />
188-
<BeaconchainTooltip />
184+
<BeaconchainTooltip isEthStore />
189185
</Label>
190186
</Cell>
191187
</Container>

0 commit comments

Comments
 (0)