LimitedTransferToken.sol 1.8 KB

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