# estimateenergy - TRON

## Description

The `estimateenergy` method in the Tron protocol allows developers to estimate energy consumption for transactions via the HTTP REST API Interface. This Web3 tool is essential for optimizing smart contract execution and resource management.

By integrating `estimateenergy` RPC protocol calls, users can predict energy costs before submitting transactions, ensuring efficient blockchain interactions. Ideal for dApp developers and node operators, it supports seamless Tron network integration with precise, real-time energy estimates. Simplify your workflow with this critical Tron RPC method.

## Supported Networks

The `estimateenergy` HTTP REST API method supports the following network types

* **Mainnet**
* **Testnets**

## Parameters

| Parameter              | Type      | Description                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ---------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **owner\_address**     | `string`  | <p>The address of the owner initiating the contract call.<br>- If <code>visible = true</code>, the address must be in <strong>Base58Check</strong> format.<br>- If <code>visible = false</code>, the address must be in <strong>hex</strong> format.<br>- For constant calls, you can use the all-zero address.<br><strong>Default</strong>: <code>TSNEe5Tf4rnc9zPMNXfaTF5fZfHDDH8oyW</code></p>                                                    |
| **contract\_address**  | `string`  | <p>The smart contract address to be called.<br>- If <code>visible = true</code>, the address must be in <strong>Base58Check</strong> format.<br>- If <code>visible = false</code>, the address must be in <strong>hex</strong> format.<br><strong>Default</strong>: <code>TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs</code></p>                                                                                                                             |
| **function\_selector** | `string`  | <p>The name of the function to be called on the smart contract.<br>This field <strong>must not</strong> be left empty.<br><strong>Default</strong>: <code>balanceOf(address)</code></p>                                                                                                                                                                                                                                                             |
| **parameter**          | `string`  | <p>The function call parameters encoded according to <strong>ABI (Application Binary Interface)</strong> rules.<br>- Use tools such as the <strong>ethers.js</strong> library to encode parameters.<br>- For detailed guidance, refer to: <strong>Guide → Smart Contract → Best Practice → Parameter Encoding and Decoding</strong>.<br><strong>Default</strong>: <code>000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c</code></p> |
| **visible**            | `boolean` | <p>Optional. Indicates the format of the address fields (<code>owner\_address</code> and <code>contract\_address</code>).<br>- <code>true</code> — addresses must be in <strong>Base58Check</strong> format.<br>- <code>false</code> — addresses must be in <strong>hex</strong> format.<br><strong>Default</strong>: <code>true</code></p>                                                                                                         |

### URL

```json
https://go.getblock.io/<ACCESS-TOKEN>/
```

Here’s a sample cURL request using estimateenergy

Request

```json
curl --request POST \
     --url https://go.getblock.io/<ACCESS-TOKEN>/walletsolidity/estimateenergy \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --data '
{
  "owner_address": "TSNEe5Tf4rnc9zPMNXfaTF5fZfHDDH8oyW",
  "contract_address": "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
  "function_selector": "balanceOf(address)",
  "parameter": "000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c",
  "visible": true
}
'
```

Response

```json
{
  "result": {
    "result": true
  },
  "energy_required": 1082
}
```

## Body Parameters

| Field                | Type                   | Description                                                  |
| -------------------- | ---------------------- | ------------------------------------------------------------ |
| **result**           | `Return`               | The result of the contract execution.                        |
| **result.result**    | `bool`                 | Indicates whether the estimation was successful.             |
| **result.code**      | `response_code (enum)` | Response code represented as an enumeration.                 |
| **result.message**   | `string`               | Message describing the result of the execution.              |
| **energy\_required** | `int64`                | Estimated amount of energy required to execute the contract. |

## Use Case

Here are some use-cases for the `estimateenergy` method in Web3 programming:

1. **Gas Fee Estimation for Transactions**\
   The `estimateenergy` method can be used to predict the computational energy (gas) required to execute a transaction on a blockchain network (like TRON). This helps users and dApps set appropriate gas limits and avoid failed transactions due to insufficient energy or bandwidth.
2. **Smart Contract Interaction Cost Analysis**\
   Developers can use this method to estimate the energy consumption of calling specific smart contract functions before execution. This allows for cost optimization, especially in dApps where users need to pay for transaction fees or where energy delegation is involved.
3. **Energy Delegation Planning**\
   In networks like TRON, where energy can be delegated or frozen, `estimateenergy` helps users determine whether they have enough resources (or need to acquire more) before submitting transactions, ensuring smooth and cost-efficient operations.

This method is particularly useful for optimizing transaction costs and improving user experience in decentralized applications.

## Code for estimateenergy

```python
import requests
import json

url = "https://go.getblock.io/<ACCESS-TOKEN>/walletsolidity/estimateenergy"
headers = {
     --header 'accept: application/json' 
     --header 'content-type: application/json' 
}
payload = {
  "owner_address": "TSNEe5Tf4rnc9zPMNXfaTF5fZfHDDH8oyW",
  "contract_address": "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
  "function_selector": "balanceOf(address)",
  "parameter": "000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c",
  "visible": true
}

response = requests.post(url, headers=headers, data=json.dumps(payload))

# Check the response and print the result
if response.status_code == 200:
    print("Result:", response.json().get("result"))
else:
    print("Error:", response.status_code, response.text)
```

## Common Errors

When using the `estimateenergy` RPC Tron method, the following issues may occur:

* **Invalid address format**: The provided address (e.g., `TCuM8e98jmPwT1RU2jW7dekUC5HpXbGzFG`) may be malformed or unsupported. Ensure the address is a valid TRON base58 or hex format.
* **Insufficient energy or bandwidth**: The transaction may fail if the account lacks sufficient energy or bandwidth. Check the account's resources and delegate or acquire more if needed.
* **Unsupported contract call**: The method may error if the target contract is incompatible or the ABI is misconfigured. Verify the contract's compatibility and input parameters.

The `estimateenergy` method is invaluable for Web3 apps, enabling developers to predict transaction costs and optimize resource allocation before execution. This reduces failed transactions and enhances user experience by providing upfront cost transparency.

### Conclusion

To estimate transaction costs or resource usage, you can leverage the `estimateenergy` RPC method on the TRON network. This feature is invaluable for developers optimizing smart contract interactions or calculating bandwidth/energy requirements before executing transactions.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.getblock.io/api-reference/tron-trx/solidity_http_api/estimateenergy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
