Commit e65e8442 by github-actions

Merge upstream release-v4.5 into patched/release-v4.5

...@@ -14,7 +14,10 @@ ...@@ -14,7 +14,10 @@
* `Base64`: add a library to parse bytes into base64 strings using `encode(bytes memory)` function, and provide examples to show how to use to build URL-safe `tokenURIs`. ([#2884](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2884)) * `Base64`: add a library to parse bytes into base64 strings using `encode(bytes memory)` function, and provide examples to show how to use to build URL-safe `tokenURIs`. ([#2884](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2884))
* `ERC20`: reduce allowance before triggering transfer. ([#3056](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3056)) * `ERC20`: reduce allowance before triggering transfer. ([#3056](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3056))
* `ERC20`: do not update allowance on `transferFrom` when allowance is `type(uint256).max`. ([#3085](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3085)) * `ERC20`: do not update allowance on `transferFrom` when allowance is `type(uint256).max`. ([#3085](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3085))
* `ERC20`: add a `_spendAllowance` internal function. ([#3170](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3170))
* `ERC20Burnable`: do not update allowance on `burnFrom` when allowance is `type(uint256).max`. ([#3170](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3170))
* `ERC777`: do not update allowance on `transferFrom` when allowance is `type(uint256).max`. ([#3085](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3085)) * `ERC777`: do not update allowance on `transferFrom` when allowance is `type(uint256).max`. ([#3085](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3085))
* `ERC777`: add a `_spendAllowance` internal function. ([#3170](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3170))
* `SignedMath`: a new signed version of the Math library with `max`, `min`, and `average`. ([#2686](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2686)) * `SignedMath`: a new signed version of the Math library with `max`, `min`, and `average`. ([#2686](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2686))
* `SignedMath`: add a `abs(int256)` method that returns the unsigned absolute value of a signed value. ([#2984](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2984)) * `SignedMath`: add a `abs(int256)` method that returns the unsigned absolute value of a signed value. ([#2984](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2984))
* `ERC1967Upgrade`: Refactor the secure upgrade to use `ERC1822` instead of the previous rollback mechanism. This reduces code complexity and attack surface with similar security guarantees. ([#3021](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3021)) * `ERC1967Upgrade`: Refactor the secure upgrade to use `ERC1822` instead of the previous rollback mechanism. This reduces code complexity and attack surface with similar security guarantees. ([#3021](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3021))
......
...@@ -107,11 +107,12 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -107,11 +107,12 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* *
* Requirements: * Requirements:
* *
* - `recipient` cannot be the zero address. * - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`. * - the caller must have a balance of at least `amount`.
*/ */
function transfer(address recipient, uint256 amount) public virtual override returns (bool) { function transfer(address to, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount); address owner = _msgSender();
_transfer(owner, to, amount);
return true; return true;
} }
...@@ -133,7 +134,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -133,7 +134,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* - `spender` cannot be the zero address. * - `spender` cannot be the zero address.
*/ */
function approve(address spender, uint256 amount) public virtual override returns (bool) { function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount); address owner = _msgSender();
_approve(owner, spender, amount);
return true; return true;
} }
...@@ -148,26 +150,19 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -148,26 +150,19 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* *
* Requirements: * Requirements:
* *
* - `sender` and `recipient` cannot be the zero address. * - `from` and `to` cannot be the zero address.
* - `sender` must have a balance of at least `amount`. * - `from` 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 ``from``'s tokens of at least
* `amount`. * `amount`.
*/ */
function transferFrom( function transferFrom(
address sender, address from,
address recipient, address to,
uint256 amount uint256 amount
) public virtual override returns (bool) { ) public virtual override returns (bool) {
uint256 currentAllowance = _allowances[sender][_msgSender()]; address spender = _msgSender();
if (currentAllowance != type(uint256).max) { _spendAllowance(from, spender, amount);
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance"); _transfer(from, to, amount);
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
}
}
_transfer(sender, recipient, amount);
return true; return true;
} }
...@@ -184,7 +179,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -184,7 +179,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* - `spender` cannot be the zero address. * - `spender` cannot be the zero address.
*/ */
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { 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; return true;
} }
...@@ -203,10 +199,11 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -203,10 +199,11 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* `subtractedValue`. * `subtractedValue`.
*/ */
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { 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"); require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked { unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue); _approve(owner, spender, currentAllowance - subtractedValue);
} }
return true; return true;
...@@ -222,30 +219,30 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -222,30 +219,30 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* *
* Requirements: * Requirements:
* *
* - `sender` cannot be the zero address. * - `from` cannot be the zero address.
* - `recipient` cannot be the zero address. * - `to` cannot be the zero address.
* - `sender` must have a balance of at least `amount`. * - `from` must have a balance of at least `amount`.
*/ */
function _transfer( function _transfer(
address sender, address from,
address recipient, address to,
uint256 amount uint256 amount
) internal virtual { ) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address"); require(from != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to 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]; uint256 fromBalance = _balances[from];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance"); require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked { 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 /** @dev Creates `amount` tokens and assigns them to `account`, increasing
...@@ -323,6 +320,28 @@ contract ERC20 is Context, IERC20, IERC20Metadata { ...@@ -323,6 +320,28 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
} }
/** /**
* @dev Spend `amount` form the allowance of `owner` toward `spender`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any transfer of tokens. This includes * @dev Hook that is called before any transfer of tokens. This includes
* minting and burning. * minting and burning.
* *
......
...@@ -18,13 +18,13 @@ interface IERC20 { ...@@ -18,13 +18,13 @@ interface IERC20 {
function balanceOf(address account) external view returns (uint256); 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. * Returns a boolean value indicating whether the operation succeeded.
* *
* Emits a {Transfer} event. * 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 * @dev Returns the remaining number of tokens that `spender` will be
...@@ -52,7 +52,7 @@ interface IERC20 { ...@@ -52,7 +52,7 @@ interface IERC20 {
function approve(address spender, uint256 amount) external returns (bool); 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 mechanism. `amount` is then deducted from the caller's
* allowance. * allowance.
* *
...@@ -61,8 +61,8 @@ interface IERC20 { ...@@ -61,8 +61,8 @@ interface IERC20 {
* Emits a {Transfer} event. * Emits a {Transfer} event.
*/ */
function transferFrom( function transferFrom(
address sender, address from,
address recipient, address to,
uint256 amount uint256 amount
) external returns (bool); ) external returns (bool);
......
...@@ -33,11 +33,7 @@ abstract contract ERC20Burnable is Context, ERC20 { ...@@ -33,11 +33,7 @@ abstract contract ERC20Burnable is Context, ERC20 {
* `amount`. * `amount`.
*/ */
function burnFrom(address account, uint256 amount) public virtual { function burnFrom(address account, uint256 amount) public virtual {
uint256 currentAllowance = allowance(account, _msgSender()); _spendAllowance(account, _msgSender(), amount);
require(currentAllowance >= amount, "ERC20: burn amount exceeds allowance");
unchecked {
_approve(account, _msgSender(), currentAllowance - amount);
}
_burn(account, amount); _burn(account, amount);
} }
} }
...@@ -293,13 +293,7 @@ contract ERC777 is Context, IERC777, IERC20 { ...@@ -293,13 +293,7 @@ contract ERC777 is Context, IERC777, IERC20 {
_callTokensToSend(spender, holder, recipient, amount, "", ""); _callTokensToSend(spender, holder, recipient, amount, "", "");
uint256 currentAllowance = _allowances[holder][spender]; _spendAllowance(holder, spender, amount);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC777: transfer amount exceeds allowance");
unchecked {
_approve(holder, spender, currentAllowance - amount);
}
}
_move(spender, holder, recipient, amount, "", ""); _move(spender, holder, recipient, amount, "", "");
...@@ -527,6 +521,28 @@ contract ERC777 is Context, IERC777, IERC20 { ...@@ -527,6 +521,28 @@ contract ERC777 is Context, IERC777, IERC20 {
} }
/** /**
* @dev Spend `amount` form the allowance of `owner` toward `spender`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC777: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any token transfer. This includes * @dev Hook that is called before any token transfer. This includes
* calls to {send}, {transfer}, {operatorSend}, minting and burning. * calls to {send}, {transfer}, {operatorSend}, minting and burning.
* *
......
...@@ -108,7 +108,7 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip ...@@ -108,7 +108,7 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip
it('reverts', async function () { it('reverts', async function () {
await expectRevert( await expectRevert(
this.token.transferFrom(tokenOwner, to, amount, { from: spender }), this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
`${errorPrefix}: transfer amount exceeds allowance`, `${errorPrefix}: insufficient allowance`,
); );
}); });
}); });
......
...@@ -98,7 +98,7 @@ function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) { ...@@ -98,7 +98,7 @@ function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) {
it('reverts', async function () { it('reverts', async function () {
await this.token.approve(burner, allowance, { from: owner }); await this.token.approve(burner, allowance, { from: owner });
await expectRevert(this.token.burnFrom(owner, allowance.addn(1), { from: burner }), await expectRevert(this.token.burnFrom(owner, allowance.addn(1), { from: burner }),
'ERC20: burn amount exceeds allowance', 'ERC20: insufficient allowance',
); );
}); });
}); });
......
...@@ -59,7 +59,7 @@ contract('ERC20', function (accounts) { ...@@ -59,7 +59,7 @@ contract('ERC20', function (accounts) {
it('missing approval', async function () { it('missing approval', async function () {
await expectRevert( await expectRevert(
this.token.depositFor(initialHolder, initialSupply, { from: initialHolder }), this.token.depositFor(initialHolder, initialSupply, { from: initialHolder }),
'ERC20: transfer amount exceeds allowance', 'ERC20: insufficient allowance',
); );
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment