Guest User

Untitled

a guest
Jun 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. pragma solidity ^0.4.21;
  2.  
  3. import "zeppelin-solidity/contracts/token/ERC20/ERC20.sol";
  4. import "zeppelin-solidity/contracts/math/SafeMath.sol";
  5. import "zeppelin-solidity/contracts/ownership/Ownable.sol";
  6.  
  7.  
  8. contract Subscription is Ownable {
  9. using SafeMath for uint256;
  10.  
  11. /// @dev The token being use
  12. ERC20 public token;
  13.  
  14. /// @dev Address where fee are collected
  15. address public wallet;
  16.  
  17. /// @dev Cost per day of membership
  18. uint256 public subscriptionRate;
  19.  
  20. mapping(uint256 => uint256) subscriptionExpiration;
  21.  
  22. /**
  23. * Event for subscription purchase logging
  24. * @param purchaser who paid for the subscription
  25. * @param userId user id who will benefit from purchase
  26. * @param day day of subscription purchased
  27. * @param amount amount of subscription purchased in wei
  28. * @param expiration expiration of user subscription.
  29. */
  30. event SubscriptionPurchase(
  31. address indexed purchaser,
  32. uint256 indexed userId,
  33. uint256 day,
  34. uint256 amount,
  35. uint256 expiration
  36. );
  37.  
  38. function Subscription(
  39. uint256 _rate,
  40. address _fundWallet,
  41. ERC20 _token) public
  42. {
  43. require(_token != address(0));
  44. require(_fundWallet != address(0));
  45. require(_rate > 0);
  46. token = _token;
  47. wallet = _fundWallet;
  48. subscriptionRate = _rate;
  49. }
  50.  
  51. function renewSubscription(uint256 _userId, uint _day) external {
  52. require(_day > 0);
  53. // Calculate amount token amount to purchase by number of day.
  54. uint256 amount = subscriptionRate.mul(_day);
  55. uint256 currentExpiration = subscriptionExpiration[_userId];
  56.  
  57. // If their membership already expired...
  58. if (currentExpiration < now) {
  59. // ...use `now` as the starting point of their new subscription
  60. currentExpiration = now;
  61. }
  62. uint256 newExpiration = currentExpiration.add(_day.mul(1 days));
  63. subscriptionExpiration[_userId] = newExpiration;
  64.  
  65. // Transfer token to our wallet. Always do this last to prevent race conditions.
  66. require(token.transferFrom(msg.sender, wallet, amount));
  67. emit SubscriptionPurchase(
  68. msg.sender,
  69. _userId,
  70. _day,
  71. amount,
  72. newExpiration);
  73. }
  74. }
Add Comment
Please, Sign In to add comment