{"version":3,"file":"query-C4skW30n-Dl7cNQEG.cjs","names":["createEvmPublicClient","createAbiFunctionItem"],"sources":["../../adapter-evm-core/dist/input-parser-CegVgfkw.mjs","../../adapter-evm-core/dist/query-C4skW30n.mjs"],"sourcesContent":["import { logger } from \"@openzeppelin/ui-utils\";\nimport { getAddress, isAddress } from \"viem\";\n\n//#region src/transform/input-parser.ts\n/**\n* Recursively parses a raw input value based on its expected ABI type definition.\n*\n* @param param The ABI parameter definition ({ name, type, components?, ... })\n* @param rawValue The raw value obtained from the form input or hardcoded config.\n* @param isRecursive Internal flag to indicate if the call is nested.\n* @returns The parsed and typed value suitable for ABI encoding.\n* @throws {Error} If parsing or type validation fails.\n*/\nfunction parseEvmInput(param, rawValue, isRecursive = false) {\n\tconst { type, name } = param;\n\tconst baseType = type.replace(/\\[\\d*\\]$/, \"\");\n\tconst isArray = type.endsWith(\"]\");\n\ttry {\n\t\tif (isArray) {\n\t\t\tlet parsedArray;\n\t\t\tif (!isRecursive) {\n\t\t\t\tif (typeof rawValue !== \"string\") throw new Error(\"Array input must be a JSON string representation.\");\n\t\t\t\ttry {\n\t\t\t\t\tparsedArray = JSON.parse(rawValue);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tthrow new Error(`Invalid JSON for array: ${e.message}`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (!Array.isArray(rawValue)) throw new Error(\"Internal error: Expected array in recursive call.\");\n\t\t\t\tparsedArray = rawValue;\n\t\t\t}\n\t\t\tif (!Array.isArray(parsedArray)) throw new Error(\"Parsed JSON is not an array.\");\n\t\t\tconst itemAbiParam = {\n\t\t\t\t...param,\n\t\t\t\ttype: baseType\n\t\t\t};\n\t\t\treturn parsedArray.map((item) => parseEvmInput(itemAbiParam, item, true));\n\t\t}\n\t\tif (baseType === \"tuple\") {\n\t\t\tif (!param.components) throw new Error(`ABI definition missing 'components' for tuple parameter '${name}'.`);\n\t\t\tlet parsedObject;\n\t\t\tif (!isRecursive) {\n\t\t\t\tif (typeof rawValue !== \"string\") throw new Error(\"Tuple input must be a JSON string representation of an object.\");\n\t\t\t\ttry {\n\t\t\t\t\tparsedObject = JSON.parse(rawValue);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tthrow new Error(`Invalid JSON for tuple: ${e.message}`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (typeof rawValue !== \"object\" || rawValue === null || Array.isArray(rawValue)) throw new Error(\"Internal error: Expected object in recursive tuple call.\");\n\t\t\t\tparsedObject = rawValue;\n\t\t\t}\n\t\t\tif (typeof parsedObject !== \"object\" || parsedObject === null || Array.isArray(parsedObject)) throw new Error(\"Parsed JSON is not an object for tuple.\");\n\t\t\tconst resultObject = {};\n\t\t\tfor (const component of param.components) {\n\t\t\t\tif (!(component.name in parsedObject)) throw new Error(`Missing component '${component.name}' in tuple JSON.`);\n\t\t\t\tresultObject[component.name] = parseEvmInput(component, parsedObject[component.name], true);\n\t\t\t}\n\t\t\tif (Object.keys(parsedObject).length !== param.components.length) {\n\t\t\t\tconst expectedKeys = param.components.map((c) => c.name).join(\", \");\n\t\t\t\tconst actualKeys = Object.keys(parsedObject).join(\", \");\n\t\t\t\tthrow new Error(`Tuple object has incorrect number of keys. Expected ${param.components.length} (${expectedKeys}), but got ${Object.keys(parsedObject).length} (${actualKeys}).`);\n\t\t\t}\n\t\t\treturn resultObject;\n\t\t}\n\t\tif (baseType.startsWith(\"bytes\")) {\n\t\t\tif (typeof rawValue !== \"string\") throw new Error(\"Bytes input must be a string.\");\n\t\t\tif (!/^0x([0-9a-fA-F]{2})*$/.test(rawValue)) throw new Error(`Invalid hex string format for ${type}: must start with 0x and contain only hex characters.`);\n\t\t\tconst fixedSizeMatch = baseType.match(/^bytes(\\d+)$/);\n\t\t\tif (fixedSizeMatch) {\n\t\t\t\tconst expectedBytes = parseInt(fixedSizeMatch[1], 10);\n\t\t\t\tconst actualBytes = (rawValue.length - 2) / 2;\n\t\t\t\tif (actualBytes !== expectedBytes) throw new Error(`Invalid length for ${type}: expected ${expectedBytes} bytes (${expectedBytes * 2} hex chars), got ${actualBytes} bytes.`);\n\t\t\t}\n\t\t\treturn rawValue;\n\t\t}\n\t\tif (baseType.startsWith(\"uint\") || baseType.startsWith(\"int\")) {\n\t\t\tif (rawValue === \"\" || rawValue === null || rawValue === void 0) throw new Error(\"Numeric value cannot be empty.\");\n\t\t\ttry {\n\t\t\t\treturn BigInt(rawValue);\n\t\t\t} catch {\n\t\t\t\tthrow new Error(`Invalid numeric value: '${rawValue}'.`);\n\t\t\t}\n\t\t} else if (baseType === \"address\") {\n\t\t\tif (typeof rawValue !== \"string\" || !rawValue) throw new Error(\"Address value must be a non-empty string.\");\n\t\t\tif (!isAddress(rawValue)) throw new Error(`Invalid address format: '${rawValue}'.`);\n\t\t\treturn getAddress(rawValue);\n\t\t} else if (baseType === \"bool\") {\n\t\t\tif (typeof rawValue === \"boolean\") return rawValue;\n\t\t\tif (typeof rawValue === \"string\") {\n\t\t\t\tconst lowerVal = rawValue.toLowerCase().trim();\n\t\t\t\tif (lowerVal === \"true\") return true;\n\t\t\t\tif (lowerVal === \"false\") return false;\n\t\t\t}\n\t\t\treturn Boolean(rawValue);\n\t\t} else if (baseType === \"string\") return String(rawValue);\n\t\tlogger.warn(\"parseEvmInput\", `Unknown EVM parameter type encountered: '${type}'. Using raw value.`);\n\t\treturn rawValue;\n\t} catch (error) {\n\t\tthrow new Error(`Failed to parse value for parameter '${name || \"(unnamed)\"}' (type '${type}'): ${error.message}`);\n\t}\n}\n\n//#endregion\nexport { parseEvmInput as t };\n//# sourceMappingURL=input-parser-CegVgfkw.mjs.map","import { t as createEvmPublicClient } from \"./public-client-DtJS2A20.mjs\";\nimport { t as createAbiFunctionItem } from \"./transformer-0vQSxsqD.mjs\";\nimport { t as parseEvmInput } from \"./input-parser-CegVgfkw.mjs\";\nimport { logger } from \"@openzeppelin/ui-utils\";\nimport { isAddress } from \"viem\";\n\n//#region src/query/view-checker.ts\n/**\n* Determines if a function is a view/pure function (read-only).\n* @param functionDetails The function details from the contract schema.\n* @returns True if the function is read-only, false otherwise.\n*/\nfunction isEvmViewFunction(functionDetails) {\n\treturn functionDetails.stateMutability === \"view\" || functionDetails.stateMutability === \"pure\";\n}\n\n//#endregion\n//#region src/query/handler.ts\n/**\n* Core logic for querying an EVM view function.\n* This is a stateless version that accepts an RPC URL directly.\n*\n* @param contractAddress - Address of the contract.\n* @param functionId - ID of the function to query.\n* @param params - Raw parameters for the function call.\n* @param schema - Contract schema with function definitions.\n* @param rpcUrl - RPC URL to use for the query.\n* @param networkConfig - Optional EVM-compatible network configuration for chain metadata (works with any ecosystem).\n* @returns The decoded result of the view function call.\n*/\nasync function queryEvmViewFunction(contractAddress, functionId, params, schema, rpcUrl, networkConfig) {\n\tlogger.info(\"queryEvmViewFunction\", `Querying view function: ${functionId} on ${contractAddress}`, { params });\n\ttry {\n\t\tif (!contractAddress || !isAddress(contractAddress)) throw new Error(`Invalid contract address provided: ${contractAddress}`);\n\t\tconst functionDetails = schema.functions.find((fn) => fn.id === functionId);\n\t\tif (!functionDetails) throw new Error(`Function with ID ${functionId} not found in contract schema.`);\n\t\tif (!isEvmViewFunction(functionDetails)) throw new Error(`Function ${functionDetails.name} is not a view function.`);\n\t\tconst expectedInputs = functionDetails.inputs;\n\t\tif (params.length !== expectedInputs.length) throw new Error(`Incorrect number of parameters provided for ${functionDetails.name}. Expected ${expectedInputs.length}, got ${params.length}.`);\n\t\tconst args = expectedInputs.map((inputParam, index) => {\n\t\t\tlet rawValue = params[index];\n\t\t\tif (typeof inputParam.type === \"string\" && inputParam.type.endsWith(\"[]\") && Array.isArray(rawValue)) rawValue = JSON.stringify(rawValue);\n\t\t\treturn parseEvmInput(inputParam, rawValue, false);\n\t\t});\n\t\tlogger.debug(\"queryEvmViewFunction\", \"Parsed Args for readContract:\", args);\n\t\tconst publicClient = createEvmPublicClient(rpcUrl, networkConfig?.viemChain);\n\t\tconst functionAbiItem = createAbiFunctionItem(functionDetails);\n\t\tlogger.debug(\"queryEvmViewFunction\", `[Query ${functionDetails.name}] Calling readContract with ABI:`, functionAbiItem, \"Args:\", args);\n\t\tlet decodedResult;\n\t\ttry {\n\t\t\tdecodedResult = await publicClient.readContract({\n\t\t\t\taddress: contractAddress,\n\t\t\t\tabi: [functionAbiItem],\n\t\t\t\tfunctionName: functionDetails.name,\n\t\t\t\targs\n\t\t\t});\n\t\t} catch (readError) {\n\t\t\tlogger.error(\"queryEvmViewFunction\", `[Query ${functionDetails.name}] publicClient.readContract specific error:`, readError);\n\t\t\tthrow new Error(`Viem readContract failed for ${functionDetails.name}: ${readError.message}`);\n\t\t}\n\t\tlogger.debug(\"queryEvmViewFunction\", `[Query ${functionDetails.name}] Raw decoded result:`, decodedResult);\n\t\treturn decodedResult;\n\t} catch (error) {\n\t\tconst errorMessage = `Failed to query view function ${functionId}: ${error.message}`;\n\t\tlogger.error(\"queryEvmViewFunction\", errorMessage, {\n\t\t\tcontractAddress,\n\t\t\tfunctionId,\n\t\t\tparams,\n\t\t\terror\n\t\t});\n\t\tthrow new Error(errorMessage);\n\t}\n}\n\n//#endregion\nexport { isEvmViewFunction as n, queryEvmViewFunction as t };\n//# sourceMappingURL=query-C4skW30n.mjs.map"],"mappings":";;;;;;;;;;;;;;;;AAaA,SAAS,cAAc,OAAO,UAAU,cAAc,OAAO;CAC5D,MAAM,EAAE,MAAM,SAAS;CACvB,MAAM,WAAW,KAAK,QAAQ,YAAY,GAAG;CAC7C,MAAM,UAAU,KAAK,SAAS,IAAI;AAClC,KAAI;AACH,MAAI,SAAS;GACZ,IAAI;AACJ,OAAI,CAAC,aAAa;AACjB,QAAI,OAAO,aAAa,SAAU,OAAM,IAAI,MAAM,oDAAoD;AACtG,QAAI;AACH,mBAAc,KAAK,MAAM,SAAS;aAC1B,GAAG;AACX,WAAM,IAAI,MAAM,2BAA2B,EAAE,UAAU;;UAElD;AACN,QAAI,CAAC,MAAM,QAAQ,SAAS,CAAE,OAAM,IAAI,MAAM,oDAAoD;AA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