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

Rename some ERC20 parameters to match the standard document. #3167

Merged
merged 6 commits into from
Feb 7, 2022
Merged
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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This line contains an unrelated change, the allowance is now read through an (overrideable) function instead of directly from storage. We were planning to make this change and it's small enough that I think it's ok to include here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I included it for that reason

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