IndividuallyCappedCrowdsale.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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) public contributions;
  12. mapping(address => uint256) public 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(contributions[_beneficiary].add(_weiAmount) <= caps[_beneficiary]);
  68. }
  69. /**
  70. * @dev Extend parent behavior to update user contributions
  71. * @param _beneficiary Token purchaser
  72. * @param _weiAmount Amount of wei contributed
  73. */
  74. function _updatePurchasingState(
  75. address _beneficiary,
  76. uint256 _weiAmount
  77. )
  78. internal
  79. {
  80. super._updatePurchasingState(_beneficiary, _weiAmount);
  81. contributions[_beneficiary] = contributions[_beneficiary].add(_weiAmount);
  82. }
  83. }