How to accurately calculate TRC20 transfer fees

162 Views Asked by At

To calculate the energy used by a contract invocation with TronWeb I'm using this snippet of code

const { required_energy } = await tron.current.transactionBuilder.estimateEnergy(
      contractAddress,
      "transfer(address,uint256)",
      {},
      [
        { type: "address", value: address },
        { type: "uint256", value: tokenAmount }
      ]
    )

or you can also use the equvalent calling triggerConstantContract (the results are similar)

const { energy_used } = await tron.current.transactionBuilder.triggerConstantContract(
      contract,
      "transfer(address,uint256)",
      {},
      [
        { type: "address", value: address },
        { type: "uint256", value: tokenAmount }
      ]
    )

In my case I'm trying to estimate the energy required to send USDT TRC20 TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t

In the vast majority of the cases the energy obtained is roughly equivalent to 14 TRX. So I can safely set the feeLimit to broadcast the transaction to 14 TRX + a little safety margin.

But then when the contract usage spikes the transaction requires like double the energy than usual, the strange thing is that the results obtained from estimateEnergy and triggerConstantContract do not follow the energy spike.

So how can I get the correct fee preview?

0

There are 0 best solutions below