I have an extremely basic contract deployed on Ropsten:
pragma solidity ^0.4.16;
contract hypergrowth {
uint256 public someNumber = 5;
function hypergrowth() public {}
function changeNumber(uint256 number) public {
someNumber = number;
}
function getNumber() public constant returns (uint256 someNumber) {
return someNumber;
}
}
I use web3js and Metamask to interact with the deployed contract.
I can initialize web3. I can get the primary account from Metamask. But I always get 0 when I call getNumber(). Here are the functions that I use with web3:
function getNumberFromContract(contract) {
contract.getNumber( (err, res) => {
console.log(res.toNumber());
});
}
function setNumberFromContract(contract) {
contract.changeNumber(10, function(err,res){
console.log("done");
});
}
I don't get why I receive 0...I can't even see if setNumber() works.
What do I do wrong?
UPDATE:
I use this code to wait for the getNumber function to execute:
var deferred = new $.Deferred();
setTimeout(function () {
deferred.resolve(getNumberFromContract(hyperContract));
}, 5000);
And still get 0.
UPDATE 2:
This is how I get the contract:
var address = '0xbabd1c95906d4ebec8977a7d0f32410ecba1103f';
var abi = [
{
"constant": true,
"inputs": [],
"name": "getNumber",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "number",
"type": "uint256"
}
],
"name": "changeNumber",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
];
var hyperContract = getContract(abi, web3.eth.accounts[0], web3);
The getContract
function is this:
function getContract(ABI, address, web3) {
return web3.eth.contract(ABI).at(address);
}