Skip to content

Commit

Permalink
Merge pull request #1502 from oasisprotocol/mz/validatorSync
Browse files Browse the repository at this point in the history
Provide data to validator staking trend chart
  • Loading branch information
buberdds authored Aug 21, 2024
2 parents 9bdcf58 + 698aea8 commit ff3e03a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
1 change: 1 addition & 0 deletions .changelog/1502.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide data to validator staking trend chart
7 changes: 6 additions & 1 deletion src/app/components/charts/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface LineChartProps<T extends object> extends Formatters {
tickMargin?: number
tooltipActiveDotRadius?: number
withLabels?: boolean
maximumFractionDigits?: number
alignDomainToDataPoints?: boolean
}

const LineChartCmp = <T extends object>({
Expand All @@ -33,6 +35,8 @@ const LineChartCmp = <T extends object>({
tickMargin = 0,
tooltipActiveDotRadius = 5,
withLabels,
maximumFractionDigits = 0,
alignDomainToDataPoints,
}: LineChartProps<T>): ReactElement => {
const { t } = useTranslation()

Expand All @@ -54,7 +58,7 @@ const LineChartCmp = <T extends object>({
}}
/>
<YAxis
domain={withLabels ? [0, 'auto'] : ['dataMin', 'dataMax']}
domain={!alignDomainToDataPoints && withLabels ? [0, 'auto'] : ['dataMin', 'dataMax']}
axisLine={false}
interval={0}
tickLine={false}
Expand All @@ -68,6 +72,7 @@ const LineChartCmp = <T extends object>({
formatParams: {
value: {
notation: 'compact',
maximumFractionDigits,
} satisfies Intl.NumberFormatOptions,
},
})
Expand Down
41 changes: 30 additions & 11 deletions src/app/pages/ValidatorDetailsPage/StakingTrend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,55 @@ import { useTranslation } from 'react-i18next'
import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
import CardContent from '@mui/material/CardContent'
import { useGetConsensusValidatorsAddressHistory } from '../../../oasis-nexus/api'
import { SearchScope } from '../../../types/searchScope'
import { LineChart } from '../../components/charts/LineChart'
import { SearchScope } from 'types/searchScope'
import { useScreenSize } from '../../hooks/useScreensize'

export const StakingTrend: FC<{ scope: SearchScope }> = ({ scope }) => {
const epochsInMonth = 720

type StakingTrendProps = {
address: string
scope: SearchScope
}

export const StakingTrend: FC<StakingTrendProps> = ({ address, scope }) => {
const { t } = useTranslation()
const { isMobile } = useScreenSize()
// TODO: provide data from the API is ready and sync dataKey value
const windows = undefined
const historyQuery = useGetConsensusValidatorsAddressHistory(scope.network, address, {
limit: epochsInMonth,
})
const history = historyQuery.data?.data?.history
const filteredHistory = history
?.map(item => ({
active_balance: item.active_balance,
epoch: item.epoch,
}))
.reverse()

return (
<Card sx={{ flex: 1 }}>
<CardHeader disableTypography component="h3" title={t('validator.stakingTrend')} />
<CardContent sx={{ height: 250 }}>
{windows && (
{history && filteredHistory && (
<LineChart
alignDomainToDataPoints
tooltipActiveDotRadius={9}
cartesianGrid
strokeWidth={3}
dataKey={'volume'}
data={windows}
dataKey="active_balance"
data={filteredHistory}
margin={{ bottom: 16, top: isMobile ? 0 : 16 }}
tickMargin={16}
withLabels
maximumFractionDigits={2}
formatters={{
data: (value: number) => value.toLocaleString(),
label: (value: string) =>
t('common.formattedDateTime', {
timestamp: new Date(value),
data: value =>
t('common.valueInToken', {
value,
ticker: history[0].ticker,
}),
label: (value: string) => `${t('common.epoch')}: ${value}`,
}}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/ValidatorDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const ValidatorDetailsPage: FC = () => {
<ValidatorDetailsCard isLoading={isLoading} validator={validator} />
<Grid container spacing={4}>
<StyledGrid item xs={12} md={6}>
<StakingTrend scope={scope} />
<StakingTrend address={address} scope={scope} />
</StyledGrid>
<StyledGrid item xs={12} md={6}>
<SignedBlocks />
Expand Down
37 changes: 37 additions & 0 deletions src/oasis-nexus/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ declare module './generated/api' {
network: Network
ticker: Ticker
}

export interface ValidatorHistoryPoint {
layer: typeof Layer.consensus
network: Network
ticker: Ticker
}
}

export const isAccountEmpty = (account: RuntimeAccount | Account) => {
Expand Down Expand Up @@ -1047,6 +1053,37 @@ export const useGetConsensusValidators: typeof generated.useGetConsensusValidato
})
}

export const useGetConsensusValidatorsAddressHistory: typeof generated.useGetConsensusValidatorsAddressHistory =
(network, address, params?, options?) => {
const ticker = getTokensForScope({ network, layer: Layer.consensus })[0].ticker
return generated.useGetConsensusValidatorsAddressHistory(network, address, params, {
...options,
request: {
...options?.request,
transformResponse: [
...arrayify(axios.defaults.transformResponse),
(data: generated.ValidatorHistory, headers, status) => {
if (status !== 200) return data
return {
...data,
history: data.history.map(history => {
return {
...history,
active_balance: history.active_balance
? fromBaseUnits(history.active_balance, consensusDecimals)
: undefined,
layer: Layer.consensus,
network,
ticker,
}
}),
}
},
...arrayify(options?.request?.transformResponse),
],
},
})
}
export const useGetConsensusAccounts: typeof generated.useGetConsensusAccounts = (
network,
params?,
Expand Down

0 comments on commit ff3e03a

Please sign in to comment.