> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://www.getfoundry.sh/api/mcp` to find what you need.
>
> **Have feedback?** Use `submit_feedback` on the same MCP server.

## Std Chains

`StdChains` (included by `forge-std/Test.sol` and `Script.sol`) gives tests and scripts a small registry of EVM chains: name, chain ID, alias, and an RPC URL.

Use it when a test needs a fork URL or chain ID without hard-coding endpoints. Prefer configuring RPCs in `foundry.toml` and reading them through `getChain` rather than embedding public URLs in Solidity.

Unlike the [`vm.getChain`](/reference/cheatcodes/get-chain) cheatcode, which returns an empty RPC URL unless one is configured (except for aliases with a Foundry built-in endpoint, such as `tempo`) and returns lowercase names (`"base"`), `StdChains` falls back to public default RPC URLs and uses display names (`"Base"`). For per-chain configuration files, see [`StdConfig`](/reference/forge-std/std-config).

### Types

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

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

### `getChain`

```solidity
function getChain(string memory chainAlias) internal virtual returns (Chain memory chain);
function getChain(uint256 chainId) internal virtual returns (Chain memory chain);
```

Returns chain data for a known alias (for example `"mainnet"`, `"optimism"`, `"base"`) or chain ID.

RPC URL resolution order for a given alias:

1. Non-empty `rpcUrl` previously set with `setChain`
2. Matching entry under `[rpc_endpoints]` in `foundry.toml`, resolved with [`vm.rpcUrl`](/reference/cheatcodes/rpc) (including `${ENV}` expansion)
3. The `<ALIAS_UPPERCASE>_RPC_URL` environment variable, for example `BASE_RPC_URL` for `base` (checked only when `vm.rpcUrl` cannot resolve the alias, that is, it has no `[rpc_endpoints]` entry and no Foundry built-in endpoint; `tempo` has a built-in endpoint, so `TEMPO_RPC_URL` and the StdChains default are never used for it)
4. Built-in default public RPC URL, only when fallback to defaults is enabled (the default)

If fallback is disabled with `setFallbackToDefaultRpcUrls(false)` and the environment variable is not set, `getChain` reverts.

:::warning
Default public RPC URLs are for local experimentation only. They are rate-limited. Configure your own endpoints in `foundry.toml` for CI and shared test suites.
:::

### `setChain`

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

Registers or overrides chain data under `chainAlias`. Useful for custom networks or for pinning an RPC URL in a test without editing `foundry.toml`.

### `setFallbackToDefaultRpcUrls`

```solidity
function setFallbackToDefaultRpcUrls(bool useDefault) internal;
```

Controls whether `getChain` falls back to the built-in default RPC URL when no URL is found through `setChain`, `[rpc_endpoints]`, or the environment variable. Enabled by default. Set it to `false` to make tests revert instead of silently using a public endpoint.

### Configuration

```toml [foundry.toml]
[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"
base = "${BASE_RPC_URL}"
```

Aliases here should match the aliases `StdChains` knows (the first argument to each default chain registration).

### Examples

#### Read chain ID and create a fork from config

```solidity [test/StdChainsExample.t.sol]
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test} from "forge-std/Test.sol";

contract StdChainsExampleTest is Test {
    function testForkBaseFromStdChains() public {
        Chain memory base = getChain("base");

        assertEq(base.chainId, 8453);
        assertEq(base.chainAlias, "base");
        assertGt(bytes(base.rpcUrl).length, 0);

        uint256 forkId = vm.createFork(base.rpcUrl);
        vm.selectFork(forkId);
        assertEq(block.chainid, base.chainId);
    }
}
```

#### Look up by chain ID

```solidity
Chain memory optimism = getChain(10);
assertEq(optimism.chainAlias, "optimism");
assertEq(optimism.chainId, 10);
```

#### Register a custom chain for a test

```solidity
setChain(
    "mychain",
    ChainData({name: "My Chain", chainId: 12345, rpcUrl: "http://127.0.0.1:8545"})
);

Chain memory mine = getChain("mychain");
assertEq(mine.chainId, 12345);
assertEq(mine.rpcUrl, "http://127.0.0.1:8545");
```

### Gotchas

* `Test` / `Script` already inherit `StdChains`, so you typically call `getChain` / `setChain` directly on the test contract.
* An empty alias, unknown alias, or chain ID `0` reverts.
* An `[rpc_endpoints]` entry that references an unset environment variable (for example `base = "${BASE_RPC_URL}"` without `BASE_RPC_URL` set) makes `vm.rpcUrl` fail with an unresolved environment variable error. `StdChains` rethrows that error, so `getChain("base")` reverts with ``environment variable `BASE_RPC_URL` not found`` instead of falling back to the default RPC URL. Set the variable or remove the entry.
