getBlockNumber โ
Returns the number of the most recent block seen.
Usage โ
ts
import { publicClient } from './client'
const block = await publicClient.getBlockNumber()
// 69420n
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
})
Returns โ
bigint
The number of the block.
Parameters โ
cacheTime (optional) โ
- Type:
number
- Default: Client's
cacheTime
Time (in ms) that cached block number will remain in memory.
ts
const block = await publicClient.getBlockNumber({
cacheTime: 4_000
})
By default, block numbers are cached for the period of the Client's cacheTime
.
- Setting a value of above zero will make block number remain in the cache for that period.
- Setting a value of
0
will disable the cache, and always retrieve a fresh block number.
Example โ
Check out the usage of getBlockNumber
in the live Fetching Blocks Example below.