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

Extract ERC20 logic into the ERC20Lib library #4799

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
105 changes: 19 additions & 86 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {IERC20} from "./IERC20.sol";
import {IERC20Metadata} from "./extensions/IERC20Metadata.sol";
import {Context} from "../../utils/Context.sol";
import {IERC20Errors} from "../../interfaces/draft-IERC6093.sol";
import {ERC20Lib} from "./libs/ERC20Lib.sol";

/**
* @dev Implementation of the {IERC20} interface.
Expand All @@ -32,14 +33,9 @@ import {IERC20Errors} from "../../interfaces/draft-IERC6093.sol";
* these events, as it isn't required by the specification.
*/
abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
mapping(address account => uint256) private _balances;
using ERC20Lib for ERC20Lib.Storage;

mapping(address account => mapping(address spender => uint256)) private _allowances;

uint256 private _totalSupply;

string private _name;
string private _symbol;
ERC20Lib.Storage private _erc20;

/**
* @dev Sets the values for {name} and {symbol}.
Expand All @@ -48,23 +44,22 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
_erc20.init(name_, symbol_);
}

/**
* @dev Returns the name of the token.
*/
function name() public view virtual returns (string memory) {
return _name;
return _erc20.name();
}

/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual returns (string memory) {
return _symbol;
return _erc20.symbol();
}

/**
Expand All @@ -88,14 +83,14 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual returns (uint256) {
return _totalSupply;
return _erc20.totalSupply();
}

/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual returns (uint256) {
return _balances[account];
return _erc20.balanceOf(account);
}

/**
Expand All @@ -107,16 +102,14 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* - the caller must have a balance of at least `value`.
*/
function transfer(address to, uint256 value) public virtual returns (bool) {
address owner = _msgSender();
_transfer(owner, to, value);
return true;
return _erc20.transfer(_msgSender(), to, value);
}

/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual returns (uint256) {
return _allowances[owner][spender];
return _erc20.allowance(owner, spender);
}

/**
Expand All @@ -130,9 +123,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 value) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, value);
return true;
return _erc20.approve(_msgSender(), spender, value);
}

/**
Expand All @@ -152,10 +143,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* `value`.
*/
function transferFrom(address from, address to, uint256 value) public virtual returns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, value);
_transfer(from, to, value);
return true;
return _erc20.transferFrom(_msgSender(), from, to, value);
}

/**
Expand All @@ -169,13 +157,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* NOTE: This function is not virtual, {_update} should be overridden instead.
*/
function _transfer(address from, address to, uint256 value) internal {
if (from == address(0)) {
revert ERC20InvalidSender(address(0));
}
if (to == address(0)) {
revert ERC20InvalidReceiver(address(0));
}
_update(from, to, value);
_erc20._transfer(from, to, value);
}

/**
Expand All @@ -186,33 +168,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* Emits a {Transfer} event.
*/
function _update(address from, address to, uint256 value) internal virtual {
if (from == address(0)) {
// Overflow check required: The rest of the code assumes that totalSupply never overflows
_totalSupply += value;
} else {
uint256 fromBalance = _balances[from];
if (fromBalance < value) {
revert ERC20InsufficientBalance(from, fromBalance, value);
}
unchecked {
// Overflow not possible: value <= fromBalance <= totalSupply.
_balances[from] = fromBalance - value;
}
}

if (to == address(0)) {
unchecked {
// Overflow not possible: value <= totalSupply or value <= fromBalance <= totalSupply.
_totalSupply -= value;
}
} else {
unchecked {
// Overflow not possible: balance + value is at most totalSupply, which we know fits into a uint256.
_balances[to] += value;
}
}

emit Transfer(from, to, value);
_erc20._update(from, to, value);
}

/**
Expand All @@ -224,10 +180,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* NOTE: This function is not virtual, {_update} should be overridden instead.
*/
function _mint(address account, uint256 value) internal {
if (account == address(0)) {
revert ERC20InvalidReceiver(address(0));
}
_update(address(0), account, value);
_erc20.mint(account, value);
}

/**
Expand All @@ -239,10 +192,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* NOTE: This function is not virtual, {_update} should be overridden instead
*/
function _burn(address account, uint256 value) internal {
if (account == address(0)) {
revert ERC20InvalidSender(address(0));
}
_update(account, address(0), value);
_erc20.burn(account, value);
}

/**
Expand All @@ -261,7 +211,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* Overrides to this logic should be done to the variant with an additional `bool emitEvent` argument.
*/
function _approve(address owner, address spender, uint256 value) internal {
_approve(owner, spender, value, true);
_erc20._approve(owner, spender, value, true);
}

/**
Expand All @@ -282,16 +232,7 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* Requirements are the same as {_approve}.
*/
function _approve(address owner, address spender, uint256 value, bool emitEvent) internal virtual {
if (owner == address(0)) {
revert ERC20InvalidApprover(address(0));
}
if (spender == address(0)) {
revert ERC20InvalidSpender(address(0));
}
_allowances[owner][spender] = value;
if (emitEvent) {
emit Approval(owner, spender, value);
}
_erc20._approve(owner, spender, value, emitEvent);
}

/**
Expand All @@ -303,14 +244,6 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* Does not emit an {Approval} event.
*/
function _spendAllowance(address owner, address spender, uint256 value) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
if (currentAllowance < value) {
revert ERC20InsufficientAllowance(spender, currentAllowance, value);
}
unchecked {
_approve(owner, spender, currentAllowance - value, false);
}
}
_erc20._spendAllowance(owner, spender, value);
}
}
Loading
Loading