0

I just wrote a simple code to test openzeplin Safemath library. I am using the latest version of remix ide and compiling for ^0.5.0. Remix is using 0.5.0_commit.1d4f565a compiler The environment is JavaScript VM EVM Version is the compiler default

The add function does not seem to be working in the code given below

I have tried x.sub(1) it throws an exception as expected, i have also tried initializing x to different values but still does not work.

pragma solidity ^0.5.0;
import "./SafeMath.sol";

contract SimpleStorage {
    using SafeMath for uint;
    uint x;

    event incremented(uint x);

    constructor() public{
        x=0;
    }

    function increment() public {
        x.add(1);
        emit incremented(x);
    }

    function get() external view returns (uint) {
        return x;
    }
}

Expected output is an increment by one on every call to the function but getting the same value every time. Emit also shows the same value.

Ferit
  • 8,692
  • 8
  • 34
  • 59
Zaid Munir
  • 19
  • 3

1 Answers1

0

Well, it's your bug :)

Instead of x.add(1) try x = x.add(1). Add function is not inplace, new value is returned and you need to assign the new value to x.

Ferit
  • 8,692
  • 8
  • 34
  • 59