0

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);
}
0

3 Answers 3

1

It seems to me that you invoke your function "getContract" with an EOA address and not the contract address !

change :

var hyperContract = getContract(abi, web3.eth.accounts[0], web3);

by :

var hyperContract = getContract(abi, address, web3); // (As address var is defined above)
0

You need to wait for the transaction to be mined.

This line:

console.log("done");

should more accurately read:

console.log("sent");

You could output the transaction receipt for giggles (res). The next step is check periodically to see if the transaction is mined. Once it's mined/confirmed, you can go ahead with your getter and based on just eyeballing the code, it looks like it will work.

Here's a gist that shows a way to wait for confirmation before proceeding with the next step.

https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6

Hope it helps.

1
  • I tried with a deffered object and wait 5 seconds. Still doesn't work. I updated my question with the code
    – Steve
    Dec 24, 2017 at 13:16
0

Another problem with your function is the return variable someNumber is shadowing the global variable with the same name someNumber.

uint256 public someNumber = 5;

// ..

function getNumber() public constant returns (uint256 someNumber) {
    return someNumber;    //   <---  Here someNumber is 0
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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