我试图将ETH从一个帐户发送到另一个帐户,但是从ETH到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));我也尝试过使用BigNumber,但它不能解决问题,我想我是搞砸了。如何准确地将ETH转换为WEI
发布于 2022-02-02 13:33:59
我更喜欢使用web3实用程序来获得更干净的代码和防止意外的bug,这样您就可以编写如下代码:
value: "0x" + Web3.utils.toBN(Web3.utils.toWei("0.11", "ether")).toString(16)发布于 2022-08-15 23:30:58
您正在使用ethers.js和硬帽吗?如果是,则使用ethers.utils将以太转换为卫:
const { ethers } = require("hardhat");
let ethersToWei = ethers.utils.parseUnits(1.toString(), "ether");以上代码将1醚转换为等效的魏。
发布于 2022-09-16 07:28:40
在ES6中使用节点模块醚,您可以按以下方式将Wei转换成以太:
import { ethers } from "ethers";
const WeiToEther = ethers.utils.formatEther(weiValue)
按如下方式将醚转换为Wei:
import { ethers } from "ethers";
const EtherToWei = ethers.utils.parseUnits("0.11","ether")
和将以太传递给您的函数:
contract.yourFunction({
value: ethers.utils.parseUnits("0.11","ether")
});
https://stackoverflow.com/questions/70955495
复制相似问题