Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/StdConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions test/Config.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down