Crowdsales
This page is incomplete. We’re working to improve it for the next release. Stay tuned! |
Core
Crowdsale
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 behavior.
constructor(uint256 rate, address payable wallet, contract IERC20 token)
public
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.
fallback()
external
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.
buyTokens(address beneficiary)
public
low level token purchase DO NOT OVERRIDE
This function has a non-reentrancy guard, so it shouldn’t be called by
another nonReentrant
function.
_preValidatePurchase(address beneficiary, uint256 weiAmount)
internal
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);
_postValidatePurchase(address beneficiary, uint256 weiAmount)
internal
Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
_deliverTokens(address beneficiary, uint256 tokenAmount)
internal
Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
_processPurchase(address beneficiary, uint256 tokenAmount)
internal
Executed when a purchase has been validated and is ready to be executed. Doesn’t necessarily emit/send tokens.
_updatePurchasingState(address beneficiary, uint256 weiAmount)
internal
Override for extensions that require an internal state to check for validity (current user contributions, etc.)
Emission
AllowanceCrowdsale
Extension of Crowdsale where tokens are held by a wallet, which approves an allowance to the crowdsale.
Validation
CappedCrowdsale
Crowdsale with a limit for total contributions.
IndividuallyCappedCrowdsale
Crowdsale with per-beneficiary caps.
setCap(address beneficiary, uint256 cap)
external
Sets a specific beneficiary’s maximum contribution.
getContribution(address beneficiary) → uint256
public
Returns the amount contributed so far by a specific beneficiary.
PausableCrowdsale
Extension of Crowdsale contract where purchases can be paused and unpaused by the pauser role.
TimedCrowdsale
Crowdsale accepting contributions only within a time frame.
constructor(uint256 openingTime, uint256 closingTime)
public
Constructor, takes crowdsale opening and closing times.
hasClosed() → bool
public
Checks whether the period in which the crowdsale is open has already elapsed.
Distribution
FinalizableCrowdsale
Extension of TimedCrowdsale with a one-off finalization action, where one can do extra work after finishing.
finalize()
public
Must be called after crowdsale ends, to do some extra finalization work. Calls the contract’s finalization function.
PostDeliveryCrowdsale
Crowdsale that locks tokens from withdrawal until it ends.
_processPurchase(address beneficiary, uint256 tokenAmount)
internal
Overrides parent by storing due balances, and delivering tokens to the vault instead of the end user. This
ensures that the tokens will be available by the time they are withdrawn (which may not be the case if
_deliverTokens
was called later).
RefundableCrowdsale
Extension of FinalizableCrowdsale
contract that adds a funding goal, and the possibility of users
getting a refund if goal is not met.
Deprecated, use RefundablePostDeliveryCrowdsale
instead. Note that if you allow tokens to be traded before the goal
is met, then an attack is possible in which the attacker purchases tokens from the crowdsale and when they sees that
the goal is unlikely to be met, they sell their tokens (possibly at a discount). The attacker will be refunded when
the crowdsale is finalized, and the users that purchased from them will be left with worthless tokens.
RefundablePostDeliveryCrowdsale
Extension of RefundableCrowdsale contract that only delivers the tokens once the crowdsale has closed and the goal met, preventing refunds to be issued to token holders.