1

I have deployed a smart contract in the testnet using MetaMask and a solidity online compiler(injected web3):

pragma solidity ^0.4.7;
contract SimpleStorage {

  uint public storedData;
  string public name = "ahmed";
  event Print(uint);

  function SimpleStorage(uint initialValue) {
    storedData = initialValue;
  }

  function set(uint x) returns (uint){
    storedData = x;
    Print(storedData);
    return storedData;
  }

  function get() constant returns (uint retVal) {
    return storedData;
  }
}

I used the ropsten.infura.io provider to interact with the contract in the testnet I can call string "name" from console and it returns value successfully. But when i want to call the function "set" or "get", I got an error:

Error: Invalid JSON RPC response: ""

This is my js file to call function:

web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/ahmed_charef"));

contractAddress = "0x752fB8626c5bEf519EfC661F6694fbE497405FfE"
ABI  = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"uint256"}],"name":"Print","type":"event"}]

myContract = web3.eth.contract(ABI).at(contractAddress);

myContract.name(function(err, res){
    console.log(res); 
  })

function voteForCand() {

  myContract.set("20");
}

What could be the issue here?

1

Calling a function that is not a constant function requires gas for execution, so you need to provide an unlocked from address to invoke this call.

For example:

myContract.set('20', { from: web3.eth.accounts[0] })

I just tried this and it works: txHash

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.