forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGas-Unit-Calculation
84 lines (59 loc) · 4.77 KB
/
Gas-Unit-Calculation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
In Ethereum, gas units are a measure of computational work and are used to determine the cost of executing operations within a smart contract. Gas is an essential concept to ensure the security and fairness of the Ethereum network.
Here's how gas units are calculated in a smart contract:
1)Operation Costs: Each operation in the Ethereum Virtual Machine (EVM) has a predefined gas cost associated with it. For example, simple operations like addition or multiplication have relatively low gas
costs, while more complex operations, such as storage reads and writes, have higher gas costs. These gas costs are defined in the Ethereum Yellow Paper and are designed to reflect the computational resources
required for each operation.
2)Gas Price: Gas units are not directly related to Ether (ETH), the cryptocurrency of the Ethereum network. Instead, gas has a price, which is measured in Gwei (a subunit of Ether).
Miners and validators on the network set the gas price they are willing to accept for including a transaction in a block. Users who want to execute a transaction specify the gas price they are willing to pay.
3)Gas Limit: When a user sends a transaction to execute a smart contract, they also specify a gas limit. The gas limit is the maximum amount of gas units they are willing to consume for the transaction.
It acts as a safety mechanism to prevent runaway code or unintended excessive gas consumption.
4)Gas Used: As the smart contract executes, the EVM keeps track of how many gas units are consumed. If the gas consumption exceeds the specified gas limit, the transaction is reverted, and any changes made by the
contract are rolled back. However, the gas used up to that point is still paid for by the sender.
5)Gas Cost Calculation: The total cost of executing a smart contract is calculated as the product of the gas used and the gas price. This is typically expressed as follows:
Total Cost (in Ether) = Gas Used * Gas Price (in Gwei) / 1e9
It's important to note that if a contract execution fails (e.g., due to insufficient gas or a failed assertion), the gas cost is still incurred, and the state changes are reverted.
6)Refunds: Ethereum has a mechanism called "refunds" that can partially offset gas costs. If a contract deletes data or self-destructs during its execution, it may generate a refund that can
be applied to reduce the overall gas cost.
In summary, gas units are calculated based on the gas cost of individual EVM operations, the gas price set by the user, and the gas limit specified for a transaction. Users must ensure that they provide enough gas
to cover the cost of executing a smart contract, and any unused gas is refunded. This mechanism ensures that the network remains secure and that users pay for the computational resources they consume.
Let's create a simple example to illustrate the calculation of gas units in a smart contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract GasExample {
uint256 public result;
function add(uint256 a, uint256 b) public returns (uint256) {
result = a + b;
return result;
}
}
//////////////////////
Certainly! Let's create a simple example to illustrate the calculation of gas units in a smart contract. In this example, we'll write a Solidity smart contract that performs a basic operation and calculate the gas cost for executing it.
solidity
Copy code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract GasExample {
uint256 public result;
function add(uint256 a, uint256 b) public returns (uint256) {
result = a + b;
return result;
}
}
In this contract:
We have a result state variable to store the result of the addition.
The add function takes two parameters, a and b, and adds them, storing the result in the result variable.
Now, let's assume a user wants to execute this contract function and send a transaction. They specify the following parameters:
a = 5
b = 3
Gas Price = 10 Gwei
Gas Limit = 1,000,000 units
To calculate the gas cost, we'll consider the gas cost of the add function execution. We'll assume that the gas costs are as follows:
The gas cost of a simple addition operation is 3,000 gas units.
The gas price is 10 Gwei.
The total cost can be calculated as follows:
Gas Used = Gas Cost of Operation * Gas Limit
Gas Used = 3,000 gas units * 1,000,000 gas units = 3,000,000,000 gas units
Total Cost (in Ether) = Gas Used * Gas Price (in Gwei) / 1e9
Total Cost = 3,000,000,000 gas units * 10 Gwei / 1e9 = 0.03 ETH
So, in this example, the user would need to provide a gas limit of 1,000,000 units and be prepared to pay a total cost of 0.03 ETH for executing the add function with the specified gas price.
If they provided a gas limit of less than 1,000,000 units, the transaction might run out of gas and fail, and the gas cost would still be incurred.