2

When I sign and send raw transactions with web3 in javascipt , to call a method from Smart Contract, I always pass the transaction gas variable after getting it with estimateGas function, from Smart Contract method, as following:

mySmartContract= new web3.eth.Contract(SmartContractABI, SmartContractAddress);
mySmartContract.methods.set(inputnum).estimateGas({from: miAddress})
      .then(function(gas){
          tx.gas = web3.utils.toHex(gas);
        })
      .catch(function(error){
        console.log("Set() - catch error");
        console.log(error);
      });

How could I do the same to invoke the fallback function from the Smart Contract? estimateGas() is a function from "methods" object, but "fallback" is not a method.

Thanks in advance

1

After some research & testing I found the solution by mysef. As a fallback function is unnamed, I just need to use estimateGas with the "eth" object to get the gas stimation of the default function (fallback):

const web3eth = require('web3-eth');
const eth = new web3eth('https://ropsten.infura.io/v3/XXXXXXX');
    
var tx = {};
tx.gas;

eth.estimateGas({
      from: miAddress
    })
    .then(function(gas) {
      tx.gas = web3.utils.toHex(gas);
    })
    .catch(function(error) {
      console.log("Estimategas() - catch error");
      console.log(error);
    });



    

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.