Skip to content

Commit

Permalink
fix: CLI encoding for arrays and structs (#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Sep 19, 2023
1 parent f852432 commit 85283bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
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

0 comments on commit 85283bd

Please sign in to comment.