Skip to main content

StdChains

Git Source

StdChains provides information about EVM compatible chains that can be used in scripts/tests. For each chain, the chain's name, chain ID, and a default RPC URL are provided. Chains are identified by their alias, which is the same as the alias in the [rpc_endpoints] section of the foundry.toml file. For best UX, ensure the alias in the foundry.toml file match the alias used in this contract, which can be found as the first argument to the setChainWithDefaultRpcUrl call in the initializeStdChains function. There are two main ways to use this contract:

  1. Set a chain with setChain(string memory chainAlias, ChainData memory chain) or setChain(string memory chainAlias, Chain memory chain)
  2. Get a chain with getChain(string memory chainAlias) or getChain(uint256 chainId). The first time either of those are used, chains are initialized with the default set of RPC URLs. This is done in initializeStdChains, which uses setChainWithDefaultRpcUrl. Defaults are recorded in defaultRpcUrls. The setChain function is straightforward, and it simply saves off the given chain data. The getChain methods use getChainWithUpdatedRpcUrl to return a chain. For example, let's say we want to retrieve the RPC URL for mainnet:
  • If you have specified data with setChain, it will return that.
  • If you have configured a mainnet RPC URL in foundry.toml, it will return the URL, provided it is valid (e.g. a URL is specified, or an environment variable is given and exists).
  • If neither of the above conditions is met, the default data is returned. Summarizing the above, the prioritization hierarchy is setChain -> foundry.toml -> environment variable -> defaults.

State Variables

vm

VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));

stdChainsInitialized

bool private stdChainsInitialized;

chains

mapping(string => Chain) private chains;

defaultRpcUrls

mapping(string => string) private defaultRpcUrls;

idToAlias

mapping(uint256 => string) private idToAlias;

fallbackToDefaultRpcUrls

bool private fallbackToDefaultRpcUrls = true;

Functions

getChain

function getChain(string memory chainAlias) internal virtual returns (Chain memory chain);

getChain

function getChain(uint256 chainId) internal virtual returns (Chain memory chain);

setChain

function setChain(string memory chainAlias, ChainData memory chain) internal virtual;

setChain

function setChain(string memory chainAlias, Chain memory chain) internal virtual;

_toUpper

function _toUpper(string memory str) private pure returns (string memory);

getChainWithUpdatedRpcUrl

function getChainWithUpdatedRpcUrl(string memory chainAlias, Chain memory chain) private view returns (Chain memory);

setFallbackToDefaultRpcUrls

function setFallbackToDefaultRpcUrls(bool useDefault) internal;

initializeStdChains

function initializeStdChains() private;

setChainWithDefaultRpcUrl

function setChainWithDefaultRpcUrl(string memory chainAlias, ChainData memory chain) private;

Structs

ChainData

struct ChainData {
string name;
uint256 chainId;
string rpcUrl;
}

Chain

struct Chain {
string name;
uint256 chainId;
string chainAlias;
string rpcUrl;
}