ghost-dao-interface/src/hooks/ghost/useErasTotalStaked.js
Uncle Fatso 3316f7640e
apply changes from pallet-slow-clap
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
2026-03-04 17:43:21 +03:00

42 lines
1.6 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 useErasTotalStake = ({ epochIndex }) => {
const { chainHead$, chainId } = useUnstableProvider()
const metadata = useMetadata()
const { data: eraTotalStake } = useSWRSubscription(
chainHead$ && chainId && metadata
? ["eraTotalStake", chainHead$, epochIndex, chainId, metadata]
: null,
([_, chainHead$, epochIndex, chainId, metadata], { next }) => {
const { finalized$, storage$ } = chainHead$
const subscription = finalized$.pipe(
filter(Boolean),
mergeMap((blockInfo) => {
const builder = getDynamicBuilder(getLookupFn(metadata))
const eraTotalStake = builder.buildStorage("Staking", "ErasTotalStake")
return storage$(blockInfo?.hash, "value", () =>
eraTotalStake?.keys.enc(epochIndex)
).pipe(
filter(Boolean),
distinct(),
map((value) => eraTotalStake?.value.dec(value))
)
}),
)
.subscribe({
next(eraTotalStake) {
next(null, eraTotalStake)
},
error: next,
})
return () => subscription.unsubscribe()
}
)
return eraTotalStake;
}