0

I have written a web3 implementation. The code below invokes a method of solidity contract that writes some value to blockchain:

var Coursetro = CoursetroContract.at('0x3958c21a7037d4a53c2990834c59ca3dcb00d988');
var fileName = "filename.pdf";
var identifier = uuid.v4();
console.log("uuid is " + identifier);

Coursetro.setFile(identifier,fileName,"hash...",function(error, result){
  if(!error)
  {
    console.log("Transaction submitted" + result);
  }
  else{
    console.log("Error " + error);
  }
});

How can I get a estimated cost for the "setFile" transaction and provide my gas limit and price ?

1

Web3js provides an estimateGas function for each transaction. You only need to suffix your transaction with estimateGas like myContract.myMethod.estimateGas(arg1, arg2, {options})

Here is an example for your case:

web3js 0.20.x (documentation)

var Coursetro = CoursetroContract.at('0x3958c21a7037d4a53c2990834c59ca3dcb00d988');
var fileName = "filename.pdf";
var identifier = uuid.v4();
console.log("uuid is " + identifier);

web3.eth.getGasPrice(function(error, result){ 
    var gasPrice = Number(result);
    console.log("Gas Price is " + gasPrice + " wei"); // "10000000000000"

    Coursetro.setFile.estimateGas(identifier, fileName, "hash...", {from: 0x, ...}).then(function(result) {
        var gas = Number(result);

        console.log("gas estimation = " + gas + " units");
        console.log("gas cost estimation = " + (gas * gasPrice) + " wei");
        console.log("gas cost estimation = " + TestContract.web3.fromWei((gas * gasPrice), 'ether') + " ether");
    });
});

web3js 1.0.x (documentation)

If you are using web3js 1.0, it's a bit different you have to make the call like that myContract.methods.myMethod(arg1, arg2, ...).estimateGas({options})

var Coursetro = CoursetroContract.at('0x3958c21a7037d4a53c2990834c59ca3dcb00d988');
var fileName = "filename.pdf";
var identifier = uuid.v4();
console.log("uuid is " + identifier);

web3.eth.getGasPrice(function(error, result){ 
    var gasPrice = Number(result);
    console.log("Gas Price is " + gasPrice + " wei"); // "10000000000000"

    Coursetro.methods.setFile.(identifier, fileName, "hash...").estimateGas.then({from: 0x}, function(result) {
        var gas = Number(result);

        console.log("gas estimation = " + gas + " units");
        console.log("gas cost estimation = " + (gas * gasPrice) + " wei");
        console.log("gas cost estimation = " + TestContract.web3.fromWei((gas * gasPrice), 'ether') + " ether");
    });
});

Of course, the gasPrice is a median value, the client can always increase it to get a faster transaction ...

| improve this answer | |
  • Thanks but why is this required web3.eth.getGasPrice(function(error, result){ ? Can't we simply use Coursetro.methods.setFile.(identifier, fileName, "hash...").estimateGas....... ? – Vivek Sadh Aug 9 '18 at 8:55
  • 1
    yeah yeah you can, it was more to illustrate how to estimate the median transaction cost – Greg Jeanmart Aug 9 '18 at 8:56
  • + 1 Thanks. How to specify a gas limit here ? – Vivek Sadh Aug 9 '18 at 9:04
  • estimateGas returns the gasLimit, you estimate the cost by calculating the gasLimit (computation cost of the transaction in unit) * gasPrice (reward by gas unit) - Bigger is the reward, faster is the transaction. – Greg Jeanmart Aug 9 '18 at 9:06
  • 1
    Once you have the gas estimation x and the gas price you are willing to pay y, you can call the function (send the transaction) by doing MyContract.MyFunction(arg1, arg2, {from: 0x, gasPrice: y, gas: x}, (tx) => console.log(tx)) | more info – Greg Jeanmart Aug 9 '18 at 9:56

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.