In Ethereum, every operation has an associated gas cost. Some typical costs include:
It’s important to understand these because many gas optimization techniques involve trading more expensive operations for cheaper ones.
With Remix, it becomes considerably straightforward to discern the gas consumed by our contract’s operations.
Static Analysis: Navigate to the Analysis
tab in Remix (represented by a microscope icon). This powerful tool will provide insights into potential pitfalls and optimization opportunities in your code.
Deploy & Interact: After deploying your contract (as you did in Lesson 1), each interaction will display a gas estimate. When you call a function, the gas used is displayed on the bottom right pane.
Details Pane: Upon deploying or interacting with a function, click the down arrow in the transaction log (bottom right). This expands the log, showing detailed gas costs for the transaction.
Let’s evaluate a sample contract and its operations:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;
contract GasDemo {
uint256 public count;
mapping(address => uint256) public balances;
function increment() public {
count += 1;
}
function updateBalance(uint256 newBalance) public {
balances[msg.sender] = newBalance;
}
}
Deploy the GasDemo
contract on Goerli Testnet via Remix.
Interact with the increment
function a few times.
Check the gas used in the transaction details.
Now, interact with the updateBalance
function, setting different balances.
Again, check the gas used.
Reflect upon:
You’ve just deepened your understanding of the gas intricacies in Ethereum smart contracts. By practicing with Remix, you can visualize and grasp how different operations impact the total gas of a transaction. In our next lesson, we’ll jump into optimization techniques, where you’ll learn how to make your contracts more gas-efficient.
Stay curious, and keep experimenting!
In Ethereum, every operation has an associated gas cost. Some typical costs include:
It’s important to understand these because many gas optimization techniques involve trading more expensive operations for cheaper ones.
With Remix, it becomes considerably straightforward to discern the gas consumed by our contract’s operations.
Static Analysis: Navigate to the Analysis
tab in Remix (represented by a microscope icon). This powerful tool will provide insights into potential pitfalls and optimization opportunities in your code.
Deploy & Interact: After deploying your contract (as you did in Lesson 1), each interaction will display a gas estimate. When you call a function, the gas used is displayed on the bottom right pane.
Details Pane: Upon deploying or interacting with a function, click the down arrow in the transaction log (bottom right). This expands the log, showing detailed gas costs for the transaction.
Let’s evaluate a sample contract and its operations:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;
contract GasDemo {
uint256 public count;
mapping(address => uint256) public balances;
function increment() public {
count += 1;
}
function updateBalance(uint256 newBalance) public {
balances[msg.sender] = newBalance;
}
}
Deploy the GasDemo
contract on Goerli Testnet via Remix.
Interact with the increment
function a few times.
Check the gas used in the transaction details.
Now, interact with the updateBalance
function, setting different balances.
Again, check the gas used.
Reflect upon:
You’ve just deepened your understanding of the gas intricacies in Ethereum smart contracts. By practicing with Remix, you can visualize and grasp how different operations impact the total gas of a transaction. In our next lesson, we’ll jump into optimization techniques, where you’ll learn how to make your contracts more gas-efficient.
Stay curious, and keep experimenting!