Skip to content

Commit

Permalink
Remove the storage associated with decimals (#2502)
Browse files Browse the repository at this point in the history
* 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 <frangio.1@gmail.com>

* Update contracts/token/ERC20/ERC20.sol

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>

* Update CHANGELOG.md

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
  • Loading branch information
Amxx and frangio authored Feb 4, 2021
1 parent ed76232 commit b840341
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* 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.
* `Context`: making `_msgData` return `bytes calldata` instead of `bytes memory` ([#2492](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2492))
* `ERC20`: Removed the `_setDecimals` function and the storage slot associated to decimals. ([#2502](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2502))

## 3.4.0 (2021-02-02)

Expand Down
10 changes: 8 additions & 2 deletions contracts/mocks/ERC20DecimalsMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ pragma solidity ^0.8.0;
import "../token/ERC20/ERC20.sol";

contract ERC20DecimalsMock is ERC20 {
constructor (string memory name, string memory symbol, uint8 decimals) ERC20(name, symbol) {
_setupDecimals(decimals);
uint8 immutable private _decimals;

constructor (string memory name_, string memory symbol_, uint8 decimals_) ERC20(name_, symbol_) {
_decimals = decimals_;
}

function decimals() public view virtual override returns (uint8) {
return _decimals;
}
}
25 changes: 6 additions & 19 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ contract ERC20 is Context, IERC20 {

string private _name;
string private _symbol;
uint8 private _decimals;

/**
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
* a default value of 18.
* @dev Sets the values for {name} and {symbol}.
*
* To select a different value for {decimals}, use {_setupDecimals}.
* The defaut value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor (string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}

/**
Expand All @@ -76,15 +74,15 @@ contract ERC20 is Context, IERC20 {
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
* called.
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overloaded;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual returns (uint8) {
return _decimals;
return 18;
}

/**
Expand Down Expand Up @@ -283,17 +281,6 @@ contract ERC20 is Context, IERC20 {
emit Approval(owner, spender, amount);
}

/**
* @dev Sets {decimals} to a value other than the default one of 18.
*
* WARNING: This function should only be called from the constructor. Most
* applications that interact with token contracts will not expect
* {decimals} to ever change, and may work incorrectly if it does.
*/
function _setupDecimals(uint8 decimals_) internal virtual {
_decimals = decimals_;
}

/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
Expand Down

0 comments on commit b840341

Please sign in to comment.