TransWikia.com

I'm trying to create an app that allows people to exchange ether between themselves but I have the error: "web3.eth.transact is not a function"

Ethereum Asked by user34915 on December 30, 2021

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <center>
    <title>dEnergy</title>
  </head>
  <body>
    <label for="name" class="col-lg-2 control-label">Power you generated</label>
    <input id="Power_generated" type="text">
    <label for="name" class="col-lg-2 control-label">Trade ether</label>
    <input id="value" type="text" value="Enter amount">
    <input id="recipient"type="text" name="" value="Enter recipient">
    <button id="etherSend"type="button" name="button" value="Send"></button>
    <button id="setYourPower"type="button" name="button"></button>
    <a href="browse.html" class="btn btn-primary">Browse</a>
    <style media="screen">
      setYourPower {
        color: green;
        width: inherit;
      }
      button {
         background-color: blue;
         font-weight: 500;
         font-size: 14px;
         border: none;
         font-style: normal;
         letter-spacing: 1px;
         margin: .4rem .8rem;
         white-space: normal;
         -webkit-transition: all 0.3s ease-in-out;
         -moz-transition: all 0.3s ease-in-out;
         transition: all 0.3s ease-in-out;
         padding: 1rem 3rem;
         border-radius: 3px;
         display: inline-flex;
         align-items: center;
         justify-content: center;
         word-break: break-word;
      }
    </style>
    <script type="text/javascript"src="file:///Users/meetagandhi/Documents/Mediachain/node_modules/web3/dist/web3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
      if (typeof web3 !== 'undefined') {
          web3 = new Web3(web3.currentProvider);
      } else {
          web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
      }
      var dEnergyContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"client","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"etherreceiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"fundtransfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"getPower","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_powerGenerated","type":"uint256"}],"name":"setPower","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"consumer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"marketPower","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"receiver","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"}]);

      var dEnergy = dEnergyContract.at('0x2becbb6ab187749487053ed21cd8ff3a28c51f80');
      $(document).ready(function() {
        alert("Welcome to dEnergy, a platform for energy trading without the need of a middleman");
      });
      $("#setYourPower").click(function() {
        dEnergy.setPower($("#Power_generated").val());
      });
      $("#etherSend").click(function sendEther() {
        web3.eth.transact({to: document.querySelector('#recipient').value ,value: document.querySelector('#value').value});
      });
    </script>
  </body>
  </center>
</html>

2 Answers

No such function. use 'sendTransaction' method for sending ether to an account.

$("#etherSend").click(function sendEther() {
        web3.eth.sendTransaction({to: document.querySelector('#recipient').value ,value: document.querySelector('#value').value});
      });

if you are using metamask you should use callback function for async methods so your code may change as follows

$("#etherSend").click(function sendEther() {
            web3.eth.sendTransaction({to: document.querySelector('#recipient').value ,value: document.querySelector('#value').value},(err,result)=>{
     if(result) {
        //success code
        console.log("this is the tx hash:"+result)
      }
     else {
       //failure code
       console.log("transaction failed")   
 }
});
          });

Hope this may help.

Answered by Akash on December 30, 2021

The error message is accurate. web3.js doesn't have a function by that name.

Are you looking for web3.eth.sendTransaction?

Answered by user19510 on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP