Skip to content

Commit 9b7faf8

Browse files
AbigailDengAbigailDeng
authored andcommitted
feat: pass params through json
1 parent 52ca774 commit 9b7faf8

File tree

10 files changed

+106
-39
lines changed

10 files changed

+106
-39
lines changed

README.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _A CLI tools built for AElf_
2121

2222
## Features
2323

24-
- Get or Set common configs, `endpoint`, `account`, `datadir`, `password`, `csv`.
24+
- Get or Set common configs, `endpoint`, `account`, `datadir`, `password`, `csv`, `json`.
2525
- For new users who are not familiar with the CLI parameters, any missing parameters will be asked in a prompting way.
2626
- Create a new `account`.
2727
- Load an account from a given `private key` or `mnemonic`.
@@ -130,7 +130,7 @@ Welcome to aelf interactive console. Ctrl + C to terminate the program. Double t
130130
║ NAME | DESCRIPTION ║
131131
║ AElf | imported from aelf-sdk ║
132132
║ aelf | the instance of an aelf-sdk, connect to ║
133-
| http://13.231.179.27:8000
133+
| https://tdvw-test-node.aelf.io
134134
║ _account | the instance of an AElf wallet, address ║
135135
| is ║
136136
| 2Ue31YTuB5Szy7cnr3SCEGU2gtGi5uMQBYarYUR… ║
@@ -155,6 +155,7 @@ Options:
155155
-p, --password <password> The password of encrypted keyStore
156156
-d, --datadir <directory> The directory that contains the AElf related files. Defaults to {home}/.local/share/aelf
157157
-c, --csv <csv> The location of the CSV file containing the parameters.
158+
-j, --json <json> The location of the JSON file containing the parameters.
158159
-h, --help output usage information
159160

