Skip to content

Commit 47b5d61

Browse files
nventurocome-maiz
authored andcommitted
Fixed how allowance crowdsale checks remaining tokens. (#1449)
(cherry picked from commit 1ac1ac9)
1 parent 984fe23 commit 47b5d61

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

contracts/crowdsale/emission/AllowanceCrowdsale.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "../Crowdsale.sol";
44
import "../../token/ERC20/IERC20.sol";
55
import "../../token/ERC20/SafeERC20.sol";
66
import "../../math/SafeMath.sol";
7+
import "../../math/Math.sol";
78

89
/**
910
* @title AllowanceCrowdsale
@@ -36,7 +37,10 @@ contract AllowanceCrowdsale is Crowdsale {
3637
* @return Amount of tokens left in the allowance
3738
*/
3839
function remainingTokens() public view returns (uint256) {
39-
return token().allowance(_tokenWallet, this);
40+
return Math.min(
41+
token().balanceOf(_tokenWallet),
42+
token().allowance(_tokenWallet, this)
43+
);
4044
}
4145

4246
/**

test/crowdsale/AllowanceCrowdsale.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW
6969
await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
7070
(await this.crowdsale.remainingTokens()).should.be.bignumber.equal(remainingAllowance);
7171
});
72+
73+
context('when the allowance is larger than the token amount', function () {
74+
beforeEach(async function () {
75+
const amount = await this.token.balanceOf(tokenWallet);
76+
await this.token.approve(this.crowdsale.address, amount.plus(1), { from: tokenWallet });
77+
});
78+
79+
it('should report the amount instead of the allowance', async function () {
80+
(await this.crowdsale.remainingTokens()).should.be.bignumber.equal(await this.token.balanceOf(tokenWallet));
81+
});
82+
});
7283
});
7384

7485
describe('when token wallet is different from token address', function () {

0 commit comments

Comments
 (0)