You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
11 KiB
245 lines
11 KiB
6 years ago
|
= Crowdsales
|
||
6 years ago
|
|
||
|
Crowdsales are a popular use for Ethereum; they let you allocate tokens to network participants in various ways, mostly in exchange for Ether. They come in a variety of shapes and flavors, so let's go over the various types available in OpenZeppelin and how to use them.
|
||
|
|
||
6 years ago
|
Crowdsales have a bunch of different properties, but here are some important ones: - Price & Rate Configuration - Does your crowdsale sell tokens at a fixed price? - Does the price change over time or as a function of demand? - Emission - How is this token actually sent to participants? - Validation — Who is allowed to purchase tokens? - Are there KYC / AML checks? - Is there a max cap on tokens? - What if that cap is per-participant? - Is there a starting and ending time frame? - Distribution - Does distribution of funds happen in real-time or after the crowdsale? - Can participants receive a refund if the goal is not met?
|
||
|
|
||
|
To manage all of the different combinations and flavors of crowdsales, OpenZeppelin provides a highly configurable link:api/crowdsale#crowdsale[`Crowdsale`] base contract that can be combined with various other functionalities to construct a bespoke crowdsale.
|
||
|
|
||
|
[[crowdsale-rate]]
|
||
|
== Crowdsale Rate
|
||
6 years ago
|
|
||
|
Understanding the rate of a crowdsale is super important, and mistakes here are a common source of bugs.
|
||
|
|
||
6 years ago
|
✨ *HOLD UP FAM THIS IS IMPORTANT* ✨
|
||
6 years ago
|
|
||
6 years ago
|
Firstly, *all currency math is done in the smallest unit of that currency and converted to the correct decimal places when _displaying_ the currency*.
|
||
6 years ago
|
|
||
|
This means that when you do math in your smart contracts, you need to understand that you're adding, dividing, and multiplying the smallest amount of a currency (like wei), _not_ the commonly-used displayed value of the currency (Ether).
|
||
|
|
||
|
In Ether, the smallest unit of the currency is wei, and `1 ETH === 10^18 wei`. In tokens, the process is _very similar_: `1 TKN === 10^(decimals) TKNbits`.
|
||
|
|
||
6 years ago
|
* The smallest unit of a token is "bits" or `TKNbits`.
|
||
|
* The display value of a token is `TKN`, which is `TKNbits * 10^(decimals)`
|
||
6 years ago
|
|
||
6 years ago
|
What people usually call "one token" is actually a bunch of TKNbits, displayed to look like `1 TKN`. This is the same relationship that Ether and wei have. And what you're _always_ doing calculations in is *TKNbits and wei*.
|
||
6 years ago
|
|
||
6 years ago
|
So, if you want to issue someone "one token for every 2 wei" and your decimals are 18, your rate is `0.5e18`. Then, when I send you `2 wei`, your crowdsale issues me `2 * 0.5e18 TKNbits`, which is exactly equal to `10^18 TKNbits` and is displayed as `1 TKN`.
|
||
6 years ago
|
|
||
|
If you want to issue someone "`1 TKN` for every `1 ETH`", and your decimals are 18, your rate is `1`. This is because what's actually happening with the math is that the contract sees a user send `10^18 wei`, not `1 ETH`. Then it uses your rate of 1 to calculate `TKNbits = rate * wei`, or `1 * 10^18`, which is still `10^18`. And because your decimals are 18, this is displayed as `1 TKN`.
|
||
|
|
||
|
One more for practice: if I want to issue "1 TKN for every dollar (USD) in Ether", we would calculate it as follows:
|
||
|
|
||
6 years ago
|
* assume 1 ETH == $400
|
||
|
* therefore, 10^18 wei = $400
|
||
|
* therefore, 1 USD is `10^18 / 400`, or `2.5 * 10^15 wei`
|
||
|
* we have a decimals of 18, so we'll use `10 ^ 18 TKNbits` instead of `1 TKN`
|
||
|
* therefore, if the participant sends the crowdsale `2.5 * 10^15 wei` we should give them `10 ^ 18 TKNbits`
|
||
|
* therefore the rate is `2.5 * 10^15 wei === 10^18 TKNbits`, or `1 wei = 400 TKNbits`
|
||
|
* therefore, our rate is `400`
|
||
6 years ago
|
|
||
|
(this process is pretty straightforward when you keep 18 decimals, the same as Ether/wei)
|
||
|
|
||
6 years ago
|
[[token-emission]]
|
||
|
== Token Emission
|
||
6 years ago
|
|
||
|
One of the first decisions you have to make is "how do I get these tokens to users?". This is usually done in one of three ways:
|
||
|
|
||
6 years ago
|
* (default) — The `Crowdsale` contract owns tokens and simply transfers tokens from its own ownership to users that purchase them.
|
||
|
* link:api/crowdsale#mintedcrowdsale[`MintedCrowdsale`] — The `Crowdsale` mints tokens when a purchase is made.
|
||
|
* link:api/crowdsale#allowancecrowdsale[`AllowanceCrowdsale`] — The `Crowdsale` is granted an allowance to another wallet (like a Multisig) that already owns the tokens to be sold in the crowdsale.
|
||
6 years ago
|
|
||
6 years ago
|
[[default-emission]]
|
||
|
=== Default Emission
|
||
6 years ago
|
|
||
|
In the default scenario, your crowdsale must own the tokens that are sold. You can send the crowdsale tokens through a variety of methods, but here's what it looks like in Solidity:
|
||
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
IERC20(tokenAddress).transfer(CROWDSALE_ADDRESS, SOME_TOKEN_AMOUNT);
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
|
Then when you deploy your crowdsale, simply tell it about the token
|
||
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
new Crowdsale(
|
||
|
1, // rate in TKNbits
|
||
|
MY_WALLET, // address where Ether is sent
|
||
|
TOKEN_ADDRESS // the token contract address
|
||
|
);
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
6 years ago
|
[[minted-crowdsale]]
|
||
|
=== Minted Crowdsale
|
||
6 years ago
|
|
||
6 years ago
|
To use a link:api/crowdsale#mintedcrowdsale[`MintedCrowdsale`], your token must also be a link:api/token/ERC20#erc20mintable[`ERC20Mintable`] token that the crowdsale has permission to mint from. This can look like:
|
||
6 years ago
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
contract MyToken is ERC20, ERC20Mintable {
|
||
6 years ago
|
// ... see "Tokens" for more info
|
||
6 years ago
|
}
|
||
|
|
||
|
contract MyCrowdsale is Crowdsale, MintedCrowdsale {
|
||
|
constructor(
|
||
|
uint256 rate, // rate in TKNbits
|
||
6 years ago
|
address payable wallet,
|
||
|
IERC20 token
|
||
6 years ago
|
)
|
||
|
MintedCrowdsale()
|
||
|
Crowdsale(rate, wallet, token)
|
||
|
public
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
contract MyCrowdsaleDeployer {
|
||
|
constructor()
|
||
|
public
|
||
|
{
|
||
|
// create a mintable token
|
||
|
ERC20Mintable token = new MyToken();
|
||
|
|
||
|
// create the crowdsale and tell it about the token
|
||
|
Crowdsale crowdsale = new MyCrowdsale(
|
||
|
1, // rate, still in TKNbits
|
||
|
msg.sender, // send Ether to the deployer
|
||
|
address(token) // the token
|
||
|
);
|
||
|
// transfer the minter role from this contract (the default)
|
||
|
// to the crowdsale, so it can mint tokens
|
||
|
token.addMinter(address(crowdsale));
|
||
|
token.renounceMinter();
|
||
|
}
|
||
|
}
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
6 years ago
|
[[allowancecrowdsale]]
|
||
|
=== AllowanceCrowdsale
|
||
6 years ago
|
|
||
6 years ago
|
Use an link:api/crowdsale#allowancecrowdsale[`AllowanceCrowdsale`] to send tokens from another wallet to the participants of the crowdsale. In order for this to work, the source wallet must give the crowdsale an allowance via the ERC20 link:api/token/ERC20#IERC20.approve(address,uint256)[`approve`] method.
|
||
6 years ago
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
contract MyCrowdsale is Crowdsale, AllowanceCrowdsale {
|
||
6 years ago
|
constructor(
|
||
|
uint256 rate,
|
||
6 years ago
|
address payable wallet,
|
||
|
IERC20 token,
|
||
6 years ago
|
address tokenWallet // <- new argument
|
||
|
)
|
||
|
AllowanceCrowdsale(tokenWallet) // <- used here
|
||
|
Crowdsale(rate, wallet, token)
|
||
|
public
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
|
Then after the crowdsale is created, don't forget to approve it to use your tokens!
|
||
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
IERC20(tokenAddress).approve(CROWDSALE_ADDRESS, SOME_TOKEN_AMOUNT);
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
6 years ago
|
[[validation]]
|
||
|
== Validation
|
||
6 years ago
|
|
||
|
There are a bunch of different validation requirements that your crowdsale might be a part of:
|
||
|
|
||
6 years ago
|
* link:api/crowdsale#cappedcrowdsale[`CappedCrowdsale`] — adds a cap to your crowdsale, invalidating any purchases that would exceed that cap
|
||
|
* link:api/crowdsale#individuallycappedcrowdsale[`IndividuallyCappedCrowdsale`] — caps an individual's contributions.
|
||
|
* link:api/crowdsale#whitelistcrowdsale[`WhitelistCrowdsale`] — only allow whitelisted participants to purchase tokens. this is useful for putting your KYC / AML whitelist on-chain!
|
||
|
* link:api/crowdsale#timedcrowdsale[`TimedCrowdsale`] — adds an link:api/crowdsale#TimedCrowdsale.openingTime()[`openingTime`] and link:api/crowdsale#TimedCrowdsale.closingTime()[`closingTime`] to your crowdsale
|
||
6 years ago
|
|
||
|
Simply mix and match these crowdsale flavors to your heart's content:
|
||
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
contract MyCrowdsale is Crowdsale, CappedCrowdsale, TimedCrowdsale {
|
||
6 years ago
|
|
||
|
constructor(
|
||
6 years ago
|
uint256 rate, // rate, in TKNbits
|
||
|
address payable wallet, // wallet to send Ether
|
||
|
IERC20 token, // the token
|
||
|
uint256 cap, // total cap, in wei
|
||
|
uint256 openingTime, // opening time in unix epoch seconds
|
||
|
uint256 closingTime // closing time in unix epoch seconds
|
||
6 years ago
|
)
|
||
|
CappedCrowdsale(cap)
|
||
|
TimedCrowdsale(openingTime, closingTime)
|
||
|
Crowdsale(rate, wallet, token)
|
||
|
public
|
||
|
{
|
||
|
// nice, we just created a crowdsale that's only open
|
||
|
// for a certain amount of time
|
||
|
// and stops accepting contributions once it reaches `cap`
|
||
|
}
|
||
|
}
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
6 years ago
|
[[distribution]]
|
||
|
== Distribution
|
||
6 years ago
|
|
||
|
There comes a time in every crowdsale's life where it must relinquish the tokens it's been entrusted with. It's your decision as to when that happens!
|
||
|
|
||
|
The default behavior is to release tokens as participants purchase them, but sometimes that may not be desirable. For example, what if we want to give users a refund if we don't hit a minimum raised in the sale? Or, maybe we want to wait until after the sale is over before users can claim their tokens and start trading them, perhaps for compliance reasons?
|
||
|
|
||
|
OpenZeppelin is here to make that easy!
|
||
|
|
||
6 years ago
|
[[postdeliverycrowdsale]]
|
||
|
=== PostDeliveryCrowdsale
|
||
6 years ago
|
|
||
6 years ago
|
The link:api/crowdsale#postdeliverycrowdsale[`PostDeliveryCrowdsale`], as its name implies, distributes tokens after the crowdsale has finished, letting users call link:api/crowdsale#PostDeliveryCrowdsale.withdrawTokens(address)[`withdrawTokens`] in order to claim the tokens they've purchased.
|
||
6 years ago
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
contract MyCrowdsale is Crowdsale, TimedCrowdsale, PostDeliveryCrowdsale {
|
||
6 years ago
|
|
||
|
constructor(
|
||
6 years ago
|
uint256 rate, // rate, in TKNbits
|
||
|
address payable wallet, // wallet to send Ether
|
||
|
IERC20 token, // the token
|
||
|
uint256 openingTime, // opening time in unix epoch seconds
|
||
|
uint256 closingTime // closing time in unix epoch seconds
|
||
6 years ago
|
)
|
||
|
PostDeliveryCrowdsale()
|
||
6 years ago
|
TimedCrowdsale(openingTime, closingTime)
|
||
6 years ago
|
Crowdsale(rate, wallet, token)
|
||
|
public
|
||
|
{
|
||
|
// nice! this Crowdsale will keep all of the tokens until the end of the crowdsale
|
||
|
// and then users can `withdrawTokens()` to get the tokens they're owed
|
||
|
}
|
||
|
}
|
||
6 years ago
|
----
|
||
6 years ago
|
|
||
6 years ago
|
[[refundablecrowdsale]]
|
||
|
=== RefundableCrowdsale
|
||
6 years ago
|
|
||
6 years ago
|
The link:api/crowdsale#refundablecrowdsale[`RefundableCrowdsale`] offers to refund users if a minimum goal is not reached. If the goal is not reached, the users can link:api/crowdsale#RefundableCrowdsale.claimRefund(address%20payable)[`claimRefund`] to get their Ether back.
|
||
6 years ago
|
|
||
6 years ago
|
[source,solidity]
|
||
|
----
|
||
6 years ago
|
contract MyCrowdsale is Crowdsale, RefundableCrowdsale {
|
||
6 years ago
|
|
||
|
constructor(
|
||
6 years ago
|
uint256 rate, // rate, in TKNbits
|
||
|
address payable wallet, // wallet to send Ether
|
||
|
IERC20 token, // the token
|
||
|
uint256 goal // the minimum goal, in wei
|
||
6 years ago
|
)
|
||
|
RefundableCrowdsale(goal)
|
||
|
Crowdsale(rate, wallet, token)
|
||
|
public
|
||
|
{
|
||
|
// nice! this crowdsale will, if it doesn't hit `goal`, allow everyone to get their money back
|
||
|
// by calling claimRefund(...)
|
||
|
}
|
||
|
}
|
||
6 years ago
|
----
|