PostDeliveryCrowdsale.test.js 2.7 KB

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