New guides (#1792)
* Improved tokens guide, add ERC777.
* Fix typo.
* Add release schedule and api stability.
* Add erc20 supply guide.
* Revamp get started
* Add Solidity version to examples
* Update access control guide.
* Add small disclaimer to blog guides
* Update tokens guide.
* Update docs/access-control.md
Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>
* Update docs/access-control.md
Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>
* Update docs/access-control.md
Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>
* Apply suggestions from code review
Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>
* Apply suggestions from code review
Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>
* Documentation: Typos and add npm init -y to setup instructions (#1793)
* Fix typos in GameItem ERC721 sample contract
* Add npm init -y to create package.json
* Address review comments.
(cherry picked from commit 852e11c2db
)
pull/2057/head
parent
5fd011d93e
commit
aa878d8b69
@ -0,0 +1,42 @@ |
||||
--- |
||||
id: api-stability |
||||
title: API Stability |
||||
--- |
||||
|
||||
On the [OpenZeppelin 2.0 release](https://github.com/OpenZeppelin/openzeppelin-solidity/releases/tag/v2.0.0), we committed ourselves to keeping a stable API. We aim to more precisely define what we understand by _stable_ and _API_ here, so users of the library can understand these guarantees and be confident their project won't break unexpectedly. |
||||
|
||||
In a nutshell, the API being stable means _if your project is working today, it will continue to do so_. New contracts and features will be added in minor releases, but only in a backwards compatible way. |
||||
|
||||
## Versioning scheme |
||||
We follow [SemVer](https://semver.org/), which means API breakage may occur between major releases. Read more about the [release schedule](release-schedule) to know how often this happens (not very). |
||||
|
||||
## Solidity functions |
||||
While the internal implementation of functions may change, their semantics and signature will remain the same. The domain of their arguments will not be less restrictive (e.g. if transferring a value of 0 is disallowed, it will remain disallowed), nor will general state restrictions be lifted (e.g. `whenPaused` modifiers). |
||||
|
||||
If new functions are added to a contract, it will be in a backwards-compatible way: their usage won't be mandatory, and they won't extend functionality in ways that may foreseeable break an application (e.g. [an `internal` method may be added to make it easier to retrieve information that was already available](https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1512)). |
||||
|
||||
### `internal` |
||||
This extends not only to `external` and `public` functions, but also `internal` ones: many OpenZeppelin contracts are meant to be used by inheriting them (e.g. `Pausable`, `PullPayment`, the different `Roles` contracts), and are therefore used by calling these functions. Similarly, since all OpenZeppelin state variables are `private`, they can only be accessed this way (e.g. to create new `ERC20` tokens, instead of manually modifying `totalSupply` and `balances`, `_mint` should be called). |
||||
|
||||
`private` functions have no guarantees on their behavior, usage, or existence. |
||||
|
||||
Finally, sometimes language limitations will force us to e.g. make `internal` a function that should be `private` in order to implement features the way we want to. These cases will be well documented, and the normal stability guarantees won't apply. |
||||
|
||||
## Libraries |
||||
Some of our Solidity libraries use `struct`s to handle internal data that should not be accessed directly (e.g. `Roles`). There's an [open issue](https://github.com/ethereum/solidity/issues/4637) in the Solidity repository requesting a language feature to prevent said access, but it looks like it won't be implemented any time soon. Because of this, we will use leading underscores and mark said `struct`s to make it clear to the user that its contents and layout are _not_ part of the API. |
||||
|
||||
## Events |
||||
No events will be removed, and their arguments won't be changed in any way. New events may be added in later versions, and existing events may be emitted under new, reasonable circumstances (e.g. [from 2.1 on, `ERC20` also emits `Approval` on `transferFrom` calls](https://github.com/OpenZeppelin/openzeppelin-solidity/issues/707)). |
||||
|
||||
## Gas costs |
||||
While attempts will generally be made to lower the gas costs of working with OpenZeppelin contracts, there are no guarantees regarding this. In particular, users should not assume gas costs will not increase when upgrading library versions. |
||||
|
||||
## Bugfixes |
||||
The API stability guarantees may need to be broken in order to fix a bug, and we will do so. This decision won't be made lightly however, and all options will be explored to make the change as non-disruptive as possible. When sufficient, contracts or functions which may result in unsafe behaviour will be deprecated instead of removed (e.g. [#1543](https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1543) and [#1550](https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1550)). |
||||
|
||||
## Solidity compiler version |
||||
Starting on version 0.5.0, the Solidity team switched to a faster release cycle, with minor releases every few weeks (v0.5.0 was released on November 2018, and v0.5.5 on March 2019), and major, breaking-change releases every couple months (with v0.6.0 scheduled for late March 2019). Including the compiler version in OpenZeppelin's stability guarantees would therefore force the library to either stick to old compilers, or release frequent major updates simply to keep up with newer Solidity releases. |
||||
|
||||
Because of this, **the minimum required Solidity compiler version is not part of the stability guarantees**, and users may be required to upgrade their compiler when using newer versions of OpenZeppelin. Bugfixes will still be backported to older library releases so that all versions currently in use receive these updates. |
||||
|
||||
You can read more about the rationale behind this, the other options we considered and why we went down this path [here](https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1498#issuecomment-449191611). |
@ -0,0 +1,102 @@ |
||||
--- |
||||
id: erc20-supply |
||||
title: Creating ERC20 Supply |
||||
--- |
||||
|
||||
In this guide you will learn how to create an ERC20 token with a custom supply mechanism. We will showcase two idiomatic ways to use OpenZeppelin for this purpose that you will be able to apply to your smart contract development practice. |
||||
|
||||
*** |
||||
|
||||
The standard interface implemented by tokens built on Ethereum is called ERC20, and OpenZeppelin includes a widely used implementation of it: the aptly named [`ERC20`](https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v2.1.2/contracts/token/ERC20/ERC20.sol) contract. This contract, like the standard itself, is quite simple and bare-bones. In fact, if you try deploy an instance of `ERC20` as-is it will be quite literally useless... it will have no supply! What use is a token with no supply? |
||||
|
||||
The way that supply is created is not defined in the ERC20 document. Every token is free to experiment with their own mechanisms, ranging from the most decentralized to the most centralized, from the most naive to the most researched, and more. |
||||
|
||||
#### Fixed supply |
||||
|
||||
Let's say we want a token with a fixed supply of 1000, initially allocated to the account that deploys the contract. If you've used OpenZeppelin v1, you may have written code like the following. |
||||
|
||||
```solidity |
||||
contract ERC20FixedSupply is ERC20 { |
||||
constructor() public { |
||||
totalSupply += 1000; |
||||
balances[msg.sender] += 1000; |
||||
} |
||||
} |
||||
``` |
||||
|
||||
Starting with OpenZeppelin v2 this pattern is not only discouraged, but disallowed. The variables `totalSupply` and `balances` are now private implementation details of `ERC20`, and you can't directly write to them. Instead, there is an internal `_mint` function that will do exactly this. |
||||
|
||||
```solidity |
||||
contract ERC20FixedSupply is ERC20 { |
||||
constructor() public { |
||||
_mint(msg.sender, 1000); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
Encapsulating state like this makes it safer to extend contracts. For instance, in the first example we had to manually keep the `totalSupply` in sync with the modified balances, which is easy to forget. In fact, I omitted something else that is also easily forgotten: the `Transfer` event that is required by the standard, and which is relied on by some clients. The second example does not have this bug, because the internal `_mint` function takes care of it. |
||||
|
||||
#### Rewarding miners |
||||
|
||||
The internal `_mint` function is the key building block that allows us to write ERC20 extensions that implement a supply mechanism. |
||||
|
||||
The mechanism we will implement is a token reward for the miners that produce Ethereum blocks. In Solidity we can access the address of the current block's miner in the global variable `block.coinbase`. We will mint a token reward to this address whenever someone calls the function `mintMinerReward()` on our token. The mechanism may sound silly, but you never know what kind of dynamic this might result in, and it's worth analyzing and experimenting with! |
||||
|
||||
```solidity |
||||
contract ERC20WithMinerReward is ERC20 { |
||||
function mintMinerReward() public { |
||||
_mint(block.coinbase, 1000); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
As we can see, `_mint` makes it super easy to do this correctly. |
||||
|
||||
#### Modularizing the mechanism |
||||
|
||||
There is one supply mechanism already included in OpenZeppelin: [`ERC20Mintable`](https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v2.1.2/contracts/token/ERC20/ERC20Mintable.sol). This is a generic mechanism in which a set of accounts is assigned the `minter` role, granting them the permission to call a `mint` function, an external version of `_mint`. |
||||
|
||||
This can be used for centralized minting, where an externally owned account (i.e. someone with a pair of cryptographic keys) decides how much supply to create and to whom. There are very legitimate use cases for this mechanism, such as [traditional asset-backed stablecoins](https://medium.com/reserve-currency/why-another-stablecoin-866f774afede#3aea). |
||||
|
||||
The accounts with the minter role don't need to be externally owned, though, and can just as well be smart contracts that implement a trustless mechanism. We can in fact implement the same behavior as the previous section. |
||||
|
||||
```solidity |
||||
contract MinerRewardMinter { |
||||
ERC20Mintable _token; |
||||
|
||||
constructor(ERC20Mintable token) public { |
||||
_token = token; |
||||
} |
||||
|
||||
function mintMinerReward() public { |
||||
_token.mint(block.coinbase, 1000); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
This contract, when initialized with an `ERC20Mintable` instance, will result in exactly the same behavior implemented in the previous section. What is interesting about using `ERC20Mintable` is that we can easily combine multiple supply mechanisms by assigning the role to multiple contracts, and moreover that we can do this dynamically. |
||||
|
||||
#### Automating the reward |
||||
|
||||
Additionally to `_mint`, `ERC20` provides other internal functions that can be used or extended, such as `_transfer`. This function implements token transfers and is used by `ERC20`, so it can be used to trigger functionality automatically. This is something that can't be done with the `ERC20Mintable` approach. |
||||
|
||||
Adding to our previous supply mechanism, we can use this to mint a miner reward for every token transfer that is included in the blockchain. |
||||
|
||||
```solidity |
||||
contract ERC20WithAutoMinerReward is ERC20 { |
||||
function _mintMinerReward() internal { |
||||
_mint(block.coinbase, 1000); |
||||
} |
||||
|
||||
function _transfer(address from, address to, uint256 value) internal { |
||||
_mintMinerReward(); |
||||
super._transfer(from, to, value); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
Note how we override `_transfer` to first mint the miner reward and then run the original implementation by calling `super._transfer`. This last step is very important to preserve the original semantics of ERC20 transfers. |
||||
|
||||
#### Wrapping up |
||||
|
||||
We've seen two ways to implement ERC20 supply mechanisms: internally through `_mint`, and externally through `ERC20Mintable`. Hopefully this has helped you understand how to use OpenZeppelin and some of the design principles behind it, and you can apply them to your own smart contracts. |
@ -0,0 +1,18 @@ |
||||
--- |
||||
id: release-schedule |
||||
title: Release Schedule |
||||
--- |
||||
|
||||
OpenZeppelin follows a [semantic versioning scheme](api-stability). |
||||
|
||||
#### Minor releases |
||||
|
||||
OpenZeppelin has a **5 week release cycle**. This means that every five weeks a new release is published. |
||||
|
||||
At the beginning of the release cycle we decide which issues we want to prioritize, and assign them to [a milestone on GitHub](https://github.com/OpenZeppelin/openzeppelin-solidity/milestones). During the next five weeks, they are worked on and fixed. |
||||
|
||||
Once the milestone is complete, we publish a feature-frozen release candidate. The purpose of the release candidate is to have a period where the community can review the new code before the actual release. If important problems are discovered, several more release candidates may be required. After a week of no more changes to the release candidate, the new version is published. |
||||
|
||||
#### Major releases |
||||
|
||||
Every several months a new major release may come out. These are not scheduled, but will be based on the need to release breaking changes such as a redesign of a core feature of the library (e.g. [roles](https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1146) in 2.0). Since we value stability, we aim for these to happen infrequently (expect no less than six months between majors). However, we may be forced to release one when there are big changes to the Solidity language. |
Loading…
Reference in new issue