PostDeliveryCrowdsale.test.js 2.8 KB

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