4

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.

Antier Solutions
  • 1,326
  • 7
  • 10
  • have you researched this? As far as I know, you should first get required bandwidth amount for the transaction, then get availabe bandwidth, then if available > required then fee is 0, otherwise its the difference * (trx to bandwidth exchange rate), but I'm not sure yet where to get the rate and how to get the required bandwidth – YakovL Dec 19 '21 at 11:20
  • @YakovL . how do you determine the amount of bandwidth required for a particular transaction eg. trc20 usdt transaction – Joey daniel darko May 23 '23 at 21:34

2 Answers2

4

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

Mohsen Haydari
  • 550
  • 5
  • 20
  • what is transaction["signatur"]? is it an array or a string? do you have a php code example? – TheNewCoder Mar 20 '23 at 04:44
  • @TheNewCoder Previously, it was possible to sign a transaction through the API of the TronGrid site, and returned data was a JSON with a signature key (signature of the transaction), but it has been a while since it was possible to sign a transaction through the site. – Mohsen Haydari Apr 18 '23 at 08:28
  • 1
    It's really strange that there isn't an API that does all these calculations and returns the result to us. Obtaining these formulas and values that you wrote is very complicated and practically impossible – reza_khalafi Jun 30 '23 at 20:07
1

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

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

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31566118) – Ethan Apr 21 '22 at 22:31