1

I've this contract and web3js html GUI that's interacting with it. The contract is deployed in ganache at localhost 8545 and unable to write data into blockchain.

SimpleStorage.sol -

pragma solidity ^0.4.17;


contract SimpleStorage {   uint myVariable;   string myName;
     //mapping (myName => address) name;

  function set(uint x) public {
    myVariable = x;   }

  function get() constant public returns (uint) {
    return myVariable;   }
     function setName(string name) constant public  {
    myName = name;   }
     function getName() constant public returns (string) {
    return myName;   }    }

SimpleStorage.html -

<!doctype><html><head><script type="text/javascript" src="bignumber.js"></script><script type="text/javascript" src="web3.js"></script><script type="text/javascript">
    var Web3 = require('web3');
    var web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider("HTTP://127.0.0.1:8545")); 
var abi =[{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
var MyContract = web3.eth.contract(abi);
var myContractInstance = MyContract.at('0xfe9052ae2dbc665deb92a735c913e73510c50cdc');
console.log("Writing to block " + myContractInstance.set(1)); // This is what is not working. I know I'm doing something wrong here but couldn't figure out.
console.log("Getting data " + myContractInstance.get()); //This returns result 
document.getElementById("connectContractAdress").innerHTML = "Connected to Contract. Contract Owner is "+ myContractInstance.get();
</script></head><body><div id="connectContractAdress"></div></body></html>
  • Please add the error output, so that we can understand the problem better – Kaki Master Of Time Mar 29 '18 at 8:16
  • Uncaught Error: invalid address -- that's in chrome console. – Matheswaran Kanagarajan Mar 29 '18 at 9:13
  • you probably need to add a from attribute to your myContractInstance.set(1,{from:0x....}) or maybe you are wrong about your contract address, the one you are adding statically there. – Kaki Master Of Time Mar 29 '18 at 9:17
  • Yeh.. And also I need to unlock the account and use eth.sendTransaction() right? So, it goes something like 1. Unlock account 2. contract.GetFunction("set").sendTransaction(coinbase, etc.,.,).. Do you have any documentation on it? – Matheswaran Kanagarajan Mar 29 '18 at 10:15
  • I don't think you need to unlockAccount since ganache gives you 10 fully unlocked and funded accounts, so when sending any transaction you only need to use web3.eth.accounts[0] or 1. you won't change anything in your code, all you need to to is add the from attribute like i mentioned earlier like this : myContractInstance.set(1,{from:web3.eth.accounts[0],gas:300000}) – Kaki Master Of Time Mar 29 '18 at 11:29
0

Thank you @Med Mansour for guiding. Here is the code to call 'set' method in contract.

function setData() { 
    var MyContract = web3.eth.contract(abi);
    var myContractInstance = MyContract.at('0xfe9052ae2dbc665deb92a735c913e73510c50cdc');

    var unlockAccount = web3.personal.unlockAccount('0xfc2AcBF73016Fb3449837b4748a87e90E82c45C7','',300);
    strUnlockAccount = (unlockAccount == null )? "NULL": unlockAccount.toString();
    document.getElementById("setdatavalue").innerText = "Account unlock status = "+strUnlockAccount;

    var number = parseInt(document.getElementById("inputNumber").value);
    //console.log("Number " , number);
    var txHash = myContractInstance.set(number,{from:'0xfc2AcBF73016Fb3449837b4748a87e90E82c45C7', gas:'6721975', gasPrice:'0'});
    document.getElementById("setdatavalue").innerText = document.getElementById("setdatavalue").innerText + "\nTransaction has after setting value to " + number + "  = " + txHash;

    var blockNumber = web3.eth.getBlock('latest').number;
    //web3.eth.getBlockNumber(function(req,res) { blockNumber = res ; } );
    console.log("Block #" + blockNumber);
    document.getElementById("setdatavalue").innerText = document.getElementById("setdatavalue").innerText + "\nCurrent Block Number = "+ blockNumber;

    document.getElementById("blockHashDetails").innerText = JSON.stringify(web3.eth.getBlock(web3.eth.getBlock('latest').number));

    var myContractInstance = MyContract.at('0xfe9052ae2dbc665deb92a735c913e73510c50cdc');
    document.getElementById("setdatavalue").innerText = document.getElementById("setdatavalue").innerText + "\nGetting data value after change= " + myContractInstance.get();
}
| improve this answer | |

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.