Truffle Function Call – How to Call a Function from Truffle

testrpctruffle

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.

Best Answer

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);
      });
Related Topic