Skip to content

Commit c963052

Browse files
nventurofrangio
andauthored
Draft and lifecycles directories cleanup (#2122)
* Move Pausable into utils * Move Strings into utils * Move Counters into utils * Move SignedSafeMath into math * Remove ERC1046 * Make ERC20Snapshot.snapshot internal * Move ERC20Snapshot into ERC20 * Add drafts deprecation notice * Remove drafts directory * Add changelog entry * Apply suggestions from code review Co-Authored-By: Francisco Giordano <frangio.1@gmail.com> Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
1 parent 8176a90 commit c963052

30 files changed

+79
-299
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
### Breaking changes
66
* `ECDSA`: when receiving an invalid signature, `recover` now reverts instead of returning the zero address. ([#2114](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2114))
7+
* `Pausable`: moved to the `utils` directory. ([#2122](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2122))
8+
* `Strings`: moved to the `utils` directory. ([#2122](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2122))
9+
* `Counters`: moved to the `utils` directory. ([#2122](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2122))
10+
* `SignedSafeMath`: moved to the `math` directory. ([#2122](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2122))
11+
* `ERC20Snapshot`: moved to the `token/ERC20` directory. `snapshot` was changed into an `internal` function. ([#2122](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2122))
712
* `Ownable`: moved to the `access` directory. ([#2120](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2120))
813
* `Ownable`: removed `isOwner`. ([#2120](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2120))
914
* `Secondary`: removed from the library, use `Ownable` instead. ([#2120](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2120))

contracts/drafts/ERC1046/ERC20Metadata.sol

-24
This file was deleted.

contracts/drafts/README.adoc

-23
This file was deleted.

contracts/lifecycle/README.adoc

-5
This file was deleted.

contracts/math/README.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ These are math-related utilities.
66

77
{{SafeMath}}
88

9+
{{SignedSafeMath}}
10+
911
{{Math}}
File renamed without changes.

contracts/mocks/CountersImpl.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pragma solidity ^0.6.0;
22

3-
import "../drafts/Counters.sol";
3+
import "../utils/Counters.sol";
44

55
contract CountersImpl {
66
using Counters for Counters.Counter;

contracts/mocks/ERC20MetadataMock.sol

-12
This file was deleted.

contracts/mocks/ERC20SnapshotMock.sol

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
pragma solidity ^0.6.0;
22

3-
import "../drafts/ERC20Snapshot.sol";
3+
import "../token/ERC20/ERC20Snapshot.sol";
44

55

66
contract ERC20SnapshotMock is ERC20Snapshot {
77
constructor(address initialAccount, uint256 initialBalance) public {
88
_mint(initialAccount, initialBalance);
99
}
1010

11+
function snapshot() public {
12+
_snapshot();
13+
}
14+
1115
function mint(address account, uint256 amount) public {
1216
_mint(account, amount);
1317
}

contracts/mocks/PausableMock.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pragma solidity ^0.6.0;
22

3-
import "../lifecycle/Pausable.sol";
3+
import "../utils/Pausable.sol";
44

55
contract PausableMock is Pausable {
66
bool public drasticMeasureTaken;

contracts/mocks/SignedSafeMathMock.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pragma solidity ^0.6.0;
22

3-
import "../drafts/SignedSafeMath.sol";
3+
import "../math/SignedSafeMath.sol";
44

55
contract SignedSafeMathMock {
66
function mul(int256 a, int256 b) public pure returns (int256) {

contracts/mocks/StringsMock.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pragma solidity ^0.6.0;
22

3-
import "../drafts/Strings.sol";
3+
import "../utils/Strings.sol";
44

55
contract StringsMock {
66
function fromUint256(uint256 value) public pure returns (string memory) {

contracts/token/ERC20/ERC20Pausable.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pragma solidity ^0.6.0;
22

33
import "./ERC20.sol";
4-
import "../../lifecycle/Pausable.sol";
4+
import "../../utils/Pausable.sol";
55

66
/**
77
* @title Pausable token

contracts/drafts/ERC20Snapshot.sol contracts/token/ERC20/ERC20Snapshot.sol

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
pragma solidity ^0.6.0;
22

3-
import "../math/SafeMath.sol";
4-
import "../utils/Arrays.sol";
5-
import "../drafts/Counters.sol";
6-
import "../token/ERC20/ERC20.sol";
3+
import "../../math/SafeMath.sol";
4+
import "../../utils/Arrays.sol";
5+
import "../../utils/Counters.sol";
6+
import "./ERC20.sol";
77

88
/**
9-
* @title ERC20 token with snapshots.
10-
* @dev Inspired by Jordi Baylina's
11-
* https://github.com/Giveth/minimd/blob/ea04d950eea153a04c51fa510b068b9dded390cb/contracts/MiniMeToken.sol[MiniMeToken]
12-
* to record historical balances.
9+
* @dev ERC20 token with snapshots.
1310
*
1411
* When a snapshot is made, the balances and total supply at the time of the snapshot are recorded for later
1512
* access.
@@ -21,6 +18,9 @@ import "../token/ERC20/ERC20.sol";
2118
* @author Validity Labs AG <info@validitylabs.org>
2219
*/
2320
contract ERC20Snapshot is ERC20 {
21+
// Inspired by Jordi Baylina's MiniMeToken to record historical balances:
22+
// https://github.com/Giveth/minimd/blob/ea04d950eea153a04c51fa510b068b9dded390cb/contracts/MiniMeToken.sol
23+
2424
using SafeMath for uint256;
2525
using Arrays for uint256[];
2626
using Counters for Counters.Counter;
@@ -40,10 +40,12 @@ contract ERC20Snapshot is ERC20 {
4040

4141
event Snapshot(uint256 id);
4242

43-
// Creates a new snapshot id. Balances are only stored in snapshots on demand: unless a snapshot was taken, a
44-
// balance change will not be recorded. This means the extra added cost of storing snapshotted balances is only paid
45-
// when required, but is also flexible enough that it allows for e.g. daily snapshots.
46-
function snapshot() public virtual returns (uint256) {
43+
/**
44+
* @dev Creates a new snapshot id. Balances are only stored in snapshots on demand: unless a snapshot was taken, a
45+
* balance change will not be recorded. This means the extra added cost of storing snapshotted balances is only paid
46+
* when required, but is also flexible enough that it allows for e.g. daily snapshots.
47+
*/
48+
function _snapshot() internal virtual returns (uint256) {
4749
_currentSnapshotId.increment();
4850

4951
uint256 currentId = _currentSnapshotId.current();
@@ -87,26 +89,27 @@ contract ERC20Snapshot is ERC20 {
8789
super._burn(account, value);
8890
}
8991

90-
// When a valid snapshot is queried, there are three possibilities:
91-
// a) The queried value was not modified after the snapshot was taken. Therefore, a snapshot entry was never
92-
// created for this id, and all stored snapshot ids are smaller than the requested one. The value that corresponds
93-
// to this id is the current one.
94-
// b) The queried value was modified after the snapshot was taken. Therefore, there will be an entry with the
95-
// requested id, and its value is the one to return.
96-
// c) More snapshots were created after the requested one, and the queried value was later modified. There will be
97-
// no entry for the requested id: the value that corresponds to it is that of the smallest snapshot id that is
98-
// larger than the requested one.
99-
//
100-
// In summary, we need to find an element in an array, returning the index of the smallest value that is larger if
101-
// it is not found, unless said value doesn't exist (e.g. when all values are smaller). Arrays.findUpperBound does
102-
// exactly this.
10392
function _valueAt(uint256 snapshotId, Snapshots storage snapshots)
10493
private view returns (bool, uint256)
10594
{
10695
require(snapshotId > 0, "ERC20Snapshot: id is 0");
10796
// solhint-disable-next-line max-line-length
10897
require(snapshotId <= _currentSnapshotId.current(), "ERC20Snapshot: nonexistent id");
10998

99+
// When a valid snapshot is queried, there are three possibilities:
100+
// a) The queried value was not modified after the snapshot was taken. Therefore, a snapshot entry was never
101+
// created for this id, and all stored snapshot ids are smaller than the requested one. The value that corresponds
102+
// to this id is the current one.
103+
// b) The queried value was modified after the snapshot was taken. Therefore, there will be an entry with the
104+
// requested id, and its value is the one to return.
105+
// c) More snapshots were created after the requested one, and the queried value was later modified. There will be
106+
// no entry for the requested id: the value that corresponds to it is that of the smallest snapshot id that is
107+
// larger than the requested one.
108+
//
109+
// In summary, we need to find an element in an array, returning the index of the smallest value that is larger if
110+
// it is not found, unless said value doesn't exist (e.g. when all values are smaller). Arrays.findUpperBound does
111+
// exactly this.
112+
110113
uint256 index = snapshots.ids.findUpperBound(snapshotId);
111114

112115
if (index == snapshots.ids.length) {

contracts/token/ERC20/README.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ NOTE: This page is incomplete. We're working to improve it for the next release.
4343

4444
{{ERC20Capped}}
4545

46+
{{ERC20Snapshot}}
47+
4648
== Utilities
4749

4850
{{SafeERC20}}

contracts/token/ERC721/ERC721.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "./IERC721.sol";
55
import "./IERC721Receiver.sol";
66
import "../../math/SafeMath.sol";
77
import "../../utils/Address.sol";
8-
import "../../drafts/Counters.sol";
8+
import "../../utils/Counters.sol";
99
import "../../introspection/ERC165.sol";
1010

1111
/**

contracts/token/ERC721/ERC721Pausable.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pragma solidity ^0.6.0;
22

33
import "./ERC721.sol";
4-
import "../../lifecycle/Pausable.sol";
4+
import "../../utils/Pausable.sol";
55

66
/**
77
* @title ERC721 Non-Fungible Pausable token
File renamed without changes.
File renamed without changes.

contracts/utils/README.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ Miscellaneous contracts containing utility functions, often related to working w
1010

1111
{{Arrays}}
1212

13+
{{Counters}}
14+
15+
{{Strings}}
16+
1317
{{EnumerableSet}}
1418

1519
{{Create2}}
1620

1721
{{ReentrancyGuard}}
22+
23+
{{Pausable}}

contracts/drafts/Strings.sol contracts/utils/Strings.sol

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ pragma solidity ^0.6.0;
66
*/
77
library Strings {
88
/**
9-
* @dev Converts a `uint256` to a `string`.
10-
* via OraclizeAPI - MIT licence
11-
* https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
9+
* @dev Converts a `uint256` to its ASCII `string` representation.
1210
*/
1311
function fromUint256(uint256 value) internal pure returns (string memory) {
12+
// Inspired by OraclizeAPI's implementation - MIT licence
13+
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
14+
1415
if (value == 0) {
1516
return "0";
1617
}

docs/modules/ROOT/pages/drafts.adoc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
= Drafts
2+
3+
All draft contracts were either moved into a different directory or removed from the OpenZeppelin Contracts library on the https://forum.openzeppelin.com/t/openzeppelin-contracts-v3-0-beta-release/2256[v3.0.0 release].
4+
5+
* `ERC20Migrator`: removed.
6+
* xref:api:token/ERC20.adoc#ERC20Snapshot[`ERC20Snapshot`]: moved to `token/ERC20`.
7+
* `ERC20Detailed` and `ERC1046`: removed.
8+
* `TokenVesting`: removed. Pending a replacement that is being discussed in https://github.com/OpenZeppelin/openzeppelin-contracts/issues/1214[`#1214`].
9+
* xref:api:utils.adoc#Counters[`Counters`]: moved to xref:api:utils.adoc[`utils`].
10+
* xref:api:utils.adoc#Strings[`Strings`]: moved to xref:api:utils.adoc[`utils`].
11+
* xref:api:utils.adoc#SignedSafeMath[`SignedSafeMath`]: moved to xref:api:utils.adoc[`utils`].
12+
13+
Removed contracts are still available on the v2.5 release of OpenZeppelin Contracts, which you can install by running:
14+
15+
```console
16+
$ npm install @openzeppelin/contracts@v2.5
17+
```
18+
19+
Refer to the xref:2.x@contracts:api:utils.adoc[v2.x documentation] when working with them.

docs/modules/ROOT/pages/erc721.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Here's what a contract for tokenized items might look like:
1515
pragma solidity ^0.5.0;
1616
1717
import "@openzeppelin/contracts/token/ERC721/ERC721Full.sol";
18-
import "@openzeppelin/contracts/drafts/Counters.sol";
18+
import "@openzeppelin/contracts/utils/Counters.sol";
1919
2020
contract GameItem is ERC721Full {
2121
using Counters for Counters.Counter;

test/drafts/ERC1046/ERC20Metadata.test.js

-26
This file was deleted.

0 commit comments

Comments
 (0)