Abhishek
Abhishek

Reputation: 159

How to get the Gasprice during transaction in Ethereum using Web3.js?

I am trying to get the Gasprice during Ethereum trnsaction in Web3.js. The code is working properly, but I need to get the Gasprice during the transaction. Can you help to get the Gasprice?

Here is the code below:

const paymentAddress='xxxxxxxxxxxxxxxxxxxx'
            
            Web3.eth.sendTransaction({
                to: paymentAddress,
                value: XXXXXXXXX
            }, (err, transactionId)=>{
                if(err){
                    console.log("Error", err)
                    
                }else{
                    console.log("Successful", transactionId)
                    
                }
            }
            )

Upvotes: 0

Views: 1786

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43481

Since you didn't set the gasPrice manually in the sendTransaction() function, it was set by the node that you're connected to.

You can retrieve the gasPrice value by transaction hash using the getTransaction() function.

// with async/await
const tx = await web3.eth.getTransaction(transactionId);
console.log(tx.gasPrice);

// or with callback function
web3.eth.getTransaction(transactionId, (err, tx) => {
    console.log(tx.gasPrice)
})

Upvotes: 1

Related Questions