When I create a token, how do I specify how much of it I want to hold for myself and not offer for sale? I have seen the mint and burn functions and those would be ideal to implement as well. I don't really want to do a crowdsale just a simple token that can be bought/sold on an exchange.
1 Answers
There are several ways to achieve what you want, but before giving my answer, I recommend you acquaint yourself with the ERC-20 specification.
First things first, you need a token implementation. I suggest you use OpenZeppelin's ERC20.sol (it's known to be safe, battle-tested in many many projects).
Then, you basically inherit from the ERC20 contract and augment the functionality as you wish. Here's one way to allocate a specific amount of tokens to yourself:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MyERC20 is ERC20 {
constructor(string memory name_, string memory symbol_) ERc20(name_, symbol_, 18) {
// Mint 1 billion tokens to the contract creator.
_mint(msg.sender, 1000000000000000000000000000);
}
}
The code above will allocate 1 billion tokens (recall that the token has 18 decimals by default, so 1 billion is represented as 1e27) to msg.sender. You could alternatively pass a third argument to the constructor and mint the tokens to that account instead.
I recommend using this technique because it's the most transparent way of distributing the initial token balances. Contract readers will see that there is no risk of inflating the token supply, ever.
However, if you want to keep the possibility of inflation, you should inherit from Ownable and implement a custom mint method that is protected by the onlyOwner modifier.
- 17,902
- 6
- 73
- 143
burn, and yes inherit fromOwnableunless you want everyone to be able to burn your tokens. – Paul Razvan Berg May 18 '21 at 19:16