SampleCrowdsale.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { BN, balance, ether, should, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const SampleCrowdsale = artifacts.require('SampleCrowdsale');
  3. const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken');
  4. contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
  5. const RATE = new BN(10);
  6. const GOAL = ether('10');
  7. const CAP = ether('20');
  8. before(async function () {
  9. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  10. await time.advanceBlock();
  11. });
  12. beforeEach(async function () {
  13. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  14. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  15. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  16. this.token = await SampleCrowdsaleToken.new({ from: deployer });
  17. this.crowdsale = await SampleCrowdsale.new(
  18. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL,
  19. { from: owner }
  20. );
  21. await this.token.addMinter(this.crowdsale.address, { from: deployer });
  22. await this.token.renounceMinter({ from: deployer });
  23. });
  24. it('should create crowdsale with correct parameters', async function () {
  25. should.exist(this.crowdsale);
  26. should.exist(this.token);
  27. (await this.crowdsale.openingTime()).should.be.bignumber.equal(this.openingTime);
  28. (await this.crowdsale.closingTime()).should.be.bignumber.equal(this.closingTime);
  29. (await this.crowdsale.rate()).should.be.bignumber.equal(RATE);
  30. (await this.crowdsale.wallet()).should.be.equal(wallet);
  31. (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL);
  32. (await this.crowdsale.cap()).should.be.bignumber.equal(CAP);
  33. });
  34. it('should not accept payments before start', async function () {
  35. await shouldFail.reverting(this.crowdsale.send(ether('1')));
  36. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { from: investor, value: ether('1') }));
  37. });
  38. it('should accept payments during the sale', async function () {
  39. const investmentAmount = ether('1');
  40. const expectedTokenAmount = RATE.mul(investmentAmount);
  41. await time.increaseTo(this.openingTime);
  42. await this.crowdsale.buyTokens(investor, { value: investmentAmount, from: investor });
  43. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  44. (await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount);
  45. });
  46. it('should reject payments after end', async function () {
  47. await time.increaseTo(this.afterClosingTime);
  48. await shouldFail.reverting(this.crowdsale.send(ether('1')));
  49. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { value: ether('1'), from: investor }));
  50. });
  51. it('should reject payments over cap', async function () {
  52. await time.increaseTo(this.openingTime);
  53. await this.crowdsale.send(CAP);
  54. await shouldFail.reverting(this.crowdsale.send(1));
  55. });
  56. it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
  57. await time.increaseTo(this.openingTime);
  58. await this.crowdsale.send(GOAL);
  59. const balanceTracker = await balance.tracker(wallet);
  60. await time.increaseTo(this.afterClosingTime);
  61. await this.crowdsale.finalize({ from: owner });
  62. (await balanceTracker.delta()).should.be.bignumber.equal(GOAL);
  63. });
  64. it('should allow refunds if the goal is not reached', async function () {
  65. const balanceTracker = await balance.tracker(investor);
  66. await time.increaseTo(this.openingTime);
  67. await this.crowdsale.sendTransaction({ value: ether('1'), from: investor, gasPrice: 0 });
  68. await time.increaseTo(this.afterClosingTime);
  69. await this.crowdsale.finalize({ from: owner });
  70. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  71. (await balanceTracker.delta()).should.be.bignumber.equal('0');
  72. });
  73. describe('when goal > cap', function () {
  74. // goal > cap
  75. const HIGH_GOAL = ether('30');
  76. it('creation reverts', async function () {
  77. await shouldFail.reverting(SampleCrowdsale.new(
  78. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, HIGH_GOAL
  79. ));
  80. });
  81. });
  82. });