IndividuallyCappedCrowdsale.sol 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. pragma solidity ^0.4.24;
  2. import "../../math/SafeMath.sol";
  3. import "../Crowdsale.sol";
  4. import "../../ownership/Ownable.sol";
  5. /**
  6. * @title IndividuallyCappedCrowdsale
  7. * @dev Crowdsale with per-user caps.
  8. */
  9. contract IndividuallyCappedCrowdsale is Ownable, Crowdsale {
  10. using SafeMath for uint256;
  11. mapping(address => uint256) private contributions_;
  12. mapping(address => uint256) private caps_;
  13. /**
  14. * @dev Sets a specific user's maximum contribution.
  15. * @param _beneficiary Address to be capped
  16. * @param _cap Wei limit for individual contribution
  17. */
  18. function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner {
  19. caps_[_beneficiary] = _cap;
  20. }
  21. /**
  22. * @dev Sets a group of users' maximum contribution.
  23. * @param _beneficiaries List of addresses to be capped
  24. * @param _cap Wei limit for individual contribution
  25. */
  26. function setGroupCap(
  27. address[] _beneficiaries,
  28. uint256 _cap
  29. )
  30. external
  31. onlyOwner
  32. {
  33. for (uint256 i = 0; i < _beneficiaries.length; i++) {
  34. caps_[_beneficiaries[i]] = _cap;
  35. }
  36. }
  37. /**
  38. * @dev Returns the cap of a specific user.
  39. * @param _beneficiary Address whose cap is to be checked
  40. * @return Current cap for individual user
  41. */
  42. function getUserCap(address _beneficiary) public view returns (uint256) {
  43. return caps_[_beneficiary];
  44. }
  45. /**
  46. * @dev Returns the amount contributed so far by a sepecific user.
  47. * @param _beneficiary Address of contributor
  48. * @return User contribution so far
  49. */
  50. function getUserContribution(address _beneficiary)
  51. public view returns (uint256)
  52. {
  53. return contributions_[_beneficiary];
  54. }
  55. /**
  56. * @dev Extend parent behavior requiring purchase to respect the user's funding cap.
  57. * @param _beneficiary Token purchaser
  58. * @param _weiAmount Amount of wei contributed
  59. */
  60. function _preValidatePurchase(
  61. address _beneficiary,
  62. uint256 _weiAmount
  63. )
  64. internal
  65. {
  66. super._preValidatePurchase(_beneficiary, _weiAmount);
  67. require(
  68. contributions_[_beneficiary].add(_weiAmount) <= caps_[_beneficiary]);
  69. }
  70. /**
  71. * @dev Extend parent behavior to update user contributions
  72. * @param _beneficiary Token purchaser
  73. * @param _weiAmount Amount of wei contributed
  74. */
  75. function _updatePurchasingState(
  76. address _beneficiary,
  77. uint256 _weiAmount
  78. )
  79. internal
  80. {
  81. super._updatePurchasingState(_beneficiary, _weiAmount);
  82. contributions_[_beneficiary] = contributions_[_beneficiary].add(
  83. _weiAmount);
  84. }
  85. }