pragma solidity ^0.4.21; /** * @title SafeMath * @dev Math operations with safety checks that throw on error */ 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; assert(c / a == b); return c; } /** * @dev Integer division of two numbers, truncating the quotient. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 // uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return a / b; } /** * @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) { assert(b <= a); return a - b; } /** * @dev Adds two numbers, throws on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256 c) { c = a + b; assert(c >= a); return c; } } contract Accounts { using SafeMath for uint256; event Logging(address sender, uint _timestamp, uint _amount, bool _state); struct Book { uint timestamp; uint amount; bool state; } mapping (address => Book[]) account; function Deposit() public payable returns(uint) { require(msg.value > 0); account[msg.sender].push(Book(now, msg.value, true)); } function ClaimAll(uint _deposit_id) public { require(account[msg.sender][_deposit_id].amount > 0); uint claimValue = account[msg.sender][_deposit_id].amount; account[msg.sender][_deposit_id].amount = 0; require(msg.sender.send(claimValue)); } // Would not work with withdraw! function ClaimPartial(uint _deposit_id, uint _amount) public { require(account[msg.sender][_deposit_id].amount > _amount); account[msg.sender][_deposit_id].amount -= _amount; require(msg.sender.send(_amount)); } function readBook(uint _deposit_id) public view { Book memory _book = account[msg.sender][_deposit_id]; uint book_timestamp = _book.timestamp; uint book_amount = _book.amount; bool book_state = _book.state; emit Logging(msg.sender, book_timestamp, book_amount, book_state); } function countBook() public view returns(uint) { uint count = account[msg.sender].length; return count; } function totalDepositAmount() public view returns(uint256) { return address(this).balance; } }
0.4.21