ERC20.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. pragma solidity ^0.4.24;
  2. import "./IERC20.sol";
  3. import "../../math/SafeMath.sol";
  4. /**
  5. * @title Standard ERC20 token
  6. *
  7. * @dev Implementation of the basic standard token.
  8. * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
  9. * Originally based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
  10. */
  11. contract ERC20 is IERC20 {
  12. using SafeMath for uint256;
  13. mapping (address => uint256) private _balances;
  14. mapping (address => mapping (address => uint256)) private _allowed;
  15. uint256 private _totalSupply;
  16. /**
  17. * @dev Total number of tokens in existence
  18. */
  19. function totalSupply() public view returns (uint256) {
  20. return _totalSupply;
  21. }
  22. /**
  23. * @dev Gets the balance of the specified address.
  24. * @param owner The address to query the balance of.
  25. * @return An uint256 representing the amount owned by the passed address.
  26. */
  27. function balanceOf(address owner) public view returns (uint256) {
  28. return _balances[owner];
  29. }
  30. /**
  31. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  32. * @param owner address The address which owns the funds.
  33. * @param spender address The address which will spend the funds.
  34. * @return A uint256 specifying the amount of tokens still available for the spender.
  35. */
  36. function allowance(
  37. address owner,
  38. address spender
  39. )
  40. public
  41. view
  42. returns (uint256)
  43. {
  44. return _allowed[owner][spender];
  45. }
  46. /**
  47. * @dev Transfer token for a specified address
  48. * @param to The address to transfer to.
  49. * @param value The amount to be transferred.
  50. */
  51. function transfer(address to, uint256 value) public returns (bool) {
  52. _transfer(msg.sender, to, value);
  53. return true;
  54. }
  55. /**
  56. * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  57. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  58. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  59. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  60. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  61. * @param spender The address which will spend the funds.
  62. * @param value The amount of tokens to be spent.
  63. */
  64. function approve(address spender, uint256 value) public returns (bool) {
  65. require(spender != address(0));
  66. _allowed[msg.sender][spender] = value;
  67. emit Approval(msg.sender, spender, value);
  68. return true;
  69. }
  70. /**
  71. * @dev Transfer tokens from one address to another
  72. * @param from address The address which you want to send tokens from
  73. * @param to address The address which you want to transfer to
  74. * @param value uint256 the amount of tokens to be transferred
  75. */
  76. function transferFrom(
  77. address from,
  78. address to,
  79. uint256 value
  80. )
  81. public
  82. returns (bool)
  83. {
  84. _allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
  85. _transfer(from, to, value);
  86. return true;
  87. }
  88. /**
  89. * @dev Increase the amount of tokens that an owner allowed to a spender.
  90. * approve should be called when allowed_[_spender] == 0. To increment
  91. * allowed value is better to use this function to avoid 2 calls (and wait until
  92. * the first transaction is mined)
  93. * From MonolithDAO Token.sol
  94. * @param spender The address which will spend the funds.
  95. * @param addedValue The amount of tokens to increase the allowance by.
  96. */
  97. function increaseAllowance(
  98. address spender,
  99. uint256 addedValue
  100. )
  101. public
  102. returns (bool)
  103. {
  104. require(spender != address(0));
  105. _allowed[msg.sender][spender] = (
  106. _allowed[msg.sender][spender].add(addedValue));
  107. emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
  108. return true;
  109. }
  110. /**
  111. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  112. * approve should be called when allowed_[_spender] == 0. To decrement
  113. * allowed value is better to use this function to avoid 2 calls (and wait until
  114. * the first transaction is mined)
  115. * From MonolithDAO Token.sol
  116. * @param spender The address which will spend the funds.
  117. * @param subtractedValue The amount of tokens to decrease the allowance by.
  118. */
  119. function decreaseAllowance(
  120. address spender,
  121. uint256 subtractedValue
  122. )
  123. public
  124. returns (bool)
  125. {
  126. require(spender != address(0));
  127. _allowed[msg.sender][spender] = (
  128. _allowed[msg.sender][spender].sub(subtractedValue));
  129. emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
  130. return true;
  131. }
  132. /**
  133. * @dev Transfer token for a specified addresses
  134. * @param from The address to transfer from.
  135. * @param to The address to transfer to.
  136. * @param value The amount to be transferred.
  137. */
  138. function _transfer(address from, address to, uint256 value) internal {
  139. require(to != address(0));
  140. _balances[from] = _balances[from].sub(value);
  141. _balances[to] = _balances[to].add(value);
  142. emit Transfer(from, to, value);
  143. }
  144. /**
  145. * @dev Internal function that mints an amount of the token and assigns it to
  146. * an account. This encapsulates the modification of balances such that the
  147. * proper events are emitted.
  148. * @param account The account that will receive the created tokens.
  149. * @param value The amount that will be created.
  150. */
  151. function _mint(address account, uint256 value) internal {
  152. require(account != address(0));
  153. _totalSupply = _totalSupply.add(value);
  154. _balances[account] = _balances[account].add(value);
  155. emit Transfer(address(0), account, value);
  156. }
  157. /**
  158. * @dev Internal function that burns an amount of the token of a given
  159. * account.
  160. * @param account The account whose tokens will be burnt.
  161. * @param value The amount that will be burnt.
  162. */
  163. function _burn(address account, uint256 value) internal {
  164. require(account != address(0));
  165. _totalSupply = _totalSupply.sub(value);
  166. _balances[account] = _balances[account].sub(value);
  167. emit Transfer(account, address(0), value);
  168. }
  169. /**
  170. * @dev Internal function that burns an amount of the token of a given
  171. * account, deducting from the sender's allowance for said account. Uses the
  172. * internal burn function.
  173. * @param account The account whose tokens will be burnt.
  174. * @param value The amount that will be burnt.
  175. */
  176. function _burnFrom(address account, uint256 value) internal {
  177. // Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted,
  178. // this function needs to emit an event with the updated approval.
  179. _allowed[account][msg.sender] = _allowed[account][msg.sender].sub(
  180. value);
  181. _burn(account, value);
  182. }
  183. }