11import AElf from 'aelf-sdk' ;
22import inquirer from 'inquirer' ;
33import chalk from 'chalk' ;
4+ import { createReadStream } from 'fs' ;
5+ import csv from 'csv-parser' ;
46import BaseSubCommand from './baseSubCommand.js' ;
57import { callCommandUsages , callCommandParameters } from '../utils/constants.js' ;
68import {
@@ -9,7 +11,8 @@ import {
911 getMethod ,
1012 promptTolerateSeveralTimes ,
1113 getParams ,
12- parseJSON
14+ parseJSON ,
15+ parseCSV
1316} from '../utils/utils.js' ;
1417import { getWallet } from '../utils/wallet.js' ;
1518import { logger } from '../utils/myLogger.js' ;
@@ -64,6 +67,19 @@ class CallCommand extends BaseSubCommand {
6467 return contractAddress ;
6568 }
6669
70+ /**
71+ * Calls a method with specified parameters.
72+ * @param {any } method The method to call.
73+ * @param {any } params The parameters for the method call.
74+ * @returns {Promise<any> } A promise that resolves with the result of the method call.
75+ */
76+ async showRes ( method , params ) {
77+ const result = await this . callMethod ( method , params ) ;
78+ // @ts -ignore
79+ logger . info ( `\nResult:\n${ JSON . stringify ( result , null , 2 ) } ` ) ;
80+ this . oraInstance . succeed ( 'Succeed!' ) ;
81+ }
82+
6783 /**
6884 * Runs the command.
6985 * @param {Command } commander The Commander instance.
@@ -75,7 +91,7 @@ class CallCommand extends BaseSubCommand {
7591 // @ts -ignore
7692 const { options, subOptions } = await super . run ( commander , ...args ) ;
7793 const subOptionsLength = Object . keys ( subOptions ) . length ;
78- const { endpoint, datadir, account, password } = options ;
94+ const { endpoint, datadir, account, password, csv } = options ;
7995 const aelf = new AElf ( new AElf . providers . HttpProvider ( endpoint ) ) ;
8096 try {
8197 let { contractAddress, method, params } = subOptions ;
@@ -109,13 +125,16 @@ class CallCommand extends BaseSubCommand {
109125 break ;
110126 case 'params' :
111127 contractAddress = await getContractInstance ( contractAddress , aelf , wallet , this . oraInstance ) ;
112-
113128 method = getMethod ( method , contractAddress ) ;
114-
115- params = await getParams ( method ) ;
116- params = typeof params === 'string' ? params : BaseSubCommand . normalizeConfig ( params ) ;
117- if ( Object . keys ( params || { } ) . length > 0 ) {
118- console . log ( chalk . hex ( '#3753d3' ) ( `The params you entered is:\n${ JSON . stringify ( params , null , 2 ) } ` ) ) ;
129+ if ( csv ) {
130+ const csvParams = await parseCSV ( csv ) ;
131+ params = csvParams ;
132+ } else {
133+ params = await getParams ( method ) ;
134+ params = typeof params === 'string' ? params : BaseSubCommand . normalizeConfig ( params ) ;
135+ if ( Object . keys ( params || { } ) . length > 0 ) {
136+ console . log ( chalk . hex ( '#3753d3' ) ( `The params you entered is:\n${ JSON . stringify ( params , null , 2 ) } ` ) ) ;
137+ }
119138 }
120139 break ;
121140 default :
@@ -124,15 +143,23 @@ class CallCommand extends BaseSubCommand {
124143 }
125144 }
126145 contractAddress = await getContractInstance ( contractAddress , aelf , wallet , this . oraInstance ) ;
127- params = parseJSON ( params ) ;
146+ if ( Array . isArray ( params ) ) {
147+ params . forEach ( param => parseJSON ( param ) ) ;
148+ } else {
149+ params = parseJSON ( params ) ;
150+ }
151+
128152 method = getMethod ( method , contractAddress ) ;
129153 if ( method . inputTypeInfo && ( Object . keys ( method . inputTypeInfo . fields ) . length === 0 || ! method . inputTypeInfo . fields ) ) {
130154 params = '' ;
131155 }
132- const result = await this . callMethod ( method , params ) ;
133- // @ts -ignore
134- logger . info ( `\nResult:\n${ JSON . stringify ( result , null , 2 ) } ` ) ;
135- this . oraInstance . succeed ( 'Succeed!' ) ;
156+ if ( Array . isArray ( params ) ) {
157+ for ( const param of params ) {
158+ await this . showRes ( method , param ) ;
159+ }
160+ } else {
161+ await this . showRes ( method , params ) ;
162+ }
136163 } catch ( e ) {
137164 this . oraInstance . fail ( 'Failed!' ) ;
138165 // @ts -ignore
0 commit comments