|
|
|
pragma solidity ^0.5.0;
|
|
|
|
|
|
|
|
import "../GSN/Context.sol";
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
import "../token/ERC20/IERC20.sol";
|
|
|
|
import "../math/SafeMath.sol";
|
|
|
|
import "../token/ERC20/SafeERC20.sol";
|
|
|
|
import "../utils/ReentrancyGuard.sol";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title Crowdsale
|
Crowdsale refactor and add new models (#744)
* Basic idea
* Fine tuning idea
* Add comments / tidy up Crowdsale base class
* fixed TimedCrowdsale constructor
* added simple crowdsale test
* added HODL directory under home to store unused contracts. ugly hack to solve Crowdsale selection in tests, better way?
* Capped no longer inherits from Timed, added capReached() method (replacing hasEnded())
* added SafeMath in TimedCrowdsale for safety, CHECK whether it is inherited from Crowdsale
* several fixes related to separating Capped from Timed. functions renamed, mocks changed. Capped tests passing
* added TimedCrowdsaleImpl.sol, TimedCrowdsale tests, passed
* added Whitelisted implementation and test, passed.
* removed unnecessary super constructor call in WhitelistedCrowdsale, removed unused dependencies in tests
* renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale, implemented IndividuallyCappedCrowdsaleImpl.sol and corresponding tests, passed.
* homogeneized use of using SafeMath for uint256 across validation crowdsales. checked that it IS indeed inherited, but leaving it there as per Frans suggestion.
* adding questions.md where I track questions, bugs and progress
* modified VariablePriceCrowdsale, added Impl.
* finished VariablePrice, fixed sign, added test, passing.
* changed VariablePrice to IncreasingPrice, added corresponding require()
* MintedCrowdsale done, mock implemented, test passing
* PremintedCrowdsale done, mocks, tests passing
* checked FinalizableCrowdsale
* PostDeliveryCrowdsale done, mock, tests passing.
* RefundableCrowdsale done. Detached Vault. modified mock and test, passing
* renamed crowdsale-refactor to crowdsale in contracts and test
* deleted HODL old contracts
* polished variable names in tests
* fixed typos and removed comments in tests
* Renamed 'crowdsale-refactor' to 'crowdsale' in all imports
* Fix minor param naming issues in Crowdsale functions and added documentation to Crowdsale.sol
* Added documentation to Crowdsale extensions
* removed residual comments and progress tracking files
* added docs for validation crowdsales
* Made user promises in PostDeliveryCrowdsale public so that users can query their promised token balance.
* added docs for distribution crowdsales
* renamed PremintedCrowdsale to AllowanceCrowdsale
* added allowance check function and corresponding test. fixed filename in AllowanceCrowdsale mock.
* spilt Crowdsale _postValidatePurchase in _postValidatePurchase and _updatePurchasingState. changed IndividuallyCappedCrowdsale accordingly.
* polished tests for linter, salve Travis
* polished IncreasingPriceCrowdsale.sol for linter.
* renamed and polished for linter WhitelistedCrowdsale test.
* fixed indentation in IncreasingPriceCrowdsaleImpl.sol for linter
* fixed ignoring token.mint return value in MintedCrowdsale.sol
* expanded docs throughout, fixed minor issues
* extended test coverage for IndividuallyCappedCrowdsale
* Extended WhitelistedCrwodsale test coverage
* roll back decoupling of RefundVault in RefundableCrowdsale
* moved cap exceedance checks in Capped and IndividuallyCapped crowdsales to _preValidatePurchase to save gas
* revert name change, IndividuallyCapped to UserCapped
* extended docs.
* added crowd whitelisting with tests
* added group capping, plus tests
* added modifiers in TimedCrowdsale and WhitelistedCrowdsale
* polished tests for linter
* moved check of whitelisted to modifier, mainly for testing coverage
* fixed minor ordering/polishingafter review
* modified TimedCrowdsale modifier/constructor ordering
* unchanged truffle-config.js
* changed indentation of visibility modifier in mocks
* changed naming of modifier and function to use Open/Closed for TimedCrowdsale
* changed ordering of constructor calls in SampleCrowdsale
* changed startTime and endTime to openingTime and closingTime throughout
* fixed exceeding line lenght for linter
* renamed _emitTokens to _deliverTokens
* renamed addCrowdToWhitelist to addManyToWhitelist
* renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale
7 years ago
|
|
|
* @dev Crowdsale is a base contract for managing a token crowdsale,
|
|
|
|
* allowing investors to purchase tokens with ether. This contract implements
|
|
|
|
* such functionality in its most fundamental form and can be extended to provide additional
|
|
|
|
* functionality and/or custom behavior.
|
|
|
|
* The external interface represents the basic interface for purchasing tokens, and conforms
|
|
|
|
* the base architecture for crowdsales. It is *not* intended to be modified / overridden.
|
|
|
|
* The internal interface conforms the extensible and modifiable surface of crowdsales. Override
|
|
|
|
* the methods to add functionality. Consider using 'super' where appropriate to concatenate
|
Crowdsale refactor and add new models (#744)
* Basic idea
* Fine tuning idea
* Add comments / tidy up Crowdsale base class
* fixed TimedCrowdsale constructor
* added simple crowdsale test
* added HODL directory under home to store unused contracts. ugly hack to solve Crowdsale selection in tests, better way?
* Capped no longer inherits from Timed, added capReached() method (replacing hasEnded())
* added SafeMath in TimedCrowdsale for safety, CHECK whether it is inherited from Crowdsale
* several fixes related to separating Capped from Timed. functions renamed, mocks changed. Capped tests passing
* added TimedCrowdsaleImpl.sol, TimedCrowdsale tests, passed
* added Whitelisted implementation and test, passed.
* removed unnecessary super constructor call in WhitelistedCrowdsale, removed unused dependencies in tests
* renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale, implemented IndividuallyCappedCrowdsaleImpl.sol and corresponding tests, passed.
* homogeneized use of using SafeMath for uint256 across validation crowdsales. checked that it IS indeed inherited, but leaving it there as per Frans suggestion.
* adding questions.md where I track questions, bugs and progress
* modified VariablePriceCrowdsale, added Impl.
* finished VariablePrice, fixed sign, added test, passing.
* changed VariablePrice to IncreasingPrice, added corresponding require()
* MintedCrowdsale done, mock implemented, test passing
* PremintedCrowdsale done, mocks, tests passing
* checked FinalizableCrowdsale
* PostDeliveryCrowdsale done, mock, tests passing.
* RefundableCrowdsale done. Detached Vault. modified mock and test, passing
* renamed crowdsale-refactor to crowdsale in contracts and test
* deleted HODL old contracts
* polished variable names in tests
* fixed typos and removed comments in tests
* Renamed 'crowdsale-refactor' to 'crowdsale' in all imports
* Fix minor param naming issues in Crowdsale functions and added documentation to Crowdsale.sol
* Added documentation to Crowdsale extensions
* removed residual comments and progress tracking files
* added docs for validation crowdsales
* Made user promises in PostDeliveryCrowdsale public so that users can query their promised token balance.
* added docs for distribution crowdsales
* renamed PremintedCrowdsale to AllowanceCrowdsale
* added allowance check function and corresponding test. fixed filename in AllowanceCrowdsale mock.
* spilt Crowdsale _postValidatePurchase in _postValidatePurchase and _updatePurchasingState. changed IndividuallyCappedCrowdsale accordingly.
* polished tests for linter, salve Travis
* polished IncreasingPriceCrowdsale.sol for linter.
* renamed and polished for linter WhitelistedCrowdsale test.
* fixed indentation in IncreasingPriceCrowdsaleImpl.sol for linter
* fixed ignoring token.mint return value in MintedCrowdsale.sol
* expanded docs throughout, fixed minor issues
* extended test coverage for IndividuallyCappedCrowdsale
* Extended WhitelistedCrwodsale test coverage
* roll back decoupling of RefundVault in RefundableCrowdsale
* moved cap exceedance checks in Capped and IndividuallyCapped crowdsales to _preValidatePurchase to save gas
* revert name change, IndividuallyCapped to UserCapped
* extended docs.
* added crowd whitelisting with tests
* added group capping, plus tests
* added modifiers in TimedCrowdsale and WhitelistedCrowdsale
* polished tests for linter
* moved check of whitelisted to modifier, mainly for testing coverage
* fixed minor ordering/polishingafter review
* modified TimedCrowdsale modifier/constructor ordering
* unchanged truffle-config.js
* changed indentation of visibility modifier in mocks
* changed naming of modifier and function to use Open/Closed for TimedCrowdsale
* changed ordering of constructor calls in SampleCrowdsale
* changed startTime and endTime to openingTime and closingTime throughout
* fixed exceeding line lenght for linter
* renamed _emitTokens to _deliverTokens
* renamed addCrowdToWhitelist to addManyToWhitelist
* renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale
7 years ago
|
|
|
* behavior.
|
|
|
|
*/
|
|
|
|
contract Crowdsale is Context, ReentrancyGuard {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
using SafeERC20 for IERC20;
|
|
|
|
|
|
|
|
// The token being sold
|
|
|
|
IERC20 private _token;
|
|
|
|
|
|
|
|
// Address where funds are collected
|
|
|
|
address payable private _wallet;
|
|
|
|
|
|
|
|
// How many token units a buyer gets per wei.
|
|
|
|
// The rate is the conversion between wei and the smallest and indivisible token unit.
|
|
|
|
// So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK
|
|
|
|
// 1 wei will give you 1 unit, or 0.001 TOK.
|
|
|
|
uint256 private _rate;
|
|
|
|
|
|
|
|
// Amount of wei raised
|
|
|
|
uint256 private _weiRaised;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event for token purchase logging
|
|
|
|
* @param purchaser who paid for the tokens
|
|
|
|
* @param beneficiary who got the tokens
|
|
|
|
* @param value weis paid for purchase
|
|
|
|
* @param amount amount of tokens purchased
|
|
|
|
*/
|
|
|
|
event TokensPurchased(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param rate Number of token units a buyer gets per wei
|
|
|
|
* @dev The rate is the conversion between wei and the smallest and indivisible
|
|
|
|
* token unit. So, if you are using a rate of 1 with a ERC20Detailed token
|
|
|
|
* with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
|
|
|
|
* @param wallet Address where collected funds will be forwarded to
|
|
|
|
* @param token Address of the token being sold
|
|
|
|
*/
|
|
|
|
constructor (uint256 rate, address payable wallet, IERC20 token) public {
|
|
|
|
require(rate > 0, "Crowdsale: rate is 0");
|
|
|
|
require(wallet != address(0), "Crowdsale: wallet is the zero address");
|
|
|
|
require(address(token) != address(0), "Crowdsale: token is the zero address");
|
|
|
|
|
|
|
|
_rate = rate;
|
|
|
|
_wallet = wallet;
|
|
|
|
_token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev fallback function ***DO NOT OVERRIDE***
|
|
|
|
* Note that other contracts will transfer funds with a base gas stipend
|
|
|
|
* of 2300, which is not enough to call buyTokens. Consider calling
|
|
|
|
* buyTokens directly when purchasing tokens from a contract.
|
|
|
|
*/
|
|
|
|
function () external payable {
|
|
|
|
buyTokens(_msgSender());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the token being sold.
|
|
|
|
*/
|
|
|
|
function token() public view returns (IERC20) {
|
|
|
|
return _token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the address where funds are collected.
|
|
|
|
*/
|
|
|
|
function wallet() public view returns (address payable) {
|
|
|
|
return _wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the number of token units a buyer gets per wei.
|
|
|
|
*/
|
|
|
|
function rate() public view returns (uint256) {
|
|
|
|
return _rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the amount of wei raised.
|
|
|
|
*/
|
|
|
|
function weiRaised() public view returns (uint256) {
|
|
|
|
return _weiRaised;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev low level token purchase ***DO NOT OVERRIDE***
|
|
|
|
* This function has a non-reentrancy guard, so it shouldn't be called by
|
|
|
|
* another `nonReentrant` function.
|
|
|
|
* @param beneficiary Recipient of the token purchase
|
|
|
|
*/
|
|
|
|
function buyTokens(address beneficiary) public nonReentrant payable {
|
|
|
|
uint256 weiAmount = msg.value;
|
|
|
|
_preValidatePurchase(beneficiary, weiAmount);
|
|
|
|
|
|
|
|
// calculate token amount to be created
|
|
|
|
uint256 tokens = _getTokenAmount(weiAmount);
|
|
|
|
|
|
|
|
// update state
|
|
|
|
_weiRaised = _weiRaised.add(weiAmount);
|
|
|
|
|
|
|
|
_processPurchase(beneficiary, tokens);
|
|
|
|
emit TokensPurchased(_msgSender(), beneficiary, weiAmount, tokens);
|
|
|
|
|
|
|
|
_updatePurchasingState(beneficiary, weiAmount);
|
|
|
|
|
|
|
|
_forwardFunds();
|
|
|
|
_postValidatePurchase(beneficiary, weiAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met.
|
|
|
|
* Use `super` in contracts that inherit from Crowdsale to extend their validations.
|
|
|
|
* Example from CappedCrowdsale.sol's _preValidatePurchase method:
|
|
|
|
* super._preValidatePurchase(beneficiary, weiAmount);
|
|
|
|
* require(weiRaised().add(weiAmount) <= cap);
|
|
|
|
* @param beneficiary Address performing the token purchase
|
|
|
|
* @param weiAmount Value in wei involved in the purchase
|
|
|
|
*/
|
|
|
|
function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
|
|
|
|
require(beneficiary != address(0), "Crowdsale: beneficiary is the zero address");
|
|
|
|
require(weiAmount != 0, "Crowdsale: weiAmount is 0");
|
|
|
|
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid
|
|
|
|
* conditions are not met.
|
|
|
|
* @param beneficiary Address performing the token purchase
|
|
|
|
* @param weiAmount Value in wei involved in the purchase
|
|
|
|
*/
|
|
|
|
function _postValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
|
|
|
|
// solhint-disable-previous-line no-empty-blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends
|
|
|
|
* its tokens.
|
|
|
|
* @param beneficiary Address performing the token purchase
|
|
|
|
* @param tokenAmount Number of tokens to be emitted
|
|
|
|
*/
|
|
|
|
function _deliverTokens(address beneficiary, uint256 tokenAmount) internal {
|
|
|
|
_token.safeTransfer(beneficiary, tokenAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Executed when a purchase has been validated and is ready to be executed. Doesn't necessarily emit/send
|
|
|
|
* tokens.
|
|
|
|
* @param beneficiary Address receiving the tokens
|
|
|
|
* @param tokenAmount Number of tokens to be purchased
|
|
|
|
*/
|
|
|
|
function _processPurchase(address beneficiary, uint256 tokenAmount) internal {
|
|
|
|
_deliverTokens(beneficiary, tokenAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Override for extensions that require an internal state to check for validity (current user contributions,
|
|
|
|
* etc.)
|
|
|
|
* @param beneficiary Address receiving the tokens
|
|
|
|
* @param weiAmount Value in wei involved in the purchase
|
|
|
|
*/
|
|
|
|
function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
|
|
|
|
// solhint-disable-previous-line no-empty-blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Override to extend the way in which ether is converted to tokens.
|
|
|
|
* @param weiAmount Value in wei to be converted into tokens
|
|
|
|
* @return Number of tokens that can be purchased with the specified _weiAmount
|
|
|
|
*/
|
|
|
|
function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
|
|
|
|
return weiAmount.mul(_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Determines how ETH is stored/forwarded on purchases.
|
|
|
|
*/
|
|
|
|
function _forwardFunds() internal {
|
|
|
|
_wallet.transfer(msg.value);
|
|
|
|
}
|
|
|
|
}
|