Skip to content

Fixed how allowance crowdsale checks remaining tokens. #1449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion contracts/crowdsale/emission/AllowanceCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/crowdsale/AllowanceCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down