How to estimate the cost for deploying smart contract on mainnet

+1 vote

I want to deploy a contract on mainnet, however I'm not able to figure out the gas estimate cost to deploy the contract there.

I referred https://ethgasstation.info/ site but I couldn't get much details.
Can somebody tell me the gas price and max gas price for an ideal contract.
Also, is it possible to make around 10 billion tokens on main net. 

Apr 9, 2018 in Blockchain by anonymous
15,687 views

4 answers to this question.

+2 votes
Best answer

Since you have already deployed the contract on Ropsten network, you must have already managed to use a sensible equivalent gas amount.

estimateGas function will give you the estimate amount of gas you need to execute the contract.

answered Apr 10, 2018 by Shashank
• 10,400 points

selected Aug 7, 2018 by Omkar
+1 vote

You should test your deployment process on private network or test network throughly before trying mainnet. I see you have tested yours on Ropsten, but I would suggest you to test preferably on Rinkeby because in Ropsten block gas limit is too low.

In my experience gasPrice setting in truffle.js is effective. I have successfully deployed my contracts using the following syntax:

geth_mainnet: {
      host: "127.0.0.1",
      port: 8545, // geth-mainnet
      network_id: "1",
      gasPrice: 2000000000, // 2 GWei
      from: "0x...your-address-goes-here..."
    }

Make sure your account has enough funds not only for your smartcontract deployment, but also for deployment of truffle's Migration contract and calls to that contract.

Hope it helps!

Join our Blockchain course today to learn more about it.

Thanks.

answered Jun 14, 2018 by Christine
• 15,790 points
0 votes

To estimate the cost for deployment, I use functions of Web3. This is what you have to do:

  1. Get the gas price (in wei) using the web3.eth.getGasPrice function.

  2. Then use the estimateGas function to get the gas estimation for a function (with the parameters passed)

  3. Gas cost estimation = number of gas * gas price

For example

var TestContract = artifacts.require("./Test.sol");
...
...
...

// getGasPrice returns the gas price on the current network
TestContract.web3.eth.getGasPrice(function(error, result){
    var gasPrice = Number(result);
    console.log("Gas Price is " + gasPrice + " wei"); // "10000000000000"

    // Get Contract instance
    TestContract.deployed().then(function(instance) {

        // Use the keyword 'estimateGas' after the function name to get the gas estimation for this particular function
        return instance.giveAwayDividend.estimateGas(1);

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

Output:

Gas Price is 20000000000 wei 

gas estimation = 26794 units 

gas cost estimation = 535880000000000 wei 

gas cost estimation = 0.00053588 ether
answered Oct 23, 2018 by Omkar
• 69,220 points
The sentence "TestContract.web3.eth.getGasPrice(function(error, result)" makes me confused. Why add "TestContract" before "web3.eth.getGasPrice", instead of directly using"web3.eth.getGasPrice"? Isn't  "web3.eth.getGasPrice" a complete method, why should we add a contract object before it?

Hey @XHQ.

Yes, you can use web3.eth.getGasPrice directly. The function is just used to get different conversions of the gas price. Just to make it more descriptive. 

I get it, thanks!

Hi, I have a very small problem but it makes me very confused. https://www.edureka.co/community/42803/solidity-function-placed-before-brace-when-define-function  Could you please answer it? It will be really kind of you.

Hi, I have answered, please check.
–1 vote

You can use testrpc for this. When you use testroc and deploy a smart contract, you can see the cost for deploying the smart contract on the terminal running testrpc.

answered Apr 25, 2019 by Giri

Hi @Giri. When I use this approach, the cost is displayed after I deploy. I want to know the cost before deployment. How can I do this in testrpc?

I don't think testrpc allows this feature. You will have to use truffle of gas estimate function as mentioned in other answers

The question was specifically asked for mainnet. Testrpc is used only for development and not actually deployment. 

Related Questions In Blockchain

0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,625 views
0 votes
2 answers

How to get notified when an event triggers on ethereum smart contract?

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2, 2020 in Blockchain by aqowcmbevs
2,410 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 6, 2018 in Blockchain by aryya
• 7,460 points
821 views
0 votes
1 answer

How to get the current state of a smart contract?

Hey, you gotta provide the contract code, and ...READ MORE

answered Aug 1, 2018 in Blockchain by Perry
• 17,100 points
1,134 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,947 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,470 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,466 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP