Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix guides for 5.0 #4654

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/governance/utils/Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {Time} from "../../utils/types/Time.sol";
*
* When using this module the derived contract must implement {_getVotingUnits} (for example, make it return
* {ERC721-balanceOf}), and can use {_transferVotingUnits} to track a change in the distribution of those units (in the
* previous example, it would be included in {ERC721-_beforeTokenTransfer}).
* previous example, it would be included in {ERC721-_update}).
*/
abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
using Checkpoints for Checkpoints.Trace208;
Expand Down
22 changes: 22 additions & 0 deletions contracts/mocks/docs/ERC20WithAutoMinerReward.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {ERC20} from "../../token/ERC20/ERC20.sol";

contract ERC20WithAutoMinerReward is ERC20 {
constructor() ERC20("Reward", "RWD") {
_mintMinerReward();
}

function _mintMinerReward() internal {
_mint(block.coinbase, 1000);
}

function _update(address from, address to, uint256 value) internal virtual override {
if (!(from == address(0) && to == block.coinbase)) {
_mintMinerReward();
}
super._update(from, to, value);
}
}
17 changes: 17 additions & 0 deletions contracts/mocks/docs/MyContractOwnable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Ownable} from "../../access/Ownable.sol";

contract MyContract is Ownable {
constructor(address initialOwner) Ownable(initialOwner) {}

function normalThing() public {
// anyone can call this normalThing()
}

function specialThing() public onlyOwner {
// only the owner can call specialThing()!
}
}
21 changes: 3 additions & 18 deletions docs/modules/ROOT/pages/access-control.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,9 @@ The most common and basic form of access control is the concept of _ownership_:

OpenZeppelin Contracts provides xref:api:access.adoc#Ownable[`Ownable`] for implementing ownership in your contracts.

[source,solidity]
----
// contracts/MyContract.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MyContract is Ownable {
function normalThing() public {
// anyone can call this normalThing()
}

function specialThing() public onlyOwner {
// only the owner can call specialThing()!
}
}
----
```solidity
include::api:example$MyContractOwnable.sol[]
```

By default, the xref:api:access.adoc#Ownable-owner--[`owner`] of an `Ownable` contract is the account that deployed it, which is usually exactly what you want.

Expand Down
24 changes: 5 additions & 19 deletions docs/modules/ROOT/pages/erc20-supply.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,13 @@ As we can see, `_mint` makes it super easy to do this correctly.
[[automating-the-reward]]
== Automating the Reward

So far our supply mechanism was triggered manually, but `ERC20` also allows us to extend the core functionality of the token through the xref:api:token/ERC20.adoc#ERC20-_beforeTokenTransfer-address-address-uint256-[`_beforeTokenTransfer`] hook (see xref:extending-contracts.adoc#using-hooks[Using Hooks]).
So far our supply mechanism was triggered manually, but `ERC20` also allows us to extend the core functionality of the token through the xref:api:token/ERC20.adoc#ERC20-_update-address-address-uint256-[`_update`] function.

Adding to the supply mechanism from the previous section, we can use this hook to mint a miner reward for every token transfer that is included in the blockchain.
Adding to the supply mechanism from the previous section, we can use this function to mint a miner reward for every token transfer that is included in the blockchain.

[source,solidity]
----
contract ERC20WithAutoMinerReward is ERC20 {
constructor() ERC20("Reward", "RWD") {}

function _mintMinerReward() internal {
_mint(block.coinbase, 1000);
}

function _beforeTokenTransfer(address from, address to, uint256 value) internal virtual override {
if (!(from == address(0) && to == block.coinbase)) {
_mintMinerReward();
}
super._beforeTokenTransfer(from, to, value);
}
}
----
```solidity
include::api:example$ERC20WithAutoMinerReward.sol[]
```

[[wrapping-up]]
== Wrapping Up
Expand Down
54 changes: 0 additions & 54 deletions docs/modules/ROOT/pages/extending-contracts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,60 +68,6 @@ The `super.revokeRole` statement at the end will invoke ``AccessControl``'s orig

