foundry icon indicating copy to clipboard operation
foundry copied to clipboard

bug(`cast`): `cast run --chain` ignores configured RPC endpoints in `foundry.toml`

Open mshakeg opened this issue 6 months ago • 5 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [x] Foundry
  • [x] Foundryup

What version of Foundry are you on?

1.2.0-nightly

What version of Foundryup are you on?

No response

What command(s) is the bug in?

cast run

Operating System

None

Describe the bug

Description

When using cast run with the --chain flag, Foundry appears to ignore the RPC endpoints configured in foundry.toml and defaults to localhost:8545 instead.

Steps to Reproduce

  1. Configure custom RPC endpoints in foundry.toml:
[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"
unichain = "${UNICHAIN_MAINNET_RPC_URL}"
  1. Set the corresponding environment variables in .env:
UNICHAIN_MAINNET_RPC_URL=https://your-unichain-rpc-url
  1. Run cast with the --chain flag:
cast run 0xb6972b3d91aa4ece3b7f2ef3aff0e3d87f049252eef64f4b6351925fe035b3d4 --chain unichain

Expected Behavior

The command should use the RPC URL configured for the unichain endpoint in foundry.toml.

Actual Behavior

The command attempts to connect to localhost:8545 and fails with:

Error: tx not found: 0xb6972b3d91aa4ece3b7f2ef3aff0e3d87f049252eef64f4b6351925fe035b3d4
Context:
- Error #0: error sending request for url (http://localhost:8545/)
- Error #1: client error (Connect)
- Error #2: tcp connect error: Connection refused (os error 61)

Configuration Files

foundry.toml:

[profile.default]
src = "src"
test = "test"
out = "out"
libs = ["lib"]
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}, { access = "read-write", path = "log/"}, { access = "read", path = "env"}]
dotenv = true
solc_version = "0.8.27"
evm_version = "cancun"
viaIR = true
optimizer = false

[profile.production]
viaIR = true
optimizer = true
optimizer_runs = 10000

[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"
unichain = "${UNICHAIN_MAINNET_RPC_URL}"

[etherscan]
mainnet = { key = "${ETHERSCAN_API_KEY}", url = "https://api.etherscan.io/api" }
unichain = { key = "${UNICHAIN_API_KEY}", url = "https://unichain.blockscout.com/api" }

mshakeg avatar Jun 04 '25 04:06 mshakeg

@mshakeg cast is a standalone tool, independent of forge, hence not configurable in foundry.toml

grandizzy avatar Jun 04 '25 06:06 grandizzy

@grandizzy why does cast run have the --chain option?

mshakeg avatar Jun 04 '25 07:06 mshakeg

we determine chain id by making a call to provider but if --chain is already provided (as chain name or EIP-155 chain ID) then we use that one and save a rpc call

grandizzy avatar Jun 04 '25 07:06 grandizzy

@grandizzy I see, though wouldn't it be better devX to autoselect the rpc if the --chain is specified?

mshakeg avatar Jun 04 '25 07:06 mshakeg

yeah, we discussed at some point cast and anvil to adhere to foundry.toml, will bring this back in discussion. CC @zerosnacks

grandizzy avatar Jun 04 '25 09:06 grandizzy