如何估算 TRC20 代币转账 Gas 费

Ant*_*ons 5 cryptocurrency tron

我正在寻找一种方法来估计 TRC20 代币的带宽/气体传输。就像我们在以太坊中一样

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

研究了TRON文档但没有找到相关方法。任何帮助,将不胜感激。提前致谢。

Moh*_*ari 6

调用合约方法,需要获取所需交易量的能量和带宽。

我的帐户中有多少可用能量和带宽? 关联

 //energy
 currentAccountEnergy = EnergyLimit - EnergyUsed

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

 currentAccountBandwidth = totalBandwidth - totalBandwidthUsed 

Run Code Online (Sandbox Code Playgroud)

如何计算交易带宽/能量?

对于传输 Trx 传输和 trc10,您只需要带宽,对于 trc20 或智能合约,您需要支付带宽和能量

计算带宽

howManyBandwidthNeed = byte length of transaction protobuf  + 64;
Run Code Online (Sandbox Code Playgroud)

请参阅本书了解如何使用 Protobuf 创建 tron 交易

如果您使用API​​进行交易,您可以使用以下公式来计算带宽,在某些情况下该值略有不同1-3带宽。

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

Run Code Online (Sandbox Code Playgroud)

计算能量

接收能量是合约特定的,与以太坊中相同 eth_estimateGas,参考此链接

需要消耗多少 trx 来获取带宽?

如果所需带宽小于您账户当前带宽,则从您账户带宽中扣除,免费,否则,所需带宽乘以1000。

需要燃烧多少 trx 来获取能量?

从您账户的能量中减去所需的能量,余数乘以420

这个API可以获取当前的能源费用


小智 1

对于那些正在寻找解决方案的人,请使用 tronweb 或 trongrid api 中的 getChainParameters 方法。

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