Unverified Commit 84e63bbf by Francisco Giordano Committed by GitHub

Fix decrease allowance and add non-zero address precondition to approve (#1293)

* rename {increase,decrease}Approval to {increase,decrease}Allowance

* add non-zero spender check to approve and {increase,decrease}Allowance

* Updated tests to reflect the new behavior.

* fix wrong test description

* fix old function names

* Fixed linter error.

* Fixed typo.
parent e7e8d8ea
...@@ -78,6 +78,8 @@ contract ERC20 is IERC20 { ...@@ -78,6 +78,8 @@ contract ERC20 is IERC20 {
* @param _value The amount of tokens to be spent. * @param _value The amount of tokens to be spent.
*/ */
function approve(address _spender, uint256 _value) public returns (bool) { function approve(address _spender, uint256 _value) public returns (bool) {
require(_spender != address(0));
allowed_[msg.sender][_spender] = _value; allowed_[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value); emit Approval(msg.sender, _spender, _value);
return true; return true;
...@@ -117,13 +119,15 @@ contract ERC20 is IERC20 { ...@@ -117,13 +119,15 @@ contract ERC20 is IERC20 {
* @param _spender The address which will spend the funds. * @param _spender The address which will spend the funds.
* @param _addedValue The amount of tokens to increase the allowance by. * @param _addedValue The amount of tokens to increase the allowance by.
*/ */
function increaseApproval( function increaseAllowance(
address _spender, address _spender,
uint256 _addedValue uint256 _addedValue
) )
public public
returns (bool) returns (bool)
{ {
require(_spender != address(0));
allowed_[msg.sender][_spender] = ( allowed_[msg.sender][_spender] = (
allowed_[msg.sender][_spender].add(_addedValue)); allowed_[msg.sender][_spender].add(_addedValue));
emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]); emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]);
...@@ -139,19 +143,17 @@ contract ERC20 is IERC20 { ...@@ -139,19 +143,17 @@ contract ERC20 is IERC20 {
* @param _spender The address which will spend the funds. * @param _spender The address which will spend the funds.
* @param _subtractedValue The amount of tokens to decrease the allowance by. * @param _subtractedValue The amount of tokens to decrease the allowance by.
*/ */
function decreaseApproval( function decreaseAllowance(
address _spender, address _spender,
uint256 _subtractedValue uint256 _subtractedValue
) )
public public
returns (bool) returns (bool)
{ {
uint256 oldValue = allowed_[msg.sender][_spender]; require(_spender != address(0));
if (_subtractedValue >= oldValue) {
allowed_[msg.sender][_spender] = 0; allowed_[msg.sender][_spender] = (
} else { allowed_[msg.sender][_spender].sub(_subtractedValue));
allowed_[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]); emit Approval(msg.sender, _spender, allowed_[msg.sender][_spender]);
return true; return true;
} }
......
...@@ -44,7 +44,7 @@ contract ERC20Pausable is ERC20, Pausable { ...@@ -44,7 +44,7 @@ contract ERC20Pausable is ERC20, Pausable {
return super.approve(_spender, _value); return super.approve(_spender, _value);
} }
function increaseApproval( function increaseAllowance(
address _spender, address _spender,
uint _addedValue uint _addedValue
) )
...@@ -52,10 +52,10 @@ contract ERC20Pausable is ERC20, Pausable { ...@@ -52,10 +52,10 @@ contract ERC20Pausable is ERC20, Pausable {
whenNotPaused whenNotPaused
returns (bool success) returns (bool success)
{ {
return super.increaseApproval(_spender, _addedValue); return super.increaseAllowance(_spender, _addedValue);
} }
function decreaseApproval( function decreaseAllowance(
address _spender, address _spender,
uint _subtractedValue uint _subtractedValue
) )
...@@ -63,6 +63,6 @@ contract ERC20Pausable is ERC20, Pausable { ...@@ -63,6 +63,6 @@ contract ERC20Pausable is ERC20, Pausable {
whenNotPaused whenNotPaused
returns (bool success) returns (bool success)
{ {
return super.decreaseApproval(_spender, _subtractedValue); return super.decreaseAllowance(_spender, _subtractedValue);
} }
} }
...@@ -196,7 +196,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA ...@@ -196,7 +196,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA
}); });
it('allows to decrease approval when unpaused', async function () { it('allows to decrease approval when unpaused', async function () {
await this.token.decreaseApproval(anotherAccount, 40, { from: pauser }); await this.token.decreaseAllowance(anotherAccount, 40, { from: pauser });
(await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60); (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60);
}); });
...@@ -205,7 +205,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA ...@@ -205,7 +205,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA
await this.token.pause({ from: pauser }); await this.token.pause({ from: pauser });
await this.token.unpause({ from: pauser }); await this.token.unpause({ from: pauser });
await this.token.decreaseApproval(anotherAccount, 40, { from: pauser }); await this.token.decreaseAllowance(anotherAccount, 40, { from: pauser });
(await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60); (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60);
}); });
...@@ -213,7 +213,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA ...@@ -213,7 +213,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA
it('reverts when trying to transfer when paused', async function () { it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: pauser }); await this.token.pause({ from: pauser });
await assertRevert(this.token.decreaseApproval(anotherAccount, 40, { from: pauser })); await assertRevert(this.token.decreaseAllowance(anotherAccount, 40, { from: pauser }));
}); });
}); });
...@@ -223,7 +223,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA ...@@ -223,7 +223,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA
}); });
it('allows to increase approval when unpaused', async function () { it('allows to increase approval when unpaused', async function () {
await this.token.increaseApproval(anotherAccount, 40, { from: pauser }); await this.token.increaseAllowance(anotherAccount, 40, { from: pauser });
(await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140); (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140);
}); });
...@@ -232,7 +232,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA ...@@ -232,7 +232,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA
await this.token.pause({ from: pauser }); await this.token.pause({ from: pauser });
await this.token.unpause({ from: pauser }); await this.token.unpause({ from: pauser });
await this.token.increaseApproval(anotherAccount, 40, { from: pauser }); await this.token.increaseAllowance(anotherAccount, 40, { from: pauser });
(await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140); (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140);
}); });
...@@ -240,7 +240,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA ...@@ -240,7 +240,7 @@ contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherA
it('reverts when trying to increase approval when paused', async function () { it('reverts when trying to increase approval when paused', async function () {
await this.token.pause({ from: pauser }); await this.token.pause({ from: pauser });
await assertRevert(this.token.increaseApproval(anotherAccount, 40, { from: pauser })); await assertRevert(this.token.increaseAllowance(anotherAccount, 40, { from: pauser }));
}); });
}); });
}); });
......
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