1/*
2This file is part of the DAO.
3
4The DAO is free software: you can redistribute it and/or modify
5it under the terms of the GNU lesser General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9The DAO is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU lesser General Public License for more details.
13
14You should have received a copy of the GNU lesser General Public License
15along with the DAO.  If not, see <http://www.gnu.org/licenses/>.
16*/
17
18
19/*
20Basic, standardized Token contract with no "premine". Defines the functions to
21check token balances, send tokens, send tokens on behalf of a 3rd party and the
22corresponding approval process. Tokens need to be created by a derived
23contract (e.g. TokenCreation.sol).
24
25Thank you ConsenSys, this contract originated from:
26https://github.com/ConsenSys/Tokens/blob/master/Token_Contracts/contracts/Standard_Token.sol
27Which is itself based on the Ethereum standardized contract APIs:
28https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs
29*/
30
31/// @title Standard Token Contract.
32
33abstract contract TokenInterface {
34    mapping (address => uint256) balances;
35    mapping (address => mapping (address => uint256)) allowed;
36
37    /// Public variables of the token, all used for display
38    string public name;
39    string public symbol;
40    uint8 public decimals;
41    string public standard = 'Token 0.1';
42
43    /// Total amount of tokens
44    uint256 public totalSupply;
45
46    /// @param _owner The address from which the balance will be retrieved
47    /// @return balance The balance
48    function balanceOf(address _owner) public virtual view returns (uint256 balance);
49
50    /// @notice Send `_amount` tokens to `_to` from `msg.sender`
51    /// @param _to The address of the recipient
52    /// @param _amount The amount of tokens to be transferred
53    /// @return success Whether the transfer was successful or not
54    function transfer(address _to, uint256 _amount) public virtual returns (bool success);
55
56    /// @notice Send `_amount` tokens to `_to` from `_from` on the condition it
57    /// is approved by `_from`
58    /// @param _from The address of the origin of the transfer
59    /// @param _to The address of the recipient
60    /// @param _amount The amount of tokens to be transferred
61    /// @return success Whether the transfer was successful or not
62    function transferFrom(address _from, address _to, uint256 _amount) public
63virtual returns (bool success);
64
65    /// @notice `msg.sender` approves `_spender` to spend `_amount` tokens on
66    /// its behalf
67    /// @param _spender The address of the account able to transfer the tokens
68    /// @param _amount The amount of tokens to be approved for transfer
69    /// @return success Whether the approval was successful or not
70    function approve(address _spender, uint256 _amount) public virtual returns (bool success);
71
72    /// @param _owner The address of the account owning tokens
73    /// @param _spender The address of the account able to transfer the tokens
74    /// @return remaining Amount of remaining tokens of _owner that _spender is allowed
75    /// to spend
76    function allowance(
77        address _owner,
78        address _spender
79    ) public virtual view returns (uint256 remaining);
80
81    event Transfer(address indexed _from, address indexed _to, uint256 _amount);
82    event Approval(
83        address indexed _owner,
84        address indexed _spender,
85        uint256 _amount
86    );
87}
88
89abstract contract tokenRecipient {
90    function receiveApproval(address _from, uint256 _value, address _token, bytes memory _extraData) public virtual;
91}
92
93contract Token is TokenInterface {
94    // Protects users by preventing the execution of method calls that
95    // inadvertently also transferred ether
96    modifier noEther() {if (msg.value > 0) revert(); _; }
97
98    function balanceOf(address _owner) public override view returns (uint256 balance) {
99        return balances[_owner];
100    }
101
102    function transfer(address _to, uint256 _amount) public virtual override returns (bool success) {
103        if (balances[msg.sender] >= _amount && _amount > 0) {
104            balances[msg.sender] -= _amount;
105            balances[_to] += _amount;
106            emit Transfer(msg.sender, _to, _amount);
107            return true;
108        } else {
109           return false;
110        }
111    }
112
113    function transferFrom(
114        address _from,
115        address _to,
116        uint256 _amount
117    ) public virtual override returns (bool success) {
118
119        if (balances[_from] >= _amount
120            && allowed[_from][msg.sender] >= _amount
121            && _amount > 0) {
122
123            balances[_to] += _amount;
124            balances[_from] -= _amount;
125            allowed[_from][msg.sender] -= _amount;
126            emit Transfer(_from, _to, _amount);
127            return true;
128        } else {
129            return false;
130        }
131    }
132
133    function approve(address _spender, uint256 _amount) public override returns (bool success) {
134        allowed[msg.sender][_spender] = _amount;
135        emit Approval(msg.sender, _spender, _amount);
136        return true;
137    }
138
139    /// Allow another contract to spend some tokens in your behalf
140    function approveAndCall(address _spender, uint256 _value, bytes memory _extraData)
141        public returns (bool success) {
142        allowed[msg.sender][_spender] = _value;
143        tokenRecipient spender = tokenRecipient(_spender);
144        spender.receiveApproval(msg.sender, _value, address(this), _extraData);
145        return true;
146    }
147
148    function allowance(address _owner, address _spender) public override view returns (uint256 remaining) {
149        return allowed[_owner][_spender];
150    }
151}
152