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

2 Answers 2

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);
    });



    
0
 var estimatedGas = await web3.eth.estimateGas({
  from: "0xa89a9290DD214B9341dc09Fe23eF0a6a633F9a9F",
   to: tokencontract,
   data: tk.methods.updateMinted(id).encodeABI()
 })




var tip = await web3.eth.getMaxPriorityFeePerGas()


var block = await web3.eth.getBlock("pending")

const baseFee = Number(block.baseFeePerGas);
const max = Number(tip) + baseFee; //increse to speed up






const tx = {
  to: tokencontract,
  gas:  web3.utils.toHex(estimatedGas),
  maxPriorityFeePerGas: web3.utils.toHex(Number(tip)),
  maxFeePerGas: web3.utils.toHex(max),
  data: tk.methods.updateMinted(id).encodeABI(),

};

const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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