/
block.ts
56 lines (50 loc) · 1.28 KB
/
block.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { useQuery } from '@tanstack/react-query'
import { GetBlockResponse, ProviderInterface } from 'starknet'
import { BlockIdentifier } from 'starknet/provider/utils'
import { useStarknet } from '~/providers'
export interface StarkNetBlockResult {
data?: GetBlockResponse
loading?: boolean
error?: string
}
/**
* @deprecated Use `useBlock`.
*/
export function useStarknetBlock(): StarkNetBlockResult {
return useBlock({})
}
function fetchBlock({
library,
blockIdentifier,
}: {
library: ProviderInterface
blockIdentifier: BlockIdentifier
}): () => Promise<GetBlockResponse | undefined> {
return async () => {
return await library.getBlock(blockIdentifier)
}
}
/**
* Hook for fetching a block.
*/
export function useBlock({
watch,
refetchInterval: refreshInterval,
blockIdentifier = 'latest',
}: {
watch?: boolean
refetchInterval?: number
blockIdentifier?: BlockIdentifier
} = {}) {
const { library } = useStarknet()
const refetchInterval = watch ? refreshInterval ?? 5000 : false
const { data, isLoading, isError } = useQuery<GetBlockResponse | undefined, string>(
['block', blockIdentifier],
fetchBlock({ library, blockIdentifier }),
{
refetchInterval,
useErrorBoundary: true,
}
)
return { data, isLoading, isError }
}