1

In my project, I'm using the ethers.js library to interact with the blockchain. After the transaction is completed, I can see on the etherscan what was the fee (please see the attachment). enter image description here

I'm looking for a way to get this fee value from the ethers.js library. I was looking for such information in the documentation but didn't find anything here: https://docs.ethers.org/v6/api/transaction/#Transaction.

Is this possible? If not with the ethers.js, then does the etherscan provide any API for that?

2 Answers 2

2

You can do this with ethers.js But seems like you are not using the correct function to get the information about the transaction.

here: https://docs.ethers.org/v6/api/providers/#TransactionReceipt

You can see gasUsed and gasPrice field and this is the information about the transaction fee.

So totalFee = gasUsed * gasPrice

Hope your project success.

1
  • Yeah, this works perfectly. Thank you!
    – Sayaki
    Commented Apr 13, 2023 at 17:04
1

If you're using NodeJs or ReactJs to call the function you can do the following procedure


 try {
      const transaction = await contract.methods
        .function()
        .call({ from: accountAddress });
      
      console.log("tx details:", transaction)
    } catch (error) {
      console.log(error);
    }

This will console, whole transaction detail and you can get transaction hash from there

2
  • Don't copy & paste the same answer into multiple questions. If the questions are too similar mark the question as duplicates.
    – Ismael
    Commented Apr 13, 2023 at 21:42
  • sure, will take care in future Commented Apr 14, 2023 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.