PostDeliveryCrowdsale.test.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const time = require('../helpers/time');
  2. const shouldFail = require('../helpers/shouldFail');
  3. const { ether } = require('../helpers/ether');
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. const PostDeliveryCrowdsaleImpl = artifacts.require('PostDeliveryCrowdsaleImpl');
  9. const SimpleToken = artifacts.require('SimpleToken');
  10. contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
  11. const rate = new BigNumber(1);
  12. const tokenSupply = new BigNumber('1e22');
  13. before(async function () {
  14. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  15. await time.advanceBlock();
  16. });
  17. beforeEach(async function () {
  18. this.openingTime = (await time.latest()) + time.duration.weeks(1);
  19. this.closingTime = this.openingTime + time.duration.weeks(1);
  20. this.afterClosingTime = this.closingTime + time.duration.seconds(1);
  21. this.token = await SimpleToken.new();
  22. this.crowdsale = await PostDeliveryCrowdsaleImpl.new(
  23. this.openingTime, this.closingTime, rate, wallet, this.token.address
  24. );
  25. await this.token.transfer(this.crowdsale.address, tokenSupply);
  26. });
  27. context('after opening time', function () {
  28. beforeEach(async function () {
  29. await time.increaseTo(this.openingTime);
  30. });
  31. context('with bought tokens', function () {
  32. const value = ether(42);
  33. beforeEach(async function () {
  34. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  35. });
  36. it('does not immediately assign tokens to beneficiaries', async function () {
  37. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  38. (await this.token.balanceOf(investor)).should.be.bignumber.equal(0);
  39. });
  40. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  41. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  42. });
  43. context('after closing time', function () {
  44. beforeEach(async function () {
  45. await time.increaseTo(this.afterClosingTime);
  46. });
  47. it('allows beneficiaries to withdraw tokens', async function () {
  48. await this.crowdsale.withdrawTokens(investor);
  49. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(0);
  50. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value);
  51. });
  52. it('rejects multiple withdrawals', async function () {
  53. await this.crowdsale.withdrawTokens(investor);
  54. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  55. });
  56. });
  57. });
  58. });
  59. });