ghost-dao-interface/src/hooks/ghost/useDisabledValidators.js
2025-12-15 15:58:37 +03:00

42 lines
1.7 KiB
JavaScript

import useSWRSubscription from "swr/subscription"
import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders"
import { distinct, filter, map, mergeMap } from "rxjs"
import { useUnstableProvider } from "./UnstableProvider"
import { useMetadata } from "./MetadataProvider"
export const useDisabledValidators = () => {
const { chainHead$, chainId } = useUnstableProvider()
const metadata = useMetadata()
const { data: disabledIndexes, error } = useSWRSubscription(
chainHead$ && chainId && metadata
? ["disabledIndexes", chainHead$, chainId, metadata]
: null,
([_, chainHead$, chainId, metadata], { next }) => {
const { finalized$, storage$ } = chainHead$
const subscription = finalized$.pipe(
filter(Boolean),
mergeMap((blockInfo) => {
const builder = getDynamicBuilder(getLookupFn(metadata))
const disabledIndexes = builder.buildStorage("Session", "DisabledValidators")
return storage$(blockInfo?.hash, "value", () =>
disabledIndexes?.keys.enc()
).pipe(
filter(Boolean),
distinct(),
map((value) => disabledIndexes?.value.dec(value))
)
}),
)
.subscribe({
next(disabledIndexes) {
next(null, disabledIndexes)
},
error: next,
})
return () => subscription.unsubscribe()
}
)
return disabledIndexes ? disabledIndexes : []
}