1
pragma solidity 0.5.1;

contract basics{
    using SafeMath for *;
    uint time;
    address payable sender;
    uint amount;
    event moneySent(address s,uint a);

    mapping(address=>person) public accounts;

    struct person {
        uint time;
        uint vault;
    }        

    function sendToContract() public payable{// isWithinLimits(msg.value){
        accounts[msg.sender].vault=accounts[msg.sender].vault.add(msg.value);
        accounts[msg.sender].time=now;
    }

    function takeFromContract() public payable{
        if(now>=accounts[msg.sender].time+10 seconds){
            msg.sender.transfer(accounts[msg.sender].vault);
            accounts[msg.sender].time=now;
        }           
    }
}

/**
 * @title SafeMath v0.1.9
 * @dev Math operations with safety checks that throw on error
 * change notes:  original SafeMath library from OpenZeppelin modified by Inventor
 * - added sqrt
 * - added sq
 * - added pwr
 * - changed asserts to requires with error log outputs
 * - removed div, its useless
 */
library SafeMath {

    /**
    * @dev Multiplies two numbers, throws on overflow.
    */
    function mul(uint256 a, uint256 b)
        internal
        pure
        returns (uint256 c)
    {
        if (a == 0) {
            return 0;
        }
        c = a * b;
        require(c / a == b, "SafeMath mul failed");
        return c;
    }

    /**
    * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
    */
    function sub(uint256 a, uint256 b)
        internal
        pure
        returns (uint256)
    {
        require(b <= a, "SafeMath sub failed");
        return a - b;
    }

    /**
    * @dev Adds two numbers, throws on overflow.
    */
    function add(uint256 a, uint256 b)
        internal
        pure
        returns (uint256 c)
    {
        c = a + b;
        require(c >= a, "SafeMath add failed");
        return c;
    }

    /**
     * @dev gives square root of given x.
     */
    function sqrt(uint256 x)
        internal
        pure
        returns (uint256 y)
    {
        uint256 z = ((add(x,1)) / 2);
        y = x;
        while (z < y)
        {
            y = z;
            z = ((add((x / z),z)) / 2);
        }
    }

    /**
     * @dev gives square. multiplies x by x
     */
    function sq(uint256 x)
        internal
        pure
        returns (uint256)
    {
        return (mul(x,x));
    }

    /**
     * @dev x to the power of y
     */
    function pwr(uint256 x, uint256 y)
        internal
        pure
        returns (uint256)
    {
        if (x==0)
            return (0);
        else if (y==0)
            return (1);
        else
        {
            uint256 z = x;
            for (uint256 i=1; i < y; i++)
                z = mul(z,x);
            return (z);
        }
    }
}

given this code, when run and tested, if send 10,000 eth from wallet containing 99 eth the wallet will show that it now has 99,900n eth and when 10,000 is sent again it reverts back to 99 eth in wallet. Could this be an underflow or overflow error?

0

No.

You're not subtracting from the msg.sender account balance in the withdrawal phase. SafeMath will prevent underflow so accounts can't overdraw, if you do that.

The problem, I think, depends on your testing methodology.

if send 10,000 eth from wallet containing 99 eth the wallet will show that it now has 99,900n eth and when 10,000 is sent again it reverts

I don't think that's accurate. You can't send 10,000 eth from a wallet that only has 99 eth. No possible. You're probably sending 10,000 wei, or some other smaller unit.

I imagine the real likely problem is you're asking the wallet (send) or the contract (withdraw) to send more funds than it actually has, also not possible, so it reverts.

There is no known way to craft a contract that violates the rule that no one can send funds they don't actually have.

Hope it helps.

| improve this answer | |

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.