LimitedTransferToken.sol 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. pragma solidity ^0.4.8;
  2. import "./ERC20.sol";
  3. /**
  4. * @title LimitedTransferToken
  5. * @dev LimitedTransferToken defines the generic interface and the implementation
  6. to limit token transferability for different events.
  7. It is intended to be used as a base class for other token contracts.
  8. LimitedTransferToken has been designed to allow for different limiting factors,
  9. this can be achieved by recursively calling super.transferableTokens() until the
  10. base class is hit. For example:
  11. function transferableTokens(address holder, uint64 time) constant public returns (uint256) {
  12. return min256(unlockedTokens, super.transferableTokens(holder, time));
  13. }
  14. A working example is VestedToken.sol:
  15. https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/VestedToken.sol
  16. */
  17. contract LimitedTransferToken is ERC20 {
  18. /**
  19. * @dev Checks whether it can transfer or otherwise throws.
  20. */
  21. modifier canTransfer(address _sender, uint _value) {
  22. if (_value > transferableTokens(_sender, uint64(now))) throw;
  23. _;
  24. }
  25. /**
  26. * @dev Checks modifier and allows transfer if tokens are not locked.
  27. * @param _to address The address that will recieve the tokens
  28. * @param _value uint The amount of tokens to be transfered
  29. */
  30. function transfer(address _to, uint _value) canTransfer(msg.sender, _value) {
  31. return super.transfer(_to, _value);
  32. }
  33. /**
  34. * @dev Checks modifier and allows transfer if tokens are not locked.
  35. * @param _from address The address that will send the tokens.
  36. * @param _to address The address that will recieve the tokens.
  37. * @param _value uint The amount of tokens to be transfered.
  38. */
  39. function transferFrom(address _from, address _to, uint _value) canTransfer(_from, _value) {
  40. return super.transferFrom(_from, _to, _value);
  41. }
  42. /**
  43. * @dev Default transferable tokens function returns all tokens for a holder (no limit).
  44. * @dev Overwriting transferableTokens(address holder, uint64 time) is the way to provide
  45. the specific logic for limiting token transferability for a holder over time.
  46. */
  47. function transferableTokens(address holder, uint64 time) constant public returns (uint256) {
  48. return balanceOf(holder);
  49. }
  50. }