IndividuallyCappedCrowdsale.sol 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  14. * @dev Sets a specific beneficiary's maximum contribution.
  15. * @param _beneficiary Address to be capped
  16. * @param _cap Wei limit for individual contribution
  17. */
  18. function setCap(address _beneficiary, uint256 _cap) external onlyCapper {
  19. caps_[_beneficiary] = _cap;
  20. }
  21. /**
  22. * @dev Returns the cap of a specific beneficiary.
  23. * @param _beneficiary Address whose cap is to be checked
  24. * @return Current cap for individual beneficiary
  25. */
  26. function getCap(address _beneficiary) public view returns (uint256) {
  27. return caps_[_beneficiary];
  28. }
  29. /**
  30. * @dev Returns the amount contributed so far by a specific beneficiary.
  31. * @param _beneficiary Address of contributor
  32. * @return Beneficiary contribution so far
  33. */
  34. function getContribution(address _beneficiary)
  35. public view returns (uint256)
  36. {
  37. return contributions_[_beneficiary];
  38. }
  39. /**
  40. * @dev Extend parent behavior requiring purchase to respect the beneficiary's funding cap.
  41. * @param _beneficiary Token purchaser
  42. * @param _weiAmount Amount of wei contributed
  43. */
  44. function _preValidatePurchase(
  45. address _beneficiary,
  46. uint256 _weiAmount
  47. )
  48. internal
  49. {
  50. super._preValidatePurchase(_beneficiary, _weiAmount);
  51. require(
  52. contributions_[_beneficiary].add(_weiAmount) <= caps_[_beneficiary]);
  53. }
  54. /**
  55. * @dev Extend parent behavior to update beneficiary contributions
  56. * @param _beneficiary Token purchaser
  57. * @param _weiAmount Amount of wei contributed
  58. */
  59. function _updatePurchasingState(
  60. address _beneficiary,
  61. uint256 _weiAmount
  62. )
  63. internal
  64. {
  65. super._updatePurchasingState(_beneficiary, _weiAmount);
  66. contributions_[_beneficiary] = contributions_[_beneficiary].add(
  67. _weiAmount);
  68. }
  69. }