ERC827Token.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* solium-disable security/no-low-level-calls */
  2. pragma solidity ^0.4.23;
  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(
  33. address _spender,
  34. uint256 _value,
  35. bytes _data
  36. )
  37. public
  38. payable
  39. returns (bool)
  40. {
  41. require(_spender != address(this));
  42. super.approve(_spender, _value);
  43. // solium-disable-next-line security/no-call-value
  44. require(_spender.call.value(msg.value)(_data));
  45. return true;
  46. }
  47. /**
  48. * @dev Addition to ERC20 token methods. Transfer tokens to a specified
  49. * @dev address and execute a call with the sent data on the same transaction
  50. *
  51. * @param _to address The address which you want to transfer to
  52. * @param _value uint256 the amout of tokens to be transfered
  53. * @param _data ABI-encoded contract call to call `_to` address.
  54. *
  55. * @return true if the call function was executed successfully
  56. */
  57. function transferAndCall(
  58. address _to,
  59. uint256 _value,
  60. bytes _data
  61. )
  62. public
  63. payable
  64. returns (bool)
  65. {
  66. require(_to != address(this));
  67. super.transfer(_to, _value);
  68. // solium-disable-next-line security/no-call-value
  69. require(_to.call.value(msg.value)(_data));
  70. return true;
  71. }
  72. /**
  73. * @dev Addition to ERC20 token methods. Transfer tokens from one address to
  74. * @dev another and make a contract call on the same transaction
  75. *
  76. * @param _from The address which you want to send tokens from
  77. * @param _to The address which you want to transfer to
  78. * @param _value The amout of tokens to be transferred
  79. * @param _data ABI-encoded contract call to call `_to` address.
  80. *
  81. * @return true if the call function was executed successfully
  82. */
  83. function transferFromAndCall(
  84. address _from,
  85. address _to,
  86. uint256 _value,
  87. bytes _data
  88. )
  89. public payable returns (bool)
  90. {
  91. require(_to != address(this));
  92. super.transferFrom(_from, _to, _value);
  93. // solium-disable-next-line security/no-call-value
  94. require(_to.call.value(msg.value)(_data));
  95. return true;
  96. }
  97. /**
  98. * @dev Addition to StandardToken methods. Increase the amount of tokens that
  99. * @dev an owner allowed to a spender and execute a call with the sent data.
  100. *
  101. * @dev approve should be called when allowed[_spender] == 0. To increment
  102. * @dev allowed value is better to use this function to avoid 2 calls (and wait until
  103. * @dev the first transaction is mined)
  104. * @dev From MonolithDAO Token.sol
  105. *
  106. * @param _spender The address which will spend the funds.
  107. * @param _addedValue The amount of tokens to increase the allowance by.
  108. * @param _data ABI-encoded contract call to call `_spender` address.
  109. */
  110. function increaseApprovalAndCall(
  111. address _spender,
  112. uint _addedValue,
  113. bytes _data
  114. )
  115. public
  116. payable
  117. returns (bool)
  118. {
  119. require(_spender != address(this));
  120. super.increaseApproval(_spender, _addedValue);
  121. // solium-disable-next-line security/no-call-value
  122. require(_spender.call.value(msg.value)(_data));
  123. return true;
  124. }
  125. /**
  126. * @dev Addition to StandardToken methods. Decrease the amount of tokens that
  127. * @dev an owner allowed to a spender and execute a call with the sent data.
  128. *
  129. * @dev approve should be called when allowed[_spender] == 0. To decrement
  130. * @dev allowed value is better to use this function to avoid 2 calls (and wait until
  131. * @dev the first transaction is mined)
  132. * @dev From MonolithDAO Token.sol
  133. *
  134. * @param _spender The address which will spend the funds.
  135. * @param _subtractedValue The amount of tokens to decrease the allowance by.
  136. * @param _data ABI-encoded contract call to call `_spender` address.
  137. */
  138. function decreaseApprovalAndCall(
  139. address _spender,
  140. uint _subtractedValue,
  141. bytes _data
  142. )
  143. public
  144. payable
  145. returns (bool)
  146. {
  147. require(_spender != address(this));
  148. super.decreaseApproval(_spender, _subtractedValue);
  149. // solium-disable-next-line security/no-call-value
  150. require(_spender.call.value(msg.value)(_data));
  151. return true;
  152. }
  153. }