From e14a5f2a87daf570863641d6789cbf07e1579e92 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Fri, 4 Feb 2022 22:22:16 +0800 Subject: [PATCH 1/5] Renames `sender` to `source`. The naming variable was incorrect. The source of the funds is *not* necessarily (and in most cases isn't) the sender of the transaction. Also, this code has a `msgSender` which further adds confusion. --- contracts/token/ERC20/ERC20.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index fa30163bad2..6fbfdbcb866 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -148,25 +148,25 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * * Requirements: * - * - `sender` and `recipient` cannot be the zero address. + * - `source` and `recipient` cannot be the zero address. * - `sender` must have a balance of at least `amount`. - * - the caller must have allowance for ``sender``'s tokens of at least + * - the caller must have allowance for ``source``'s tokens of at least * `amount`. */ function transferFrom( - address sender, + address source, address recipient, uint256 amount ) public virtual override returns (bool) { - uint256 currentAllowance = _allowances[sender][_msgSender()]; + uint256 currentAllowance = _allowances[source][_msgSender()]; if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance"); unchecked { - _approve(sender, _msgSender(), currentAllowance - amount); + _approve(source, _msgSender(), currentAllowance - amount); } } - _transfer(sender, recipient, amount); + _transfer(source, recipient, amount); return true; } From 65af1e6ae69dc3b8141fb0edeb1a14ccf1a7caa2 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Sat, 5 Feb 2022 00:05:52 +0800 Subject: [PATCH 2/5] Changes to `from/to` instead of `source`. --- contracts/token/ERC20/ERC20.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 6fbfdbcb866..8925315e020 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -154,19 +154,19 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * `amount`. */ function transferFrom( - address source, - address recipient, + address from, + address to, uint256 amount ) public virtual override returns (bool) { - uint256 currentAllowance = _allowances[source][_msgSender()]; + uint256 currentAllowance = _allowances[from][_msgSender()]; if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance"); unchecked { - _approve(source, _msgSender(), currentAllowance - amount); + _approve(from, _msgSender(), currentAllowance - amount); } } - _transfer(source, recipient, amount); + _transfer(from, to, amount); return true; } From b95d73778b2b763cddc443a6ea6c9b1a511492bf Mon Sep 17 00:00:00 2001 From: Daniel Von Fange Date: Fri, 4 Feb 2022 11:41:28 -0500 Subject: [PATCH 3/5] Function documentation matches new names --- contracts/token/ERC20/ERC20.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 8925315e020..4402369bfee 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -148,9 +148,9 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * * Requirements: * - * - `source` and `recipient` cannot be the zero address. - * - `sender` must have a balance of at least `amount`. - * - the caller must have allowance for ``source``'s tokens of at least + * - `from` and `to` cannot be the zero address. + * - `from` must have a balance of at least `amount`. + * - the caller must have allowance for ``from``'s tokens of at least * `amount`. */ function transferFrom( From 70038388a4f69c5e1df63f7efd4cd53460528949 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Sun, 6 Feb 2022 14:16:01 +0800 Subject: [PATCH 4/5] Changed other instances of sender/recipient to from/to. Also changed `msgSender` to `owner` in the approval related methods. --- contracts/token/ERC20/ERC20.sol | 51 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 4402369bfee..797a4ece26b 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -107,11 +107,12 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * * Requirements: * - * - `recipient` cannot be the zero address. + * - `to` cannot be the zero address. * - the caller must have a balance of at least `amount`. */ - function transfer(address recipient, uint256 amount) public virtual override returns (bool) { - _transfer(_msgSender(), recipient, amount); + function transfer(address to, uint256 amount) public virtual override returns (bool) { + address owner = _msgSender(); + _transfer(owner, to, amount); return true; } @@ -133,7 +134,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * - `spender` cannot be the zero address. */ function approve(address spender, uint256 amount) public virtual override returns (bool) { - _approve(_msgSender(), spender, amount); + address owner = _msgSender(); + _approve(owner, spender, amount); return true; } @@ -158,11 +160,12 @@ contract ERC20 is Context, IERC20, IERC20Metadata { address to, uint256 amount ) public virtual override returns (bool) { - uint256 currentAllowance = _allowances[from][_msgSender()]; + address owner = _msgSender(); + uint256 currentAllowance = _allowances[from][owner]; if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance"); unchecked { - _approve(from, _msgSender(), currentAllowance - amount); + _approve(from, owner, currentAllowance - amount); } } @@ -184,7 +187,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * - `spender` cannot be the zero address. */ function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { - _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue); + address owner = _msgSender(); + _approve(owner, spender, _allowances[owner][spender] + addedValue); return true; } @@ -203,10 +207,11 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * `subtractedValue`. */ function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { - uint256 currentAllowance = _allowances[_msgSender()][spender]; + address owner = _msgSender(); + uint256 currentAllowance = _allowances[owner][spender]; require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero"); unchecked { - _approve(_msgSender(), spender, currentAllowance - subtractedValue); + _approve(owner, spender, currentAllowance - subtractedValue); } return true; @@ -222,30 +227,30 @@ contract ERC20 is Context, IERC20, IERC20Metadata { * * Requirements: * - * - `sender` cannot be the zero address. - * - `recipient` cannot be the zero address. - * - `sender` must have a balance of at least `amount`. + * - `from` cannot be the zero address. + * - `to` cannot be the zero address. + * - `from` must have a balance of at least `amount`. */ function _transfer( - address sender, - address recipient, + address from, + address to, uint256 amount ) internal virtual { - require(sender != address(0), "ERC20: transfer from the zero address"); - require(recipient != address(0), "ERC20: transfer to the zero address"); + require(from != address(0), "ERC20: transfer from the zero address"); + require(to != address(0), "ERC20: transfer to the zero address"); - _beforeTokenTransfer(sender, recipient, amount); + _beforeTokenTransfer(from, to, amount); - uint256 senderBalance = _balances[sender]; - require(senderBalance >= amount, "ERC20: transfer amount exceeds balance"); + uint256 fromBalance = _balances[from]; + require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); unchecked { - _balances[sender] = senderBalance - amount; + _balances[from] = fromBalance - amount; } - _balances[recipient] += amount; + _balances[to] += amount; - emit Transfer(sender, recipient, amount); + emit Transfer(from, to, amount); - _afterTokenTransfer(sender, recipient, amount); + _afterTokenTransfer(from, to, amount); } /** @dev Creates `amount` tokens and assigns them to `account`, increasing From 2e35d349deac54b721b625463325a48a72ecb130 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Mon, 7 Feb 2022 16:31:11 +0100 Subject: [PATCH 5/5] apply changes to IERC20.sol + minor renaming in ERC20.sol --- contracts/token/ERC20/ERC20.sol | 6 +++--- contracts/token/ERC20/IERC20.sol | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 797a4ece26b..08e1efc6ac3 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -160,12 +160,12 @@ contract ERC20 is Context, IERC20, IERC20Metadata { address to, uint256 amount ) public virtual override returns (bool) { - address owner = _msgSender(); - uint256 currentAllowance = _allowances[from][owner]; + address spender = _msgSender(); + uint256 currentAllowance = allowance(from, spender); if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance"); unchecked { - _approve(from, owner, currentAllowance - amount); + _approve(from, spender, currentAllowance - amount); } } diff --git a/contracts/token/ERC20/IERC20.sol b/contracts/token/ERC20/IERC20.sol index c89cd48daf6..b00b85bd7c4 100644 --- a/contracts/token/ERC20/IERC20.sol +++ b/contracts/token/ERC20/IERC20.sol @@ -18,13 +18,13 @@ interface IERC20 { function balanceOf(address account) external view returns (uint256); /** - * @dev Moves `amount` tokens from the caller's account to `recipient`. + * @dev Moves `amount` tokens from the caller's account to `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ - function transfer(address recipient, uint256 amount) external returns (bool); + function transfer(address to, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be @@ -52,7 +52,7 @@ interface IERC20 { function approve(address spender, uint256 amount) external returns (bool); /** - * @dev Moves `amount` tokens from `sender` to `recipient` using the + * @dev Moves `amount` tokens from `from` to `to` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * @@ -61,8 +61,8 @@ interface IERC20 { * Emits a {Transfer} event. */ function transferFrom( - address sender, - address recipient, + address from, + address to, uint256 amount ) external returns (bool);