42 lines
1.7 KiB
JavaScript
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 useValidators = ({ currentSession }) => {
|
|
const { chainHead$, chainId } = useUnstableProvider()
|
|
const metadata = useMetadata()
|
|
const { data: slowClapValidators } = useSWRSubscription(
|
|
chainHead$ && chainId && metadata
|
|
? ["slowClapValidators", chainHead$, currentSession, chainId, metadata]
|
|
: null,
|
|
([_, chainHead$, currentSession, chainId, metadata], { next }) => {
|
|
const { finalized$, storage$ } = chainHead$
|
|
const subscription = finalized$.pipe(
|
|
filter(Boolean),
|
|
mergeMap((blockInfo) => {
|
|
const builder = getDynamicBuilder(getLookupFn(metadata))
|
|
const slowClapValidators = builder.buildStorage("GhostSlowClaps", "Validators")
|
|
return storage$(blockInfo?.hash, "value", () =>
|
|
slowClapValidators?.keys.enc(currentSession)
|
|
).pipe(
|
|
filter(Boolean),
|
|
distinct(),
|
|
map((value) => slowClapValidators?.value.dec(value))
|
|
)
|
|
}),
|
|
)
|
|
.subscribe({
|
|
next(slowClapValidators) {
|
|
next(null, slowClapValidators)
|
|
},
|
|
error: next,
|
|
})
|
|
return () => subscription.unsubscribe()
|
|
}
|
|
)
|
|
return slowClapValidators
|
|
}
|