Skip to content

Commit

Permalink
feat: listing expected args in CLI (#2423)
Browse files Browse the repository at this point in the history
Fixes
[#2382](#2382)

CLI now lists out what args were expected:

> Error in command execution
> Error: Invalid args provided.
> Expected args: [initial_supply: field, owner: field]
> Received args: 1000
  • Loading branch information
benesjan authored Sep 21, 2023
1 parent b58cfb5 commit b2243ad
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
4 changes: 3 additions & 1 deletion yarn-project/cli/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ function encodeArg(arg: string, abiType: ABIType, name: string): any {
export function encodeArgs(args: any[], params: ABIParameter[]) {
if (args.length !== params.length) {
throw new Error(
`Invalid number of args provided. Expected: ${params.length}, received: ${args.length}\nReceived args: ${args}`,
`Invalid args provided.\nExpected args: [${params
.map(param => param.name + ': ' + param.type.kind)
.join(', ')}]\nReceived args: ${args.join(', ')}`,
);
}
return args.map((arg: any, index) => {
Expand Down
12 changes: 0 additions & 12 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {

const constructor = getAbiFunction(contractAbi, 'constructor');
if (!constructor) throw new Error(`Constructor not found in contract ABI`);
if (constructor.parameters.length !== options.args.length) {
throw new Error(
`Invalid number of args passed (expected ${constructor.parameters.length} but got ${options.args.length})`,
);
}

debugLogger(`Input arguments: ${options.args.map((x: any) => `"${x}"`).join(', ')}`);
const args = encodeArgs(options.args, constructorAbi!.parameters);
Expand Down Expand Up @@ -376,13 +371,6 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
log,
);

const fnAbi = getAbiFunction(contractAbi, functionName);
if (fnAbi.parameters.length !== options.args.length) {
throw Error(
`Invalid number of args passed. Expected ${fnAbi.parameters.length}; Received: ${options.args.length}`,
);
}

const privateKey = GrumpkinScalar.fromString(stripLeadingHex(options.privateKey));

const client = await createCompatibleClient(options.rpcUrl, debugLogger);
Expand Down
4 changes: 1 addition & 3 deletions yarn-project/cli/src/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ describe('CLI Utils', () => {
it('Errors on invalid inputs', () => {
// invalid number of args
const args1 = [field.toString(), 'false'];
expect(() => encodeArgs(args1, mockContractAbi.functions[1].parameters)).toThrow(
'Invalid number of args provided. Expected: 5, received: 2',
);
expect(() => encodeArgs(args1, mockContractAbi.functions[1].parameters)).toThrow('Invalid args provided');

// invalid array length
const invalidArray = fieldArray.concat([Fr.random().toString()]);
Expand Down

0 comments on commit b2243ad

Please sign in to comment.