0

I'm having accessing my smart contracts' setters from console using a simple HTML / JS website. I'm able to access public variables and getters, but when trying to use my setters from console it throws an "invalid address" error.

Some google'ing told most of the time the reason for this problem is not having set an default address, but I did this in my JS script (web3.eth.DefaultAccount = web3.eth.accounts[0];). Is there something else I've missed?

I'm using the Remix IDE for writing / publishing the smart contracts and connect to my locally hosted testRPC network. I'm trying to use my setter by entering Contract.setter("test") in the console.

error:

web3.min.js:2 Uncaught Error: invalid address
    at v (web3.min.js:2)
    at l (web3.min.js:2)
    at web3.min.js:2
    at Array.map (<anonymous>)
    at i.formatInput (web3.min.js:2)
    at i.toPayload (web3.min.js:2)
    at r.e [as sendTransaction] (web3.min.js:2)
    at c.sendTransaction (web3.min.js:2)
    at c.execute (web3.min.js:2)
    at <anonymous>:1:10

Smart contract:

pragma solidity ^0.4.4;

contract c {
    string test;

    function setter(string _s) {
        test = _s;
    }

    function  getTest() constant returns(string) {
        return(test);
    }
}

HTML / JS page:

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="Brightsend/brightsend/src/js/bootstrap.min.js"></script>
  <script src="Brightsend/brightsend/src/js/web3.min.js"></script>
  <script>


  var contractAddress = '0xeecc539002e9216665b485cd3182e187bb64dbec';
  var web3 = require('web3');

  //using testRPC & setting the default account to the first address provided by testRPC
  web3 = new Web3(new web3.providers.HttpProvider('http://127.0.0.1:8545'));
  web3.eth.DefaultAccount = web3.eth.accounts[0];
  console.log("default account: " + web3.eth.DefaultAccount);

  //contract data regarding all variables / functions
  var ContractAbi = web3.eth.contract([{"constant":false,"inputs":[{"name":"_s","type":"string"}],"name":"setter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getTest","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}])

  // Contract address to be able to communicate with it
  var Contract = ContractAbi.at(contractAddress);
  console.log(Contract);
  </script>
</head>
<body>
  <h1>testpage</h1>
</html>
0

Found the error. Since JS is case sensitive it didn't set defaultAccount because I used 'DefaultAccount'.

  • 1
    For housekeeping purposes, please accept your own answer :-) (There might be a delay before the site allows you to do so.) – Richard Horrocks Dec 1 '17 at 12:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.