Skip to content

Commit 914eaa2

Browse files
authored
Merge pull request #93 from AElfProject/feature/callMethod
feat: feature/callMethod
2 parents 7bb708a + b0fe203 commit 914eaa2

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

package.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aelf-command",
3-
"version": "0.1.50-beta.0",
3+
"version": "0.1.51-beta.0",
44
"description": "A CLI tools for AElf",
55
"main": "src/index.js",
66
"type": "module",
@@ -114,14 +114,5 @@
114114
"commitizen": {
115115
"path": "./node_modules/cz-conventional-changelog"
116116
}
117-
},
118-
"jest": {
119-
"collectCoverageFrom": [
120-
"src/**/*.js"
121-
],
122-
"transform": {
123-
"^.+\\.js$": "babel-jest"
124-
},
125-
"testEnvironment": "node"
126117
}
127118
}

src/command/call.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import AElf from 'aelf-sdk';
22
import inquirer from 'inquirer';
33
import chalk from 'chalk';
44
import BaseSubCommand from './baseSubCommand.js';
5-
import { callCommandUsages, callCommandParameters } from '../utils/constants.js';
5+
import { callCommandUsages, callCommandParameters, callGlobalOptionValidatorDesc } from '../utils/constants.js';
66
import {
77
getContractMethods,
88
getContractInstance,
@@ -37,9 +37,10 @@ class CallCommand extends BaseSubCommand {
3737
description = 'Call a read-only method on a contract.',
3838
parameters = callCommandParameters,
3939
usage = callCommandUsages,
40-
options = []
40+
options = [],
41+
validatorDesc = callGlobalOptionValidatorDesc
4142
) {
42-
super(name, parameters, description, options, usage, rc);
43+
super(name, parameters, description, options, usage, rc, validatorDesc);
4344
}
4445
/**
4546
* Calls a method with specified parameters.
@@ -94,7 +95,13 @@ class CallCommand extends BaseSubCommand {
9495
const aelf = new AElf(new AElf.providers.HttpProvider(endpoint));
9596
try {
9697
let { contractAddress, method, params } = subOptions;
97-
const wallet = getWallet(datadir, account, password);
98+
let wallet;
99+
if (!account || !password) {
100+
// no need to provide account and password
101+
wallet = AElf.wallet.createNewWallet();
102+
} else {
103+
wallet = getWallet(datadir, account, password);
104+
}
98105
if (subOptionsLength < this.parameters.length) {
99106
for (const prompt of this.parameters.slice(subOptionsLength)) {
100107
switch (prompt.name) {

src/utils/constants.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,21 @@ Object.entries(commonGlobalOptionValidatorDesc).forEach((/** @type {[CommonGloba
389389
};
390390
});
391391

392+
const callGlobalOptionValidatorDesc = /**@type {CommonGlobalOptionValidatorDesc}*/ ({});
393+
// @ts-ignore
394+
Object.entries(commonGlobalOptionValidatorDesc).forEach((/** @type {[CommonGlobalOptionKey, any]} */ [key, value]) => {
395+
if (key === 'account' || key === 'password') {
396+
strictGlobalOptionValidatorDesc[key] = {
397+
...value,
398+
required: false
399+
};
400+
} else {
401+
strictGlobalOptionValidatorDesc[key] = {
402+
...value
403+
};
404+
}
405+
});
406+
392407
/**
393408
* Array of global option prompts.
394409
* @type {GlobalOptionPrompt[]}
@@ -449,6 +464,7 @@ export {
449464
callCommandParameters,
450465
commonGlobalOptionValidatorDesc,
451466
strictGlobalOptionValidatorDesc,
467+
callGlobalOptionValidatorDesc,
452468
blkInfoCommandParameters,
453469
blkInfoCommandUsage,
454470
txResultCommandParameters,

test/command/call.test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import AElf from 'aelf-sdk';
44
import inquirer from 'inquirer';
55
import { CallCommand } from '../../src/command';
66
import { callCommandUsages, callCommandParameters } from '../../src/utils/constants.js';
7-
import { getContractInstance } from '../../src/utils/utils.js';
87
import { userHomeDir } from '../../src/utils/userHomeDir.js';
98
import { logger } from '../../src/utils/myLogger.js';
109
import { endpoint as endPoint, account, password, dataDir, csvDir, jsonDir } from '../constants.js';
@@ -14,7 +13,7 @@ jest.mock('../../src/utils/myLogger');
1413

1514
describe('CallCommand', () => {
1615
let callCommand;
17-
let mockOraInstance;
16+
let backup, mockOraInstance;
1817
const aelf = new AElf(new AElf.providers.HttpProvider(endPoint));
1918
const wallet = AElf.wallet.getWalletByPrivateKey('943df6d39fd1e1cc6ae9813e54f7b9988cf952814f9c31e37744b52594cb4096');
2019
const address = 'ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx';
@@ -72,6 +71,26 @@ describe('CallCommand', () => {
7271
);
7372
expect(logger.info).toHaveBeenCalled();
7473
});
74+
test('should run without account', async () => {
75+
const commander = new Command();
76+
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
77+
commander.option('-a, --account <account>', 'The address of AElf wallet');
78+
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
79+
commander.option(
80+
'-d, --datadir <directory>',
81+
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
82+
);
83+
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-d', dataDir]);
84+
await callCommand.run(
85+
commander,
86+
'AElf.ContractNames.Token',
87+
'GetTokenInfo',
88+
JSON.stringify({
89+
symbol: 'ELF'
90+
})
91+
);
92+
expect(logger.info).toHaveBeenCalled();
93+
});
7594
test('should run without contractAddress', async () => {
7695
inquirer.prompt = questions => Promise.resolve('');
7796
const commander = new Command();

0 commit comments

Comments
 (0)