Energy Costs Calculation
When writing, testing and debugging smart contracts, you should be aware of the different OpCode call costs. This article provides details on the calculation of energy consumption and provides links to relevant references.
For energy costs and price calculations, see Tron Station.
OpCode Energy
The energy consumption of different OpCode is divided into different levels, with the cost level classification and corresponding energy consumption as follows:
public enum Tier {
ZeroTier(0),
BaseTier(2),
VeryLowTier(3),
LowTier(5),
MidTier(8),
HighTier(10),
ExtTier(20),
SpecialTier(1),
InvalidTier(0);
}
The consumption of different OpCode can be found in OpCode.java.
The concept of memory and storage exists in virtual machines, and the OpCode consumption for these two types of operations is calculated separately.
For example, for the memory type of OpCode, the memory size of the operation affects the consumption, for example MLOAD
, MSTORE
OpCode, the consumption is related to the word length of the operating memory.
For the storage type SSTORE
OpCode, the consumption is not only related to the operation word length, but also need to distinguish whether the operation is reset, add, or delete.
Current value | Operation | Energy Consumption | Type of Operation |
---|---|---|---|
val = 0x0 | sstore(val, n) n != 0 (val = n) | 20000 | SET |
val != 0x0 | sstore(val, n) n != 0 (val = n) | 5000 | RESET |
val != 0x0 | sstore(val, 0) (val = 0) | 5000 | CLEAR |
Other higher-consumption operations
When creating an account by transferring TRX or TRC10 from a contract to an inactive account, an additional 25000 energy is consumed and there is no account creation fee of 0.1 TRX.
When invoking other contracts with actions such as CALL
, DELEGATECALL
, etc., and additional transfers are made, an additional 9000 energy is consumed.
When using CREATE
, CREATE2
to dynamically create a contract within a contract, it consumes 32000 energy.
When using pre-compiled contracts, different calls consume different amounts of energy, see PrecompiledContracts.java.
Energy consumption for deploying contracts
When deploying a contract, the contract code consumes 200 energy per byte.
Tips
The bytecode provided when deploying a contract is generally divided into two parts, one called the creation code and the other is the contract's runtime code. The deployment code is generally used to execute the contract constructor logic and return the actual contract runtime code. To calculate the energy consumption when deploying a contract, the length of the runtime code is used. It typically starts with the second
6060
or6080
of the bytecode of the deployment code.
Updated over 4 years ago