Skip to content

Commit

Permalink
CHANGELOG updated, custom error messages added to ERC20, ERC721 and E…
Browse files Browse the repository at this point in the history
…RC777 for subtraction related exceptions.
  • Loading branch information
elch-yan committed Jul 24, 2019
1 parent f563e76 commit ca40fe5
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 77 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
### Improvements:
* `Address.isContract`: switched from `extcodesize` to `extcodehash` for less gas usage. ([#1802](https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1802))

* `SafeMath`: added support for custom error messages. `ERC20`, `ERC721` and `ERC777` updated to throw custom errors on subtraction overflows. ([#1828](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/1828))

### Bugfixes

## 2.3.0 (2019-05-27)
Expand Down
12 changes: 6 additions & 6 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ contract ERC20 is IERC20 {
*/
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount, "ERC20: transfer of tokens exceeding allowance"));
return true;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ contract ERC20 is IERC20 {
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
_approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue, "ERC20: decrease of higher allowance than granted"));
return true;
}

Expand All @@ -154,7 +154,7 @@ contract ERC20 is IERC20 {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");

_balances[sender] = _balances[sender].sub(amount);
_balances[sender] = _balances[sender].sub(amount, "ERC20: transfer of tokens exceeding balance");
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
Expand Down Expand Up @@ -190,8 +190,8 @@ contract ERC20 is IERC20 {
function _burn(address account, uint256 value) internal {
require(account != address(0), "ERC20: burn from the zero address");

_totalSupply = _totalSupply.sub(value);
_balances[account] = _balances[account].sub(value);
_totalSupply = _totalSupply.sub(value, "ERC20: burn of tokens exceeding balance");
_balances[account] = _balances[account].sub(value, "ERC20: burn of tokens exceeding balance");
emit Transfer(account, address(0), value);
}

Expand Down Expand Up @@ -224,6 +224,6 @@ contract ERC20 is IERC20 {
*/
function _burnFrom(address account, uint256 amount) internal {
_burn(account, amount);
_approve(account, msg.sender, _allowances[account][msg.sender].sub(amount));
_approve(account, msg.sender, _allowances[account][msg.sender].sub(amount, "ERC20: burn of tokens exceeding allowance"));
}
}
2 changes: 1 addition & 1 deletion contracts/token/ERC20/SafeERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ library SafeERC20 {
}

function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value);
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decrease of higher allowance than granted");
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/token/ERC777/ERC777.sol
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ contract ERC777 is IERC777, IERC20 {
_callTokensToSend(spender, holder, recipient, amount, "", "");

_move(spender, holder, recipient, amount, "", "");
_approve(holder, spender, _allowances[holder][spender].sub(amount));
_approve(holder, spender, _allowances[holder][spender].sub(amount, "ERC777: transfer of tokens exceeding allowance"));

_callTokensReceived(spender, holder, recipient, amount, "", "", false);

Expand Down Expand Up @@ -383,8 +383,8 @@ contract ERC777 is IERC777, IERC20 {
_callTokensToSend(operator, from, address(0), amount, data, operatorData);

// Update state variables
_totalSupply = _totalSupply.sub(amount);
_balances[from] = _balances[from].sub(amount);
_totalSupply = _totalSupply.sub(amount, "ERC777: burn of tokens exceeding balance");
_balances[from] = _balances[from].sub(amount, "ERC777: burn of tokens exceeding balance");

emit Burned(operator, from, amount, data, operatorData);
emit Transfer(from, address(0), amount);
Expand All @@ -400,7 +400,7 @@ contract ERC777 is IERC777, IERC20 {
)
private
{
_balances[from] = _balances[from].sub(amount);
_balances[from] = _balances[from].sub(amount, "ERC777: transfer of tokens exceeding balance");
_balances[to] = _balances[to].add(amount);

emit Sent(operator, from, to, amount, userData, operatorData);
Expand Down
96 changes: 45 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/token/ERC20/ERC20.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip

it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), 'SafeMath: subtraction overflow'
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer of tokens exceeding balance`
);
});
});
Expand All @@ -104,7 +104,7 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip

it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), 'SafeMath: subtraction overflow'
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer of tokens exceeding allowance`
);
});
});
Expand All @@ -114,7 +114,7 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip

it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), 'SafeMath: subtraction overflow'
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer of tokens exceeding balance`
);
});
});
Expand Down Expand Up @@ -166,7 +166,7 @@ function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer

it('reverts', async function () {
await expectRevert(transfer.call(this, from, to, amount),
'SafeMath: subtraction overflow'
`${errorPrefix}: transfer of tokens exceeding balance`
);
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/token/ERC20/ERC20.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
describe('when there was no approved amount before', function () {
it('reverts', async function () {
await expectRevert(this.token.decreaseAllowance(
spender, amount, { from: initialHolder }), 'SafeMath: subtraction overflow'
spender, amount, { from: initialHolder }), 'ERC20: decrease of higher allowance than granted'
);
});
});
Expand Down Expand Up @@ -63,7 +63,7 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
it('reverts when more than the full allowance is removed', async function () {
await expectRevert(
this.token.decreaseAllowance(spender, approvedAmount.addn(1), { from: initialHolder }),
'SafeMath: subtraction overflow'
'ERC20: decrease of higher allowance than granted'
);
});
});
Expand All @@ -88,7 +88,7 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {

it('reverts', async function () {
await expectRevert(this.token.decreaseAllowance(
spender, amount, { from: initialHolder }), 'SafeMath: subtraction overflow'
spender, amount, { from: initialHolder }), 'ERC20: decrease of higher allowance than granted'
);
});
});
Expand Down Expand Up @@ -221,7 +221,7 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
describe('for a non zero account', function () {
it('rejects burning more than balance', async function () {
await expectRevert(this.token.burn(
initialHolder, initialSupply.addn(1)), 'SafeMath: subtraction overflow'
initialHolder, initialSupply.addn(1)), 'ERC20: burn of tokens exceeding balance'
);
});

Expand Down Expand Up @@ -276,13 +276,13 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
describe('for a non zero account', function () {
it('rejects burning more than allowance', async function () {
await expectRevert(this.token.burnFrom(initialHolder, allowance.addn(1)),
'SafeMath: subtraction overflow'
'ERC20: burn of tokens exceeding allowance'
);
});

it('rejects burning more than balance', async function () {
await expectRevert(this.token.burnFrom(initialHolder, initialSupply.addn(1)),
'SafeMath: subtraction overflow'
'ERC20: burn of tokens exceeding balance'
);
});

Expand Down
4 changes: 2 additions & 2 deletions test/token/ERC20/SafeERC20.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function shouldOnlyRevertOnErrors () {
it('reverts when decreasing the allowance', async function () {
await expectRevert(
this.wrapper.decreaseAllowance(10),
'SafeMath: subtraction overflow'
'SafeERC20: decrease of higher allowance than granted'
);
});
});
Expand Down Expand Up @@ -125,7 +125,7 @@ function shouldOnlyRevertOnErrors () {
it('reverts when decreasing the allowance to a negative value', async function () {
await expectRevert(
this.wrapper.decreaseAllowance(200),
'SafeMath: subtraction overflow'
'SafeERC20: decrease of higher allowance than granted'
);
});
});
Expand Down
Loading

0 comments on commit ca40fe5

Please sign in to comment.