0

I'm new to ethereum. I followed a tutorial and I ended up with a contract that looks like this

pragma solidity ^0.4.15;
contract HelloWorld {
  uint public balance;

  function HelloWorld()  {
      balance = 1000;
  }

  function deposit(uint _value)  returns (uint _newValue) {
      balance += _value;
      return balance;
  }
}

After the contract is deployed at testrpc I try to call the deposit function with respect to the recent changes in syntax like so:

var hw = HelloWorld.deployed();
hw.then(function(i){
 return i.deposit(500)
})
.then(function(r){
  console.log(r)
})

but I get a Error: VM Exception while processing transaction: invalid opcode How do I call this function?

Thanks.

1

You should do it like this:

this.HelloWorld
      .deployed()
      .then(instance => {
        return instance.deposit(500,{
          from: this.account
        });
      })
      .then(result => {
        console.log(result);
      })
      .catch(e => {
        console.log(e);
      });
  • Thanks Pablo! However, if I do it like that I get 'Error: invalid address'. If I replace this.account with web3.eth.accounts[0] i get 'invalid opcode' again. – vlad Oct 14 '17 at 13:58
  • Yes, sorry, I assumed you had assigned this.account to web3.eth.accounts[0]. Then, there must be a problem with the contract. Is that all the code there is to it? – pabloruiz55 Oct 14 '17 at 14:04
  • That's all the code. I just discovered ether does get subtracted from the account, even though there's that error msg (opcode) – vlad Oct 14 '17 at 14:13
  • 2
    ok, it was stupid. I just restarted testrpc, recompiled and migrated the contract again, and now it works. – vlad Oct 14 '17 at 15:27

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.