PostDeliveryCrowdsale.test.js 2.8 KB

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