sendTransaction 的值格式有问题

Gia*_*ash 1 javascript transactions ethereum web3js metamask

我对 MetaMask 中的交易有一个关于值格式的问题,我将代码放在下面:

<script type="text/javascript">

    const sendEthButton = document.querySelector('.sendEthButton');

    sendEthButton.addEventListener('click', () => {
      ethereum
        .request({
          method: 'eth_sendTransaction',
          params: [
            {
              from: ethereum.selectedAddress,
              to: '{{contractCreator}}',
              value: '0x29a2241af62c0000',
            },
          ],
        })
        .then((txHash) => console.log(txHash))
        .catch((error) => console.error);
    });

</script>
Run Code Online (Sandbox Code Playgroud)

我已将 MetaMask 正确连接到网站和 Ganache,当我按下页面中的按钮时,交易也会出现在插件中,但只有在代码中使用此值时,我才能看到正确的值 (3),但我不知道如何是格式化。

我尝试过 web3.toWei(number, 'ether') 和 web3.toHex(number) 但它显示了奇怪的值。

例如,如果我用 web3.toWei(1, ether) 替换该值,它会在 MetaMask 窗口中显示 4722.366483。

edd*_*yoc 5

对我有用的是

const amount = web3.utils.toWei('1', 'ether');
const value = web3.utils.toHex(amount);
Run Code Online (Sandbox Code Playgroud)

当amount作为字符串发送时,您将在 Metamask 中看到 4722.366483 ETH。

发送value后您将看到 1 ETH。