From b0fe203134e572edcdb2506160b0b4ad68353dc3 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Tue, 15 Oct 2024 10:44:16 +0800 Subject: [PATCH] feat: feature/callMethod --- package.json | 11 +---------- src/command/call.js | 15 +++++++++++---- src/utils/constants.js | 16 ++++++++++++++++ test/command/call.test.js | 23 +++++++++++++++++++++-- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 4eb6781..e937d8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-command", - "version": "0.1.50-beta.0", + "version": "0.1.51-beta.0", "description": "A CLI tools for AElf", "main": "src/index.js", "type": "module", @@ -114,14 +114,5 @@ "commitizen": { "path": "./node_modules/cz-conventional-changelog" } - }, - "jest": { - "collectCoverageFrom": [ - "src/**/*.js" - ], - "transform": { - "^.+\\.js$": "babel-jest" - }, - "testEnvironment": "node" } } diff --git a/src/command/call.js b/src/command/call.js index b9ac3a8..00e8843 100644 --- a/src/command/call.js +++ b/src/command/call.js @@ -2,7 +2,7 @@ import AElf from 'aelf-sdk'; import inquirer from 'inquirer'; import chalk from 'chalk'; import BaseSubCommand from './baseSubCommand.js'; -import { callCommandUsages, callCommandParameters } from '../utils/constants.js'; +import { callCommandUsages, callCommandParameters, callGlobalOptionValidatorDesc } from '../utils/constants.js'; import { getContractMethods, getContractInstance, @@ -37,9 +37,10 @@ class CallCommand extends BaseSubCommand { description = 'Call a read-only method on a contract.', parameters = callCommandParameters, usage = callCommandUsages, - options = [] + options = [], + validatorDesc = callGlobalOptionValidatorDesc ) { - super(name, parameters, description, options, usage, rc); + super(name, parameters, description, options, usage, rc, validatorDesc); } /** * Calls a method with specified parameters. @@ -94,7 +95,13 @@ class CallCommand extends BaseSubCommand { const aelf = new AElf(new AElf.providers.HttpProvider(endpoint)); try { let { contractAddress, method, params } = subOptions; - const wallet = getWallet(datadir, account, password); + let wallet; + if (!account || !password) { + // no need to provide account and password + wallet = AElf.wallet.createNewWallet(); + } else { + wallet = getWallet(datadir, account, password); + } if (subOptionsLength < this.parameters.length) { for (const prompt of this.parameters.slice(subOptionsLength)) { switch (prompt.name) { diff --git a/src/utils/constants.js b/src/utils/constants.js index db824dc..8e71d9a 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -389,6 +389,21 @@ Object.entries(commonGlobalOptionValidatorDesc).forEach((/** @type {[CommonGloba }; }); +const callGlobalOptionValidatorDesc = /**@type {CommonGlobalOptionValidatorDesc}*/ ({}); +// @ts-ignore +Object.entries(commonGlobalOptionValidatorDesc).forEach((/** @type {[CommonGlobalOptionKey, any]} */ [key, value]) => { + if (key === 'account' || key === 'password') { + strictGlobalOptionValidatorDesc[key] = { + ...value, + required: false + }; + } else { + strictGlobalOptionValidatorDesc[key] = { + ...value + }; + } +}); + /** * Array of global option prompts. * @type {GlobalOptionPrompt[]} @@ -449,6 +464,7 @@ export { callCommandParameters, commonGlobalOptionValidatorDesc, strictGlobalOptionValidatorDesc, + callGlobalOptionValidatorDesc, blkInfoCommandParameters, blkInfoCommandUsage, txResultCommandParameters, diff --git a/test/command/call.test.js b/test/command/call.test.js index a5fc09c..ad0f818 100644 --- a/test/command/call.test.js +++ b/test/command/call.test.js @@ -4,7 +4,6 @@ import AElf from 'aelf-sdk'; import inquirer from 'inquirer'; import { CallCommand } from '../../src/command'; import { callCommandUsages, callCommandParameters } from '../../src/utils/constants.js'; -import { getContractInstance } from '../../src/utils/utils.js'; import { userHomeDir } from '../../src/utils/userHomeDir.js'; import { logger } from '../../src/utils/myLogger.js'; import { endpoint as endPoint, account, password, dataDir, csvDir, jsonDir } from '../constants.js'; @@ -14,7 +13,7 @@ jest.mock('../../src/utils/myLogger'); describe('CallCommand', () => { let callCommand; - let mockOraInstance; + let backup, mockOraInstance; const aelf = new AElf(new AElf.providers.HttpProvider(endPoint)); const wallet = AElf.wallet.getWalletByPrivateKey('943df6d39fd1e1cc6ae9813e54f7b9988cf952814f9c31e37744b52594cb4096'); const address = 'ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx'; @@ -72,6 +71,26 @@ describe('CallCommand', () => { ); expect(logger.info).toHaveBeenCalled(); }); + test('should run without account', async () => { + const commander = new Command(); + commander.option('-e, --endpoint ', 'The URI of an AElf node. Eg: http://127.0.0.1:8000'); + commander.option('-a, --account ', 'The address of AElf wallet'); + commander.option('-p, --password ', 'The password of encrypted keyStore'); + commander.option( + '-d, --datadir ', + `The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf` + ); + commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-d', dataDir]); + await callCommand.run( + commander, + 'AElf.ContractNames.Token', + 'GetTokenInfo', + JSON.stringify({ + symbol: 'ELF' + }) + ); + expect(logger.info).toHaveBeenCalled(); + }); test('should run without contractAddress', async () => { inquirer.prompt = questions => Promise.resolve(''); const commander = new Command();