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);
| Description | Parameters | Returns | Modifiers |
|---|---|---|---|
Converts block.chainid to uint32 chain ID | None | uint32 chain ID | internalview |
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;
| Description | Parameters | Returns | Modifiers |
|---|---|---|---|
| Requires current chain to be the same as requested | chainId: Requested chain ID | None | internalview |
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.chainidto 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 theuint256chain ID touint32, 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.