IndividuallyCappedCrowdsale.sol 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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) public view returns (uint256) {
  36. return _contributions[beneficiary];
  37. }
  38. /**
  39. * @dev Extend parent behavior requiring purchase to respect the beneficiary's funding cap.
  40. * @param beneficiary Token purchaser
  41. * @param weiAmount Amount of wei contributed
  42. */
  43. function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
  44. super._preValidatePurchase(beneficiary, weiAmount);
  45. require(_contributions[beneficiary].add(weiAmount) <= _caps[beneficiary]);
  46. }
  47. /**
  48. * @dev Extend parent behavior to update beneficiary contributions
  49. * @param beneficiary Token purchaser
  50. * @param weiAmount Amount of wei contributed
  51. */
  52. function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
  53. super._updatePurchasingState(beneficiary, weiAmount);
  54. _contributions[beneficiary] = _contributions[beneficiary].add(weiAmount);
  55. }
  56. }