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

feat: feature/callMethod #93

Merged
merged 1 commit into from
Oct 15, 2024
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
11 changes: 1 addition & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -114,14 +114,5 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"transform": {
"^.+\\.js$": "babel-jest"
},
"testEnvironment": "node"
}
}
15 changes: 11 additions & 4 deletions src/command/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
Expand Down Expand Up @@ -449,6 +464,7 @@ export {
callCommandParameters,
commonGlobalOptionValidatorDesc,
strictGlobalOptionValidatorDesc,
callGlobalOptionValidatorDesc,
blkInfoCommandParameters,
blkInfoCommandUsage,
txResultCommandParameters,
Expand Down
23 changes: 21 additions & 2 deletions test/command/call.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -72,6 +71,26 @@ describe('CallCommand', () => {
);
expect(logger.info).toHaveBeenCalled();
});
test('should run without account', async () => {
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`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();
Expand Down
Loading