Is there a way to directly be able to retrieve CHAIN_ID
information about the executing chain from a smart contract?
I know that for any chain that has EIP-155
implemented, CHAIN_ID
s are retrievable from the v
component of a signature but would require the passing of one for extraction.
Since CHAIN_ID
is information about the chain I suspect that there should be a way to access this. Is there a way for Solidity to directly get this information?
Best Answer
As of version
0.5.12
, Solidity includes an assembly functionchainid()
that provides access to the newCHAINID
opcode:To use it, ensure you set the compiler's EVM version to Istanbul with the
--evm-version istanbul
flag.Related documentation: Solidity Assembly - v0.5.12