Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 23x 7x 7x 7x 7x 2x 2x 2x 6x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 1x 7x 1x 1x 6x 7x 5x 7x 1x 1x 1x 1x 1x 4x 4x 4x 1x 3x 1x 2x 1x 1x 1x 3x 1x 6x 6x 1x 5x 6x 6x 6x 1x 1x 5x 2x 2x | import AElf from 'aelf-sdk'; import inquirer from 'inquirer'; import chalk from 'chalk'; import BaseSubCommand from './baseSubCommand.js'; import { callCommandUsages, callCommandParameters, commonGlobalOptionValidatorDesc } from '../utils/constants.js'; import { getContractMethods, getContractInstance, getMethod, promptTolerateSeveralTimes, getParams, parseJSON, parseCSV, parseJSONFile } from '../utils/utils.js'; import { getWallet } from '../utils/wallet.js'; import { logger } from '../utils/myLogger.js'; /** * @typedef {import('commander').Command} Command * @typedef {import('ora').Options} OraOptions * @typedef {import('../../types/rc/index.js').default} Registry */ class CallCommand extends BaseSubCommand { /** * Creates an instance of CallCommand. * @param {Registry} rc The instance of the Registry. * @param {string} [name] The name of the command. * @param {string} [description] The description of the command. * @param {Object[]} [parameters] The parameters for the command. * @param {string[]} [usage] The usage examples for the command. * @param {any[]} [options] The options for the command. */ constructor( rc, name = 'call', description = 'Call a read-only method on a contract.', parameters = callCommandParameters, usage = callCommandUsages, options = [], validatorDesc = commonGlobalOptionValidatorDesc ) { super(name, parameters, description, options, usage, rc, validatorDesc); } /** * Calls a method with specified parameters. * @param {any} method The method to call. * @param {any} params The parameters for the method call. * @returns {Promise<any>} A promise that resolves with the result of the method call. */ async callMethod(method, params) { this.oraInstance.start('Calling method...'); const result = await method.call(params); this.oraInstance.succeed('Calling method successfully!'); return result; } /** * Processes address after prompting for input. * @param {any} aelf The AElf instance. * @param {any} wallet The wallet instance. * @param {Object.<string, any>} answerInput The input parameters. * @returns {Promise<any>} A promise that resolves with the processed result. */ async processAddressAfterPrompt(aelf, wallet, answerInput) { let { contractAddress } = BaseSubCommand.normalizeConfig(answerInput); contractAddress = await getContractInstance(contractAddress, aelf, wallet, this.oraInstance); return contractAddress; } /** * Calls a method with specified parameters. * @param {any} method The method to call. * @param {any} params The parameters for the method call. * @returns {Promise<any>} A promise that resolves with the result of the method call. */ async showRes(method, params) { const result = await this.callMethod(method, params); // @ts-ignore logger.info(`\nResult:\n${JSON.stringify(result, null, 2)}`); this.oraInstance.succeed('Succeed!'); } /** * Runs the command. * @param {Command} commander The Commander instance. * @param {...any[]} args Additional arguments passed to the command. * @returns {Promise<void>} A promise that resolves when the command execution completes. */ async run(commander, ...args) { this.setCustomPrompts(true); // @ts-ignore const { options, subOptions } = await super.run(commander, ...args); const subOptionsLength = Object.keys(subOptions).length; const { endpoint, datadir, account, password, csv, json } = options; const aelf = new AElf(new AElf.providers.HttpProvider(endpoint)); try { let { contractAddress, method, params } = subOptions; let wallet; if (!account) { // No account and password provided, create a new wallet wallet = AElf.wallet.createNewWallet(); } else if (account && !password) { // Account provided but no password, prompt user for password const passwordPrompt = await inquirer.prompt({ type: 'password', name: 'password', message: 'Please enter your password:', mask: '*' }); wallet = getWallet(datadir, account, passwordPrompt.password); } else { wallet = getWallet(datadir, account, password); } if (subOptionsLength < this.parameters.length) { for (const prompt of this.parameters.slice(subOptionsLength)) { switch (prompt.name) { case 'contract-address': contractAddress = await promptTolerateSeveralTimes( { times: 3, prompt, processAfterPrompt: this.processAddressAfterPrompt.bind(this, aelf, wallet), pattern: /^((?!null).)*$/ }, this.oraInstance ); break; case 'method': contractAddress = await getContractInstance(contractAddress, aelf, wallet, this.oraInstance); method = getMethod( ( await inquirer.prompt({ ...prompt, choices: getContractMethods(contractAddress) }) ).method, contractAddress ); break; case 'params': contractAddress = await getContractInstance(contractAddress, aelf, wallet, this.oraInstance); method = getMethod(method, contractAddress); if (csv) { params = await parseCSV(csv); } else if (json) { params = await parseJSONFile(json); } else { params = await getParams(method); params = typeof params === 'string' ? params : BaseSubCommand.normalizeConfig(params); Eif (Object.keys(params || {}).length > 0) { console.log(chalk.hex('#3753d3')(`The params you entered is:\n${JSON.stringify(params, null, 2)}`)); } } break; default: break; } } } contractAddress = await getContractInstance(contractAddress, aelf, wallet, this.oraInstance); if (Array.isArray(params)) { params.forEach(param => parseJSON(param)); } else { params = parseJSON(params); } method = getMethod(method, contractAddress); Iif (method.inputTypeInfo && (Object.keys(method.inputTypeInfo.fields).length === 0 || !method.inputTypeInfo.fields)) { params = ''; } if (Array.isArray(params)) { for (const param of params) { await this.showRes(method, param); } } else { await this.showRes(method, params); } } catch (e) { this.oraInstance.fail('Failed!'); // @ts-ignore logger.fatal(e); } } } export default CallCommand; |