Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. pragma solidity ^0.4.18;
  2.  
  3. import "zeppelin-solidity/contracts/ownership/Claimable.sol";
  4. import "zeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";
  5. import "zeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
  6.  
  7. import "./mixins/ReissuableToken.sol";
  8. import "./RegulatorService/IRegulatorService.sol";
  9. import "./ServiceRegistry.sol";
  10.  
  11.  
  12. /// @notice An ERC-20 token that has the ability to check for trade validity
  13. contract RegulatedToken is ReissuableToken, DetailedERC20, MintableToken, Claimable {
  14.  
  15. /**
  16. * @notice R-Token decimals setting (used when constructing DetailedERC20)
  17. */
  18. uint8 constant public RTOKEN_DECIMALS = 18;
  19.  
  20. /**
  21. * @notice Triggered when regulator checks pass or fail
  22. */
  23. event LogCheckResponse(
  24. uint8 code,
  25. address indexed spender,
  26. address indexed from,
  27. address indexed to,
  28. uint256 value
  29. );
  30.  
  31. /**
  32. * @notice Address of the `ServiceRegistry` that has the location of the
  33. * `RegulatorService` contract responsible for checking trade
  34. * permissions.
  35. */
  36. ServiceRegistry public registry;
  37.  
  38. /**
  39. * @notice Constructor
  40. *
  41. * @param _registry Address of `ServiceRegistry` contract
  42. * @param _name Name of the token: See DetailedERC20
  43. * @param _symbol Symbol of the token: See DetailedERC20
  44. */
  45. function RegulatedToken(
  46. ServiceRegistry _registry,
  47. string _name,
  48. string _symbol
  49. )
  50. public
  51. DetailedERC20(_name, _symbol, RTOKEN_DECIMALS)
  52. {
  53. require(_registry != address(0), "Registry address cannot be 0");
  54. registry = _registry;
  55. }
  56.  
  57. function mint(
  58. address _to,
  59. uint256 _amount
  60. )
  61. public
  62. onlyOwner
  63. canMint
  64. returns (bool)
  65. {
  66. bool mintSuccess = super.mint(_to, _amount);
  67.  
  68. if (mintSuccess) {
  69. _onMintSuccess(_to, _amount);
  70. }
  71. return mintSuccess;
  72. }
  73.  
  74. /**
  75. * @notice ERC-20 overridden function that include logic to check for
  76. * trade validity.
  77. *
  78. * @dev Must pass `msg.sender` as `from` field to `_check()`
  79. *
  80. * @param _to The address of the receiver
  81. * @param _value The number of tokens to transfer
  82. *
  83. * @return `true` if successful and `false` if unsuccessful
  84. */
  85. function transfer(address _to, uint256 _value) public returns (bool) {
  86. if (_check(msg.sender, _to, _value)) {
  87. bool transferSuccess = super.transfer(_to, _value);
  88.  
  89. if (transferSuccess) {
  90. _onTransferSuccess(msg.sender, msg.sender, _to, _value);
  91. }
  92. return transferSuccess;
  93.  
  94. } else {
  95. return false;
  96. }
  97. }
  98.  
  99. /**
  100. * @notice ERC-20 overridden function that include logic to check for
  101. * trade validity.
  102. *
  103. * @param _from The address of the sender
  104. * @param _to The address of the receiver
  105. * @param _value The number of tokens to transfer
  106. *
  107. * @return `true` if successful and `false` if unsuccessful
  108. */
  109. function transferFrom(
  110. address _from,
  111. address _to,
  112. uint256 _value
  113. ) public returns (bool) {
  114. if (_check(_from, _to, _value)) {
  115. bool transferSuccess = super.transferFrom(_from, _to, _value);
  116.  
  117. if (transferSuccess) {
  118. _onTransferSuccess(msg.sender, _from, _to, _value);
  119. }
  120. return transferSuccess;
  121. } else {
  122. return false;
  123. }
  124. }
  125.  
  126. /**
  127. * @dev Modify `reissuer()` to call `_onTransferSuccess` if it returns true.
  128. */
  129. function reissue(address _from, address _to, uint256 _amount)
  130. public
  131. onlyReissuer
  132. returns (bool)
  133. {
  134. bool reissuerSuccess = super.reissue(_from, _to, _amount);
  135.  
  136. if (reissuerSuccess) {
  137. _onTransferSuccess(msg.sender, _from, _to, _amount);
  138. }
  139. return reissuerSuccess;
  140. }
  141.  
  142. /**
  143. * @notice Performs the regulator check
  144. *
  145. * @dev This method raises a CheckStatus event indicating success or
  146. * failure of the check
  147. *
  148. * @param _from The address of the sender
  149. * @param _to The address of the receiver
  150. * @param _value The number of tokens to transfer
  151. *
  152. * @return `true` if the check was successful and `false` if unsuccessful
  153. */
  154. function _check(
  155. address _from,
  156. address _to,
  157. uint256 _value
  158. ) private returns (bool) {
  159. uint8 code = _service().check(this, msg.sender, _from, _to, _value);
  160. emit LogCheckResponse(code, msg.sender, _from, _to, _value);
  161. return code == 0;
  162. }
  163.  
  164. /**
  165. * @notice Calls back to `RegulatedService` to handle successful mints
  166. *
  167. * @param _to The address of the receiver account
  168. * @param _amount The quantity of the token to trade
  169. */
  170. function _onMintSuccess(address _to, uint256 _amount) private {
  171. _service().onMintSuccess(_to, _amount);
  172. }
  173.  
  174. /**
  175. * @notice Calls back to `RegulatedService` to handle successful transfers
  176. *
  177. * @param _spender The address of the spender of the token
  178. * @param _from The address of the sender account
  179. * @param _to The address of the receiver account
  180. * @param _value The quantity of the token to trade
  181. */
  182. function _onTransferSuccess(
  183. address _spender,
  184. address _from,
  185. address _to,
  186. uint256 _value
  187. ) private {
  188. _service().onTransferSuccess(_spender, _from, _to, _value);
  189. }
  190.  
  191. /**
  192. * @notice Retreives the address of the `RegulatorService` that manages
  193. * this token.
  194. *
  195. * @dev This function *MUST NOT* memoize the `RegulatorService` address.
  196. * This would break the ability to upgrade the `RegulatorService`.
  197. *
  198. * @return The `RegulatorService` that manages this token.
  199. */
  200. function _service() constant public returns (IRegulatorService) {
  201. return IRegulatorService(registry.service());
  202. }
  203. }
Add Comment
Please, Sign In to add comment