发送交易时如何准确地将ETH转换为WEI?

2023-12-31

我正在尝试发送ETH从一个帐户到另一个帐户,但转换ETH to WEI一直让我头疼。在这种情况下,我正在尝试发送0.11 ETH但在确认窗口中,我得到313.59464925 ETH反而。

// 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));

我也尝试过使用大数 https://github.com/indutny/bn.js/但这并不能解决问题,我想我把事情搞砸了。如何准确转换ETH to WEI?


我更喜欢使用web3 实用程序 https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#拥有更清晰的代码并防止意外错误,这样您就可以编写以下内容:

value: "0x" + Web3.utils.toBN(Web3.utils.toWei("0.11", "ether")).toString(16)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

发送交易时如何准确地将ETH转换为WEI? 的相关文章

随机推荐