EstimateEnergy

Estimate the energy required for the successful execution of smart contract transactions or deploying a contract.

This method will use 30 Compute Units.


Note:

  • This API is closed by default. To open this interface, the two configuration items vm.estimateEnergy and vm.supportConstant must be enabled in the node configuration file at the same time.

  • The wallet/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, Compared to the existing wallet/triggerconstantcontract API, wallet/estimateEnergy 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 (this node does not support estimate energy) shows when calling the new API, it is recommended to continue using the wallet/triggerconstantcontract API to estimate energy consumption.

  • estimateenergy will not generate an on-chain transaction, nor will it change the status of the current node.

  • The energy_required field in the returned value is the energy estimated amount. Therefore, the fee_limit of the transaction can be set to energy_required x energy unit price.

  • Parameter encoding and decoding example: Parameter and return value encoding and decoding

Parameters

  • owner_address - String - Owner address that triggers the contract. If visible=true, use base58check format, otherwise use hex format. For constant call you can use the all-zero address.

  • contract_address - String - Smart contract address. If visible=true, use base58check format, otherwise use hex format

  • function_selector - String - Function call, must not be left blank

  • parameter - String - Parameter encoding needs to be in accordance with the ABI rules, the rules are more complicated, users can use the ethers library to encode,For details, please refer to the document-Guide-Smart Contract-Best Practice-Parameter Encoding and Decoding.

  • data - String - The data for interacting with smart contracts, including the contract function and parameters. You can choose to use this field, or you can choose to use function_selector and parameter for contract interaction. When both of data and function_selector exist, function_selector is preferred

  • call_value - int64 - Amount of TRX transferred to the contract with this transaction, the unit is sun

  • call_token_value - int64 - Amount of TRC10 token transferred with this transaction

  • token_id - int64 - TRC10 token id

  • visible - boolean - Optional. Specifies whether the address is in Base58 format (default: false)

Returns

Field
Type
Description

result

Return

Run result

result.result

bool

Is the estimate successful

result.code

response_code(enum)

response code, an enum type

result.message

string

Result message

energy_required

int64

Estimated energy to run the contract

// Result
{
  "result": {
    "result": true
  },
  "energy_required": 1082
}
curl https://mainnet.tron.validationcloud.io/v1/<YOUR_API_KEY_HERE>/wallet/estimateenergy \
    -X POST \
    -H "Content-Type: application/json" \
    -d '
{
  "owner_address": "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g",
  "contract_address": "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
  "function_selector": "balanceOf(address)",
  "parameter": "000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c",
  "visible": true
}
'

Last updated

Was this helpful?