Skip to content
Advertisement

Solidity Function Modifying Value Failing

Using Truffle Development framework (web3, react, and solidity) to test a simple smart contract. I have a basic function I am testing when I press a button on the UI, but it doesn’t seem to be working the way I’d expect it. I expect the contract to increment the value and return true. Note, that the value is later being displayed on the front-end as this is what I was playing with. Code is below:

Solidity contract

    uint total = 0;

    function setTotalValue() public returns(bool) {

    total = total + 2;
    return true;
}

App.js

async modifyTotal() {

fantasyInstance.setKittyValue.call().then(function(res) {
  console.log(res); //THIS RETURNS FALSE TO THE CONSOLE
});

For clarification, modiftyTotal is called when an HTML button is clicked. The console statement returns a value of “false” and the actual total value doesn’t seem to change ever. Any ideas?

Advertisement

Answer

Your JS code doesn’t match the contract code and you omitted the portion of code that retrieves the contact instance, but I’m going to guess your issue is you’re using call instead of sendTransaction. call executes the method in the local node without changing state. You should only use it for testing or constant functions. See the web3js documentation.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement