ERC827Token.sol 5.0 KB

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