diff --git a/bal_tools/ts_config_loader.py b/bal_tools/ts_config_loader.py index 37a1fe9..a919e6d 100644 --- a/bal_tools/ts_config_loader.py +++ b/bal_tools/ts_config_loader.py @@ -298,14 +298,33 @@ def convert_single_quote(match): # Also handle when they appear as object values (: AaveV3Arbitrum) obj = re.sub(r":\s*([A-Z][a-zA-Z0-9_]+)(?=\s*[,}])", r": null", obj) - # 7) Handle specific ternary expressions that are common in config files - # Replace the rpcUrl ternary with the fallback value for simplicity - obj = re.sub( - r'"rpcUrl":\s*env\.DRPC_API_KEY\s*\?\s*"[^"]+"\s*:\s*([^,]+),', - r'"rpcUrl": \1,', - obj, - flags=re.MULTILINE | re.DOTALL, - ) + # 6d) Handle ternary expressions (e.g. env.VAR ? "url1" : "url2") + # This must happen BEFORE env.* replacement to properly extract the else branch + # Pattern matches: condition ? "then_value" : else_value + def handle_ternary(text): + # Handle multi-line ternaries like: + # "key": env.VAR + # ? `string` + # : 'string', + pattern = r'("[\w]+":\s*)[a-zA-Z_.]+\s*\?\s*(?:`[^`]*`|"[^"]*"|\'[^\']*\')\s*:\s*(`[^`]*`|"[^"]*"|\'[^\']*\')' + match = re.search(pattern, text, flags=re.DOTALL) + while match: + key_part = match.group(1) # e.g. '"rpcUrl": ' + else_value = match.group(2) # the else branch value + # Convert backticks/single quotes to double quotes for the else value + if else_value.startswith("`"): + else_value = '"' + else_value[1:-1] + '"' + elif else_value.startswith("'"): + else_value = '"' + else_value[1:-1] + '"' + text = text[: match.start()] + key_part + else_value + text[match.end() :] + match = re.search(pattern, text, flags=re.DOTALL) + return text + + obj = handle_ternary(obj) + + # 6e) Handle environment variable references like env.PAXOS_APR_KEY + # BUT preserve ${env.VAR} template interpolations - these are handled by get_subgraph_url_from_backend_config + obj = re.sub(r"(?