Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getChain fails on chain aliases defined in foundry.toml #440

Open
emo-eth opened this issue Aug 24, 2023 · 1 comment
Open

getChain fails on chain aliases defined in foundry.toml #440

emo-eth opened this issue Aug 24, 2023 · 1 comment

Comments

@emo-eth
Copy link
Contributor

emo-eth commented Aug 24, 2023

The comment for getChain(string memory chainAlias) says // The RPC URL will be fetched from config or defaultRpcUrls if possible., so it seems to me that intended behavior is not to fail on aliases besides the hard-coded canonical ones.

Here's a minimal reproduction.

In foundry.toml, add the stanza:

[rpc_endpoints]
my_alias = 'https://mycoolalias.com'

Test:

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

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

contract AliasTest is Test {
    function testGetChain() public {
        assertEq(getChain("my_alias").rpcUrl, "https://mycoolalias.com");
    }

    function testRpcUrl() public {
        assertEq(vm.rpcUrl("my_alias"), "https://mycoolalias.com");
    }
}

The root of the issue is in StdChains.getChain:

        chain = chains[chainAlias];
        require(
            chain.chainId != 0,
            string(abi.encodePacked("StdChains getChain(string): Chain with alias \"", chainAlias, "\" not found."))
        );

        chain = getChainWithUpdatedRpcUrl(chainAlias, chain);

A chain is validated as existing before calling getChainWithUpdatedRpcUrl, which uses vm.rpcUrl to fetch an alias from the config.

Another pain point is that there is no way of specifying chainId in the config – but the method checks chainId to see if it's been defined.

StdChains uses VmSafe,so it's not possible to spin up a temporary fork and call chainId() and set the chainId on the Chain struct without changing or casting the VmSafe to a regular Vm. Otherwise a fix would look something like this:

// The RPC URL will be fetched from config or defaultRpcUrls if possible.
    function getChain(string memory chainAlias) internal virtual returns (Chain memory chain) {
        require(bytes(chainAlias).length != 0, "StdChains getChain(string): Chain alias cannot be the empty string.");

        initializeStdChains();
        chain = chains[chainAlias];
        bool uninitialized = chain.chainId == 0;
        chain = getChainWithUpdatedRpcUrl(chainAlias, chain);

        require(
            chain.chainId != 0,
            string(abi.encodePacked("StdChains getChain(string): Chain with alias \"", chainAlias, "\" not found."))
        );
        // If the chain was not initialized, save it.
        if (uninitialized) {
            chains[chainAlias] = chain;
        }
    }
@mds1
Copy link
Collaborator

mds1 commented Aug 28, 2023

Thanks for the details! For chains not present in the default list, you should be able to use the setChain method here to define new chains. Adding this makes both tests pass:

function setUp() public {
    Chain memory chain =
        Chain({name: "MyCoolChain", chainId: 123, chainAlias: "my_alias", rpcUrl: ""});
    setChain("my_alias", chain);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants