Learning ERC-7208
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
Thrown when the chain ID is not supported (i.e., when it's larger than uint32.max
).
2. UnexpectedChain
Thrown when the current chain ID doesn't match the requested chain ID.
Functions
1. chainid()
Description | Parameters | Returns | Modifiers |
---|---|---|---|
Converts block.chainid to uint32 chain ID | None | uint32 chain ID | internal 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()
Description | Parameters | Returns | Modifiers |
---|---|---|---|
Requires current chain to be the same as requested | chainId : Requested chain ID | None | internal 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 theuint256
chain 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.