FeeLimit Parameter Setting

FeeLimit is a parameter of the smart contract transaction, which is used to set the upper limit of the energy cost that the caller is willing to undertake for the deployment or invocation of the smart contract, in sun (1TRX = 1e6 sun). The default value is 0. Currently, the upper limit of feelimit can be set is 15000 TRX.

When executing the contract, Energy is calculated and deducted one by one instruction. If the energy used is exceeded, the contract execution will fail and the deducted Energy will not be refunded. Therefore, before deploying or calling the contract, it is recommended to set an appropriate feelimit to ensure the normal execution of smart contract transactions. The following describes how to estimate the energy consumption of smart contract transactions and set the feelimit parameter.

How to determine the FeeLimit parameter?

Due to the Dynamic Energy Model mechanism, the energy consumption of popular contracts changes dynamically, so calling the same contract function in different time periods may result in different energy consumption, so in different time periods, transactions that call popular contracts need to set different feelimit parameters. Here we introduce three ways of setting feelimit:

  • Estimate energy before each contract call
    Before each transaction is sent, the total energy consumption of the transaction is estimated through API, and the FeeLimit parameter of the transaction is determined according to the estimated energy consumption:

    FeeLimit of contract transaction = estimated total energy consumption * EnergyPrice
    
  • Get the contract energy_factor once every maintenance cycle
    First, determine the basic energy consumption of a function of a contract through triggerconstantcontract API or past historical experience of the contract, and then get the energy_factor parameter of a contract at each maintenance cycle:

    FeeLimit of contract transaction = estimated basic energy consumption * (1 + energy_factor) * EnergyPrice
    
  • Set feelimit according to max_factor
    First, determine the basic energy consumption of a function of a contract through triggerconstantcontract API or past historical experience of the contract, and then obtain the max_factor parameter of the chain. max_factor is the maximum ratio of the energy penalty coefficient, so no matter how the energy consumption of popular contracts fluctuates, it will not exceed this maximum ratio:

    FeeLimit of contract transaction = estimated basic energy consumption *(1 + max_factor)* EnergyPrice
    

The advantage of the first method above is that the feelimit setting is very accurate, but the disadvantage is that the operation is more cumbersome, and each transaction needs to be estimated. Compared with the first method, the second method maintains the accuracy of the feelimit setting, but still needs to obtain the energy_factor parameter of the contract every maintenance cycle (6 hours). The advantage of the third method is that it is easy to operate and does not need to frequently obtain the max_factor parameter, but the calculated FeeLimit will be greater than the actual energy cost, because the energy_factor of most contracts will not reach max_factor.

How to estimate energy consumption?

Developers can call wallet/triggerconstantcontract API to estimate the energy consumption value of calling contracts.

An example is listed as follows:

$ curl -X POST  https://nile.trongrid.io/wallet/triggerconstantcontract -d '{
    "owner_address": "TTGhREx2pDSxFX555NWz1YwGpiBVPvQA7e",
    "contract_address": "TVSvjZdyDSNocHm7dP3jvCmMNsCnMTPa5W",
    "function_selector": "transfer(address,uint256)",
    "parameter": "0000000000000000000000002ce5de57373427f799cc0a3dd03b841322514a8c00000000000000000000000000000000000000000000000000038d7ea4c68000",
    "visible": true
}'

It returns:

{
   ……
   "result": {
       "result": true
   },
   "energy_used": 46236,
   "energy_penalty": 32983,
   ……


}

The result.result=true in the example stands for the successful execution of estimating operation, the value of energy_used is the estimated energy consumption of the transaction, where the basic energy consumption value is (energy_used — energy_penalty), and value of energy_penalty is the additional energy consumption.

The triggerconstantcontract API can be used to estimate the energy consumption value of calling most smart contracts on the chain, such as USDD, USDT, USDC, TUSD, etc. Meanwhile, in the Java-tron 4.7.0.1 version, a new API of wallet/estimateenergy is added. Compared to the existing wallet/triggerconstantcontract API, the new API will be more accurate in estimating the energy consumption of calling a small number of special contract. But for FullNode, enabling the wallet/estimateEnergy API is optional. So please pay attention that when developers call wallet/estimateEnergy, if the error message shows that the node does not support this function when calling the new API (this node does not support estimate energy), it is recommended to continue using the wallet/triggerconstantcontract API to estimate energy consumption.

An example is listed as follows:

$ curl -X POST  https://nile.trongrid.io/wallet/estimateenergy -d '{
    "owner_address": "TTGhREx2pDSxFX555NWz1YwGpiBVPvQA7e",
    "contract_address": "TVSvjZdyDSNocHm7dP3jvCmMNsCnMTPa5W",
    "function_selector": "transfer(address,uint256)",
    "parameter": "0000000000000000000000002ce5de57373427f799cc0a3dd03b841322514a8c00000000000000000000000000000000000000000000000000038d7ea4c68000",
    "visible": true
}'

It returns:

{
   "result": {
      "result": true
   },
   "energy_required": 34830
}

The result.result = true in the example stands for the successful execution of estimating operation, the value of energy_equired is the estimated energy consumption of the transaction, it contains the basic energy consumption and additional energy consumption.