Skip to content

Commit e201e3d

Browse files
committed
Handle single unnamed param (#6242)
TOOL-3401 Dashboard handles single unnamed param as "*". Handle this in sdk. Throw error for all other cases. <!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## 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}` <!-- end pr-codex -->
1 parent c8ef941 commit e201e3d

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

packages/thirdweb/src/utils/abi/normalizeFunctionParams.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("normalizeFunctionParams", () => {
5656
});
5757

5858
it("should throw an error if a parameter name is missing", () => {
59-
const abiFunction: AbiFunction = {
59+
let abiFunction: AbiFunction = {
6060
inputs: [{ name: undefined, type: "uint256" }],
6161
type: "function",
6262
stateMutability: "pure",
@@ -67,6 +67,31 @@ describe("normalizeFunctionParams", () => {
6767
expect(() => normalizeFunctionParams(abiFunction, {})).toThrow(
6868
"Missing named parameter for test at index 0",
6969
);
70+
71+
abiFunction = {
72+
inputs: [{ name: "", type: "uint256" }],
73+
type: "function",
74+
stateMutability: "pure",
75+
name: "test",
76+
outputs: [],
77+
};
78+
79+
expect(() => normalizeFunctionParams(abiFunction, {})).toThrow(
80+
"Missing named parameter for test at index 0",
81+
);
82+
83+
abiFunction = {
84+
inputs: [{ name: undefined, type: "uint256" }],
85+
type: "function",
86+
stateMutability: "pure",
87+
name: "test",
88+
outputs: [],
89+
};
90+
91+
const normalized = normalizeFunctionParams(abiFunction, { "*": 123 });
92+
93+
expect(normalized.length).to.eq(1);
94+
expect(normalized[0]).to.eq(123);
7095
});
7196

7297
it("should throw an error if a parameter value is missing", () => {

packages/thirdweb/src/utils/abi/normalizeFunctionParams.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ export function normalizeFunctionParams(
1313
abiFunction.inputs.map((i) => i.type),
1414
abiFunction.inputs.map((input, index) => {
1515
const value = input.name;
16-
if (value === undefined) {
17-
throw new Error(
18-
`Missing named parameter for ${"name" in abiFunction ? abiFunction.name : "constructor"} at index ${index}`,
19-
);
16+
if (value === undefined || value.length === 0) {
17+
// TODO: Handle multiple unnamed params
18+
if (!params["*"]) {
19+
throw new Error(
20+
`Missing named parameter for ${"name" in abiFunction ? abiFunction.name : "constructor"} at index ${index}`,
21+
);
22+
}
23+
24+
return params["*"];
2025
}
2126
const valueWithoutUnderscore = value.replace(/^_+/, "");
2227
const normalizedValue =

0 commit comments

Comments
 (0)