No se pudo llamar a la función de contrato con su firma

La documentación de Web3.js dice aquí que los métodos de un contrato inteligente están disponibles a través de:

  1. El nombre: myContract.methods.myMethod
  2. El nombre con parámetros: myContract.methods['myMethod(uint256)']
  3. La firma: myContract.methods['0x58cf5f10']

pero cuando llamo al método con la segunda y la tercera opción, no pude recuperar el valor, sino que muestra muchas firmas de objetos ... aquí está mi código

async function d () {

  var  myContAddr = '0x8175036782E9564C084eD1DA6C44C27150F24316';
  var myContractAbiDefenition = myAbi;
  var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);

  tokens = await myContractInstance.methods['0xc4e41b22'].call();
  //tokens = await myContractInstance.methods['getTotalSupply()'].call(); // its also give same output
  //  tokens = await myContractInstance.methods.getTotalSupply().call(); // this gives me required output "Tokens :  6"  
  console.log("Tokens : ",tokens);
}
d();

aquí está mi salida

Tokens :  { call:
   { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
  send:
   { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
  encodeABI: [Function: bound _encodeMethodABI],
  estimateGas: [Function: bound _executeMethod],
  arguments: [],
  _method:
   { constant: true,
     inputs: [],
     name: 'getTotalSupply',
     outputs: [ [Object] ],
     payable: false,
     stateMutability: 'view',
     type: 'function',
     signature: '0xc4e41b22' },
  _parent:
   Contract {
     currentProvider: [Getter/Setter],
     _requestManager:
      RequestManager {
        provider: [HttpProvider],
        providers: [Object],
        subscriptions: {} },
     givenProvider: null,
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider:
      HttpProvider {
        host: 'HTTP://127.0.0.1:7545',
        httpAgent: [Agent],
        withCredentials: false,
        timeout: 0,
        headers: undefined,
        connected: false },
     setProvider: [Function],
     BatchRequest: [Function: bound Batch],
     extend:
      { [Function: ex]
        formatters: [Object],
        utils: [Object],
        Method: [Function: Method] },
     clearSubscriptions: [Function],
     options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
     transactionBlockTimeout: 50,
     transactionConfirmationBlocks: 24,
     transactionPollingTimeout: 750,
     defaultChain: undefined,
     defaultHardfork: undefined,
     defaultCommon: undefined,
     defaultAccount: [Getter/Setter],
     defaultBlock: [Getter/Setter],
     methods:
      { balances: [Function: bound _createTxObject],
        '0x27e235e3': [Function: bound _createTxObject],
        'balances(address)': [Function: bound _createTxObject],
        buyToken: [Function: bound _createTxObject],
        '0xa4821719': [Function: bound _createTxObject],
        'buyToken()': [Function: bound _createTxObject],
        getTotalSupply: [Function: bound _createTxObject],
        '0xc4e41b22': [Function: bound _createTxObject],
        'getTotalSupply()': [Function: bound _createTxObject],
        owner: [Function: bound _createTxObject],
        '0x8da5cb5b': [Function: bound _createTxObject],
        'owner()': [Function: bound _createTxObject],
        setTotalSupply: [Function: bound _createTxObject],
        '0xf7ea7a3d': [Function: bound _createTxObject],
        'setTotalSupply(uint256)': [Function: bound _createTxObject],
        totalSupply: [Function: bound _createTxObject],
        '0x18160ddd': [Function: bound _createTxObject],
        'totalSupply()': [Function: bound _createTxObject] },
     events: { allEvents: [Function: bound ] },
     _address: '0x8175036782E9564C084eD1DA6C44C27150F24316',
     _jsonInterface:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ] },
  _ethAccounts:
   Accounts {
     currentProvider: [Getter/Setter],
     _requestManager:
      RequestManager {
        provider: [HttpProvider],
        providers: [Object],
        subscriptions: {} },
     givenProvider: null,
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider:
      HttpProvider {
        host: 'HTTP://127.0.0.1:7545',
        httpAgent: [Agent],
        withCredentials: false,
        timeout: 0,
        headers: undefined,
        connected: false },
     setProvider: [Function],
     _ethereumCall:
      { getNetworkId: [Function],
        getChainId: [Function],
        getGasPrice: [Function],
        getTransactionCount: [Function] },
     wallet:
      Wallet {
        _accounts: [Circular],
        length: 0,
        defaultKeyName: 'web3js_wallet' } } }
Te estás perdiendo ()después myContractInstance.methods['0xc4e41b22'].
muchas gracias... si, me funciono...
por favor, si tiene tiempo, responda mi pregunta que publiqué anteriormente... ethereum.stackexchange.com/questions/77518/…

Respuestas (1)

Te estás perdiendo ()después myContractInstance.methods['0xc4e41b22'].