【问题标题】:(MetaMask - Javascript) Problem with value format for sendTransaction(MetaMask - Javascript)sendTransaction 的值格式问题
【发布时间】:2021-03-12 13:17:29
【问题描述】:

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>

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

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

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

我在网上找到了这个值,但是我找不到解决这个问题的方法,谢谢大家!

【问题讨论】:

    标签: javascript transactions ethereum web3 metamask


    【解决方案1】:

    对我有用的是

    const amount = web3.utils.toWei('1', 'ether');
    const value = web3.utils.toHex(amount);
    

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

    发送value,您将看到 1 ETH。

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 2021-11-30
      • 2018-07-06
      • 1970-01-01
      • 2011-09-09
      • 2013-07-03
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多