{"id":3673,"date":"2018-10-17T10:19:12","date_gmt":"2018-10-17T16:19:12","guid":{"rendered":"http:\/\/benincosa.com\/?p=3673"},"modified":"2018-10-17T10:19:12","modified_gmt":"2018-10-17T16:19:12","slug":"technical-details-on-gas-on-ethereum","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=3673","title":{"rendered":"Technical Details on Gas on Ethereum"},"content":{"rendered":"<p>I&#8217;ve been working over the last year off and on on different smart contracts.\u00a0 What follows are some technical details I sent to some of my colleagues here at Cisco regarding gas price and gas limits.\u00a0 Two important parts of Ethereum that are not understood very well: Units and Gas.<\/p>\n<h2>Units<\/h2>\n<div class=\"\">1 Ether in Ethereum is divisible up to 18 decimal units. \u00a0The smallest unit is called a wei. \u00a0Thus, 1 wei \u00a0followed by 18 zeros equals 1 ether. \u00a0So:<\/div>\n<div class=\"\">\n<pre class=\"lang:sh decode:true\">1 ether = 1,000,000,000,000,000,000 wei.<\/pre>\n<\/div>\n<div class=\"\">The other important unit to know is a gwei. \u00a0A gwei is halfway between 1 ether and 1 wei.<\/div>\n<pre class=\"lang:sh decode:true\">1 gwei = 1,000,000,000 wei\r\n1 ether = 1,000,000,000 gwei<\/pre>\n<p>In solidity everything is stored in wei values. \u00a0There are no fractions nor decimals, we just work with integers. \u00a0So by having this long zero values provide a way to have decimals without having decimals.<\/p>\n<div class=\"\">There are several functions I\u2019ve used in some recent test cases that use this conversion as it is much easier. \u00a0Here are some examples:<\/div>\n<div class=\"\">\n<pre class=\"lang:sh decode:true \">result = await devnetcoin.buyDEV({from: hank,\r\n                        value: web3.toWei(1, \"ether\"),\r\n                        gas: 508460,\r\n                        gasPrice: web3.toWei('20', \"gwei\")})<\/pre>\n<pre class=\"lang:sh decode:true \">web3.fromWei(web3.eth.getBalance(val).toString(), 'ether').toString()<\/pre>\n<div class=\"\">\n<div class=\"\">Essentially, the fromWei function lets you format a value from wei to something else, like ether.\u00a0 The toWei let&#8217;s you convert from ether to wei without having to write out all the zeros.\u00a0 In Ethereum we have this whole other BigNumber class to deal with.\u00a0 BigNumber is the class that allows us to store these giant 18 digit numbers as well as manipulate them.\u00a0\u00a0 It&#8217;s important to understand the units before understanding how gas works.<\/div>\n<\/div>\n<\/div>\n<h2>Gas<\/h2>\n<div class=\"\"><strong class=\"\">Gas<\/strong><span class=\"\">\u00a0<\/span><span class=\"\">is a unit or number of steps to be execute for your contract. Gas has fixed number of units for a specific operation\/computation, this is fixed by Ethereum community. For example to add two numbers EVM consumes it may consumes 3 gas units. You set a Gas Limit in your transaction to tell how many steps you are willing to take.<\/span><span class=\"\">\u00a0 The unit of gas is not in Ether, Gwei, or Wei.\u00a0 It&#8217;s just an integer representing a number of steps.<br \/>\n<\/span><\/div>\n<div class=\"\">\n<p class=\"\">To get basic fees to get some idea of how much gas, note the following:<\/p>\n<ul class=\"\">\n<li class=\"\">32k gas to create a contract.<\/li>\n<li class=\"\">21k gas for normal transaction<\/li>\n<li class=\"\">200 gas * 1 byte of byte code.<\/li>\n<li class=\"\">Transaction data costs 64 for non-zero bytes and 4 for zero bytes<\/li>\n<li class=\"\">Constructor cost<\/li>\n<\/ul>\n<p class=\"\">See this response on\u00a0<a class=\"\" href=\"https:\/\/ethereum.stackexchange.com\/questions\/35539\/what-is-the-real-price-of-deploying-a-contract-on-the-mainnet\/37971\">Ethereum Stack Exchange<\/a>.<\/p>\n<p class=\"\">The limit of computation per block is not constant. Miners can set this.<\/p>\n<p class=\"\"><strong class=\"\">VALUE<\/strong>\u00a0field &#8211; The amount of wei to transfer from the sender to the recipient, this is the value we are going to send to either another user or to a contract.<\/p>\n<p class=\"\"><strong class=\"\">GASPRICE<\/strong>\u00a0value, representing the fee the sender is willing to pay for gas. One unit of gas corresponds to the execution of one atomic instruction, Gasprice is in wei. Wei is a smallest unit. (1 wei = 10^-18 ether or 10^18 wei = 1 ether. Because it is quite high, we use\u00a0<code class=\"\">gwei<\/code>.\u00a0\u00a0<code class=\"\">1 gwei = 1,000,000,000 wei.<\/code><\/p>\n<p class=\"\">Total cost of transaction is\u00a0<code class=\"\">Gas Limit * Gas Price<\/code><\/p>\n<p class=\"\">Gas price fluctuates. During normal times:<\/p>\n<ul class=\"\">\n<li class=\"\">40 GWEI will always get you in next block<\/li>\n<li class=\"\">20 GWEI will get you in the next few blocks<\/li>\n<li class=\"\">2 GWEI will get you in the next few minutes.<\/li>\n<\/ul>\n<p class=\"\">Depends on what minors are willing to take.<\/p>\n<pre class=\"lang:sh decode:true \">1 ether = 1,000,000,000 gwei<\/pre>\n<p>To see the current price for a unit of gas you can run:<\/p>\n<pre class=\"lang:sh decode:true \">web3.eth.gasPrice<\/pre>\n<p>The new function in web3.js 1.0.0 (still beta as of this writing) requires a callback:<\/p>\n<pre class=\"lang:sh decode:true \">web3.eth.getGasPrice(console.log)<\/pre>\n<p>(the callbacks are always error, response in 1.0.0 so in the new stuff you&#8217;ll get two values printed in the console log)<\/p>\n<p class=\"\">This will be about 20 Gwei. Or you can look to see what the Gas price is at the\u00a0<a class=\"\" href=\"https:\/\/ethgasstation.info\/\">ETH Gas Station<\/a>. The current price shown at the ETH Gas station is 11 Gwei (std) and 9 Gwei (safe low). \u00a0Creating contracts seem to cost a little more.<\/p>\n<p class=\"\">In practice when we&#8217;ve created contracts we&#8217;ve seen that we can use the estimateGas function to tell how much for storing the bytes:<\/p>\n<pre class=\"lang:sh decode:true \">this.state.provider.eth.estimateGas({ data: contract.bytecode }, somecallbackfunction)<\/pre>\n<p>The callback function for the contracts we&#8217;ve been working on shows the gas limit (or amount of gas) is about 645234.\u00a0 But this is only the cost of storing the contract code on the blockchain.\u00a0 You need to add more gas limit for creating the contract, for passing parameters, etc.\u00a0 We&#8217;ve found that by adding 80000 more to our gas Limit the contract goes through.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working over the last year off and on on different smart contracts.\u00a0 What follows are some technical details I sent to some of my colleagues here at Cisco regarding gas price and gas limits.\u00a0 Two important parts of Ethereum that are not understood very well: Units and Gas. Units 1 Ether in Ethereum&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[826,872],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3673"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3673"}],"version-history":[{"count":1,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3673\/revisions"}],"predecessor-version":[{"id":3674,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3673\/revisions\/3674"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}