AllowanceCrowdsale.sol 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. pragma solidity ^0.4.24;
  2. import "../Crowdsale.sol";
  3. import "../../token/ERC20/IERC20.sol";
  4. import "../../token/ERC20/SafeERC20.sol";
  5. import "../../math/SafeMath.sol";
  6. import "../../math/Math.sol";
  7. /**
  8. * @title AllowanceCrowdsale
  9. * @dev Extension of Crowdsale where tokens are held by a wallet, which approves an allowance to the crowdsale.
  10. */
  11. contract AllowanceCrowdsale is Crowdsale {
  12. using SafeMath for uint256;
  13. using SafeERC20 for IERC20;
  14. address private _tokenWallet;
  15. /**
  16. * @dev Constructor, takes token wallet address.
  17. * @param tokenWallet Address holding the tokens, which has approved allowance to the crowdsale
  18. */
  19. constructor(address tokenWallet) internal {
  20. require(tokenWallet != address(0));
  21. _tokenWallet = tokenWallet;
  22. }
  23. /**
  24. * @return the address of the wallet that will hold the tokens.
  25. */
  26. function tokenWallet() public view returns(address) {
  27. return _tokenWallet;
  28. }
  29. /**
  30. * @dev Checks the amount of tokens left in the allowance.
  31. * @return Amount of tokens left in the allowance
  32. */
  33. function remainingTokens() public view returns (uint256) {
  34. return Math.min(
  35. token().balanceOf(_tokenWallet),
  36. token().allowance(_tokenWallet, this)
  37. );
  38. }
  39. /**
  40. * @dev Overrides parent behavior by transferring tokens from wallet.
  41. * @param beneficiary Token purchaser
  42. * @param tokenAmount Amount of tokens purchased
  43. */
  44. function _deliverTokens(
  45. address beneficiary,
  46. uint256 tokenAmount
  47. )
  48. internal
  49. {
  50. token().safeTransferFrom(_tokenWallet, beneficiary, tokenAmount);
  51. }
  52. }