async function call(args, apis, options) {
  if (args.length < 2) {
    console.log(
      'invalid number of args: should be at least 2(contract name, method name), got {0}'.format(
        args.length
      )
    );
    return;
  }

  const contractKey = args[0];
  const methodName = args[1];

  if (apis[contractKey] === undefined) {
    console.log('no {0} contract is exist'.format(args[0]));
    return;
  }

  const params = args.slice(2, args.length).map(param => {
    // convert array string to array
    // "[0x123, 0x234]" to ["0x123", "0x234"]
    if (param.startsWith('[') && param.endsWith(']')) {
      return param
        .slice(1, param.length - 1)
        .split(/\s*,\s*/)
        .map(v => {
          // remove empty space for each element
          return v.trim();
        });
    }
    return param;
  });

  params.push({
    gasLimit: options.config.gasLimit,
    gasPrice: options.config.gasPrice,
    platform: options.config.platform
  });

  const receiptFilter = [
    'transactionHash',
    'transactionIndex',
    'blockNumber',
    'from',
    'to',
    'gasUsed',
    'logs',
    'name',
    'args'
  ];
  const contract = new apis[contractKey]['api'](
    apis[contractKey]['address'].toLowerCase()
  );
  try {
    if (!contract.methods[methodName]) {
      throw new Error(`There is no function name of ${methodName}`);
    }
    const receipt = await contract.methods[methodName].apply(undefined, params);
    if (typeof receipt !== 'object') {
      // result of constant functions
      console.log(JSON.stringify(receipt, undefined, 2));
    } else if (Array.isArray(receipt.logs)) {
      // receipt is Receipt of transaction
      const events = parseLogs(receipt.logs, apis[contractKey].api['abi']);
      const logs = [];
      for (const key in events) {
        logs.push({
          transactionHash: events[key].transactionHash,
          name: events[key].name,
          args: events[key].args
        });
      }

      receipt.logs = undefined;
      const result = JSON.parse(JSON.stringify(receipt, receiptFilter));
      result.logs = logs;
      console.log(JSON.stringify(result, undefined, 2));
    } else {
      // multiple constant return values
      const result = JSON.parse(JSON.stringify(receipt, null));
      console.log(JSON.stringify(result, undefined, 2));
    }
  } catch (e) {
    console.log(e);
  }
}
/**
 *
 * Call api of a smart contract and print a result.
 * @param {object} args is a list of command args
 * @param {object} apis has an api of smart contracts
 * @param {object} options is a global option
 */
Call api of a smart contract and print a result.
@param {object} args is a list of command args
@param {object} apis has an api of smart contracts
@param {object} options is a global option
Call api of a smart contract and print a result . @param { object } args is a list of command args @param { object } apis has an api of smart contracts @param { object } options is a global option
async function call(args, apis, options) {
  if (args.length < 2) {
    console.log(
      'invalid number of args: should be at least 2(contract name, method name), got {0}'.format(
        args.length
      )
    );
    return;
  }
  const contractKey = args[0];
  const methodName = args[1];
  if (apis[contractKey] === undefined) {
    console.log('no {0} contract is exist'.format(args[0]));
    return;
  }
  const params = args.slice(2, args.length).map(param => {
    if (param.startsWith('[') && param.endsWith(']')) {
      return param
        .slice(1, param.length - 1)
        .split(/\s*,\s*/)
        .map(v => {
          return v.trim();
        });
    }
    return param;
  });
  params.push({
    gasLimit: options.config.gasLimit,
    gasPrice: options.config.gasPrice,
    platform: options.config.platform
  });
  const receiptFilter = [
    'transactionHash',
    'transactionIndex',
    'blockNumber',
    'from',
    'to',
    'gasUsed',
    'logs',
    'name',
    'args'
  ];
  const contract = new apis[contractKey]['api'](
    apis[contractKey]['address'].toLowerCase()
  );
  try {
    if (!contract.methods[methodName]) {
      throw new Error(`There is no function name of ${methodName}`);
    }
    const receipt = await contract.methods[methodName].apply(undefined, params);
    if (typeof receipt !== 'object') {
      console.log(JSON.stringify(receipt, undefined, 2));
    } else if (Array.isArray(receipt.logs)) {
      const events = parseLogs(receipt.logs, apis[contractKey].api['abi']);
      const logs = [];
      for (const key in events) {
        logs.push({
          transactionHash: events[key].transactionHash,
          name: events[key].name,
          args: events[key].args
        });
      }
      receipt.logs = undefined;
      const result = JSON.parse(JSON.stringify(receipt, receiptFilter));
      result.logs = logs;
      console.log(JSON.stringify(result, undefined, 2));
    } else {
      const result = JSON.parse(JSON.stringify(receipt, null));
      console.log(JSON.stringify(result, undefined, 2));
    }
  } catch (e) {
    console.log(e);
  }
}
async function call ( args , apis , options ) { if ( args . length < 2 ) { console . log ( 'invalid number of args: should be at least 2(contract name, method name), got {0}' . format ( args . length ) ) ; return ; } const contractKey = args [ 0 ] ; const methodName = args [ 1 ] ; if ( apis [ contractKey ] === undefined ) { console . log ( 'no {0} contract is exist' . format ( args [ 0 ] ) ) ; return ; } const params = args . slice ( 2 , args . length ) . map ( param => { if ( param . startsWith ( '[' ) && param . endsWith ( ']' ) ) { return param . slice ( 1 , param . length - 1 ) . split ( / \s*,\s* / ) . map ( v => { return v . trim ( ) ; } ) ; } return param ; } ) ; params . push ( { gasLimit : options . config . gasLimit , gasPrice : options . config . gasPrice , platform : options . config . platform } ) ; const receiptFilter = [ 'transactionHash' , 'transactionIndex' , 'blockNumber' , 'from' , 'to' , 'gasUsed' , 'logs' , 'name' , 'args' ] ; const contract = new apis [ contractKey ] [ 'api' ] ( apis [ contractKey ] [ 'address' ] . toLowerCase ( ) ) ; try { if ( ! contract . methods [ methodName ] ) { throw new Error ( ` ${ methodName } ` ) ; } const receipt = await contract . methods [ methodName ] . apply ( undefined , params ) ; if ( typeof receipt !== 'object' ) { console . log ( JSON . stringify ( receipt , undefined , 2 ) ) ; } else if ( Array . isArray ( receipt . logs ) ) { const events = parseLogs ( receipt . logs , apis [ contractKey ] . api [ 'abi' ] ) ; const logs = [ ] ; for ( const key in events ) { logs . push ( { transactionHash : events [ key ] . transactionHash , name : events [ key ] . name , args : events [ key ] . args } ) ; } receipt . logs = undefined ; const result = JSON . parse ( JSON . stringify ( receipt , receiptFilter ) ) ; result . logs = logs ; console . log ( JSON . stringify ( result , undefined , 2 ) ) ; } else { const result = JSON . parse ( JSON . stringify ( receipt , null ) ) ; console . log ( JSON . stringify ( result , undefined , 2 ) ) ; } } catch ( e ) { console . log ( e ) ; } }
Call api of a smart contract and print a result.
Call api of a smart contract and print a result .
// convert array string to array // "[0x123, 0x234]" to ["0x123", "0x234"] // remove empty space for each element // result of constant functions // receipt is Receipt of transaction // multiple constant return values