PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 22, 2022

[FIXED] How do I accurately convert ETH to WEI when sending transaction?

 September 22, 2022     blockchain, ethereum, metamask     No comments   

Issue

I am trying to send ETH from one account to another but the conversion from ETH to WEI keeps giving me headaches. In this case, I am trying to send 0.11 ETH but in the confirmation window, I get 313.59464925 ETH instead.

// This is my transaction code

await window.ethereum
  .request({
    method: "eth_sendTransaction",
    params: [
        {
          from: window.ethereum.selectedAddress,
          to: "0x4dxxxxxxxxxxxxxxxxxx2dr9820C",
          value: String(0.11 * 1000000000000000000), // convert to WEI
          },
        ],
      })
  .then((result) => console.log(result))
  .catch((error) => console.log(error));

I have also tried using BigNumber but it doesn't solve the problem, I guess I'm messing something up. How do I accurately convert ETH to WEI?


Solution

i prefer using web3 utils to have cleaner code and prevent unexpected bugs so with that you can write this:

value: "0x" + Web3.utils.toBN(Web3.utils.toWei("0.11", "ether")).toString(16)


Answered By - mahdikmg
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing