2

I have an array (i.e. functionNames[]) of getter functions's names of my contract and another array (i.e. finalGettersParams) of encoded parameters (which has been populated from web3.eth.abi.decodeParameters) of that getter functions.

  finalGettersParams = [ Result { __length__: 0 },
  Result {
    '0': '0x0000000000000000000000000000000000000000',
    __length__: 1 },
  Result {
    '0': '0x0000000000000000000000000000000000000000',
    '1': '0x0000000000000000000000000000000000000000',
    __length__: 2 } 
    ]

now i want to call all getter functions with above available information. Is it possible? One possible solution which i am trying (but its not working, being incomplete) ....

for(j=0; j<functionNames.length; j++) {
      myFunc = functionNames[j].funcName;
      try {
        res = await myContractInstance.methods[myFunc](...finalGettersParams[?????????]).call({from: account1}) // here i need help 
        console.log("Getter output", res);
      } catch (error) {
         console.log("Getter ERROR", error);
      }

Anyone can suggest me how to proceed, further?

1

This should generally work for you:

...Object.values(finalGettersParams[j]).slice(0, -1)

However, it implicitly assumes that the values will always appear in increasing order of their keys, and that the value of the __length__ key will always appear last.

So in order to "remove all doubts", you can also use this:

...[...Array(finalGettersParams[j].__length__).keys()].map(key => finalGettersParams[j][key])
|improve this answer|||||
  • yes, it works for me ... thanks a lot... you always help me and avoid my head from burning.... – Amir Ali Dec 18 '19 at 8:33
  • @AmirAli: Hahaha, no problem; see updated answer just in case it matters (though I'm not saying you should use it, if the original suggestion works well enough). – goodvibration Dec 18 '19 at 8:34
  • yes, length always apear last (as per output generated from web3.eth.abi.decodeParameters) ... anyhow, it will also helpful for generic solution ... thanks again... – Amir Ali Dec 18 '19 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.