ethereumjs / ethereumjs-tx

Project is in active development and has been moved to the EthereumJS VM monorepo.
https://github.com/ethereumjs/ethereumjs-vm/tree/master/packages/tx
Mozilla Public License 2.0
779 stars 235 forks source link

How to get return value of smart contract by calling encoded function #189

Closed saarshah closed 4 years ago

saarshah commented 4 years ago

I have encoded function of my smart contract, which has methodID and parameters i.e. 0xf7ea7a3d4000000000000000000015000000000000000000ff7f0000b200000000f7ea7a............... Suppose, in this encoded function, there are also two arguments , one is uint and other is string. This encoded function actually is a getter (not changing status) of my contract (means in ABI its stateMutability =view) and also it return a value i.e. totalSupply. Now, i want to call this funciton through web3js/nodejs, as below. This code gives me transaction receipt/hash, as expected, but i am interested to retrieve the getter function return value i.e. totalSupply;

    try {
            await web3.eth.sendTransaction(
            {from:account1,
            to:myContAddr,
            data: myFunc
                }).then(function(res){
                  console.log("Normal Getter", res);
              });
        } catch (error) {
          console.log(" Normal Getters: ERROR !"); 
        }

One possible solution is to extract function Signature/methodID (which is easy for me) from given encoded function and its parameters (yet, not known to me how to decode parameters) and call function like this .. i.e.

try {
        res = await myContractInstance.methods[myFuncID](????).call({from: account1})  // without parameters
        console.log("Getter output", res);
      } catch (error) {
        console.log("Getter output: ERROR !", error);
      }

So, my questions are

  1. how to decode parameters from above mentioned encoded function, to proceed as per my mentioned solution(if my solution is correct) ?
  2. Or is there any other feasible/easy procedure to call such encoded (getter) function ?
ryanio commented 4 years ago

Hi @saarshah, unfortunately this is not a support forum so in the future the best place to ask and have your questions answered would be: https://ethereum.stackexchange.com

In my experience the easiest way to call the getter function you are looking for would be to instantiate the contract with web3.js using the jsonInterface and address:

const contract = new web3.eth.Contract(jsonInterface[, address][, options])

Docs link

The const contract should then have your contract methods on it, including getters if you set up your contract code correctly, which you can then call using methods.myMethod.call.

saarshah commented 4 years ago

Thanks for your answer... I already asked there, but no one is replying... I think, you did't got my point... I am asking help for my scenario, not for available easiest options.... i know them all. My scenario is to call contract function (which is in encoded form with all its parameters), can you please reconsider my question ? May be my question is demanding new feature, since I think, to call contract function with encoded values , such feature is not available in web3js, so far..v(if i am not wrong. ??????

alcuadrado commented 4 years ago

@saarshah I understand your frustration, but I think @ryanio was right on closing this issue. Your question is not related to this library.

What you are looking for is probably eth_call. You should call it manually through your web3 provider, using the encoded sig+params as data.