IndividuallyCappedCrowdsale.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. pragma solidity ^0.4.21;
  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 Crowdsale, Ownable {
  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(address[] _beneficiaries, uint256 _cap) external onlyOwner {
  27. for (uint256 i = 0; i < _beneficiaries.length; i++) {
  28. caps[_beneficiaries[i]] = _cap;
  29. }
  30. }
  31. /**
  32. * @dev Returns the cap of a specific user.
  33. * @param _beneficiary Address whose cap is to be checked
  34. * @return Current cap for individual user
  35. */
  36. function getUserCap(address _beneficiary) public view returns (uint256) {
  37. return caps[_beneficiary];
  38. }
  39. /**
  40. * @dev Returns the amount contributed so far by a sepecific user.
  41. * @param _beneficiary Address of contributor
  42. * @return User contribution so far
  43. */
  44. function getUserContribution(address _beneficiary) public view returns (uint256) {
  45. return contributions[_beneficiary];
  46. }
  47. /**
  48. * @dev Extend parent behavior requiring purchase to respect the user's funding cap.
  49. * @param _beneficiary Token purchaser
  50. * @param _weiAmount Amount of wei contributed
  51. */
  52. function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  53. super._preValidatePurchase(_beneficiary, _weiAmount);
  54. require(contributions[_beneficiary].add(_weiAmount) <= caps[_beneficiary]);
  55. }
  56. /**
  57. * @dev Extend parent behavior to update user contributions
  58. * @param _beneficiary Token purchaser
  59. * @param _weiAmount Amount of wei contributed
  60. */
  61. function _updatePurchasingState(address _beneficiary, uint256 _weiAmount) internal {
  62. super._updatePurchasingState(_beneficiary, _weiAmount);
  63. contributions[_beneficiary] = contributions[_beneficiary].add(_weiAmount);
  64. }
  65. }