الدرس رقم 2

Understanding Gas Costs

Welcome back! Now that you have a foundational understanding of gas and have interacted with a basic smart contract, it's time to delve deeper into the intricacies of gas costs. This lesson will guide you through the fundamental gas operations in Ethereum and teach you how to identify the gas costs of operations within the Remix IDE.

Basic Ethereum Operations & Gas Costs

Operational Costs

In Ethereum, every operation has an associated gas cost. Some typical costs include:

  • Base Costs: Basic operations like addition or setting a variable.
  • Memory Costs: Storing or retrieving data in memory.
  • Storage Costs: Interacting with the Ethereum state (e.g., updating contract storage) – typically the most expensive.

It’s important to understand these because many gas optimization techniques involve trading more expensive operations for cheaper ones.

Identifying Gas Costs in Remix

With Remix, it becomes considerably straightforward to discern the gas consumed by our contract’s operations.

Procedure

  1. 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.

  2. 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.

  3. 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.

Practice: Identifying Gas Heavy Operations

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;
    }
}
  1. Deploy the GasDemo contract on Goerli Testnet via Remix.

  2. Interact with the increment function a few times.

  3. Check the gas used in the transaction details.

  4. Now, interact with the updateBalance function, setting different balances.

  5. Again, check the gas used.

Reflect upon:

  • Which function consumes more gas and why? (Tip: you have displayed the estimated gas to be consumed near each function)
  • How does updating the mapping compare in terms of gas to incrementing a simple counter?

Recap & Preparing for the Next Lesson

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!

إخلاء المسؤولية
* ينطوي الاستثمار في العملات الرقمية على مخاطر كبيرة. فيرجى المتابعة بحذر. ولا تهدف الدورة التدريبية إلى تقديم المشورة الاستثمارية.
* تم إنشاء الدورة التدريبية من قبل المؤلف الذي انضم إلى مركز التعلّم في Gate. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.
الكتالوج
الدرس رقم 2

Understanding Gas Costs

Welcome back! Now that you have a foundational understanding of gas and have interacted with a basic smart contract, it's time to delve deeper into the intricacies of gas costs. This lesson will guide you through the fundamental gas operations in Ethereum and teach you how to identify the gas costs of operations within the Remix IDE.

Basic Ethereum Operations & Gas Costs

Operational Costs

In Ethereum, every operation has an associated gas cost. Some typical costs include:

  • Base Costs: Basic operations like addition or setting a variable.
  • Memory Costs: Storing or retrieving data in memory.
  • Storage Costs: Interacting with the Ethereum state (e.g., updating contract storage) – typically the most expensive.

It’s important to understand these because many gas optimization techniques involve trading more expensive operations for cheaper ones.

Identifying Gas Costs in Remix

With Remix, it becomes considerably straightforward to discern the gas consumed by our contract’s operations.

Procedure

  1. 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.

  2. 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.

  3. 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.

Practice: Identifying Gas Heavy Operations

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;
    }
}
  1. Deploy the GasDemo contract on Goerli Testnet via Remix.

  2. Interact with the increment function a few times.

  3. Check the gas used in the transaction details.

  4. Now, interact with the updateBalance function, setting different balances.

  5. Again, check the gas used.

Reflect upon:

  • Which function consumes more gas and why? (Tip: you have displayed the estimated gas to be consumed near each function)
  • How does updating the mapping compare in terms of gas to incrementing a simple counter?

Recap & Preparing for the Next Lesson

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!

إخلاء المسؤولية
* ينطوي الاستثمار في العملات الرقمية على مخاطر كبيرة. فيرجى المتابعة بحذر. ولا تهدف الدورة التدريبية إلى تقديم المشورة الاستثمارية.
* تم إنشاء الدورة التدريبية من قبل المؤلف الذي انضم إلى مركز التعلّم في Gate. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.