Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CLI encoding for arrays and structs #2407

Merged
merged 3 commits into from
Sep 19, 2023
Merged
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
14 changes: 6 additions & 8 deletions yarn-project/cli/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ function encodeArg(arg: string, abiType: ABIType, name: string): any {
throw new Error(`Unable to parse arg ${arg} as struct`);
}
if (Array.isArray(obj)) throw Error(`Array passed for arg ${name}. Expected a struct.`);
const res = [];
const res: any = {};
for (const field of abiType.fields) {
// Remove field name from list as it's present
const arg = obj[field.name];
if (!arg) throw Error(`Expected field ${field.name} not found in struct ${name}.`);
res.push(encodeArg(obj[field.name], field.type, field.name));
res[field.name] = encodeArg(obj[field.name], field.type, field.name);
}
return res;
}
Expand All @@ -94,10 +94,8 @@ export function encodeArgs(args: any[], params: ABIParameter[]) {
`Invalid number of args provided. Expected: ${params.length}, received: ${args.length}\nReceived args: ${args}`,
);
}
return args
.map((arg: any, index) => {
const { type, name } = params[index];
return encodeArg(arg, type, name);
})
.flat();
return args.map((arg: any, index) => {
const { type, name } = params[index];
return encodeArg(arg, type, name);
});
}
10 changes: 5 additions & 5 deletions yarn-project/cli/src/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ describe('CLI Utils', () => {
addr1.toBigInt(),
false,
33n,
addr1.toBigInt(),
addr2.toBigInt(),
addr3.toBigInt(),
field.toBigInt(),
true,
[addr1.toBigInt(), addr2.toBigInt(), addr3.toBigInt()],
{
subField1: field.toBigInt(),
subField2: true,
},
];
expect(result).toEqual(exp);
});
Expand Down