LimitedTransferToken.sol 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. pragma solidity ^0.4.11;
  2. import "./ERC20.sol";
  3. /**
  4. * @title LimitedTransferToken
  5. * @dev LimitedTransferToken defines the generic interface and the implementation to limit token
  6. * transferability for different events. It is intended to be used as a base class for other token
  7. * contracts.
  8. * LimitedTransferToken has been designed to allow for different limiting factors,
  9. * this can be achieved by recursively calling super.transferableTokens() until the base class is
  10. * 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 The address that will recieve the tokens.
  28. * @param _value The amount of tokens to be transferred.
  29. */
  30. function transfer(address _to, uint _value) canTransfer(msg.sender, _value) {
  31. super.transfer(_to, _value);
  32. }
  33. /**
  34. * @dev Checks modifier and allows transfer if tokens are not locked.
  35. * @param _from The address that will send the tokens.
  36. * @param _to The address that will recieve the tokens.
  37. * @param _value The amount of tokens to be transferred.
  38. */
  39. function transferFrom(address _from, address _to, uint _value) canTransfer(_from, _value) {
  40. 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 the
  45. * 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. }