Testing buyToken in crowdsale and investor token balanceOf returns 0

With the erc20 token and crowdsale contracts created, I am using truffle framework doing testing, and 1 testcase failed: after crowdsale.buyToken(investor1) successfully fulfilled, I expect token.balanceOf(investor1) return non-zero, but it returns 0:

:computer: Environment

Truffle

:memo:Details
testcase:

  beforeEach(async function() {
await this.crowdsale.buyTokens(investor1, { value: ether(1), from: investor1 });
});

describe('during crowdsale', function() {
  it('check investor1 should have the tokens purchaged', async function() {
    let tb = await this.token.balanceOf(investor1);
console.log("  investor1 tokens: " + tb + " @ " + investor1);
      expect(tb).to.be.equal(500);
  });
}):

Even the events correctly generated:

IERC20.Transfer(
from: <indexed> 0x0000000000000000000000000000000000000000 (type: address),
to: <indexed> 0xBf449E4c98361630005ADf5cfb129707d7B0714d (type: address),
value: 500000000000000000000 (type: uint256)
)

Crowdsale.TokensPurchased(
  purchaser: <indexed> 0x46F5862A84Cd5138BE62530AE6a563D4F110DB3e (type: address),
  beneficiary: <indexed> 0x46F5862A84Cd5138BE62530AE6a563D4F110DB3e (type: address),
  value: 1000000000000000000 (type: uint256),
  amount: 500000000000000000000 (type: uint256)
)
1 failing

1) Contract: WJWNpmTokenCrowdsale
refundable crowdsale
during crowdsale
check investor1 should have the tokens purchaged:
AssertionError: expected <BN: 0> to equal 500
at Context._callee14$ (test/WJWNpmTokenCrowdsale.test.js:416:34)
at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)
at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)
at step (test/WJWNpmTokenCrowdsale.test.js:21:191)
at /home/james/workspace/ico_wjw/test/WJWNpmTokenCrowdsale.test.js:21:361
at process._tickCallback (internal/process/next_tick.js:68:7)

:1234: Code to reproduce

  beforeEach(async function() {
await this.crowdsale.buyTokens(investor1, { value: ether(1), from: investor1 });
});

describe('during crowdsale', function() {
  it('check investor1 should have the tokens purchaged', async function() {
    let tb = await this.token.balanceOf(investor1);
console.log("  investor1 tokens: " + tb + " @ " + investor1);
      expect(tb).to.be.equal(500);
  });
}):

the rate of the crowdsale is 500.

1 Like

According to the contract events above, it seems like the token was transferred to the account 0xBf449E4c98361630005ADf5cfb129707d7B0714d rather than the caller account investor1 : 0x46F5862A84Cd5138BE62530AE6a563D4F110DB3e, so maybe you should check your crowdsale contract.

If you can paste all your code, maybe we will find more.

BTW, abcoathup has ever wrote a tutorial about the crowdsale contract, maybe you can have a look at it:

2 Likes

Really appreciate your support - I’ll check my crowdsale code, and definitely will check the tutorial you suggest.

Have a nice day!

2 Likes

Hi @jingwenw,

Welcome to the community :wave:

(@Skyge thanks for being awesome :pray:)

Thanks guys - I got the support I needed from this awesome community, and just feel lucky being part of it!

1 Like