IndividuallyCappedCrowdsale.sol 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. pragma solidity ^0.4.24;
  2. import "../../math/SafeMath.sol";
  3. import "../Crowdsale.sol";
  4. import "../../access/roles/CapperRole.sol";
  5. /**
  6. * @title IndividuallyCappedCrowdsale
  7. * @dev Crowdsale with per-beneficiary caps.
  8. */
  9. contract IndividuallyCappedCrowdsale is Crowdsale, CapperRole {
  10. using SafeMath for uint256;
  11. mapping(address => uint256) private _contributions;
  12. mapping(address => uint256) private _caps;
  13. constructor() internal {}
  14. /**
  15. * @dev Sets a specific beneficiary's maximum contribution.
  16. * @param beneficiary Address to be capped
  17. * @param cap Wei limit for individual contribution
  18. */
  19. function setCap(address beneficiary, uint256 cap) external onlyCapper {
  20. _caps[beneficiary] = cap;
  21. }
  22. /**
  23. * @dev Returns the cap of a specific beneficiary.
  24. * @param beneficiary Address whose cap is to be checked
  25. * @return Current cap for individual beneficiary
  26. */
  27. function getCap(address beneficiary) public view returns (uint256) {
  28. return _caps[beneficiary];
  29. }
  30. /**
  31. * @dev Returns the amount contributed so far by a specific beneficiary.
  32. * @param beneficiary Address of contributor
  33. * @return Beneficiary contribution so far
  34. */
  35. function getContribution(address beneficiary)
  36. public view returns (uint256)
  37. {
  38. return _contributions[beneficiary];
  39. }
  40. /**
  41. * @dev Extend parent behavior requiring purchase to respect the beneficiary's funding cap.
  42. * @param beneficiary Token purchaser
  43. * @param weiAmount Amount of wei contributed
  44. */
  45. function _preValidatePurchase(
  46. address beneficiary,
  47. uint256 weiAmount
  48. )
  49. internal
  50. view
  51. {
  52. super._preValidatePurchase(beneficiary, weiAmount);
  53. require(
  54. _contributions[beneficiary].add(weiAmount) <= _caps[beneficiary]);
  55. }
  56. /**
  57. * @dev Extend parent behavior to update beneficiary contributions
  58. * @param beneficiary Token purchaser
  59. * @param weiAmount Amount of wei contributed
  60. */
  61. function _updatePurchasingState(
  62. address beneficiary,
  63. uint256 weiAmount
  64. )
  65. internal
  66. {
  67. super._updatePurchasingState(beneficiary, weiAmount);
  68. _contributions[beneficiary] = _contributions[beneficiary].add(
  69. weiAmount);
  70. }
  71. }