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 account { mapping(uint => Book[]) books; } struct Book { uint timestamp; uint amount; bool state; } mapping (address => account) accounts; function readBook(uint _deposit_id, uint _index) public { Book storage _books = accounts[msg.sender].books[_deposit_id][_index]; uint book_timestamp = _books.timestamp; uint book_amount = _books.amount; bool book_state = _books.state; emit Logging(msg.sender, book_timestamp, book_amount, book_state); } function deposit(uint _deposit_id) public payable { require(msg.value > 0); accounts[msg.sender].books[_deposit_id].push(Book(now, msg.value, true)); } function withdraw1(uint _deposit_id, uint _amount) public { uint id_income = 0; uint id_outcome = 0; for (uint i = 0; i <= accounts[msg.sender].books[_deposit_id].length; i++) { if (accounts[msg.sender].books[_deposit_id][i].state == true) { id_income += accounts[msg.sender].books[_deposit_id][i].amount; } if (accounts[msg.sender].books[_deposit_id][i].state == false) { id_outcome -= accounts[msg.sender].books[_deposit_id][i].amount; } } uint id_balance = id_income - id_outcome; require(id_balance >= _amount); accounts[msg.sender].books[_deposit_id].push(Book(now, _amount, false)); require(msg.sender.send(_amount)); } function withdrawAll() public { msg.sender.transfer(address(this).balance); } function withdraw2(uint _deposit_id, uint _amount) payable public { uint id_balance = 0; for (uint i = 0; i <= accounts[msg.sender].books[_deposit_id].length; i++) { if (accounts[msg.sender].books[_deposit_id][i].state == true) { id_balance += accounts[msg.sender].books[_deposit_id][i].amount; } if (accounts[msg.sender].books[_deposit_id][i].state == false) { id_balance -= accounts[msg.sender].books[_deposit_id][i].amount; } } require(id_balance >= _amount); accounts[msg.sender].books[_deposit_id].push(Book(now, _amount, false)); msg.sender.transfer(_amount); } function totalDepositAmount() public view returns(uint256) { return address(this).balance; } }
0.4.21