diff --git a/contracts/crowdsale/emission/AllowanceCrowdsale.sol b/contracts/crowdsale/emission/AllowanceCrowdsale.sol index ec9918febe7..346275c7d36 100644 --- a/contracts/crowdsale/emission/AllowanceCrowdsale.sol +++ b/contracts/crowdsale/emission/AllowanceCrowdsale.sol @@ -4,6 +4,7 @@ import "../Crowdsale.sol"; import "../../token/ERC20/IERC20.sol"; import "../../token/ERC20/SafeERC20.sol"; import "../../math/SafeMath.sol"; +import "../../math/Math.sol"; /** * @title AllowanceCrowdsale @@ -36,7 +37,10 @@ contract AllowanceCrowdsale is Crowdsale { * @return Amount of tokens left in the allowance */ function remainingTokens() public view returns (uint256) { - return token().allowance(_tokenWallet, this); + return Math.min( + token().balanceOf(_tokenWallet), + token().allowance(_tokenWallet, this) + ); } /** diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 4298dcce61a..413d2554489 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -69,6 +69,17 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }); (await this.crowdsale.remainingTokens()).should.be.bignumber.equal(remainingAllowance); }); + + context('when the allowance is larger than the token amount', function () { + beforeEach(async function () { + const amount = await this.token.balanceOf(tokenWallet); + await this.token.approve(this.crowdsale.address, amount.plus(1), { from: tokenWallet }); + }); + + it('should report the amount instead of the allowance', async function () { + (await this.crowdsale.remainingTokens()).should.be.bignumber.equal(await this.token.balanceOf(tokenWallet)); + }); + }); }); describe('when token wallet is different from token address', function () {