Skip to content

Commit

Permalink
Rename some ERC20 parameters to match the standard document. (#3167)
Browse files Browse the repository at this point in the history
* 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.

* Changes to `from/to` instead of `source`.

* Function documentation matches new names

* Changed other instances of sender/recipient to from/to.

Also changed `msgSender` to `owner` in the approval related methods.

* apply changes to IERC20.sol + minor renaming in ERC20.sol

Co-authored-by: Daniel Von Fange <daniel@leancoder.com>
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
(cherry picked from commit 63b4669)
  • Loading branch information
MicahZoltu authored and frangio committed Feb 9, 2022
1 parent 2248e37 commit 234a971
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
63 changes: 34 additions & 29 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -148,25 +150,26 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
*
* Requirements:
*
* - `sender` 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
* - `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(
address sender,
address recipient,
address from,
address to,
uint256 amount
) public virtual override returns (bool) {
uint256 currentAllowance = _allowances[sender][_msgSender()];
address spender = _msgSender();
uint256 currentAllowance = allowance(from, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
_approve(from, spender, currentAllowance - amount);
}
}

_transfer(sender, recipient, amount);
_transfer(from, to, amount);

return true;
}
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions contracts/token/ERC20/IERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);

Expand Down

0 comments on commit 234a971

Please sign in to comment.