首页 > 解决方案 > 将结果形成元掩码

问题描述

我正在尝试使用来自表单的输入来创建元掩码交易的详细信息,获取表单地址和以太币数量,然后使用元掩码调用它们。当使用地址和数量进行硬编码时,元掩码代码可以工作。不知道我哪里错了。

运行 npx serve 因为元掩码可能很棘手。

谢谢您的帮助!

编辑 - 尝试了一些新东西,但仍然无法正常工作

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
  <div>
    <input onclick="fill_amount" id="amount" type="number" placeholder="eth">
    <input onclick="fill_address" id="address" placeholder="address">
    <button class="pay-button">Pay</button>
    <div id="status"></div>
  </div>
  <script type="text/javascript">
    window.addEventListener('load', async () => {
      if (window.ethereum) {
        window.web3 = new Web3(ethereum);
        try {
          await ethereum.enable();
          initPayButton()
        } catch (err) {
          $('#status').html('User denied account access', err)
        }
      } else if (window.web3) {
        window.web3 = new Web3(web3.currentProvider)
        initPayButton()
      } else {
        $('#status').html('No Metamask (or other Web3 Provider) installed')
      }
    })

    const initPayButton = () => {
      $('.pay-button').click(() => {
        // paymentAddress is where funds will be send to
        var paymentAddress = document.getElementById("address").innerHTML
        var amountEth = document.getElementById("amount").innerHTML

        web3.eth.sendTransaction({
          to: paymentAddress,
          value: web3.toWei(amountEth, 'ether')
        }, (err, transactionId) => {
          if  (err) {
            console.log('Payment failed', err)
            $('#status').html('Payment failed')
          } else {
            console.log('Payment successful', transactionId)
            $('#status').html('Payment successful')
          }
        })
      })
    }

    function fill_amount() {
            var amountEth = document.getElementById("amount").innerHTML
        }

        function fill_address() {
            var paymentAddress = document.getElementById("address").innerHTML
        }

  </script>
</body>
</html>

标签: ethereumweb3cryptocurrencymetamask

解决方案


在您的 fill_amount 和 fill_address 函数中,您使用的是相同的付款地址变量。我相信在 fill_amount 函数中,变量应该是 amountEth 而不是 paymentAddress。


推荐阅读