ethereumweb3jscryptocurrencyuniswap

Is there a way to get the transaction fees on uniswap and sushiswap_


I'm building a trading bot and I need to get the cost of the swap transaction on uniswap and sushiswap. But I cant find anything that can help me get it from an API or something similar.

I tried looking at the docs of uniswap and sushiswap but they only talked about how they calculate the fees but not about if there was any way to get them using the SDK or something similar.


Solution

  • Using web3.js, you can estimate the cost in gas units, and then multiply by the gas price, to get the transaction cost in ETH.

    Simplified example estimating execution cost of function foo():

    // how many gas units is the transaction going to cost
    const gasUnits = await myContract.methods.foo().estimateGas();
    
    // price (recommended by your node) of each gas unit in wei
    const gasPrice = await web3.eth.getGasPrice();
    

    Docs: