2

Working with a simple contract to get the hang of this. I can't seem to get the correct value back from my contract function. It always outputs 0.

Truffle version: 4.1.15

Contract:

pragma solidity 0.4.24;

contract HelloWorld {
    uint public balance;

    constructor() public {
        balance = 1000;
    }

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

    function get() constant public returns (uint) {
        return balance;
    }
}

Truffle commands:

const hw = HelloWorld.at("0x90BB90BA04612eb9B30A2640BC7516168A684cE7");
undefined
hw.deposit(500).then(function(balance){n = balance.toNumber(10); console.log(n);})
0
undefined
hw.get().then(function(balance){n = balance.toNumber(10); console.log(n);})
0
undefined

Both deposit() and get() seem to return 0 no matter what. This should at least return the initial value of balance (1000), right?

  • You're probably going too fast and the transaction isn't mined. When scripting this sort of thing it's important to wait for confirmation of the transaction before pressing on. – Rob Hitchens - B9lab Feb 17 at 23:20
  • Shouldn't this still return 1000 (the default value) at least? @RobHitchensB9lab – SQueryL Feb 18 at 0:49
  • On closer look, yes it should. Try toString (10) instead of toNumber. – Rob Hitchens - B9lab Feb 18 at 1:36
  • No dice. Still returns 0, but as a string (text is a slightly different color than integers in console). @RobHitchensB9lab – SQueryL Feb 18 at 4:21
  • truffle version? – Aniket Feb 18 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.