AllowanceCrowdsale.sol 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. pragma solidity ^0.5.0;
  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) public {
  20. require(tokenWallet != address(0), "AllowanceCrowdsale: token wallet is the zero address");
  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(token().balanceOf(_tokenWallet), token().allowance(_tokenWallet, address(this)));
  35. }
  36. /**
  37. * @dev Overrides parent behavior by transferring tokens from wallet.
  38. * @param beneficiary Token purchaser
  39. * @param tokenAmount Amount of tokens purchased
  40. */
  41. function _deliverTokens(address beneficiary, uint256 tokenAmount) internal {
  42. token().safeTransferFrom(_tokenWallet, beneficiary, tokenAmount);
  43. }
  44. }