如何计算BSC(BEP-20)区块链中USDT等代币交易的gasLimit?

时间:2021-07-11 08:18:10

标签: node.js smartcontracts web3 web3js binance-smart-chain

我正在 Binance Smart Chain 中开发一个 DAPP,我想知道如何计算 gasLimit 之类的令牌交易的 USDT,就像它的 chrome 扩展程序建议交易 {{1 }} 并计算其gasLimit。我有一个计算 BNB 交易中 transactionFee 的公式,但这不适用于代币交易。 BNB 交易计算公式:

gasLimit

如果我也能像上面一样计算 transactionFee 就好了!!! 任何帮助???

1 个答案:

答案 0 :(得分:0)

在执行代币交易时,您可以使用 web3.eth.Contract 在 JS 中实例化合约助手对象。

然后您可以使用包含基于合约 ABI 和公共函数的辅助函数的 .methods 属性。

然后您可以将 .estimateGas() 函数链接到合约函数。

const myContract = new web3.eth.Contract(abiJson, contractAddress);
const gasLimit = await myContract.methods
    .transfer(to, amount)       // the contract function
    .estimateGas({from: ...});  // the transaction object

文档:https://web3js.readthedocs.io/en/v1.3.4/web3-eth-contract.html#methods-mymethod-estimategas