erc20.adoc 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. = ERC20
  2. An ERC20 token contract keeps track of xref:tokens.adoc#different-kinds-of-tokens[_fungible_ tokens]: any one token is exactly equal to any other token; no tokens have special rights or behavior associated with them. This makes ERC20 tokens useful for things like a *medium of exchange currency*, *voting rights*, *staking*, and more.
  3. OpenZeppelin Contracts provides many ERC20-related contracts. On the xref:api:token/ERC20.adoc[`API reference`] you'll find detailed information on their properties and usage.
  4. [[constructing-an-erc20-token-contract]]
  5. == Constructing an ERC20 Token Contract
  6. Using Contracts, we can easily create our own ERC20 token contract, which will be used to track _Gold_ (GLD), an internal currency in a hypothetical game.
  7. Here's what our GLD token might look like.
  8. [source,solidity]
  9. ----
  10. pragma solidity ^0.5.0;
  11. import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
  12. import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
  13. contract GLDToken is ERC20, ERC20Detailed {
  14. constructor(uint256 initialSupply) ERC20Detailed("Gold", "GLD", 18) public {
  15. _mint(msg.sender, initialSupply);
  16. }
  17. }
  18. ----
  19. Our contracts are often used via https://solidity.readthedocs.io/en/latest/contracts.html#inheritance[inheritance], and here we're reusing xref:api:token/ERC20.adoc#erc20[`ERC20`] for the basic standard implementation and xref:api:token/ERC20.adoc#ERC20Detailed[`ERC20Detailed`] to get the xref:api:token/ERC20.adoc#ERC20Detailed-name--[`name`], xref:api:token/ERC20.adoc#ERC20Detailed-symbol--[`symbol`], and xref:api:token/ERC20.adoc#ERC20Detailed-decimals--[`decimals`] properties. Additionally, we're creating an `initialSupply` of tokens, which will be assigned to the address that deploys the contract.
  20. TIP: For a more complete discussion of ERC20 supply mechanisms, see xref:erc20-supply.adoc[Creating ERC20 Supply].
  21. That's it! Once deployed, we will be able to query the deployer's balance:
  22. [source,javascript]
  23. ----
  24. > GLDToken.balanceOf(deployerAddress)
  25. 1000
  26. ----
  27. We can also xref:api:token/ERC20.adoc#IERC20-transfer-address-uint256-[transfer] these tokens to other accounts:
  28. [source,javascript]
  29. ----
  30. > GLDToken.transfer(otherAddress, 300)
  31. > GLDToken.balanceOf(otherAddress)
  32. 300
  33. > GLDToken.balanceOf(deployerAddress)
  34. 700
  35. ----
  36. [[a-note-on-decimals]]
  37. == A Note on `decimals`
  38. Often, you'll want to be able to divide your tokens into arbitrary amounts: say, if you own `5 GLD`, you may want to send `1.5 GLD` to a friend, and keep `3.5 GLD` to yourself. Unfortunately, Solidity and the EVM do not support this behavior: only integer (whole) numbers can be used, which poses an issue. You may send `1` or `2` tokens, but not `1.5`.
  39. To work around this, xref:api:token/ERC20.adoc#ERC20Detailed[`ERC20Detailed`] provides a xref:api:token/ERC20.adoc#ERC20Detailed-decimals--[`decimals`] field, which is used to specify how many decimal places a token has. To be able to transfer `1.5 GLD`, `decimals` must be at least `1`, since that number has a single decimal place.
  40. How can this be achieved? It's actually very simple: a token contract can use larger integer values, so that a balance of `50` will represent `5 GLD`, a transfer of `15` will correspond to `1.5 GLD` being sent, and so on.
  41. It is important to understand that `decimals` is _only used for display purposes_. All arithmetic inside the contract is still performed on integers, and it is the different user interfaces (wallets, exchanges, etc.) that must adjust the displayed values according to `decimals`. The total token supply and balance of each account are not specified in `GLD`: you need to divide by `10^decimals` to get the actual `GLD` amount.
  42. You'll probably want to use a `decimals` value of `18`, just like Ether and most ERC20 token contracts in use, unless you have a very special reason not to. When minting tokens or transferring them around, you will be actually sending the number `num GLD * 10^decimals`.
  43. So if you want to send `5` tokens using a token contract with 18 decimals, the the method to call will actually be:
  44. ```solidity
  45. transfer(recipient, 5 * 10^18);
  46. ```