Skip to main content

ChainIdTools Library

The ChainidTools library provides helpers to convert the uint256 chain ID provided by block.chainid to a uint32 chain ID used across this Data Index implementation.

Errors

1. UnsupportedChain

error UnsupportedChain(uint256 chainId);

Thrown when the chain ID is not supported (i.e., when it's larger than uint32.max).

2. UnexpectedChain

error UnexpectedChain(uint32 expected, uint32 requested);

Thrown when the current chain ID doesn't match the requested chain ID.

Functions

1. chainid()

function chainid() internal view returns (uint32);
DescriptionParametersReturnsModifiers
Converts block.chainid to uint32 chain IDNoneuint32 chain IDinternal
view

This function returns the current chain ID as a uint32. If the current chain ID is larger than uint32.max, it reverts with an UnsupportedChain error.

2. requireCurrentChain()

function requireCurrentChain(uint32 chainId) internal view;
DescriptionParametersReturnsModifiers
Requires current chain to be the same as requestedchainId: Requested chain IDNoneinternal
view

This function checks if the current chain ID matches the requested chain ID. If they don't match, it reverts with an UnexpectedChain error.

Implementation Details

  • The library uses block.chainid to get the current chain ID.
  • It assumes that most chain IDs can fit within a uint32, which is true for all major blockchain networks as of now.
  • The chainid() function provides a safe way to convert the uint256 chain ID to uint32, reverting if the conversion would result in data loss.
  • The requireCurrentChain() function is useful for ensuring that a function is being called on the expected blockchain network.