It happen when use truffle test,
here my contract file:
pragma solidity ^0.5.0;
import "./DappToken.sol";
contract DappTokenSale {
address payable admin;
DappToken public tokenContract;
uint256 public tokenPrice;
uint256 public tokensSold;
event Sell(address _buyer, uint256 _amount);
constructor (DappToken _tokenContract, uint256 _tokenPrice) public {
admin = msg.sender;
tokenContract = _tokenContract;
tokenPrice = _tokenPrice;
}
function multiply (uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x);
}
function buyTokens (uint256 _numberOfTokens) public payable {
require (msg.value == multiply(_numberOfTokens, tokenPrice));
require (tokenContract.balanceOf(address(this)) >= _numberOfTokens);
require (tokenContract.transfer(msg.sender, _numberOfTokens));
tokensSold += _numberOfTokens;
emit Sell(msg.sender, _numberOfTokens);
}
function endSale () public {
require (msg.sender == admin);
require (tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));
selfdestruct(admin);
}
}
here my test js file:
var DappToken = artifacts.require('./DappToken.sol');
var DappTokenSale = artifacts.require('./DappTokenSale.sol');
contract('DappTokenSale', function(accounts) {
var tokenInstance;
var tokenSaleInstance;
var admin = accounts[0];
var buyer = accounts[1];
var tokenPrice = 1000000000000000;
var tokensAvailable = 750000;
var numberOfTokens;
it('Initializes the contract with the correct values', function() {
return DappTokenSale.deployed().then(function(instance) {
tokenSaleInstance = instance;
return tokenSaleInstance.address
}).then(function(address) {
assert.notEqual(address, 0x0, "has contract address");
return tokenSaleInstance.tokenContract();
}).then(function(address) {
assert.notEqual(address, 0x0, "has token contract address");
return tokenSaleInstance.tokenPrice();
}).then(function(price) {
assert.equal(price, tokenPrice, 'token price is correct');
});
});
it('facilitates token buying', function(){
return DappToken.deployed().then(function(instance) {
tokenInstance = instance;
return DappTokenSale.deployed();
}).then(function(instance){
tokenSaleInstance = instance;
return tokenInstance.transfer(tokenSaleInstance.address, tokensAvailable, {from: admin})
}).then(function(receipt){
numberOfTokens = 10;
return tokenSaleInstance.buyTokens(numberOfTokens, { from: buyer, value: numberOfTokens*tokenPrice})
}).then(function(receipt) {
assert.equal(receipt.logs.length, 1, 'triggers one event');
assert.equal(receipt.logs[0].event, 'Sell', 'should be the "Sell" event');
assert.equal(receipt.logs[0].args._buyer, buyer, 'logs the account that purchased the tokens');
assert.equal(receipt.logs[0].args._amount, numberOfTokens, 'logs the number of tokens purchased');
return tokenSaleInstance.tokensSold();
}).then(function(amount){
assert.equal(amount.toNumber(), numberOfTokens, 'increments the number of tokens sold');
return tokenInstance.balanceOf(buyer);
}).then(function(balance) {
assert.equal(balance.toNumber(), numberOfTokens);
return tokenInstance.balanceOf(tokenSaleInstance.address);
}).then(function(balance){
assert.equal(balance.toNumber(), tokensAvailable - numberOfTokens);
return tokenSaleInstance.buyTokens(numberOfTokens, { from: buyer, value: 1});
}).then(assert.fail).catch(function(error) {
assert(error.message.indexOf('revert') >= 0, 'msg.value must equal number of tokens in wei')
return tokenSaleInstance.buyTokens(800000, {from: buyer, value: numberOfTokens*tokenPrice})
}).then(assert.fail).catch(function(error) {
assert(error.message.indexOf('revert') >= 0, 'cannot purchased more than available token');
});
});
});
it('end token sale', function() {
return DappTokenSale.deployed().then(function(instance){
tokenInstance = instance;
return DappTokenSale.deployed();
}).then(function(instance){
tokenSaleInstance = instance;
return tokenSaleInstance.endSale({from:buyer });
}).then(assert.fail).catch(function(error){
assert(error.message.indexOf('revert' >= 0, 'must be admin to end sale'));
return tokenSaleInstance.endSale({from: admin});
}).then(function(receipt){
return tokenInstance.balanceOf(admin);
}).then(function(balance){
assert.equal(balance.toNumber(), 999990, 'return all unsold dapp tokens to admin')
return tokenSaleInstance.tokenPrice();
}).then(function(price) {
assert.equal(price.toNumber(), 0,'token price was reset')
});
});