diff --git a/src/StdConfig.sol b/src/StdConfig.sol index f208ea4d..80c2ed42 100644 --- a/src/StdConfig.sol +++ b/src/StdConfig.sol @@ -321,6 +321,25 @@ contract StdConfig { return get(vm.getChainId(), key); } + /// @dev Checks the existence of a variable for a given chain ID and key, and returns a boolean. + /// Example: `bool hasKey = config.exists(1, "my_key");` + /// + /// @param chain_id The chain ID to check. + /// @param key The variable key name. + /// @return `bool` indicating whether a variable with the given key exists. + function exists(uint256 chain_id, string memory key) public view returns (bool) { + return _dataOf[chain_id][key].length > 0; + } + + /// @dev Checks existance of a variable for the current chain id and a given key, and returns a boolean. + /// Example: `bool hasKey = config.exists("my_key");` + /// + /// @param key The variable key name. + /// @return `bool` indicating whether a variable with the given key exists. + function exists(string memory key) public view returns (bool) { + return exists(vm.getChainId(), key); + } + /// @notice Returns the numerical chain ids for all configured chains. function getChainIds() public view returns (uint256[] memory) { string[] memory keys = _chainKeys; diff --git a/test/Config.t.sol b/test/Config.t.sol index df77d719..b5f3154d 100644 --- a/test/Config.t.sol +++ b/test/Config.t.sol @@ -125,6 +125,35 @@ contract ConfigTest is Test, Config { assertEq(vm.getChainId(), 10); } + function test_configExists() public { + _loadConfig("./test/fixtures/config.toml", false); + + string[] memory keys = new string[](7); + keys[0] = "is_live"; + keys[1] = "weth"; + keys[2] = "word"; + keys[3] = "number"; + keys[4] = "signed_number"; + keys[5] = "b"; + keys[6] = "str"; + + // Read and assert RPC URL for Mainnet (chain ID 1) + assertEq(config.getRpcUrl(1), "https://reth-ethereum.ithaca.xyz/rpc"); + + for (uint256 i = 0; i < keys.length; ++i) { + assertTrue(config.exists(1, keys[i])); + assertFalse(config.exists(1, string.concat(keys[i], "_"))); + } + + // Assert RPC URL for Optimism (chain ID 10) + assertEq(config.getRpcUrl(10), "https://mainnet.optimism.io"); + + for (uint256 i = 0; i < keys.length; ++i) { + assertTrue(config.exists(10, keys[i])); + assertFalse(config.exists(10, string.concat(keys[i], "_"))); + } + } + function test_writeConfig() public { // Create a temporary copy of the config file to avoid modifying the original. string memory originalConfig = "./test/fixtures/config.toml";