Antier Solutions
Antier Solutions

Reputation: 1382

How to estimate TRC20 token transfer gas fee

I am looking for a method to estimate bandwidth/gas transfer for TRC20 tokens. Like we have in Ethereum

myContract.methods.myMethod(123).estimateGas({from: 'ERC20 address'})
    .then(function(gasAmount){
        ...
    })
    .catch(function(error){
        ...
    });

Studied TRON documentation but did not found any relative method. Any help would be appreciated. Thanks in advance.

Upvotes: 5

Views: 8658

Answers (3)

How to Estimate TRC20 Token Transfer Resources on Typescript

To estimate the bandwidth and energy required for a TRC20 token transfer on the Tron network, you can use the tronweb package in TypeScript. Here's an approach inspired by a code snippet found in the tron-playground repository

Bandwidth Estimation:

  1. Obtain a signedTx object representing the transaction you want to estimate.

  2. Calculate the bandwidth using the following formula

    const bandwidth = (signedTx.raw_data_hex.length + signedTx.signature[0].length) / 2 + 68;

Energy Estimation:

  1. Use the tronWeb.transactionBuilder.triggerConstantContract method to simulate the transaction without executing it.

  2. Extract the estimated energy from the returned object:

    const estimatedEnergy = txConstant.energy_used || 0;

More information about triggerConstantContract

Upvotes: 0

Mohsen Haydari
Mohsen Haydari

Reputation: 675

To call the contract method, you need to get the required transaction amount of energy and bandwidth.

How much energy and bandwidth is available in my account? link

 //energy
 currentAccountEnergy = EnergyLimit - EnergyUsed

 //bandwidth
 totalBandwidth = freeNetLimit + NetLimit;
 totalBandwidthUsed = NetUsed + freeNetUsed;

 currentAccountBandwidth = totalBandwidth - totalBandwidthUsed 

how to calculate transaction bandwidth/energy?

for transfer Trx transfer and trc10, you need only bandwidth and for trc20 or smart contracts you need to pay for bandwidth and energy

calculate bandwidth

howManyBandwidthNeed = byte length of transaction protobuf  + 64;

Refer to this book for how to create a tron transaction using Protobuf

If you use API to make a transaction, you can use the following formula to calculate the bandwidth,in some cases the value is slightly different 1-3 Bandwidth.

 int howManyBandwidthNeed = transaction["raw_data_hex"].length;
 for (String i in transaction["signatur"]) {
     howManyBandwidthNeed += i.length;
 }
 howManyBandwidthNeed = (howManyBandwidthNeed / 2).round() + 68;

calculate energy

Receiving energy is specific to contracts and it is the same as eth_estimateGas in Ethereum, refer to this link

how much trx needs to burn for bandwidth?

If the required amount of bandwidth is less than the current bandwidth of your account, it will be deducted from the bandwidth of your account and is free, otherwise, the required amount will be multiplied by 1000.

how much trx needs to burn for energy?

The required energy is subtracted from the energy of your account and the remainder is multiplied by 420

this API got you current energy fee

Upvotes: 7

ehsanbigzad
ehsanbigzad

Reputation: 36

To those who is searching for a solution, use getChainParameters method in tronweb or trongrid api.

https://developers.tron.network/reference/getchainparameters

Upvotes: 1

Related Questions