NOTE: The same rule is implemented and extended in xref:api:access.adoc#AccessControlDefaultAdminRules[`AccessControlDefaultAdminRules`], an extension that also adds enforced security measures for the `DEFAULT_ADMIN_ROLE`.

[[using-hooks]]
== Using Hooks

Sometimes, in order to extend a parent contract you will need to override multiple related functions, which leads to code duplication and increased likelihood of bugs.

For example, consider implementing safe xref:api:token/ERC20.adoc#ERC20[`ERC20`] transfers in the style of xref:api:token/ERC721.adoc#IERC721Receiver[`IERC721Receiver`]. You may think overriding xref:api:token/ERC20.adoc#ERC20-transfer-address-uint256-[`transfer`] and xref:api:token/ERC20.adoc#ERC20-transferFrom-address-address-uint256-[`transferFrom`] would be enough, but what about xref:api:token/ERC20.adoc#ERC20-_transfer-address-address-uint256-[`_transfer`] and xref:api:token/ERC20.adoc#ERC20-_mint-address-uint256-[`_mint`]? To prevent you from having to deal with these details, we introduced **hooks**.

Hooks are simply functions that are called before or after some action takes place. They provide a centralized point to _hook into_ and extend the original behavior.

Here's how you would implement the `IERC721Receiver` pattern in `ERC20`, using the xref:api:token/ERC20.adoc#ERC20-_beforeTokenTransfer-address-address-uint256-[`_beforeTokenTransfer`] hook:

```solidity
pragma solidity ^0.8.20;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract ERC20WithSafeTransfer is ERC20 {
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal virtual override
{
super._beforeTokenTransfer(from, to, amount);

require(_validRecipient(to), "ERC20WithSafeTransfer: invalid recipient");
}

function _validRecipient(address to) private view returns (bool) {
...
}

...
}
```

Using hooks this way leads to cleaner and safer code, without having to rely on a deep understanding of the parent's internals.

=== Rules of Hooks

There's a few guidelines you should follow when writing code that uses hooks in order to prevent issues. They are very simple, but do make sure you follow them:

1. Whenever you override a parent's hook, re-apply the `virtual` attribute to the hook. That will allow child contracts to add more functionality to the hook.
2. **Always** call the parent's hook in your override using `super`. This will make sure all hooks in the inheritance tree are called: contracts like xref:api:token/ERC20.adoc#ERC20Pausable[`ERC20Pausable`] rely on this behavior.

```solidity
contract MyToken is ERC20 {
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal virtual override // Add virtual here!
{
super._beforeTokenTransfer(from, to, amount); // Call parent hook
...
}
}
```
That's it! Enjoy simpler code using hooks!

== Security

The maintainers of OpenZeppelin Contracts are mainly concerned with the correctness and security of the code as published in the library, and the combinations of base contracts with the official extensions from the library.
Expand Down
4 changes: 2 additions & 2 deletions test/token/ERC20/extensions/ERC4626.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ contract('ERC4626', function (accounts) {
const sharesForDeposit = await vault.previewDeposit(value, { from: holder });
const sharesForReenter = await vault.previewDeposit(reenterValue, { from: holder });

// Do deposit normally, triggering the _beforeTokenTransfer hook
// Do deposit normally, triggering the _update hook
frangio marked this conversation as resolved.
Show resolved Hide resolved
const receipt = await vault.deposit(value, holder, { from: holder });

// Main deposit event
Expand Down Expand Up @@ -170,7 +170,7 @@ contract('ERC4626', function (accounts) {
// Price before
const sharesBefore = await vault.previewDeposit(value);

// Deposit, triggering the _beforeTokenTransfer hook
// Deposit, triggering the _update hook
frangio marked this conversation as resolved.
Show resolved Hide resolved
const receipt = await vault.deposit(value, holder, { from: holder });

// Price is as previewed
Expand Down