0

Cannot read property 'toNumber' of undefined?

Error message

1) Contract: DappTokenSale ends token sale: TypeError: Cannot read property 'toNumber' of undefined at test/DappTokenSale.js:85:28 at process.internalTickCallback (internal/process/next_tick.js:77:7)

Code

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; // in wei
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) {
  // Grab token instance first
  tokenInstance = instance;
  return DappTokenSale.deployed();
}).then(function(instance) {
  // Then grab token sale instance
  tokenSaleInstance = instance;
  // Provision 75% of all tokens to the token sale
  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);
  // 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');
  return tokenSaleInstance.buyTokens(800000, { from: buyer, value: numberOfTokens * tokenPrice })
}).then(assert.fail).catch(function(error) {
  assert(error.message.indexOf('revert') >= 0, 'cannot purchase more tokens than available');
});
});

it('ends token sale', function() {
return DappToken.deployed().then(function(instance) {
  // Grab token instance first
  tokenInstance = instance;
  return DappTokenSale.deployed();
}).then(function(instance) {
  // Then grab token sale instance
  tokenSaleInstance = instance;
  // Try to end sale from account other than the admin
  return tokenSaleInstance.endSale({ from: buyer });
}).then(assert.fail).catch(function(error) {
  assert(error.message.indexOf('revert' >= 0, 'must be admin to end sale'));
  // End sale as admin
  return tokenSaleInstance.endSale({ from: admin });
}).then(function(receipt) {
  return tokenInstance.balanceOf(admin);
}).then(function(balance) {
  assert.equal(balance.toNumber(), 999990, 'returns all unsold dapp tokens to admin');
  // Check that the contract has no balance
  }).then(function(price) {
    assert.equal(price.toNumber(), 0, 'token price was reset');
});
});
});
1

The problem seems to be here:

.then(function(balance) {
   assert.equal(balance.toNumber(), 999990, 'returns all unsold dapp tokens to admin');
   // Check that the contract has no balance
}).then(function(price) {
   assert.equal(price.toNumber(), 0, 'token price was reset');
});

There's no return value from that first function, so of course price is undefined.

  • Thanks Smarx! How would you write that return value? – zummon Jan 13 at 1:16
  • 1
    I have no idea. There's no mention of a "price" anywhere in your code, so I don't know what value you're trying to test. – user19510 Jan 13 at 1:17

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.