BlockReq
返回全部文章

Rpc Tutorials

在 Foundry 中使用 BlockReq RPC

面向 Foundry fork 测试、Cast 检查和脚本广播的 BlockReq 公共 Ethereum RPC 接入指南。

BlockReq Engineering2026年7月1日7 分钟阅读

Using BlockReq RPC with Foundry

Foundry works well with a plain HTTPS JSON-RPC endpoint. For the public Ethereum endpoint, keep the URL in an environment variable so Cast, Forge tests, and scripts all read the same value.

export BLOCKREQ_ETH_RPC_URL="https://ethereum-rpc.blockreq.com/v1/rpc/public"
cast chain-id --rpc-url "$BLOCKREQ_ETH_RPC_URL"
cast block-number --rpc-url "$BLOCKREQ_ETH_RPC_URL"

The chain id should be 1 for Ethereum mainnet. If you are targeting another chain, use the matching BlockReq endpoint and keep the Foundry command pointed at that chain. Do not reuse a mainnet URL with a Base, BSC, or Arbitrum chain configuration.

Fork tests

Use --fork-url for local tests that need mainnet state. The command below does not broadcast anything; it only asks Forge to resolve state through the configured RPC endpoint while running your test suite.

forge test --fork-url "$BLOCKREQ_ETH_RPC_URL" -vv

For test code that creates a fork directly, read the URL from the environment. This keeps the endpoint out of git and makes CI or local shells responsible for injecting the value.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

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

contract BlockReqForkTest is Test {
    function setUp() public {
        string memory rpcUrl = vm.envString("BLOCKREQ_ETH_RPC_URL");
        vm.createSelectFork(rpcUrl);
    }

    function testMainnetForkIsLive() public view {
        assertGt(block.number, 0);
        assertEq(block.chainid, 1);
    }
}

Scripts

Foundry scripts can dry-run against the same endpoint before any transaction is sent. Keep --broadcast out of your default command and add it only for an intentional deployment or transaction run.

forge script script/Deploy.s.sol:Deploy \
  --rpc-url "$BLOCKREQ_ETH_RPC_URL" \
  -vv

When you are ready to send transactions, add signer configuration and --broadcast intentionally. Treat private keys, hardware wallet configuration, and account selection as deployment concerns, not article or repository content.

forge script script/Deploy.s.sol:Deploy \
  --rpc-url "$BLOCKREQ_ETH_RPC_URL" \
  --broadcast \
  -vv

Method caveats

Basic methods such as eth_chainId, eth_blockNumber, calls, reads, logs, and transaction submission follow normal JSON-RPC behavior. Historical state, debug_*, and trace_* calls can be chain-specific, provider-specific, plan-gated, or archive-dependent. Build tests so they fail with clear errors when a method is unavailable instead of assuming every endpoint supports every diagnostic method.