160161
Commands:
@@ -216,7 +217,8 @@ aelf-command console
216217
- `endpoint`: The endpoint for the RPC service.
217218
- `account`: The account to be used to interact with the blockchain `endpoint`.
218219
- `password`: The password for unlocking the given `account`.
219-
- `csv>`: The location of the CSV file containing the parameters.
220+
- `csv`: The location of the CSV file containing the parameters.
221+
- `json`: The location of the JSON file containing the parameters.
220222
221223
You can specified options above in several ways, and the priority is in the order of low to high.
222224
@@ -670,7 +672,7 @@ In each item:
670672
671673
```bash
672674
$ aelf-command send
673-
✔ Enter the the URI of an AElf node … http://13.231.179.27:8000
675+
✔ Enter the the URI of an AElf node … https://tdvw-test-node.aelf.io
674676
✔ Enter a valid wallet address, if you don't have, create one by aelf-command create … D3vSjRYL8MpeRpvUDy85ktXijnBe2tHn8NTACsggUVteQCNGP
675677
✔ Enter the password you typed when creating a wallet … ********
676678
✔ Enter contract name (System contracts only) or the address of contract … AElf.ContractNames.Token
@@ -708,7 +710,7 @@ aelf-command send AElf.ContractNames.Token Transfer '{"symbol": "ELF", "to": "C9
708710
709711
```bash
710712
$ aelf-command call
711-
✔ Enter the the URI of an AElf node … http://13.231.179.27:8000
713+
✔ Enter the the URI of an AElf node … https://tdvw-test-node.aelf.io
712714
✔ Enter a valid wallet address, if you don't have, create one by aelf-command create … D3vSjRYL8MpeRpvUDy85ktXijnBe2tHn8NTACsggUVteQCNGP
713715
✔ Enter the password you typed when creating a wallet … ********
714716
✔ Enter contract name (System contracts only) or the address of contract … AElf.ContractNames.Token
@@ -744,6 +746,38 @@ Result:
744746
aelf-command call AElf.ContractNames.Token GetTokenInfo '{"symbol":"ELF"}'
745747
```
746748
749+
#### Call a read-only method and pass params through CSV
750+
751+
```bash
752+
$ aelf-command call -e https://tdvw-test-node.aelf.io/ -a GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk -p 1234*Qwer -j ./test.csv AElf.ContractNames.Token GetBalance
753+
✔ Fetching contract successfully!
754+
✔ Calling method successfully!
755+
AElf [Info]:
756+
Result:
757+
{
758+
"symbol": "ELF",
759+
"owner": "GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk",
760+
"balance": "155000"
761+
}
762+
✔ Succeed!
763+
```
764+
765+
#### Call a read-only method and pass params through JSON
766+
767+
```bash
768+
$ aelf-command call -e https://tdvw-test-node.aelf.io/ -a GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk -p 1234*Qwer -j ./test.json AElf.ContractNames.Token GetBalance
769+
✔ Fetching contract successfully!
770+
✔ Calling method successfully!
771+
AElf [Info]:
772+
Result:
773+
{
774+
"symbol": "ELF",
775+
"owner": "GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk",
776+
"balance": "155000"
777+
}
778+
✔ Succeed!
779+
```
780+
747781
### get-chain-status - Get the current status of the block chain
748782
749783
```bash
@@ -770,7 +804,7 @@ $ aelf-command get-chain-status
770804
771805
```bash
772806
$ aelf-command get-tx-result
773-
✔ Enter the the URI of an AElf node … http://13.231.179.27:8000
807+
✔ Enter the the URI of an AElf node … https://tdvw-test-node.aelf.io
774808
✔ Enter a valid transaction id in hex format … 7b620a49ee9666c0c381fdb33f94bd31e1b5eb0fdffa081463c3954e9f734a02
775809
✔ Succeed!
776810
{ TransactionId:
@@ -800,7 +834,7 @@ $ aelf-command get-tx-result
800834
801835
```bash
802836
$ aelf-command get-blk-height
803-
✔ Enter the the URI of an AElf node … http://13.231.179.27:8000
837+
✔ Enter the the URI of an AElf node … https://tdvw-test-node.aelf.io
804838
> 7902091
805839
```
806840
@@ -810,7 +844,7 @@ You can pass a block height or a block hash to this sub-command.
810844
811845
```bash
812846
$ aelf-command get-blk-info
813-
✔ Enter the the URI of an AElf node: http://13.231.179.27:8000
847+
✔ Enter the the URI of an AElf node: https://tdvw-test-node.aelf.io
814848
✔ Enter a valid height or block hash: 123
815849
✔ Include transactions whether or not: no / yes
816850
{ BlockHash:
@@ -845,7 +879,7 @@ aelf-command get-blk-info ca61c7c8f5fc1bc8af0536bc9b51c61a94f39641a93a748e72802b
845879
846880
```bash
847881
$ aelf-command console
848-
✔ Enter the the URI of an AElf node … http://13.231.179.27:8000
882+
✔ Enter the the URI of an AElf node … https://tdvw-test-node.aelf.io
849883
✔ Enter a valid wallet address, if you don't have, create one by aelf-command create … 2Ue31YTuB5Szy7cnr3SCEGU2gtGi5uMQBYarYUR5oGin1sys6H
850884
✔ Enter the password you typed when creating a wallet … ********
851885
✔ Succeed!
@@ -856,7 +890,7 @@ Welcome to aelf interactive console. Ctrl + C to terminate the program. Double t
856890
║ NAME | DESCRIPTION ║
857891
║ AElf | imported from aelf-sdk ║
858892
║ aelf | instance of aelf-sdk, connect to ║
859-
| http://13.231.179.27:8000
893+
| https://tdvw-test-node.aelf.io
860894
║ _account | instance of AElf wallet, wallet address ║
861895
| is ║
862896
| 2Ue31YTuB5Szy7cnr3SCEGU2gtGi5uMQBYarYUR… ║

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aelf-command",
3-
"version": "0.1.49-beta.0",
3+
"version": "0.1.50-beta.0",
44
"description": "A CLI tools for AElf",
55
"main": "src/index.js",
66
"type": "module",

src/command/call.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import AElf from 'aelf-sdk';
22
import inquirer from 'inquirer';
33
import chalk from 'chalk';
4-
import { createReadStream } from 'fs';
5-
import csv from 'csv-parser';
64
import BaseSubCommand from './baseSubCommand.js';
75
import { callCommandUsages, callCommandParameters } from '../utils/constants.js';
86
import {
@@ -12,7 +10,8 @@ import {
1210
promptTolerateSeveralTimes,
1311
getParams,
1412
parseJSON,
15-
parseCSV
13+
parseCSV,
14+
parseJSONFile
1615
} from '../utils/utils.js';
1716
import { getWallet } from '../utils/wallet.js';
1817
import { logger } from '../utils/myLogger.js';
@@ -91,7 +90,7 @@ class CallCommand extends BaseSubCommand {
9190
// @ts-ignore
9291
const { options, subOptions } = await super.run(commander, ...args);
9392
const subOptionsLength = Object.keys(subOptions).length;
94-
const { endpoint, datadir, account, password, csv } = options;
93+
const { endpoint, datadir, account, password, csv, json } = options;
9594
const aelf = new AElf(new AElf.providers.HttpProvider(endpoint));
9695
try {
9796
let { contractAddress, method, params } = subOptions;
@@ -127,8 +126,9 @@ class CallCommand extends BaseSubCommand {
127126
contractAddress = await getContractInstance(contractAddress, aelf, wallet, this.oraInstance);
128127
method = getMethod(method, contractAddress);
129128
if (csv) {
130-
const csvParams = await parseCSV(csv);
131-
params = csvParams;
129+
params = await parseCSV(csv);
130+
} else if (json) {
131+
params = await parseJSONFile(json);
132132
} else {
133133
params = await getParams(method);
134134
params = typeof params === 'string' ? params : BaseSubCommand.normalizeConfig(params);

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function init(options) {
5656
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
5757
);
5858
commander.option('-c, --csv <csv>', 'The location of the CSV file containing the parameters.');
59+
commander.option('-j, --json <json>', 'The location of the JSON file containing the parameters.');
5960
const rc = new RC();
6061
Object.values(commands).forEach(Value => {
6162
const command = new Value(rc);

src/utils/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ const commonGlobalOptionValidatorDesc = {
371371
type: 'string',
372372
required: false,
373373
message: 'set params in csv file by -c <csv>'
374+
},
375+
json: {
376+
type: 'string',
377+
required: false,
378+
message: 'set params in csv file by -j <json>'
374379
}
375380
};
376381

src/utils/utils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,17 @@ const parseCSV = async address => {
481481
return results;
482482
};
483483

484+
const parseJSONFile = async address => {
485+
try {
486+
const absolutePath = path.resolve(address);
487+
const data = await fs.readFileSync(absolutePath);
488+
const jsonObject = JSON.parse(data.toString('utf8'));
489+
return jsonObject;
490+
} catch (error) {
491+
throw new Error(`An error occurred while reading or parsing the JSON file: ${error.message}`);
492+
}
493+
};
494+
484495
export {
485496
promisify,
486497
camelCase,
@@ -494,5 +505,6 @@ export {
494505
randomId,
495506
getParams,
496507
deserializeLogs,
497-
parseCSV
508+
parseCSV,
509+
parseJSONFile
498510
};

test/command/call.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { callCommandUsages, callCommandParameters } from '../../src/utils/consta
77
import { getContractInstance } from '../../src/utils/utils.js';
88
import { userHomeDir } from '../../src/utils/userHomeDir.js';
99
import { logger } from '../../src/utils/myLogger.js';
10-
import { endpoint as endPoint, account, password, dataDir, csvDir } from '../constants.js';
10+
import { endpoint as endPoint, account, password, dataDir, csvDir, jsonDir } from '../constants.js';
1111

1212
const sampleRc = { getConfigs: jest.fn() };
1313
jest.mock('../../src/utils/myLogger');
@@ -122,6 +122,26 @@ describe('CallCommand', () => {
122122
expect(logger.info).toHaveBeenCalled();
123123
});
124124

125+
test('should run with json', async () => {
126+
inquirer.prompt = questions =>
127+
Promise.resolve({
128+
symbol: 'ELF',
129+
owner: 'GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk'
130+
});
131+
const commander = new Command();
132+
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
133+
commander.option('-a, --account <account>', 'The address of AElf wallet');
134+
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
135+
commander.option(
136+
'-d, --datadir <directory>',
137+
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
138+
);
139+
commander.option('-j, --json <json>', 'The location of the JSON file containing the parameters.');
140+
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir, '-j', jsonDir]);
141+
await callCommand.run(commander, 'AElf.ContractNames.Token', 'GetBalance');
142+
expect(logger.info).toHaveBeenCalled();
143+
});
144+
125145
test('should run with invalid parameters', async () => {
126146
inquirer.prompt = backup;
127147
callCommand = new CallCommand(sampleRc, 'call', 'Call a read-only method on a contract.', [

test/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export const account = 'GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk';
55
export const password = '1234*Qwer';
66
export const dataDir = path.resolve(__dirname, './dataDir/aelf');
77
export const csvDir = path.resolve(__dirname, './test.csv');
8+
export const jsonDir = path.resolve(__dirname, './test.json');

test/test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"symbol": "ELF",
3+
"owner": "GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk"
4+
}

types/utils/constants.d.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,32 @@ export interface CallCommandParameter {
1111
}
1212
export const callCommandParameters: CallCommandParameter[];
1313

14-
export interface PasswordValidatorDesc {
14+
interface BaseValidatorDesc {
1515
type: string;
1616
required: boolean;
1717
message: string;
18+
}
19+
export interface PasswordValidatorDesc extends BaseValidatorDesc {
1820
validator(rule: any, value: any): boolean;
1921
}
2022

21-
export interface EndpointValidatorDesc {
22-
type: string;
23-
required: boolean;
23+
export interface EndpointValidatorDesc extends BaseValidatorDesc {
2424
pattern: RegExp;
25-
message: string;
2625
}
2726

28-
export interface DatadirValidatorDesc {
29-
type: string;
30-
required: boolean;
31-
message: string;
32-
}
27+
export interface DatadirValidatorDesc extends BaseValidatorDesc {}
3328

34-
export interface AccountValidatorDesc {
35-
type: string;
36-
required: boolean;
37-
message: string;
38-
}
39-
export interface CSVValidatorDesc {
40-
type: string;
41-
required: boolean;
42-
message: string;
43-
}
29+
export interface AccountValidatorDesc extends BaseValidatorDesc {}
30+
export interface CSVValidatorDesc extends BaseValidatorDesc {}
31+
32+
export interface JSONValidatorDesc extends BaseValidatorDesc {}
4433
export interface CommonGlobalOptionValidatorDesc {
4534
password: PasswordValidatorDesc;
4635
endpoint: EndpointValidatorDesc;
4736
datadir: DatadirValidatorDesc;
4837
account: AccountValidatorDesc;
4938
csv: CSVValidatorDesc;
39+
json: JSONValidatorDesc;
5040
}
5141
export const commonGlobalOptionValidatorDesc: CommonGlobalOptionValidatorDesc;
5242

0 commit comments

Comments
 (0)