0

I would like to deploy a contract using truffle.

My account has 0.02 ether (20000000000000000 wei). I set gas: 4612390 in the truffle.js and don't set anything for gasPrice. eth.gasPrice returns 1000000000 wei. If I multiply the gasPrice with gas I get 4,612,390,000,000,000 which is lower than my 20,000,000,000,000,000. But I got the error insufficient funds for gas * price + value. If I set the gas price to low I got intrinsic gas too low.

Is there a way to find out how much gas do need to deploy my contract? What is the difference between gas and gasPrice?

1

I can answer your question about difference between gas and gas price.

Transaction cost equals to gas multiplied by gas price. Gas does not have any unit while gas price unit is wei. Gas used by the same transaction will always be the same (it is a constant value) and depends on the complexity of the transaction. Gas price on the other hand can vary. It is that way because miners have to pay for their electricity in traditional currency. Imagine price of ethereum droping while on the same time price of electricity goes up. If the gas price stays the same, miners will pay more for electricity than they get rewarded in ethereum. Thus gas price must be increased.

You can read about this in more detail here: https://hackernoon.com/ether-purchase-power-df40a38c5a2f

  • Thx. Does that mean I could try to deploy my contract on testnet to find out how much gas is needed? – MarcS82 Apr 16 '18 at 20:13
  • 1
    Yes, you can for example go to remix.ethereum.org and paste your code in place of the one you will see by default. Then on the right side choose "Run" tab and select JavaScript VM for Environment. Then click pink "Create" button. Your contract will be deployed. Then in the middle section, at the very bottom, click the icon with two arrows up. You will see the console and if the deployment went ok, you shoul see two buttons "Details" and "Debug". Click "Details". There you will find information about your transaction. – Mike A. Clearance Apr 16 '18 at 20:35
  • There you will find information about your transaction and also transaction cost, which is the value you are looking for. Oh, one more thing. Unfortunately, from what I can see, in remix IDE "gas" means "gas * gas price" so it may be a little confusing if you just read my previous answer. – Mike A. Clearance Apr 16 '18 at 20:40
1

First of all, let us try to clarify what both gas and gasPrice is.

If you want to execute something on the Ethereum Virtual Machine (EVM), such as a transaction, gas is needed for the computation. More precisely, every operation has a well-defined amount of gas it uses. Hence, for a transaction, the gas that is needed is equal to the sum over the gas needed for each EVM operation. You can specify an upper limit on how much gas a transaction should require maximally. If you are interested in learning more, you can check yellowpaper.io, where all of this is discussed in more detail. Note that creating (or deploying) a Smart Contract costs gas as well, because you are invoking the constructor and are storing all the code on the Blockchain.

One might ask, why is there such as thing as gas in the first place though? Well, if your contract would, for example, contain

function infiniteLoop() public { 
    while (true) { 
        // do some stuff 
    } 
}

a transaction calling infiniteLoop() would never finish and miners would be executing infinitely. The idea of gas is to prevent just that: If there is only so much gas that can be used, execution has to stop at some point. You can use the Solidity compiler solc to get an estimate of how much gas a function will use. The same can be done online using remix.ethereum.org (Compile the contract and click the constructor to get an estimate of the deployment gas costs).

Now to the gasPrice. Imagine, gas wouldn't cost anything. You could then, theoretically, write a contract doing useless, but intense work and set an unreasonable gas limit. Moreover, why would miners even be willing to execute the transaction on your behalf? The gasPrice, which indicates what you are willing to pay per amount of gas, thus incentivises the miners in the network to execute your transaction and add it to a block.

Having not seen your contract, I can only make a guess what has gone wrong. It could be that indeed you have specified a gas limit that wasn't high enough.

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.