Convert gasPrice to wei [

So actually i am having a issue.
I am trying to substract my current gasPrice from the current balance.
My balance is 1115865402486255446.
And the gasPrice is “48495318280”

I am making like 3 trasnactions and at the end a money trasnaction. But somehow metamask dont update. I already contacted the support and the solution was to substract it from my baalnce variable.
I am calling web3.eth.getTrasnsaction… and it returns a n object of the pedning trasnactions. I wanna fetch the transactions costs. I only require the gas fee necause there is no actual value. Its a call to my contract.

I guess u get the user baalnce in wei but i dont know how i can convert the gasFee to wei to. It says the gasFee is 1.70$ on metamask but how can i actually substrac it ?

thats a snippet of my code atm:

                substractBalance +=  parseFloat(web3.utils.fromWei(pendingTransaction.gasPrice,'ether'));
                console.log(parseFloat(web3.utils.fromWei(pendingTransaction.gasPrice,'ether')));

But somehow it doesnt substrac it correctly to the value i want. It says like 0.007$ even though its 1.70$. Please help me i am stuck for so long in that issue ?

If you have confirmed transaction object you can find the gas value in wei used for the transaction.

This is an example transaction object of this transaction.

im not using polygon. I am using mainnet etherium. And there is no object key gasUsed. On you it says 2100. When i convert wei to ether and look what eth is in usd it says like 0.00007$usd.

thats the value i wanna substract from my trasnaction. That was my latest gasPrice $2.47

am using. await web3.eth.getTransaction(hash, (error, result) => {

you understand ? When metamask pops up it says for examplke 2.70$ gasPrice. I am trying to fetch this amount. The exact price

You are looking for a price is wei that is used for the transaction only right? It doesn’t have to be a conversion from USD to Wei.

I suppose you will also get the gas used in your eth mainnet transaction when you run .wait() function on your transaction object result.

so this is my baalnce 1115865402486255446.
When i substract 2100 2.70$ are getting substracted ? I dont think so right ?
1115865402486255446 is almost 2k in usd.

i dont want the gas Value. I dont care how much gas was used. I want the amount of it in wei/ether. When metamask pops up it says gasFee is 2.5$ for example. Then when i click sign i want to fetch this amount 2.50$ in wei so i can substract it from the user balance

you have to multiply the gas price with 21000, that will be the gas cost associated with a native transfer.

thank you this helped me. Was the thing i was looking for.

1 Like

isnt it like

            let gasPrice = await web3.eth.getGasPrice();
let gasLimit = await web3.eth.estimateGas(transactionObject);
            let transactionFee = gasPrice * gasLimit;

??

the gas limit will be fixed 21000 for a native transfer, that is only a particular case

doesn’t work what you have there?

1 Like

works fine but its not exactly. I need it like to be exactly.

What is the difference that remains?

like 1-2$. Should i use my way ?