Skip to content

Commit b840341

Browse files
Amxxfrangio
andauthored
Remove the storage associated with decimals (#2502)
* Removing the storage associated with decimals * changelog entry * changelog link to new issue number * Update contracts/token/ERC20/ERC20.sol Co-authored-by: Francisco Giordano <[email protected]> * Update contracts/token/ERC20/ERC20.sol Co-authored-by: Francisco Giordano <[email protected]> * Update CHANGELOG.md Co-authored-by: Francisco Giordano <[email protected]> Co-authored-by: Francisco Giordano <[email protected]>
1 parent ed76232 commit b840341

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Now targeting the 0.8.x line of Solidity compilers. For 0.6.x (resp 0.7.x) support, use version 3.4.0 (resp 3.4.0-solc-0.7) of OpenZeppelin.
66
* `Context`: making `_msgData` return `bytes calldata` instead of `bytes memory` ([#2492](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2492))
7+
* `ERC20`: Removed the `_setDecimals` function and the storage slot associated to decimals. ([#2502](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2502))
78

89
## 3.4.0 (2021-02-02)
910

contracts/mocks/ERC20DecimalsMock.sol

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ pragma solidity ^0.8.0;
55
import "../token/ERC20/ERC20.sol";
66

77
contract ERC20DecimalsMock is ERC20 {
8-
constructor (string memory name, string memory symbol, uint8 decimals) ERC20(name, symbol) {
9-
_setupDecimals(decimals);
8+
uint8 immutable private _decimals;
9+
10+
constructor (string memory name_, string memory symbol_, uint8 decimals_) ERC20(name_, symbol_) {
11+
_decimals = decimals_;
12+
}
13+
14+
function decimals() public view virtual override returns (uint8) {
15+
return _decimals;
1016
}
1117
}

contracts/token/ERC20/ERC20.sol

+6-19
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ contract ERC20 is Context, IERC20 {
3838

3939
string private _name;
4040
string private _symbol;
41-
uint8 private _decimals;
4241

4342
/**
44-
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
45-
* a default value of 18.
43+
* @dev Sets the values for {name} and {symbol}.
4644
*
47-
* To select a different value for {decimals}, use {_setupDecimals}.
45+
* The defaut value of {decimals} is 18. To select a different value for
46+
* {decimals} you should overload it.
4847
*
4948
* All three of these values are immutable: they can only be set once during
5049
* construction.
5150
*/
5251
constructor (string memory name_, string memory symbol_) {
5352
_name = name_;
5453
_symbol = symbol_;
55-
_decimals = 18;
5654
}
5755

5856
/**
@@ -76,15 +74,15 @@ contract ERC20 is Context, IERC20 {
7674
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
7775
*
7876
* Tokens usually opt for a value of 18, imitating the relationship between
79-
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
80-
* called.
77+
* Ether and Wei. This is the value {ERC20} uses, unless this function is
78+
* overloaded;
8179
*
8280
* NOTE: This information is only used for _display_ purposes: it in
8381
* no way affects any of the arithmetic of the contract, including
8482
* {IERC20-balanceOf} and {IERC20-transfer}.
8583
*/
8684
function decimals() public view virtual returns (uint8) {
87-
return _decimals;
85+
return 18;
8886
}
8987

9088
/**
@@ -283,17 +281,6 @@ contract ERC20 is Context, IERC20 {
283281
emit Approval(owner, spender, amount);
284282
}
285283

286-
/**
287-
* @dev Sets {decimals} to a value other than the default one of 18.
288-
*
289-
* WARNING: This function should only be called from the constructor. Most
290-
* applications that interact with token contracts will not expect
291-
* {decimals} to ever change, and may work incorrectly if it does.
292-
*/
293-
function _setupDecimals(uint8 decimals_) internal virtual {
294-
_decimals = decimals_;
295-
}
296-
297284
/**
298285
* @dev Hook that is called before any transfer of tokens. This includes
299286
* minting and burning.

0 commit comments

Comments
 (0)