ERC827Token.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* solium-disable security/no-low-level-calls */
  2. pragma solidity ^0.4.21;
  3. import "./ERC827.sol";
  4. import "../ERC20/StandardToken.sol";
  5. /**
  6. * @title ERC827, an extension of ERC20 token standard
  7. *
  8. * @dev Implementation the ERC827, following the ERC20 standard with extra
  9. * @dev methods to transfer value and data and execute calls in transfers and
  10. * @dev approvals.
  11. *
  12. * @dev Uses OpenZeppelin StandardToken.
  13. */
  14. contract ERC827Token is ERC827, StandardToken {
  15. /**
  16. * @dev Addition to ERC20 token methods. It allows to
  17. * @dev approve the transfer of value and execute a call with the sent data.
  18. *
  19. * @dev Beware that changing an allowance with this method brings the risk that
  20. * @dev someone may use both the old and the new allowance by unfortunate
  21. * @dev transaction ordering. One possible solution to mitigate this race condition
  22. * @dev is to first reduce the spender's allowance to 0 and set the desired value
  23. * @dev afterwards:
  24. * @dev https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  25. *
  26. * @param _spender The address that will spend the funds.
  27. * @param _value The amount of tokens to be spent.
  28. * @param _data ABI-encoded contract call to call `_to` address.
  29. *
  30. * @return true if the call function was executed successfully
  31. */
  32. function approveAndCall(address _spender, uint256 _value, bytes _data) public payable returns (bool) {
  33. require(_spender != address(this));
  34. super.approve(_spender, _value);
  35. // solium-disable-next-line security/no-call-value
  36. require(_spender.call.value(msg.value)(_data));
  37. return true;
  38. }
  39. /**
  40. * @dev Addition to ERC20 token methods. Transfer tokens to a specified
  41. * @dev address and execute a call with the sent data on the same transaction
  42. *
  43. * @param _to address The address which you want to transfer to
  44. * @param _value uint256 the amout of tokens to be transfered
  45. * @param _data ABI-encoded contract call to call `_to` address.
  46. *
  47. * @return true if the call function was executed successfully
  48. */
  49. function transferAndCall(address _to, uint256 _value, bytes _data) public payable returns (bool) {
  50. require(_to != address(this));
  51. super.transfer(_to, _value);
  52. // solium-disable-next-line security/no-call-value
  53. require(_to.call.value(msg.value)(_data));
  54. return true;
  55. }
  56. /**
  57. * @dev Addition to ERC20 token methods. Transfer tokens from one address to
  58. * @dev another and make a contract call on the same transaction
  59. *
  60. * @param _from The address which you want to send tokens from
  61. * @param _to The address which you want to transfer to
  62. * @param _value The amout of tokens to be transferred
  63. * @param _data ABI-encoded contract call to call `_to` address.
  64. *
  65. * @return true if the call function was executed successfully
  66. */
  67. function transferFromAndCall(
  68. address _from,
  69. address _to,
  70. uint256 _value,
  71. bytes _data
  72. )
  73. public payable returns (bool)
  74. {
  75. require(_to != address(this));
  76. super.transferFrom(_from, _to, _value);
  77. // solium-disable-next-line security/no-call-value
  78. require(_to.call.value(msg.value)(_data));
  79. return true;
  80. }
  81. /**
  82. * @dev Addition to StandardToken methods. Increase the amount of tokens that
  83. * @dev an owner allowed to a spender and execute a call with the sent data.
  84. *
  85. * @dev approve should be called when allowed[_spender] == 0. To increment
  86. * @dev allowed value is better to use this function to avoid 2 calls (and wait until
  87. * @dev the first transaction is mined)
  88. * @dev From MonolithDAO Token.sol
  89. *
  90. * @param _spender The address which will spend the funds.
  91. * @param _addedValue The amount of tokens to increase the allowance by.
  92. * @param _data ABI-encoded contract call to call `_spender` address.
  93. */
  94. function increaseApprovalAndCall(address _spender, uint _addedValue, bytes _data) public payable returns (bool) {
  95. require(_spender != address(this));
  96. super.increaseApproval(_spender, _addedValue);
  97. // solium-disable-next-line security/no-call-value
  98. require(_spender.call.value(msg.value)(_data));
  99. return true;
  100. }
  101. /**
  102. * @dev Addition to StandardToken methods. Decrease the amount of tokens that
  103. * @dev an owner allowed to a spender and execute a call with the sent data.
  104. *
  105. * @dev approve should be called when allowed[_spender] == 0. To decrement
  106. * @dev allowed value is better to use this function to avoid 2 calls (and wait until
  107. * @dev the first transaction is mined)
  108. * @dev From MonolithDAO Token.sol
  109. *
  110. * @param _spender The address which will spend the funds.
  111. * @param _subtractedValue The amount of tokens to decrease the allowance by.
  112. * @param _data ABI-encoded contract call to call `_spender` address.
  113. */
  114. function decreaseApprovalAndCall(address _spender, uint _subtractedValue, bytes _data) public payable returns (bool) {
  115. require(_spender != address(this));
  116. super.decreaseApproval(_spender, _subtractedValue);
  117. // solium-disable-next-line security/no-call-value
  118. require(_spender.call.value(msg.value)(_data));
  119. return true;
  120. }
  121. }