0

Can you please double-check this command for me and tell me if I screwed up the units?

eth.sendTransaction({ from: "blah", to: "blah", value:20000000000000000, nonce:5, gas:120000, gasPrice:80000000000 })

I had a little panic attack afterward about whether I was supposed to put gasPrice in wei or gwei. I checked the documentation and it says wei, but nevertheless I'd like the peace of mind of a second set of eyes.

If one screws it up and puts in a ridiculously high gas price beyond the available funds, will that burn up all of what's remaining in the account before it fails?

2 Answers 2

1

You can use web3.toWei to easily convert amounts to wei from other units

eth.sendTransaction({
    from: "blah",
    to: "blah",
    value: web3.toWei(0.02, "ether"),
    nonce: 5,
    gas: 120000,
    gasPrice: web3.toWei(80, "gwei")
});

Also useful is web.fromWei to convert from wei to other units

> web3.fromWei("80000000000", "gwei");
"80"
> web3.fromWei("20000000000000000", "ether");
"0.02"
5
  • Thanks; this is very helpful! So my values were reasonable? i.e. My gasPrice was 80 gwei and I was sending 0.02 eth? What happens if you screw up and use a ballistic gas price either within or outside the means of your account balance? Does anything protect you from a typo with extra zeroes recklessly burning through all your account funds?
    – rkagerer
    Commented Sep 14, 2017 at 4:02
  • 1
    Gas price is a dynamic value, sometime during a popular ICO the cost goes up. You can use ethgasstation.info to determine the convenient price at given moment, ie how much time can you wait for a transaction to be mined vs the price you want to pay. A good wallet can calculate the total of a transaction, but otherwise if you have enough funds there's nothing that can save you if you put too many zeros in the gas price.
    – Ismael
    Commented Sep 14, 2017 at 7:41
  • Thanks that answers my question. Ethgasstation.info was down at the time so I just looked at gas prices of recent transactions on Etherscan.io and added a nice fat margin.
    – rkagerer
    Commented Sep 18, 2017 at 14:31
  • web3.utils.toWei("10", "ether"). (*web3 updates)
    – EHM
    Commented Mar 29, 2022 at 1:05
  • @EHM It is a 5 years old answer! It was for web3 v0.20.xx. I'd suggest to add an new answer with the updates for web3 v1.xx.
    – Ismael
    Commented Mar 29, 2022 at 1:27
0

Version updates

As of the current version of web3 (v1.xx) the toWei function has been moved to web3.utils, so instead of web3.toWei(0.02, "ether"), you want to do web3.utils.toWei(0.02, "ether"). For more, check documentation.

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.