0

I'm deploying via Remix and Metamask.

I'm using this SafeMath function, which seems to be preventing my contract from initializing all its state variables (contract deploys, but all vars are zero):

function add(uint256 a, uint256 b) internal constant returns (uint256) {
  uint256 c = a + b;
  assert(c >= a);
  return c;
}

The library function seems to be causing the problem in the following function. To be clear, the deposit function below isn't even being called during deployment. But when I deploy the contract with the FIRST line in the if-block enabled (which uses the .add SafeMath formula), the contract deploys but none of the state variables are set.

Everything works fine when I use the second line.

Am I using the SafeMath library incorrectly?

function deposit(address addr) payable {

  uint256 amt = msg.value;

  uint alreadyDeposited = deposits[addr]; // deposits is an address => uint mapping

  if (alreadyDeposited > 0) {
    // deposits[addr] = alreadyDeposited.add(amt); // When this line is enabled, my contract doesn't initialize (for example, "ownerWallet", which should be msg.sender, is not set when the contract deploys).
    deposits[addr] = alreadyDeposited + amt; // this line lets contract vars initialize appropriately.
  }

  sendFunds();
}

PS can someone explain how add knows how to use itself as the first parameter?

1

I think this error comes from your if code, when calling deposite function, the value of address deposite is zero, because there is no way initializes value of address. And i remove if condition, everything is OK. This is my test code, as follows:

pragma solidity ^0.4.13;

import "./SafeMath.sol";

contract Test{

    using SafeMath for uint;
    mapping (address => uint) deposits;

    function deposit(address addr) payable {
        uint256 amt = msg.value;
        uint alreadyDeposited = deposits[addr]; // deposits is an address => uint mapping
        // if (alreadyDeposited > 0) {
            deposits[addr] =  alreadyDeposited.add(amt);// When this line is enabled, my contract doesn't initialize (for example, "ownerWallet", which should be msg.sender, is not set when the contract deploys).
            // deposits[addr] = alreadyDeposited + amt; // this line lets contract vars initialize appropriately.
        // }
        //sendFunds();
    }

    function getBalance(address _addr) constant returns (uint){
        return deposits[_addr];
    }

}

Hope it helps~

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.