# RPC URLs in Use and How Infura Is Accessed

This doc lists which RPC URLs the scripts use and how Infura RPCs are accessed (including the fix for "error sending request" when running from your machine).

---

## Where RPC URLs Come From

| Source | Description |
|--------|-------------|
| **`.env`** | All Infura and chain-specific RPC URLs are set here (e.g. `ETHEREUM_MAINNET_RPC`, `RPC_URL_138`, `POLYGON_MAINNET_RPC`). |
| **`scripts/lib/infura.sh`** | Builds Infura URLs from `INFURA_PROJECT_ID` and optional `INFURA_PROJECT_SECRET` (Basic Auth). |
| **Fallbacks** | Scripts use public or alias URLs when an env var is unset (e.g. BSC: `https://bsc-dataseed.binance.org`, Cronos: `https://evm.cronos.org`). |

---

## RPC URLs Used by Scripts

### Required (balance check, verify-all-rpcs, deploy)

| Env var | Example / format | Used by |
|---------|------------------|--------|
| `ETHEREUM_MAINNET_RPC` | `https://mainnet.infura.io/v3/<INFURA_PROJECT_ID>` | Balance check, RPC verify, mainnet deploy |
| `RPC_URL_138` | `http://192.168.11.211:8545` | Chain 138 balance, deploy, bridge scripts |

### Infura chains (from `.env` when set)

All of these are typically set in `.env` as plain Infura URLs (project ID only):

- `ETHEREUM_MAINNET_RPC`, `ETHEREUM_SEPOLIA_RPC`
- `POLYGON_MAINNET_RPC`, `POLYGON_AMOY_RPC`
- `BASE_MAINNET_RPC`, `BASE_SEPOLIA_RPC`
- `OPTIMISM_MAINNET_RPC`, `OPTIMISM_SEPOLIA_RPC`
- `ARBITRUM_MAINNET_RPC`, `ARBITRUM_SEPOLIA_RPC`
- `AVALANCHE_MAINNET_RPC`, `AVALANCHE_FUJI_RPC`
- `BSC_MAINNET_RPC`, `CELO_MAINNET_RPC`, `LINEA_MAINNET_RPC`
- (and other `*_MAINNET_RPC` / `*_TESTNET_RPC` Infura entries in `.env`)

### Other chains (aliases / public)

- `AVALANCHE_RPC_URL`, `ARBITRUM_MAINNET_RPC` (or `ARBITRUM_RPC`) — sometimes overridden to public RPCs in `.env`
- `CRONOS_RPC_URL` — e.g. `https://evm.cronos.org` (Cronos not on Infura)

---

## How Infura RPCs Are Accessed

### Without secret (plain URL from `.env`)

- **URL format**: `https://mainnet.infura.io/v3/<INFURA_PROJECT_ID>` (and same for other chains, e.g. `https://polygon-mainnet.infura.io/v3/<ID>`).
- **When it’s used**: If `INFURA_PROJECT_SECRET` is not set, scripts and `cast` use this URL as-is. This can fail with "error sending request for url (https://...infura.io/v3/...)" if Infura requires the project secret (e.g. "Private Key Only" or stricter access).

### With secret (Basic Auth) — **fix for failing Infura from your machine**

- **URL format**: `https://<project_id>:<urlencoded_secret>@mainnet.infura.io/v3/<project_id>` (same host path, with Basic Auth).
- **How it’s built**:
  - **`scripts/lib/infura.sh`**  
    - `build_infura_rpc(host_path)` builds `https://[id:urlencoded_secret@]host.infura.io/v3/id` from `INFURA_PROJECT_ID` and optional `INFURA_PROJECT_SECRET`.  
    - **`ensure_infura_rpc_url(url)`** (added for this fix): given any URL, if it is an Infura URL and `INFURA_PROJECT_SECRET` is set, it returns the same URL with Basic Auth; otherwise returns the URL unchanged. Scripts use this before calling `cast` so Infura is always accessed with the secret when it’s set.
  - **Relay (`services/relay`)** builds the mainnet URL with Basic Auth from `INFURA_PROJECT_ID` and `INFURA_PROJECT_SECRET` in its config (e.g. `config.js`).
- **Which scripts use it**:
  - **`scripts/deployment/verify-all-rpcs.sh`** — sources `infura.sh` and passes each RPC through `ensure_infura_rpc_url()` before `cast block-number`.
  - **`scripts/deployment/check-balances-gas-and-deploy.sh`** — sources `infura.sh`; `check_network()` and `get_gas_price_for_chain()` use `ensure_infura_rpc_url()` so every Infura RPC used for balance and gas price is Basic Auth when the secret is set.
  - Other deploy scripts that use `ETHEREUM_MAINNET_RPC` or Infura URLs (e.g. `deploy-all-mainnet.sh`) already use the same pattern or can source `infura.sh` and use `ensure_infura_rpc_url()` for consistency.

So: **Infura RPCs are accessed by building a URL from `.env` (or from `build_infura_rpc`), and scripts now ensure that when `INFURA_PROJECT_SECRET` is set, the URL is converted to the Basic Auth form before any `cast` (or equivalent) call.**

---

## Why It Failed From Your Environment (e.g. ASERET)

- **Observed**: All Infura and some public RPCs failed with "error sending request for url (https://mainnet.infura.io/v3/43b945…)" (and similar); only Chain 138 (e.g. `http://192.168.11.211:8545`) worked.
- **Likely causes**:
  1. **Infura requiring the project secret**: If the project has "Private Key Only" or similar, requests without Basic Auth are rejected (often reported as connection/request errors).
  2. **Scripts using the plain URL**: Previously, `verify-all-rpcs.sh` and `check-balances-gas-and-deploy.sh` passed the raw `.env` URL (no secret) to `cast`, so Infura never received the secret.

---

## Fix Applied

1. **`scripts/lib/infura.sh`**  
   - Added **`ensure_infura_rpc_url(url)`**: if `url` is an Infura URL and `INFURA_PROJECT_SECRET` is set, it returns the Basic Auth URL; otherwise returns `url` unchanged.

2. **`scripts/deployment/verify-all-rpcs.sh`**  
   - Sources `scripts/lib/infura.sh`.  
   - Before each `cast block-number`, the RPC URL is passed through `ensure_infura_rpc_url()` so Infura endpoints are called with Basic Auth when the secret is set.

3. **`scripts/deployment/check-balances-gas-and-deploy.sh`**  
   - Sources `scripts/lib/infura.sh`.  
   - In `check_network()`, the RPC is passed through `ensure_infura_rpc_url()` before `cast balance`.  
   - In `get_gas_price_for_chain()`, the RPC URL is passed through `ensure_infura_rpc_url()` before `cast gas-price`.

**What you need**: Ensure `.env` has both `INFURA_PROJECT_ID` and `INFURA_PROJECT_SECRET` set (no need to change the existing `*_RPC` URLs in `.env`). Scripts will add Basic Auth when calling Infura. Then re-run:

- `bash scripts/deployment/verify-all-rpcs.sh`
- `DEPLOYER_ADDRESS=0x4A66... bash scripts/deployment/check-balances-gas-and-deploy.sh`

If Infura still fails, check network/firewall/proxy and that the project secret is correct in the Infura dashboard.
