From b2243ad1e46f6fdc961f0002e87842f7600f5bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Thu, 21 Sep 2023 09:03:30 +0200 Subject: [PATCH] feat: listing expected args in CLI (#2423) Fixes [#2382](https://github.com/AztecProtocol/aztec-packages/issues/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 --- yarn-project/cli/src/encoding.ts | 4 +++- yarn-project/cli/src/index.ts | 12 ------------ yarn-project/cli/src/test/utils.test.ts | 4 +--- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/yarn-project/cli/src/encoding.ts b/yarn-project/cli/src/encoding.ts index 50a088cc4c5..a118c3b12b1 100644 --- a/yarn-project/cli/src/encoding.ts +++ b/yarn-project/cli/src/encoding.ts @@ -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) => { diff --git a/yarn-project/cli/src/index.ts b/yarn-project/cli/src/index.ts index 71e61409f65..fe38efd4508 100644 --- a/yarn-project/cli/src/index.ts +++ b/yarn-project/cli/src/index.ts @@ -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); @@ -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); diff --git a/yarn-project/cli/src/test/utils.test.ts b/yarn-project/cli/src/test/utils.test.ts index b37ec4fd7dd..3d34a48a217 100644 --- a/yarn-project/cli/src/test/utils.test.ts +++ b/yarn-project/cli/src/test/utils.test.ts @@ -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()]);