import { useQuery } from '@tanstack/react-query'
import { GetBlockResponse, ProviderInterface } from 'starknet'
import { BlockNumber } from 'starknet'
import { useStarknet } from '../providers'
/** Value returned from `useStarknetBlock`. */
export interface StarkNetBlockResult {
/** Block data. */
data?: GetBlockResponse
/** True if loading block. */
loading?: boolean
/** Error loading block. */
error?: string
}
/**
* Hook for fetching a block.
*
* @remarks
*
* This hook fetches the `latest` block using the default provider.
* Block data is continuously refreshed in the background.
*
* @deprecated Use {@link useBlock}.
*
* @example
* This example shows how to fetch the latest block.
* ```tsx
* function Component() {
* const { data, loading, error } = useStarknetBlock()
*
* if (loading) return Loading...
* if (error) return Error...
* return Hash: {data.block_hash}
* }
* ```
*/
export function useStarknetBlock(): StarkNetBlockResult {
const { data, isLoading, isError } = useBlock({
blockIdentifier: 'latest',
})
return {
data,
loading: isLoading,
error: isError ? 'error loading block number' : undefined,
}
}
export interface FetchBlockArgs {
/** Identifier for the block to fetch. */
blockIdentifier: BlockNumber
}
/** Arguments for `useBlock`. */
export type UseBlockProps = Partial & {
/** How often to refresh the data. */
refetchInterval?: number | false
/** Callback fired every time a new block is fetched. */
onSuccess?: (block: GetBlockResponse) => void
}
/** Value returned from `useBlock`. */
export interface UseBlockResult {
/** Block data. */
data?: GetBlockResponse
/** True if loading block data. */
isLoading: boolean
/** True if error while loading data. */
isError: boolean
/** Error fetching block. */
error?: unknown
}
/**
* Hook for fetching a block.
*
* @remarks
*
* Specify which block to fetch with the `blockIdentifier` argument.
* Control if and how often data is refreshed with `refetchInterval`.
*
* @example
* This example shows how to fetch the latest block only once.
* ```tsx
* function Component() {
* const { data, isLoading, isError } = useBlock({
* refetchInterval: false,
* blockIdentifier: 'latest'
* })
*
* if (isLoading) return Loading...
* if (isError) return Error...
* return Hash: {data.block_hash}
* }
* ```
*
* @example
* This example shows how to fetch the pending block every 3 seconds.
* Use your browser network monitor to verify that the hook is refetching the
* data.
* ```tsx
* function Component() {
* const { data, isLoading, isError } = useBlock({
* refetchInterval: 3000,
* blockIdentifier: 'pending'
* })
*
* if (isLoading) return Loading...
* if (isError) return Error...
* return Hash: {data.block_hash}
* }
* ```
*/
export function useBlock({
refetchInterval,
onSuccess,
blockIdentifier = 'latest',
}: UseBlockProps = {}): UseBlockResult {
const { library } = useStarknet()
const { data, isLoading, isError, error } = useQuery(
['block', blockIdentifier],
fetchBlock({ library, args: { blockIdentifier } }),
{
refetchInterval,
useErrorBoundary: true,
onSuccess: (block) => {
if (block && onSuccess) onSuccess(block)
},
}
)
return { data, isLoading, isError, error }
}
function fetchBlock({
library,
args,
}: {
library: ProviderInterface
args: FetchBlockArgs
}): () => Promise {
return async () => {
return await library.getBlock(args.blockIdentifier)
}
}