How to estimate Tron smart contract energy costs

A Step-by-Step Guide

In this guide, we'll take you through the process of estimating energy costs on the Tron network using TronWeb, a JavaScript library developed by the Tron community which allows developers to interact with the Tron network. We will also show you how to use these estimates to rent energy on the Tron network.

Prerequisites

Before we begin, please ensure that you have the following:

  • Node.js and npm installed on your system

  • Basic understanding of JavaScript and Solidity

  • TronWeb is installed in your project. You can install it using npm with the command npm install tronweb

Step 1 - Setting Up TronWeb

Firstly, set up TronWeb by initializing a new instance and providing it with the required parameters. Make sure to replace 'privateKey' with your own private key. In order to use TronWeb for energy estimation the node needs to be configured correctly. TronGrid is not configured to allow for this, we have set up a node with TronQL.com which will allow for it. Feel free to use our API key or if you would prefer your own you can get started for free by creating an account.

var tronWeb = new TronWeb({
   fullNode: 'https://mainnet.tron.tronql.com/',
   solidityNode: 'https://mainnet.tron.tronql.com/',
   eventServer: 'https://mainnet.tron.tronql.com/',
   privateKey: "" ,//owner address private key
   headers: {
       'Authorization': 'YOUR-API-KEY-FROM-TRONQL.com'
   }
});

Step 2 - Estimating Energy Cost

The next step is to estimate the energy cost of executing a function on the contract. TronWeb provides a function for this purpose - estimateEnergy. Here's an example of how to estimate energy costs for a 'mint' function: In this example we are using the TronNinjas NFT mint contract call to estimate the energy costs. This call does not require any parameters so we pass an empty array, however, it does require a call value. If your call does require parameters you can see an example of how to build this object below commented out in the code.

This table shows the parameters required to use the estimateEnergy function

tronWeb.transactionBuilder.estimateEnergy(

contractAddress,

functionSelector,

options,

parameter,

issuerAddress);

let energyCostObject = {
    feeLimit: tronWeb.toSun('400'),
    callValue: 1250000000, 
    shouldPollResponse: false
};

/*let parameters = [
    {type: "address", value: tronWeb.address.toHex("TVssappkhEiyqrL24BA4BKaAzetzEJkT3a")},
    {type: "uint256", value: tronWeb.toHex(800)},
    {type: "uint256[]", value: [0, 1]},
    {type: "uint256", value: 2},
];/*
let parameters = [];
let energyEstimate = await tronWeb.transactionBuilder.estimateEnergy(tronWeb.address.toHex("TERdH6zm9eqGjDST9GXJ37qVBoutpJuUzV"), "mint()", energyCostObject, parameters, tronWeb.address.toHex("TVssappkhEiyqrL24BA4BKaAzetzEJkT3a"));
console.log(energyEstimate);

That's it! You have now successfully estimated the energy costs for executing a function on the Tron network, you can now go to the next step and learn how to check user energy levels. Be sure to handle any potential errors in your code to ensure that your application runs smoothly. Happy coding!

Last updated