From e201e3d423bd42781e53239c74e311e5474706c7 Mon Sep 17 00:00:00 2001 From: kumaryash90 Date: Fri, 14 Feb 2025 06:22:30 +0000 Subject: [PATCH] Handle single unnamed param (#6242) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TOOL-3401 Dashboard handles single unnamed param as "*". Handle this in sdk. Throw error for all other cases. --- ## PR-Codex overview This PR enhances the `normalizeFunctionParams` function to handle cases where parameter names may be missing or empty. It introduces error handling for unnamed parameters and updates the associated tests to cover these scenarios. ### Detailed summary - Updated the condition to check for both `undefined` and empty string in parameter names. - Added a comment for future handling of multiple unnamed parameters. - Modified test cases to include checks for empty parameter names. - Ensured that unnamed parameters can be accessed via the `params["*"]` syntax. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .../utils/abi/normalizeFunctionParams.test.ts | 27 ++++++++++++++++++- .../src/utils/abi/normalizeFunctionParams.ts | 13 ++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/thirdweb/src/utils/abi/normalizeFunctionParams.test.ts b/packages/thirdweb/src/utils/abi/normalizeFunctionParams.test.ts index 35d377f10c8..79ed5fc09ff 100644 --- a/packages/thirdweb/src/utils/abi/normalizeFunctionParams.test.ts +++ b/packages/thirdweb/src/utils/abi/normalizeFunctionParams.test.ts @@ -56,7 +56,7 @@ describe("normalizeFunctionParams", () => { }); it("should throw an error if a parameter name is missing", () => { - const abiFunction: AbiFunction = { + let abiFunction: AbiFunction = { inputs: [{ name: undefined, type: "uint256" }], type: "function", stateMutability: "pure", @@ -67,6 +67,31 @@ describe("normalizeFunctionParams", () => { expect(() => normalizeFunctionParams(abiFunction, {})).toThrow( "Missing named parameter for test at index 0", ); + + abiFunction = { + inputs: [{ name: "", type: "uint256" }], + type: "function", + stateMutability: "pure", + name: "test", + outputs: [], + }; + + expect(() => normalizeFunctionParams(abiFunction, {})).toThrow( + "Missing named parameter for test at index 0", + ); + + abiFunction = { + inputs: [{ name: undefined, type: "uint256" }], + type: "function", + stateMutability: "pure", + name: "test", + outputs: [], + }; + + const normalized = normalizeFunctionParams(abiFunction, { "*": 123 }); + + expect(normalized.length).to.eq(1); + expect(normalized[0]).to.eq(123); }); it("should throw an error if a parameter value is missing", () => { diff --git a/packages/thirdweb/src/utils/abi/normalizeFunctionParams.ts b/packages/thirdweb/src/utils/abi/normalizeFunctionParams.ts index 92fb86e1ce3..70b1d6d1335 100644 --- a/packages/thirdweb/src/utils/abi/normalizeFunctionParams.ts +++ b/packages/thirdweb/src/utils/abi/normalizeFunctionParams.ts @@ -13,10 +13,15 @@ export function normalizeFunctionParams( abiFunction.inputs.map((i) => i.type), abiFunction.inputs.map((input, index) => { const value = input.name; - if (value === undefined) { - throw new Error( - `Missing named parameter for ${"name" in abiFunction ? abiFunction.name : "constructor"} at index ${index}`, - ); + if (value === undefined || value.length === 0) { + // TODO: Handle multiple unnamed params + if (!params["*"]) { + throw new Error( + `Missing named parameter for ${"name" in abiFunction ? abiFunction.name : "constructor"} at index ${index}`, + ); + } + + return params["*"]; } const valueWithoutUnderscore = value.replace(/^_+/, ""); const normalizedValue =