PostDeliveryCrowdsale.test.js 2.6 KB

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