RPC related cheatcodes
Signature
Section titled “Signature”struct Rpc { /// The alias of the RPC URL. string key; /// The RPC URL. string url;}// Returns the URL for a configured aliasfunction rpcUrl(string calldata alias) external returns (string memory);// Returns all configured (alias, URL) pairsfunction rpcUrls() external returns(string[2][] memory);// Returns all rpc urls and their aliases as structs.function rpcUrlStructs() external view returns (Rpc[] memory urls);/// Performs an Ethereum JSON-RPC request to the current fork URL.function rpc(string calldata method, string calldata params) external returns (bytes memory data);/// Performs an Ethereum JSON-RPC request to the given endpoint.function rpc(string calldata urlOrAlias, string calldata method, string calldata params) external returns (bytes memory data);Description
Section titled “Description”Provides cheatcodes to access all RPC endpoints configured in the forking.rpcEndpoints object of the Solidity tests configuration, and the ability to make rpc calls using the configured fork URL.
Examples
Section titled “Examples”The following rpcEndpoints in Solidity tests configuration registers two RPC aliases:
optimismreferences the URL directlymainnetreferences theRPC_MAINNETconfiguration variable that is expected to contain the actual URL
For more details on how to use configuration variables, see Configuration Variables.
// --snip--forking: { // --snip-- rpcEndpoints: { optimism: "https://optimism.alchemyapi.io/v2/...", mainnet: configVariable("MAINNET_URL"), }}string memory url = vm.rpcUrl("optimism");assertEq(url, "https://optimism.alchemyapi.io/v2/...");If a ENV var is missing, rpcUrl() will revert:
vm.expectRevert("Failed to resolve env var `${RPC_MAINNET}` in `RPC_MAINNET`: environment variable not found");string memory url = vm.rpcUrl("mainnet");Retrieve all available alias -> URL pairs
string[2][] memory allUrls = vm.rpcUrls();assertEq(allUrls.length, 2);
string[2] memory val = allUrls[0];assertEq(val[0], "optimism");
string[2] memory env = allUrls[1];assertEq(env[0], "mainnet");Make an RPC call to eth_getBalance
// balance at block <https://etherscan.io/block/18332681>bytes memory result = vm.rpc("eth_getBalance", "[\"0x8D97689C9818892B700e27F316cc3E41e17fBeb9\", \"0x117BC09\"]")assertEq(hex"10b7c11bcb51e6", result);