From 7332feba5a13b316956efe3cab4da28f5e712524 Mon Sep 17 00:00:00 2001 From: Nikola Malic Date: Wed, 17 Dec 2025 13:45:07 +0100 Subject: [PATCH 1/2] add configuration exists method and tests --- src/StdConfig.sol | 19 +++++++++++++++++++ test/Config.t.sol | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/StdConfig.sol b/src/StdConfig.sol index f208ea4d..ddf0fceb 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("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"; From c30a16ac7f5e26facaadce92538775532cc6a184 Mon Sep 17 00:00:00 2001 From: Nikola Malic Date: Fri, 19 Dec 2025 11:41:39 +0100 Subject: [PATCH 2/2] add chain_id to example --- src/StdConfig.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StdConfig.sol b/src/StdConfig.sol index ddf0fceb..80c2ed42 100644 --- a/src/StdConfig.sol +++ b/src/StdConfig.sol @@ -322,7 +322,7 @@ contract StdConfig { } /// @dev Checks the existence of a variable for a given chain ID and key, and returns a boolean. - /// Example: `bool hasKey = config.exists("my_key");` + /// Example: `bool hasKey = config.exists(1, "my_key");` /// /// @param chain_id The chain ID to check. /// @param key The variable key name.