I want to create exchange contract by editing your Crowdsale.sol and adding to it sellToken().
Inside sellToken() I want burn amount of tokens which msg.sender want to sell.
When I test it with truffle the subtraction overflow error appear.
How I can make it work properly?
Bellow short versions of contract and test.
Exchange.sol
contract Exchange {
using SafeMath for uint256;
Token private _token;
address payable private _wallet;
uint256 private _rate;
uint256 private _weiRaised;
event TokensBurned(address indexed seller, uint256 amount);
constructor (uint256 rate, address payable wallet, Token token) public {
_rate = rate;
_wallet = wallet;
_token = token;
}
function token() public view returns (Token) {
return _token;
}
function sellTokens(address beneficiary, uint256 tokenAmount) public {
Token(address(token())).burnFrom(beneficiary, tokenAmount);
emit TokensBurned(beneficiary, tokenAmount);
}
}
BTW.
The same issue appears also when I run test and inside sellTokens(); there is withdraw(); instead of burnFrom();
Before run 'should sell token' test I've checked that balanceOf(investor) > sellingTokenAmount
Looking at the code you provided sellTokens calls burnFrom (the exchange contract is msg.sender). The exchange contract has no allowance, so burnFrom (I assume this calls _burnFrom) fails when it attempts to decrease the callers allowance.
but unfortunately subtraction overflow error still remains. If I skip value: sellingTokenAmount the test is passed. It seems to me that exchange contract treat balanceOf(investor) like equal to 0, but I can’t figure out why. Do you have any other ideas?
The issue is still that the caller of burnFrom doesn’t have an allowance, what I missed when I looked at this the first time is that the caller of burnFrom is the Exchange contract. Apologies for my error, I have updated my original answer.
The token holder needs to set an allowance for the exchange contract before calling sellTokens, so the test could look as follows: