TransWikia.com

I'm trying to use web3js to return the value of a variable in a contract but I'm told web3.eth.watch isn't a function, anyone know a better method?

Ethereum Asked on January 3, 2022

 <!DOCTYPE html>
<html>
<center>
  <head>
    <meta charset="utf-8">
    <title>Mediachain</title>
  </head>
  <body background="blockchain_background.jpeg">
    <h1>Mediachain</h1>
    <h2 id="Return"></h2>
    <label for="name" class="col-lg-2 control-label">Full name</label>
    <input id="Name" type="text">
    <label for="name" class="col-lg-2 control-label">Your facebook password</label>
    <input id="Facebookpasswd" type="text"></input>
    <button id="set1"type="button" name="button">Enter</button>
    <label for="name" class="col-lg-2 control-label">Your linkedin password</label>
    <input id="Linkedinpasswd" type="text">
    <button id="set2"type="button" name="button">Enter</button>
    <label for="name" class="col-lg-2 control-label">Your instagram password</label>
    <input id="Instagrampasswd" type="text">
    <button id="set3"type="button" name="button">Enter</button>
    <input type="file" name="product-image" class="form-control" id="newFile" required="required">
    <button id="setSomething"type="button" name="button">Store file</button>
    <button id="returnFile"type="button" name="button">Retreive</button>
    <textarea id="showContract"name="name" rows="8" cols="80"></textarea>
    <style media="screen">
       label {
         color: white;
         font-size: 100%
         text-align:center;
       }
       input {
          padding:10px;
          width: 50%;
          margin-bottom: 1em;
       }
       h1 {
         font-size: 200%;
         font-weight: bold;
         color: white;
       }
       button {
          background-color: green;
          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"));
  }

  web3.eth.defaultAccount = web3.eth.accounts[0];

  var mediachainContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_instagramPassword","type":"bytes32"}],"name":"setSocialMedia3","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"userCount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_linkedinPassword","type":"bytes32"}],"name":"setSocialMedia2","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_newFileContent","type":"bytes32"}],"name":"setFile","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"user","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getAccounts","outputs":[{"name":"facebookPassword","type":"bytes32"},{"name":"linkedinPassword","type":"bytes32"},{"name":"instagramPassoword","type":"bytes32"},{"name":"fileContent","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_facebookPassword","type":"bytes32"}],"name":"setSocialMedia","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);

  var mediachain = mediachainContract.at('0x5d8e0ed3e15468c94fe17fbd215756a78d40e857');
  console.log(mediachain);
  var badPassword = $("#Name").val();
  $("#set1").click(function() {
    mediachain.setSocialMedia($("#Facebookpasswd").val());
    var safePassword = $("#Facebookpasswd").val();
    if (safePassword.length <= 10) {
      alert("Password doesn't meet minimum requirements");
    }
    var goodPassword = [safePassword];
    if (goodPassword.includes("1234567890") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword.includes("abcdefghij") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword.includes("ABCDEFGHIJ") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword.includes("Password") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword.includes(badPassword) == true) {
      alert("Password doesn't meet minimum requirements");
    }
  });
  $("#set2").click(function() {
    mediachain.setSocialMedia2($("#Linkedinpasswd").val());
    var safePassword2 = $("#Linkedinpasswd").val();
    if (safePassword2.length <= 10) {
      alert("Password doesn't meet minimum requirements");
    }
    var goodPassword2 = [safePassword2];
    if (goodPassword2.includes("1234567890") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword2.includes("abcdefghij") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword2.includes("ABCDEFGHIJ") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword2.includes("Password") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword2.includes(badPassword) == true) {
      alert("Password doesn't meet minimum requirements");
    }
  });
  $("#set3").click(function() {
    mediachain.setSocialMedia3($("#Instagrampasswd").val());
    var safePassword3 = $("#Instagrampasswd").val();
    if (safePassword3.length <= 10) {
      alert("Password doesn't meet minimum requirements");
    }
    var goodPassword3 = [safePassword3];
    if (goodPassword3.includes("1234567890") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword3.includes("abcdefghij") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword3.includes("ABCDEFGHIJ") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword3.includes("Password") == true) {
      alert("Password doesn't meet minimum requirements");
    }
    if (goodPassword3.includes(badPassword) == true) {
      alert("Password doesn't meet minimum requirements");
    }
  });
  $("#newFile").change(function(event) {
    const file = event.target.files[0]
    reader = new window.FileReader()
    reader.readAsArrayBuffer(file)
  });
  $("#setSomething").click(function() {
    mediachain.setFile($("#newFile").val());
  });
  $("#returnFile").click(function() {
    var contractString = web3.eth.getStorageAt('0x5d8e0ed3e15468c94fe17fbd215756a78d40e857', 0).then(console.log);
    document.getElementById("#showContract").innerText = contractString;
  });
 var mediachain = mediachainContract.new(
  {
    from: web3.eth.accounts[0],
    data: '0x6060604052341561000f57600080fd5b6000809054906101000a9050506103258061002b6000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063017791bc1461008857806307973ccf146100af57806312d101d1146100c457806340fadae2146100eb5780634f8632ba146101125780638a48ac0314610167578063c8d5ff36146101c5575b600080fd5b341561009357600080fd5b6100ad6004808035600019169060200190919050506101ec565b005b34156100ba57600080fd5b6100c26101ef565b005b34156100cf57600080fd5b6100e9600480803560001916906020019091905050610258565b005b34156100f657600080fd5b61011060048080356000191690602001909190505061025b565b005b341561011d57600080fd5b61012561025e565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561017257600080fd5b61017a610283565b60405180856000191660001916815260200184600019166000191681526020018360001916600019168152602001826000191660001916815260200194505050505060405180910390f35b34156101d057600080fd5b6101ea6004808035600019169060200190919050506102f6565b005b50565b3373ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614156102565760016005600082825401925050819055505b565b50565b50565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000806000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102e457600080fd5b83838383935093509350935090919293565b505600a165627a7a72305820ab6b1ae2a64c6f9e1dc4cf3775fe39dc8e3d5d0b46d5ca807abca662e0b472f20029',
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
       console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
 });
</script>

One Answer

Your post is pretty illegible and poorly drafted. If you want a quality answer you should spend the time to craft a proper post. You do not clearly define your question at all, and your code is half in a comment and half in a code tag.

Please edit your post and clearly and concisely pose your inquiry and move all the code into a singular code tag.

Your code also does not show watching for any event, and you don't include the smart contract code so there is not enough information to answer.

However, I can tell you that web3.eth.watch is Not a function, so of course it isn't going to work. The syntax is contractobject.eventname.watch(...)

mediachainContract.eventName.watch(function(error, result){ ... });

This however is not the solution for you. You have no events in your contract ABI so you probably don't want to read from events at all. You likely want to return the response from your constant functions and this can be done with a simple call():

mediachainContract.getAccounts(function(error, result){ // result contains result of getAccounts() ;P });

Keep in mind that in order to be accessible with Calls, they need to be marked constant. Also be aware your ABI is full of spelling errors like "instagramPassoword".

In the future do not expect people to be able to deduce even what you are asking, let alone provide an answer with such a poorly crafted post.

Answered by Dan Conway on January 3, 2022

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