0

I am coding an ICO project. However when trying to make the token purchase I got the following error:

Using network 'test'.


Compiling your contracts...
===========================
> Compiling .\contracts\DappToken.sol
> Compiling .\contracts\DappTokenSale.sol
> Compiling .\contracts\Migrations.sol
> Artifacts written to C:\Users\TRUNGL~1\AppData\Local\Temp\test--19172-maVtFUdroS6W
> Compiled successfully using:
   - solc: 0.6.0+commit.26b70077.Emscripten.clang


  Contract: DappToken
    ✔ initializes the contract with the correct values (74ms)
    ✔ allocates the initial supply upon deployment (41ms)
    ✔ transfers token ownership (1698ms)
    ✔ approves tokens for delegated transfer (1150ms)
    ✔ handles delegated tokens transfers (3383ms)

  Contract: DappTokenSale
    ✔ initializes the contract with the correct values (67ms)
    1) facilities token buying
    > No events were emitted


  6 passing (7s)
  1 failing

  1) Contract: DappTokenSale
       facilities token buying:
     AssertionError: cannot purchased more tokens than available
      at D:\BlockChain\ICO\test\DappTokenSale.js:111:17
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

This is the file contracts/DappTokenSale.sol:

pragma solidity >0.5.0;
import "./DappToken.sol";

contract DappTokenSale {
    // Assign an admin 
    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 that value is equal to tokens
        require(msg.value == multiply(_numberOfTokens, tokenPrice));

        // Require that contract has enough tokens
        require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);

        // Require that a transfer is sucessful
        require(tokenContract.transfer(msg.sender, _numberOfTokens));

        // Keep track of tokensSold
        tokensSold += _numberOfTokens;

        // Trigger Sell event
        emit Sell(msg.sender, _numberOfTokens);
    }
}

This is the file test/DappTokenSale.js:

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 tokenAvailable = 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("facilities token buying", function () {
        return DappTokenSale.deployed()
            .then(function (instance) {
                tokenInstance = instance;
                return DappTokenSale.deployed();
            })
            .then(function (instance) {
                // Then grab token instance
                tokenSaleInstance = instance;
                // Provision 75% of all tokens to the tokens sale
                return tokenInstance.transfer(
                    tokenSaleInstance.address,
                    tokenAvailable,
                    { 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 "Sell" event'
                );
                assert.equal(
                    receipt.logs[0].args._buyer,
                    buyer,
                    "log the account that purchased the tokens"
                );
                assert.equal(
                    receipt.logs[0].args._amount,
                    numberOfTokens,
                    "log 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(),
                    tokenAvailable - numberOfTokens
                );
                // Try to buy tokens different from the ether value
                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"
                );
                // Try to buy tokens different from the ether value
                return tokenSaleInstance.buyTokens(8000, {
                    from: buyer,
                    value: numberOfTokens * tokenPrice,
                });
            })
            .then(assert.fail)
            .catch(function (error) {
                assert(
                    error.message.indexOf("revert") >= 0,
                    "cannot purchased more tokens than available"
                );
            });
    });
});

Is there any way to fix this?

1 Answer 1

0

The second test is using DappTokenSale as the token

it("facilities token buying", function () {
    //--------------------------
    // Should use DappToken here
    //--------------------------
    return DappToken.deployed()
        .then(function (instance) {
            tokenInstance = instance;
            return DappTokenSale.deployed();
        })
        .then(function (instance) {
            // Then grab token instance
            tokenSaleInstance = instance;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.