Skip to content

Commit

Permalink
Merge pull request #391 from rudygodoy/pr-224-tests
Browse files Browse the repository at this point in the history
Added tests for PR #224
  • Loading branch information
frangio committed Aug 21, 2017
2 parents 3d5c759 + 289fd87 commit 6317484
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions contracts/token/StandardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,30 @@ contract StandardToken is ERC20, BasicToken {
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}

/*
* approve should be called when allowed[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
*/
function increaseApproval (address _spender, uint _addedValue)
returns (bool success) {
allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}

function decreaseApproval (address _spender, uint _subtractedValue)
returns (bool success) {
uint oldValue = allowed[msg.sender][_spender];
if (_subtractedValue > oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}

}
18 changes: 18 additions & 0 deletions test/StandardToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,22 @@ contract('StandardToken', function(accounts) {
}
});

describe('validating allowance updates to spender', function() {
let preApproved;

it('should start with zero', async function() {
preApproved = await token.allowance(accounts[0], accounts[1]);
assert.equal(preApproved, 0);
})

it('should increase by 50 then decrease by 10', async function() {
await token.increaseApproval(accounts[1], 50);
let postIncrease = await token.allowance(accounts[0], accounts[1]);
preApproved.plus(50).should.be.bignumber.equal(postIncrease);
await token.decreaseApproval(accounts[1], 10);
let postDecrease = await token.allowance(accounts[0], accounts[1]);
postIncrease.minus(10).should.be.bignumber.equal(postDecrease);
})
});

});

0 comments on commit 6317484

Please sign in to comment.