getBlock โ
Returns information about a block at a block number, hash or tag.
Usage โ
ts
import { publicClient } from './client'
const block = await publicClient.getBlock()
/**
* {
* baseFeePerGas: 10789405161n,
* difficulty: 11569232145203128n,
* extraData: '0x75732d656173742d38',
* ...
* }
*/
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
})
Returns โ
Information about the block.
Parameters โ
blockHash (optional) โ
- Type:
Hash
Information at a given block hash.
ts
const block = await publicClient.getBlock({
blockHash: '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d'
})
blockNumber (optional) โ
- Type:
bigint
Information at a given block number.
ts
const block = await publicClient.getBlock({
blockNumber: 42069n
})
blockTag (optional) โ
- Type:
'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'
- Default:
'latest'
Information at a given block tag.
ts
const block = await publicClient.getBlock({
blockTag: 'safe'
})
Example โ
Check out the usage of getBlock
in the live Fetching Blocks Example below.
JSON-RPC Method โ
- Calls
eth_getBlockByNumber
forblockNumber
&blockTag
. - Calls
eth_getBlockByHash
forblockHash
.