|
| 1 | +pragma solidity ^0.4.18; |
| 2 | + |
| 3 | +import './interfaces/IERC20.sol'; |
| 4 | +import './SafeMath.sol'; |
| 5 | + |
| 6 | +/* |
| 7 | +Copyright (c) 2016 Smart Contract Solutions, Inc. |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining |
| 10 | +a copy of this software and associated documentation files (the |
| 11 | +"Software"), to deal in the Software without restriction, including |
| 12 | +without limitation the rights to use, copy, modify, merge, publish, |
| 13 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 14 | +permit persons to whom the Software is furnished to do so, subject to |
| 15 | +the following conditions: |
| 16 | +
|
| 17 | +The above copyright notice and this permission notice shall be included |
| 18 | +in all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 21 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 23 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 24 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 25 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 26 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +*/ |
| 28 | + |
| 29 | +/** |
| 30 | + * @title Standard ERC20 token |
| 31 | + * |
| 32 | + * @dev Implementation of the basic standard token. |
| 33 | + * @dev https://github.com/ethereum/EIPs/issues/20 |
| 34 | + */ |
| 35 | +contract PolyToken is IERC20 { |
| 36 | + using SafeMath for uint256; |
| 37 | + |
| 38 | + // Poly Token parameters |
| 39 | + string public name = 'Polymath'; |
| 40 | + string public symbol = 'POLY'; |
| 41 | + uint8 public constant decimals = 18; |
| 42 | + uint256 public constant decimalFactor = 10 ** uint256(decimals); |
| 43 | + uint256 public constant totalSupply = 1000000000 * decimalFactor; |
| 44 | + mapping (address => uint256) balances; |
| 45 | + mapping (address => mapping (address => uint256)) internal allowed; |
| 46 | + |
| 47 | + event Transfer(address indexed from, address indexed to, uint256 value); |
| 48 | + event Approval(address indexed owner, address indexed spender, uint256 value); |
| 49 | + |
| 50 | + /** |
| 51 | + * @dev Constructor for Poly creation |
| 52 | + * @dev Assigns the totalSupply to the PolyDistribution contract |
| 53 | + */ |
| 54 | + function PolyToken(address _polyDistributionContractAddress) public { |
| 55 | + require(_polyDistributionContractAddress != address(0)); |
| 56 | + balances[_polyDistributionContractAddress] = totalSupply; |
| 57 | + Transfer(address(0), _polyDistributionContractAddress, totalSupply); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dev Gets the balance of the specified address. |
| 62 | + * @param _owner The address to query the the balance of. |
| 63 | + * @return An uint256 representing the amount owned by the passed address. |
| 64 | + */ |
| 65 | + function balanceOf(address _owner) public view returns (uint256 balance) { |
| 66 | + return balances[_owner]; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @dev Function to check the amount of tokens that an owner allowed to a spender. |
| 71 | + * @param _owner address The address which owns the funds. |
| 72 | + * @param _spender address The address which will spend the funds. |
| 73 | + * @return A uint256 specifying the amount of tokens still available for the spender. |
| 74 | + */ |
| 75 | + function allowance(address _owner, address _spender) public view returns (uint256) { |
| 76 | + return allowed[_owner][_spender]; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @dev transfer token for a specified address |
| 81 | + * @param _to The address to transfer to. |
| 82 | + * @param _value The amount to be transferred. |
| 83 | + */ |
| 84 | + function transfer(address _to, uint256 _value) public returns (bool) { |
| 85 | + require(_to != address(0)); |
| 86 | + require(_value <= balances[msg.sender]); |
| 87 | + |
| 88 | + // SafeMath.sub will throw if there is not enough balance. |
| 89 | + balances[msg.sender] = balances[msg.sender].sub(_value); |
| 90 | + balances[_to] = balances[_to].add(_value); |
| 91 | + Transfer(msg.sender, _to, _value); |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @dev Transfer tokens from one address to another |
| 97 | + * @param _from address The address which you want to send tokens from |
| 98 | + * @param _to address The address which you want to transfer to |
| 99 | + * @param _value uint256 the amount of tokens to be transferred |
| 100 | + */ |
| 101 | + function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { |
| 102 | + require(_to != address(0)); |
| 103 | + require(_value <= balances[_from]); |
| 104 | + require(_value <= allowed[_from][msg.sender]); |
| 105 | + |
| 106 | + balances[_from] = balances[_from].sub(_value); |
| 107 | + balances[_to] = balances[_to].add(_value); |
| 108 | + allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); |
| 109 | + Transfer(_from, _to, _value); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. |
| 115 | + * |
| 116 | + * Beware that changing an allowance with this method brings the risk that someone may use both the old |
| 117 | + * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this |
| 118 | + * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: |
| 119 | + * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 |
| 120 | + * @param _spender The address which will spend the funds. |
| 121 | + * @param _value The amount of tokens to be spent. |
| 122 | + */ |
| 123 | + function approve(address _spender, uint256 _value) public returns (bool) { |
| 124 | + allowed[msg.sender][_spender] = _value; |
| 125 | + Approval(msg.sender, _spender, _value); |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @dev Increase the amount of tokens that an owner allowed to a spender. |
| 131 | + * |
| 132 | + * approve should be called when allowed[_spender] == 0. To increment |
| 133 | + * allowed value is better to use this function to avoid 2 calls (and wait until |
| 134 | + * the first transaction is mined) |
| 135 | + * From MonolithDAO Token.sol |
| 136 | + * @param _spender The address which will spend the funds. |
| 137 | + * @param _addedValue The amount of tokens to increase the allowance by. |
| 138 | + */ |
| 139 | + function increaseApproval(address _spender, uint _addedValue) public returns (bool) { |
| 140 | + allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue); |
| 141 | + Approval(msg.sender, _spender, allowed[msg.sender][_spender]); |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @dev Decrease the amount of tokens that an owner allowed to a spender. |
| 147 | + * |
| 148 | + * approve should be called when allowed[_spender] == 0. To decrement |
| 149 | + * allowed value is better to use this function to avoid 2 calls (and wait until |
| 150 | + * the first transaction is mined) |
| 151 | + * From MonolithDAO Token.sol |
| 152 | + * @param _spender The address which will spend the funds. |
| 153 | + * @param _subtractedValue The amount of tokens to decrease the allowance by. |
| 154 | + */ |
| 155 | + function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) { |
| 156 | + uint oldValue = allowed[msg.sender][_spender]; |
| 157 | + if (_subtractedValue > oldValue) { |
| 158 | + allowed[msg.sender][_spender] = 0; |
| 159 | + } else { |
| 160 | + allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue); |
| 161 | + } |
| 162 | + Approval(msg.sender, _spender, allowed[msg.sender][_spender]); |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments