Skip to content

Block.fromRpc

Converts a Block.Rpc to an Block.Block.

Imports

Named
import { Block } from 'ox'

Examples

import { Block } from 'ox'
 
const block = Block.fromRpc({
  // ...
  hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd',
  number: '0xec6fc6',
  size: '0x208',
  timestamp: '0x63198f6f',
  // ...
})
{
// ...
hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd',
number: 19868020n,
size: 520n,
timestamp: 1662222222n,
// ...
}

End-to-end

Below is an end-to-end example of using Block.fromRpc to fetch a block from the network and convert it to an Block.Block.

import 'ox/window'
import { Block } from 'ox'
 
const block = await window.ethereum!
  .request({
    method: 'eth_getBlockByNumber',
    params: ['latest', false],
  })
  .then(Block.fromRpc)
{
// ...
hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd',
number: 19868020n,
size: 520n,
timestamp: 1662222222n,
// ...
}

Definition

function fromRpc<block, includeTransactions, blockTag>(
  block: block | Rpc | null,
  _options?: fromRpc.Options<includeTransactions, blockTag>,
): block extends Rpc ? Block<includeTransactions, blockTag> : null

Source: src/Block.ts

Parameters

block

  • Type: block | Rpc | null

The RPC block to convert.

Return Type

An instantiated Block.Block.

block extends Rpc ? Block<includeTransactions, blockTag> : null