0

I am a newbie and try to learn to code web3 interfaces to call basic smart contract funtions. For now I made a button - when clicked it send 1 ETH to a specific (static) address. This worked fine... Now I try to do the same but this time I want an input field to let the ETH be dynamic... I did not find any template for a "Presale-Website" or a "Payment Website with dynamic value input" ... So I tried to hardcode it. But whatever I try, either it doesnt work AT ALL or the button works but will always try to send 0 ETH, no matter what input-value i type into the input-field...

Any help (a link to a working template, any hint, any tutorial etc.) is welcome...

Here is my code:

    <!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">
       <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
        const paymentAddress = '0x0000000000000000000000000000000000000000'
        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
        }

        

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.