From ebd4b5e73db0b1adf546a7c312604c9f0364f0e1 Mon Sep 17 00:00:00 2001 From: zaryab Date: Fri, 20 Jul 2018 22:52:22 +0500 Subject: [PATCH] decrease approval condition fix (greater than and equal to) (#1063) * decrease approval condition fix (greater than and equal to) * sol lint fixed * Improved standard token decreaseApprovalTests. --- contracts/token/ERC20/StandardToken.sol | 2 +- test/token/ERC20/StandardToken.test.js | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/contracts/token/ERC20/StandardToken.sol b/contracts/token/ERC20/StandardToken.sol index 0a4c5b8c6..ff09e12ea 100644 --- a/contracts/token/ERC20/StandardToken.sol +++ b/contracts/token/ERC20/StandardToken.sol @@ -112,7 +112,7 @@ contract StandardToken is ERC20, BasicToken { returns (bool) { uint256 oldValue = allowed[msg.sender][_spender]; - if (_subtractedValue > oldValue) { + if (_subtractedValue >= oldValue) { allowed[msg.sender][_spender] = 0; } else { allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue); diff --git a/test/token/ERC20/StandardToken.test.js b/test/token/ERC20/StandardToken.test.js index 18239f2a4..9aa97cae9 100644 --- a/test/token/ERC20/StandardToken.test.js +++ b/test/token/ERC20/StandardToken.test.js @@ -295,15 +295,29 @@ contract('StandardToken', function ([_, owner, recipient, anotherAccount]) { }); describe('when the spender had an approved amount', function () { + const approvedAmount = amount; + beforeEach(async function () { - await this.token.approve(spender, amount + 1, { from: owner }); + await this.token.approve(spender, approvedAmount, { from: owner }); }); it('decreases the spender allowance subtracting the requested amount', async function () { - await this.token.decreaseApproval(spender, amount, { from: owner }); + await this.token.decreaseApproval(spender, approvedAmount - 5, { from: owner }); const allowance = await this.token.allowance(owner, spender); - assert.equal(allowance, 1); + assert.equal(allowance, 5); + }); + + it('sets the allowance to zero when all allowance is removed', async function () { + await this.token.decreaseApproval(spender, approvedAmount, { from: owner }); + const allowance = await this.token.allowance(owner, spender); + assert.equal(allowance, 0); + }); + + it('sets the allowance to zero when more than the full allowance is removed', async function () { + await this.token.decreaseApproval(spender, approvedAmount + 5, { from: owner }); + const allowance = await this.token.allowance(owner, spender); + assert.equal(allowance, 0); }); }); });